From 2f0d8d5384fb4eea6a6d5e5e48fd242f8d0c40fa Mon Sep 17 00:00:00 2001 From: Ruben Fiszel Date: Tue, 26 Jul 2022 01:52:36 +0200 Subject: [PATCH] fix(frontend): get refresh token for google services --- backend/src/oauth2.rs | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/backend/src/oauth2.rs b/backend/src/oauth2.rs index dce1526be4..fa89b79429 100644 --- a/backend/src/oauth2.rs +++ b/backend/src/oauth2.rs @@ -185,9 +185,19 @@ pub fn build_basic_client( login: bool, base_url: &str, ) -> (String, OClient) { - let auth_url = Url::parse(&config.auth_url).expect("Invalid authorization endpoint URL"); + let mut auth_url = Url::parse(&config.auth_url).expect("Invalid authorization endpoint URL"); let token_url = Url::parse(&config.token_url).expect("Invalid token endpoint URL"); + if ["gcal", "gdrive", "gsheets", "gcloud", "gmail"] + .into_iter() + .any(|x| x == name) + { + auth_url + .query_pairs_mut() + .append_pair("access_type", "offline") + .append_pair("prompt", "consent"); + } + let redirect_url = if login { format!("{base_url}/user/login_callback/{name}") } else { @@ -500,8 +510,7 @@ async fn connect_callback( .to_owned(); let token_response = - exchange_code::(callback, &cookies, client, &http_client, &client_name) - .await?; + exchange_code::(callback, &cookies, client, &http_client).await?; Ok(Json(token_response)) } @@ -518,8 +527,7 @@ async fn connect_slack_callback( .ok_or_else(|| error::Error::BadRequest("slack client not setup".to_string()))? .to_owned(); let token = - exchange_code::(callback, &cookies, client, &http_client, "slack") - .await?; + exchange_code::(callback, &cookies, client, &http_client).await?; Ok(Json(token)) } @@ -686,9 +694,7 @@ async fn login_callback( .ok_or_else(|| error::Error::BadRequest("invalid client".to_string()))? .client) .to_owned(); - let token_res = - exchange_code::(callback, &cookies, client, &http_client, "no_refresh") - .await; + let token_res = exchange_code::(callback, &cookies, client, &http_client).await; if let Ok(token) = token_res { let token = &token.access_token.to_string(); @@ -765,7 +771,6 @@ async fn exchange_code( cookies: &Cookies, client: OClient, http_client: &Client, - client_name: &str, ) -> error::Result { let csrf_state = cookies .get("csrf") @@ -774,16 +779,9 @@ async fn exchange_code( if callback.state != csrf_state { return Err(error::Error::BadRequest("csrf did not match".to_string())); } - let mut refresh_client = client.exchange_code(callback.code); - if ["gcal", "gdrive", "gsheets", "gcloud", "gmail"] - .into_iter() - .any(|x| x == client_name) - { - refresh_client = refresh_client - .param("access_type", "offline") - .param("prompt", "consent"); - } - refresh_client + + client + .exchange_code(callback.code) .with_client(http_client) .execute::() .await @@ -908,6 +906,7 @@ fn oauth_redirect( for scope in scopes_iter.iter() { client.add_scope(scope); } + let url = client.authorize_url(&state); set_cookie(&state, cookies); Ok(Redirect::to(url.as_str()))