Skip to main content

simple_oauth/
provider.rs

1use std::{fmt::Debug, sync::Arc};
2
3use crate::types::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/// Trait for OAuth providers that support fetching normalized user info.
16pub trait UserInfoProvider: SimpleOAuthProvider {
17    /// The URL to fetch the user info from the provider
18    fn user_info_url(&self) -> &str;
19    /// Extract the user data from the provider's user response
20    fn extract_user_info(&self, val: serde_json::Value) -> Result<UserInfo, serde_json::Error>;
21    /// Additional headers to send when making user info requests to the provider
22    fn user_info_headers(&self) -> Vec<(String, String)> {
23        vec![]
24    }
25}
26
27impl<T> SimpleOAuthProvider for Box<T>
28where
29    T: SimpleOAuthProvider + ?Sized,
30{
31    fn authorize_url(&self) -> &str {
32        (**self).authorize_url()
33    }
34    fn token_url(&self) -> &str {
35        (**self).token_url()
36    }
37    fn default_scopes(&self) -> &'static [&'static str] {
38        (**self).default_scopes()
39    }
40}
41
42impl<T> UserInfoProvider for Box<T>
43where
44    T: UserInfoProvider + ?Sized,
45{
46    fn user_info_url(&self) -> &str {
47        (**self).user_info_url()
48    }
49    fn extract_user_info(&self, val: serde_json::Value) -> Result<UserInfo, serde_json::Error> {
50        (**self).extract_user_info(val)
51    }
52    fn user_info_headers(&self) -> Vec<(String, String)> {
53        (**self).user_info_headers()
54    }
55}
56
57impl<T> SimpleOAuthProvider for Arc<T>
58where
59    T: SimpleOAuthProvider + ?Sized,
60{
61    fn authorize_url(&self) -> &str {
62        (**self).authorize_url()
63    }
64    fn token_url(&self) -> &str {
65        (**self).token_url()
66    }
67    fn default_scopes(&self) -> &'static [&'static str] {
68        (**self).default_scopes()
69    }
70}
71
72impl<T> UserInfoProvider for Arc<T>
73where
74    T: UserInfoProvider + ?Sized,
75{
76    fn user_info_url(&self) -> &str {
77        (**self).user_info_url()
78    }
79    fn extract_user_info(&self, val: serde_json::Value) -> Result<UserInfo, serde_json::Error> {
80        (**self).extract_user_info(val)
81    }
82    fn user_info_headers(&self) -> Vec<(String, String)> {
83        (**self).user_info_headers()
84    }
85}