Ported from gitlab to gitea

This commit is contained in:
2023-02-11 01:24:54 +01:00
parent c687058a06
commit 2ea430817f
3 changed files with 23 additions and 30 deletions

View File

@@ -24,22 +24,28 @@ pub struct GitlabUser {
}
pub static REDIRECT_URL: Lazy<String> = Lazy::new(|| CONFIG.gitlab_redirect_url.clone() + "/api/auth/gitlab_callback");
pub static TOKEN_URL: Lazy<String> = Lazy::new(|| format!("{}/oauth/token", CONFIG.gitlab_api_url.clone()));
pub static USER_URL: Lazy<String> = Lazy::new(|| format!("{}/api/v4/user", CONFIG.gitlab_api_url.clone()));
pub static AUTHORIZE_URL: Lazy<String> = Lazy::new(|| format!("{}/oauth/authorize", CONFIG.gitlab_url.clone()));
pub static TOKEN_URL: Lazy<String> = Lazy::new(|| format!("{}/login/oauth/access_token", CONFIG.gitlab_api_url.clone()));
pub static USER_URL: Lazy<String> = Lazy::new(|| format!("{}/api/v1/user", CONFIG.gitlab_api_url.clone()));
pub static AUTHORIZE_URL: Lazy<String> = Lazy::new(|| format!("{}/login/oauth/authorize", CONFIG.gitlab_url.clone()));
pub fn get_gitlab_token(span: &Span, code_or_token: String, token: bool) -> Option<GitlabTokens> {
let _span = metrics::span("get_gitlab_token", span);
let mut req = ureq::post(&TOKEN_URL)
.query("redirect_uri", &REDIRECT_URL)
.query("client_id", &CONFIG.gitlab_id)
.query("client_secret", &CONFIG.gitlab_secret);
if token {
req = req.query("refresh_token", &code_or_token).query("grant_type", "refresh_token");
} else {
req = req.query("code", &code_or_token).query("grant_type", "authorization_code");
}
req.call().ok()?.into_json().ok()
let grant_type = match token {
true => "refresh_token",
false => "authorization_code"
};
let code_name = match token {
true => "refresh_token",
false => "code"
};
ureq::post(&TOKEN_URL)
.send_json(ureq::json! ({
"grant_type": grant_type,
"client_id": &CONFIG.gitlab_id,
"client_secret": &CONFIG.gitlab_secret,
"redirect_uri": *REDIRECT_URL,
code_name: code_or_token
})).ok()?.into_json().ok()
}
pub fn get_gitlab_user(span: &Span, token: String) -> Option<GitlabUser> {