diff --git a/src/authenticator.rs b/src/authenticator.rs index 13668c0..2701ce3 100644 --- a/src/authenticator.rs +++ b/src/authenticator.rs @@ -667,6 +667,16 @@ impl AuthenticatorBuilder { ..self } } + /// Force the user to select an account on the initial request + pub fn force_account_selection(self, force: bool)->Self{ + AuthenticatorBuilder { + auth_flow: InstalledFlow { + force_account_selection: force, + ..self.auth_flow + }, + ..self + } + } /// Create the authenticator. pub async fn build(self) -> io::Result> diff --git a/src/installed.rs b/src/installed.rs index bba20b2..cab6d71 100644 --- a/src/installed.rs +++ b/src/installed.rs @@ -33,6 +33,7 @@ fn build_authentication_request_url( client_id: &str, scopes: &[T], redirect_uri: Option<&str>, + force_account_selection: bool, ) -> String where T: AsRef, @@ -51,13 +52,17 @@ where } } - vec![ + let mut params = vec![ format!("scope={}", scopes_string), "&access_type=offline".to_string(), format!("&redirect_uri={}", redirect_uri.unwrap_or(OOB_REDIRECT_URI)), "&response_type=code".to_string(), format!("&client_id={}", client_id), - ] + ]; + if force_account_selection { + params.push("&prompt=select_account+consent".to_string()); + } + params .into_iter() .fold(url, |mut u, param| { u.push_str(&percent_encode(param.as_ref(), &QUERY_SET).to_string()); @@ -87,6 +92,7 @@ pub struct InstalledFlow { pub(crate) app_secret: ApplicationSecret, pub(crate) method: InstalledFlowReturnMethod, pub(crate) flow_delegate: Box, + pub(crate) force_account_selection: bool, } impl InstalledFlow { @@ -99,6 +105,7 @@ impl InstalledFlow { app_secret, method, flow_delegate: Box::new(DefaultInstalledFlowDelegate), + force_account_selection: false } } @@ -154,6 +161,7 @@ impl InstalledFlow { &app_secret.client_id, scopes, self.flow_delegate.redirect_uri(), + self.force_account_selection, ); log::debug!("Presenting auth url to user: {}", url); let auth_code = self @@ -196,6 +204,7 @@ impl InstalledFlow { &app_secret.client_id, scopes, Some(redirect_uri.as_ref()), + self.force_account_selection, ); log::debug!("Presenting auth url to user: {}", url); let _ = self @@ -406,7 +415,8 @@ mod tests { "812741506391-h38jh0j4fv0ce1krdkiq0hfvt6n5am\ rf.apps.googleusercontent.com", &["email", "profile"], - None + None, + false ) ); } @@ -423,7 +433,8 @@ mod tests { "812741506391-h38jh0j4fv0ce1krdkiq0hfvt6n5am\ rf.apps.googleusercontent.com", &["email", "profile"], - None + None, + false ) ); }