diff --git a/examples/test-installed/src/main.rs b/examples/test-installed/src/main.rs index 174957e..b57ec82 100644 --- a/examples/test-installed/src/main.rs +++ b/examples/test-installed/src/main.rs @@ -19,7 +19,7 @@ fn main() { client.clone(), ad, secret, - yup_oauth2::InstalledFlowReturnMethod::HTTPRedirect(8081), + yup_oauth2::InstalledFlowReturnMethod::HTTPRedirectEphemeral, ); let mut auth = Authenticator::new_disk( client, diff --git a/src/installed.rs b/src/installed.rs index 893fbe6..8372247 100644 --- a/src/installed.rs +++ b/src/installed.rs @@ -97,8 +97,12 @@ 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). + HTTPRedirectEphemeral, + /// 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. + /// parameter is the port to listen on. Users should typically prefer + /// HTTPRedirectEphemeral unless they need to specify the port to listen on. HTTPRedirect(u16), } @@ -134,7 +138,12 @@ impl<'c, FD: 'static + FlowDelegate + Clone + Send, C: 'c + hyper::client::conne ) -> impl 'a + Future + Send { let rduri = self.fd.redirect_uri(); // Start server on localhost to accept auth code. - let server = if let InstalledFlowReturnMethod::HTTPRedirect(port) = self.method { + let server_bind_port = match self.method { + InstalledFlowReturnMethod::HTTPRedirect(port) => Some(port), + InstalledFlowReturnMethod::HTTPRedirectEphemeral => Some(0), + _ => None, + }; + let server = if let Some(port) = server_bind_port { match InstalledFlowServer::new(port) { Result::Err(e) => Err(RequestError::ClientError(e)), Result::Ok(server) => Ok(Some(server)), @@ -338,8 +347,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], port).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 +653,7 @@ mod tests { client.clone(), ), app_secret, - InstalledFlowReturnMethod::HTTPRedirect(8081), + InstalledFlowReturnMethod::HTTPRedirectEphemeral, ); let _m = mock("POST", "/token") .match_body(mockito::Matcher::Regex(".*code=authorizationcodefromlocalserver.*client_id=9022167.*".to_string())) diff --git a/src/lib.rs b/src/lib.rs index 84cb08f..761f663 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -70,7 +70,7 @@ //! client.clone(), //! ad, //! secret, -//! yup_oauth2::InstalledFlowReturnMethod::HTTPRedirect(8081), +//! yup_oauth2::InstalledFlowReturnMethod::HTTPRedirectEphemeral, //! ); //! // 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