armature_auth/providers/
gitlab.rs1use crate::error::AuthError;
4use crate::oauth2::OAuth2Config;
5use serde::{Deserialize, Serialize};
6
7const AUTH_URL: &str = "https://gitlab.com/oauth/authorize";
8const TOKEN_URL: &str = "https://gitlab.com/oauth/token";
9const USER_INFO_URL: &str = "https://gitlab.com/api/v4/user";
10
11#[derive(Debug, Clone, Serialize, Deserialize)]
12pub struct GitLabUser {
13 pub id: u64,
14 pub username: String,
15 pub name: String,
16 pub email: String,
17 pub avatar_url: Option<String>,
18 pub state: String,
19}
20
21pub struct GitLabProvider;
22
23impl GitLabProvider {
24 pub fn config(client_id: String, client_secret: String, redirect_url: String) -> OAuth2Config {
26 OAuth2Config::new(
27 client_id,
28 client_secret,
29 AUTH_URL.to_string(),
30 TOKEN_URL.to_string(),
31 redirect_url,
32 )
33 .with_scopes(vec!["read_user".to_string()])
34 .with_user_info_url(USER_INFO_URL.to_string())
35 }
36
37 pub async fn get_user_info(access_token: &str) -> Result<GitLabUser, AuthError> {
38 let client = reqwest::Client::new();
39
40 let user: GitLabUser = client
41 .get(USER_INFO_URL)
42 .header("Authorization", format!("Bearer {}", access_token))
43 .send()
44 .await
45 .map_err(|e| AuthError::HttpRequest(e.to_string()))?
46 .json()
47 .await
48 .map_err(|e| AuthError::InvalidResponse(e.to_string()))?;
49
50 Ok(user)
51 }
52}