diff --git a/ee/tabby-webserver/src/oauth/github.rs b/ee/tabby-webserver/src/oauth/github.rs index 69b500434401..6016100744af 100644 --- a/ee/tabby-webserver/src/oauth/github.rs +++ b/ee/tabby-webserver/src/oauth/github.rs @@ -123,15 +123,30 @@ impl OAuthClient for GithubClient { async fn get_authorization_url(&self) -> Result { let credentials = self.read_credential().await?; - let mut url = reqwest::Url::parse("https://github.com/login/oauth/authorize")?; - let params = vec![ - ("client_id", credentials.client_id.as_str()), - ("response_type", "code"), - ("scope", "read:user user:email"), - ]; - for (k, v) in params { - url.query_pairs_mut().append_pair(k, v); - } - Ok(url.to_string()) + create_authorization_url(&credentials.client_id) + } +} + +fn create_authorization_url(client_id: &str) -> Result { + let mut url = reqwest::Url::parse("https://github.com/login/oauth/authorize")?; + let params = vec![ + ("client_id", client_id), + ("response_type", "code"), + ("scope", "read:user user:email"), + ]; + for (k, v) in params { + url.query_pairs_mut().append_pair(k, v); + } + Ok(url.to_string()) +} + +#[cfg(test)] +mod tests { + use super::create_authorization_url; + + #[test] + fn test_create_authorization_url() { + let url = create_authorization_url("client_id").unwrap(); + assert_eq!(url, "https://github.com/login/oauth/authorize?client_id=client_id&response_type=code&scope=read%3Auser+user%3Aemail"); } } diff --git a/ee/tabby-webserver/src/oauth/google.rs b/ee/tabby-webserver/src/oauth/google.rs index 0da526144854..ccba46ee4829 100644 --- a/ee/tabby-webserver/src/oauth/google.rs +++ b/ee/tabby-webserver/src/oauth/google.rs @@ -122,17 +122,32 @@ impl OAuthClient for GoogleClient { async fn get_authorization_url(&self) -> Result { let credential = self.read_credential().await?; let redirect_uri = self.auth.oauth_callback_url(OAuthProvider::Google).await?; - let mut url = reqwest::Url::parse("https://accounts.google.com/o/oauth2/v2/auth")?; - let params = vec![ - ("client_id", credential.client_id.as_str()), - ("redirect_uri", redirect_uri.as_str()), - ("response_type", "code"), - ("scope", "https://www.googleapis.com/auth/userinfo.email"), - ("access_type", "offline"), - ]; - for (k, v) in params { - url.query_pairs_mut().append_pair(k, v); - } - Ok(url.to_string()) + create_authorization_url(&credential.client_id, &redirect_uri) + } +} + +fn create_authorization_url(client_id: &str, redirect_uri: &str) -> Result { + let mut url = reqwest::Url::parse("https://accounts.google.com/o/oauth2/v2/auth")?; + let params = vec![ + ("client_id", client_id), + ("redirect_uri", redirect_uri), + ("response_type", "code"), + ("scope", "https://www.googleapis.com/auth/userinfo.email"), + ("access_type", "offline"), + ]; + for (k, v) in params { + url.query_pairs_mut().append_pair(k, v); + } + Ok(url.to_string()) +} + +#[cfg(test)] +mod tests { + use super::create_authorization_url; + + #[test] + fn test_create_authorization_url() { + let url = create_authorization_url("client_id", "localhost").unwrap(); + assert_eq!(url, "https://accounts.google.com/o/oauth2/v2/auth?client_id=client_id&redirect_uri=localhost&response_type=code&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email&access_type=offline"); } }