1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
use oauth2_client::re_exports::{
    Deserialize_enum_str, Scope, Serialize_enum_str, Url, UrlParseError,
};

pub const BASE_URL_GITLAB_COM: &str = "https://gitlab.com/";

pub mod authorization_code_grant;

pub use authorization_code_grant::GitlabProviderForEndUsers;

pub mod extensions;
pub use extensions::GitlabExtensionsBuilder;

pub fn token_url(base_url: impl AsRef<str>) -> Result<Url, UrlParseError> {
    Url::parse(base_url.as_ref())?.join("/oauth/token")
}
pub fn authorization_url(base_url: impl AsRef<str>) -> Result<Url, UrlParseError> {
    Url::parse(base_url.as_ref())?.join("/oauth/authorize")
}

// Ref https://gitlab.com/-/profile/applications
#[derive(Deserialize_enum_str, Serialize_enum_str, Debug, Clone, PartialEq, Eq)]
pub enum GitlabScope {
    #[serde(rename = "api")]
    Api,
    #[serde(rename = "read_user")]
    ReadUser,
    #[serde(rename = "read_api")]
    ReadApi,
    #[serde(rename = "read_repository")]
    ReadRepository,
    #[serde(rename = "write_repository")]
    WriteRepository,
    #[serde(rename = "read_registry")]
    ReadRegistry,
    #[serde(rename = "write_registry")]
    WriteRegistry,
    #[serde(rename = "sudo")]
    Sudo,
    #[serde(rename = "openid")]
    Openid,
    #[serde(rename = "profile")]
    Profile,
    #[serde(rename = "email")]
    Email,
    //
    //
    //
    #[serde(other)]
    Other(String),
}
impl Scope for GitlabScope {}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_token_url() {
        assert_eq!(
            token_url("https://mastodon.social/").unwrap().as_str(),
            "https://mastodon.social/oauth/token"
        );
        assert_eq!(
            token_url("https://mastodon.social").unwrap().as_str(),
            "https://mastodon.social/oauth/token"
        );
    }

    #[test]
    fn test_authorization_url() {
        assert_eq!(
            authorization_url("https://mastodon.social/")
                .unwrap()
                .as_str(),
            "https://mastodon.social/oauth/authorize"
        );
        assert_eq!(
            authorization_url("https://mastodon.social")
                .unwrap()
                .as_str(),
            "https://mastodon.social/oauth/authorize"
        );
    }
}