oauth2_mastodon/
lib.rs

1use oauth2_client::re_exports::{
2    Deserialize_enum_str, Scope, Serialize_enum_str, Url, UrlParseError,
3};
4
5pub const BASE_URL_MASTODON_SOCIAL: &str = "https://mastodon.social/";
6
7pub mod authorization_code_grant;
8pub mod client_credentials_grant;
9pub mod resource_owner_password_credentials_grant;
10
11pub use authorization_code_grant::MastodonProviderForEndUsers;
12pub use client_credentials_grant::MastodonProviderForApplications;
13pub use resource_owner_password_credentials_grant::MastodonProviderForBots;
14
15pub mod extensions;
16pub use extensions::MastodonExtensionsBuilder;
17
18pub fn token_url(base_url: impl AsRef<str>) -> Result<Url, UrlParseError> {
19    Url::parse(base_url.as_ref())?.join("/oauth/token")
20}
21pub fn authorization_url(base_url: impl AsRef<str>) -> Result<Url, UrlParseError> {
22    Url::parse(base_url.as_ref())?.join("/oauth/authorize")
23}
24
25// Ref https://docs.joinmastodon.org/api/oauth-scopes/
26#[derive(Deserialize_enum_str, Serialize_enum_str, Debug, Clone, PartialEq, Eq)]
27pub enum MastodonScope {
28    //
29    //
30    //
31    #[serde(rename = "read")]
32    Read,
33    //
34    //
35    //
36    #[serde(rename = "write")]
37    Write,
38    //
39    //
40    //
41    #[serde(rename = "follow")]
42    Follow,
43    //
44    //
45    //
46    #[serde(rename = "push")]
47    Push,
48    //
49    //
50    //
51    #[serde(rename = "admin:read")]
52    AdminRead,
53    //
54    //
55    //
56    #[serde(rename = "admin:write")]
57    AdminWrite,
58    //
59    //
60    //
61    #[serde(rename = "crypto")]
62    Crypto,
63    //
64    //
65    //
66    #[serde(other)]
67    Other(String),
68}
69impl Scope for MastodonScope {}
70
71#[cfg(test)]
72mod tests {
73    use super::*;
74
75    #[test]
76    fn test_token_url() {
77        assert_eq!(
78            token_url("https://mastodon.social/").unwrap().as_str(),
79            "https://mastodon.social/oauth/token"
80        );
81        assert_eq!(
82            token_url("https://mastodon.social").unwrap().as_str(),
83            "https://mastodon.social/oauth/token"
84        );
85    }
86
87    #[test]
88    fn test_authorization_url() {
89        assert_eq!(
90            authorization_url("https://mastodon.social/")
91                .unwrap()
92                .as_str(),
93            "https://mastodon.social/oauth/authorize"
94        );
95        assert_eq!(
96            authorization_url("https://mastodon.social")
97                .unwrap()
98                .as_str(),
99            "https://mastodon.social/oauth/authorize"
100        );
101    }
102}