Skip to content

Commit

Permalink
test(webserver): test oauth url formats (#2073)
Browse files Browse the repository at this point in the history
* feat(ui): filtering repos by active status

relay

feat(ui): filtering repos by active status

* fetching

* rename

* sort

* update

* test(webserver): test oauth url formats

* Fix tests

---------

Co-authored-by: liangfung <[email protected]>
  • Loading branch information
boxbeam and liangfung authored May 10, 2024
1 parent 76df681 commit 5778507
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 22 deletions.
35 changes: 25 additions & 10 deletions ee/tabby-webserver/src/oauth/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,30 @@ impl OAuthClient for GithubClient {

async fn get_authorization_url(&self) -> Result<String> {
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<String> {
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");
}
}
39 changes: 27 additions & 12 deletions ee/tabby-webserver/src/oauth/google.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,17 +122,32 @@ impl OAuthClient for GoogleClient {
async fn get_authorization_url(&self) -> Result<String> {
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<String> {
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");
}
}

0 comments on commit 5778507

Please sign in to comment.