mirror of
https://github.com/OMGeeky/yup-oauth2.git
synced 2026-01-24 10:50:24 +01:00
Merge pull request #103 from ggriffiniii/master
Have the installed flow http server always listen on an ephemeral port.
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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()))
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user