Skip to main content

simple_oauth/
provider.rs

1use std::{fmt::Debug, sync::Arc};
2
3use crate::types::{OAuthCredentials, TokenAuthMethod, UserInfo};
4
5/// Trait for all OAuth providers
6pub trait SimpleOAuthProvider: Debug + Send + Sync {
7    /// The authorization endpoint of the provider
8    fn authorize_url(&self) -> &str;
9    /// The token endpoint of the provider
10    fn token_url(&self) -> &str;
11    /// Default scopes used when building the provider's authorization URL.
12    fn default_scopes(&self) -> &'static [&'static str] {
13        &[]
14    }
15    /// How the OAuth client should authenticate to the provider's token endpoint.
16    fn token_auth_method(&self) -> TokenAuthMethod {
17        TokenAuthMethod::BasicAuth
18    }
19}
20
21/// Trait for OAuth providers that support fetching normalized user info.
22pub trait UserInfoProvider: SimpleOAuthProvider {
23    /// The URL to fetch the user info from the provider
24    fn user_info_url(&self) -> &str;
25    /// Extract the user data from the provider's user response
26    fn extract_user_info(&self, val: serde_json::Value) -> Result<UserInfo, serde_json::Error>;
27    /// Additional headers to send when making user info requests to the provider
28    fn user_info_headers(&self, _credentials: &OAuthCredentials) -> Vec<(String, String)> {
29        vec![]
30    }
31}
32
33impl<T> SimpleOAuthProvider for Box<T>
34where
35    T: SimpleOAuthProvider + ?Sized,
36{
37    fn authorize_url(&self) -> &str {
38        (**self).authorize_url()
39    }
40    fn token_url(&self) -> &str {
41        (**self).token_url()
42    }
43    fn default_scopes(&self) -> &'static [&'static str] {
44        (**self).default_scopes()
45    }
46    fn token_auth_method(&self) -> TokenAuthMethod {
47        (**self).token_auth_method()
48    }
49}
50
51impl<T> UserInfoProvider for Box<T>
52where
53    T: UserInfoProvider + ?Sized,
54{
55    fn user_info_url(&self) -> &str {
56        (**self).user_info_url()
57    }
58    fn extract_user_info(&self, val: serde_json::Value) -> Result<UserInfo, serde_json::Error> {
59        (**self).extract_user_info(val)
60    }
61    fn user_info_headers(&self, credentials: &OAuthCredentials) -> Vec<(String, String)> {
62        (**self).user_info_headers(credentials)
63    }
64}
65
66impl<T> SimpleOAuthProvider for Arc<T>
67where
68    T: SimpleOAuthProvider + ?Sized,
69{
70    fn authorize_url(&self) -> &str {
71        (**self).authorize_url()
72    }
73    fn token_url(&self) -> &str {
74        (**self).token_url()
75    }
76    fn default_scopes(&self) -> &'static [&'static str] {
77        (**self).default_scopes()
78    }
79    fn token_auth_method(&self) -> TokenAuthMethod {
80        (**self).token_auth_method()
81    }
82}
83
84impl<T> UserInfoProvider for Arc<T>
85where
86    T: UserInfoProvider + ?Sized,
87{
88    fn user_info_url(&self) -> &str {
89        (**self).user_info_url()
90    }
91    fn extract_user_info(&self, val: serde_json::Value) -> Result<UserInfo, serde_json::Error> {
92        (**self).extract_user_info(val)
93    }
94    fn user_info_headers(&self, credentials: &OAuthCredentials) -> Vec<(String, String)> {
95        (**self).user_info_headers(credentials)
96    }
97}