Merge pull request #103 from ggriffiniii/master

Have the installed flow http server always listen on an ephemeral port.
This commit is contained in:
Lewin Bormann
2019-08-29 20:06:16 +02:00
committed by GitHub
3 changed files with 16 additions and 7 deletions

View File

@@ -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,

View File

@@ -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<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 {
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()))

View File

@@ -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