mirror of
https://github.com/OMGeeky/yup-oauth2.git
synced 2025-12-30 08:03:32 +01:00
Have the installed flow http server always listen on an ephemeral port.
Specifying a port of zero has the server listen on an ephemeral port. Many users may not be aware of that unless they have a background in networking where that's common practice. I'm also not able to think of any use cases where listening on a hardcoded port would be beneficial, so with this change I've opted to remove the ability entirely rather than simply documenting that almost everybody should specify zero.
This commit is contained in:
@@ -19,7 +19,7 @@ fn main() {
|
||||
client.clone(),
|
||||
ad,
|
||||
secret,
|
||||
yup_oauth2::InstalledFlowReturnMethod::HTTPRedirect(8081),
|
||||
yup_oauth2::InstalledFlowReturnMethod::HTTPRedirect,
|
||||
);
|
||||
let mut auth = Authenticator::new_disk(
|
||||
client,
|
||||
|
||||
@@ -97,9 +97,8 @@ pub enum InstalledFlowReturnMethod {
|
||||
/// (default)
|
||||
Interactive,
|
||||
/// Involves spinning up a local HTTP server and Google redirecting the browser to
|
||||
/// the server with a URL containing the code (preferred, but not as reliable). The
|
||||
/// parameter is the port to listen on.
|
||||
HTTPRedirect(u16),
|
||||
/// the server with a URL containing the code (preferred, but not as reliable).
|
||||
HTTPRedirect,
|
||||
}
|
||||
|
||||
impl<'c, FD: 'static + FlowDelegate + Clone + Send, C: 'c + hyper::client::connect::Connect>
|
||||
@@ -134,8 +133,8 @@ impl<'c, FD: 'static + FlowDelegate + Clone + Send, C: 'c + hyper::client::conne
|
||||
) -> impl 'a + Future<Item = Token, Error = RequestError> + Send {
|
||||
let rduri = self.fd.redirect_uri();
|
||||
// Start server on localhost to accept auth code.
|
||||
let server = if let InstalledFlowReturnMethod::HTTPRedirect(port) = self.method {
|
||||
match InstalledFlowServer::new(port) {
|
||||
let server = if let InstalledFlowReturnMethod::HTTPRedirect = self.method {
|
||||
match InstalledFlowServer::new() {
|
||||
Result::Err(e) => Err(RequestError::ClientError(e)),
|
||||
Result::Ok(server) => Ok(Some(server)),
|
||||
}
|
||||
@@ -328,7 +327,7 @@ struct InstalledFlowServer {
|
||||
}
|
||||
|
||||
impl InstalledFlowServer {
|
||||
fn new(port: u16) -> Result<InstalledFlowServer, hyper::error::Error> {
|
||||
fn new() -> Result<InstalledFlowServer, hyper::error::Error> {
|
||||
let (auth_code_tx, auth_code_rx) = oneshot::channel::<String>();
|
||||
let (shutdown_tx, shutdown_rx) = oneshot::channel::<()>();
|
||||
|
||||
@@ -338,8 +337,8 @@ impl InstalledFlowServer {
|
||||
.build();
|
||||
let service_maker = InstalledFlowServiceMaker::new(auth_code_tx);
|
||||
|
||||
let addr = format!("127.0.0.1:{}", port);
|
||||
let builder = hyper::server::Server::try_bind(&addr.parse().unwrap())?;
|
||||
let addr: std::net::SocketAddr = ([127, 0, 0, 1], 0).into();
|
||||
let builder = hyper::server::Server::try_bind(&addr)?;
|
||||
let server = builder.http1_only(true).serve(service_maker);
|
||||
let port = server.local_addr().port();
|
||||
let server_future = server
|
||||
@@ -644,7 +643,7 @@ mod tests {
|
||||
client.clone(),
|
||||
),
|
||||
app_secret,
|
||||
InstalledFlowReturnMethod::HTTPRedirect(8081),
|
||||
InstalledFlowReturnMethod::HTTPRedirect,
|
||||
);
|
||||
let _m = mock("POST", "/token")
|
||||
.match_body(mockito::Matcher::Regex(".*code=authorizationcodefromlocalserver.*client_id=9022167.*".to_string()))
|
||||
@@ -706,8 +705,8 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_server_random_local_port() {
|
||||
let addr1 = InstalledFlowServer::new(0).unwrap();
|
||||
let addr2 = InstalledFlowServer::new(0).unwrap();
|
||||
let addr1 = InstalledFlowServer::new().unwrap();
|
||||
let addr2 = InstalledFlowServer::new().unwrap();
|
||||
assert_ne!(addr1.port, addr2.port);
|
||||
}
|
||||
|
||||
@@ -730,7 +729,7 @@ mod tests {
|
||||
hyper::Client::builder()
|
||||
.executor(runtime.executor())
|
||||
.build_http();
|
||||
let mut server = InstalledFlowServer::new(0).unwrap();
|
||||
let mut server = InstalledFlowServer::new().unwrap();
|
||||
|
||||
let response = client
|
||||
.get(
|
||||
|
||||
@@ -70,7 +70,7 @@
|
||||
//! client.clone(),
|
||||
//! ad,
|
||||
//! secret,
|
||||
//! yup_oauth2::InstalledFlowReturnMethod::HTTPRedirect(8081),
|
||||
//! yup_oauth2::InstalledFlowReturnMethod::HTTPRedirect,
|
||||
//! );
|
||||
//! // You could already use InstalledFlow by itself, but usually you want to cache tokens and
|
||||
//! // refresh them, rather than ask the user every time to log in again. Authenticator wraps
|
||||
|
||||
Reference in New Issue
Block a user