From 6c433a6fb506b94c1cbcda0f288574fecf4dbaf3 Mon Sep 17 00:00:00 2001 From: Michael Bryant Date: Mon, 25 Jan 2021 09:53:22 -0800 Subject: [PATCH] installed flow: support urls with existing query params --- src/installed.rs | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/installed.rs b/src/installed.rs index 41507bb..b0a390a 100644 --- a/src/installed.rs +++ b/src/installed.rs @@ -36,8 +36,18 @@ where let scopes_string = crate::helper::join(scopes, " "); url.push_str(auth_uri); + + if !url.contains('?') { + url.push('?'); + } else { + match url.chars().last() { + Some('?') | None => {}, + Some(_) => url.push('&'), + } + } + vec![ - format!("?scope={}", scopes_string), + format!("scope={}", scopes_string), "&access_type=offline".to_string(), format!("&redirect_uri={}", redirect_uri.unwrap_or(OOB_REDIRECT_URI)), "&response_type=code".to_string(), @@ -371,6 +381,23 @@ mod tests { ); } + #[test] + fn test_request_url_builder_appends_queries() { + assert_eq!( + "https://accounts.google.\ + com/o/oauth2/auth?unknown=testing&scope=email%20profile&access_type=offline&redirect_uri=urn:ietf:wg:oauth:2.0:\ + oob&response_type=code&client_id=812741506391-h38jh0j4fv0ce1krdkiq0hfvt6n5amr\ + f.apps.googleusercontent.com", + build_authentication_request_url( + "https://accounts.google.com/o/oauth2/auth?unknown=testing", + "812741506391-h38jh0j4fv0ce1krdkiq0hfvt6n5am\ + rf.apps.googleusercontent.com", + &["email", "profile"], + None + ) + ); + } + #[tokio::test] async fn test_server_random_local_port() { let addr1 = InstalledFlowServer::run().unwrap().local_addr();