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    /// The URL to fetch the user info from the provider
12    fn user_info_url(&self) -> &str;
13    /// Minimum scopes needed to get basic profile info (id, name, username). Email is not included
14    /// by default - the user can specify that by passing
15    /// in custom scopes when calling `client.authorize_url()`
16    fn default_scopes(&self) -> &'static [&'static str];
17    /// Extract the user data from the provider's user response
18    fn extract_user_info(&self, val: serde_json::Value) -> Result<UserInfo, serde_json::Error>;
19    /// Additional headers to send when making requests to the provider
20    fn additional_headers(&self) -> Vec<(String, String)> {
21        vec![]
22    }
23}
24
25impl<T> SimpleOAuthProvider for Box<T>
26where
27    T: SimpleOAuthProvider + ?Sized,
28{
29    fn authorize_url(&self) -> &str {
30        (**self).authorize_url()
31    }
32    fn token_url(&self) -> &str {
33        (**self).token_url()
34    }
35    fn user_info_url(&self) -> &str {
36        (**self).user_info_url()
37    }
38    fn default_scopes(&self) -> &'static [&'static str] {
39        (**self).default_scopes()
40    }
41    fn extract_user_info(&self, val: serde_json::Value) -> Result<UserInfo, serde_json::Error> {
42        (**self).extract_user_info(val)
43    }
44    fn additional_headers(&self) -> Vec<(String, String)> {
45        (**self).additional_headers()
46    }
47}
48
49impl<T> SimpleOAuthProvider for Arc<T>
50where
51    T: SimpleOAuthProvider + ?Sized,
52{
53    fn authorize_url(&self) -> &str {
54        (**self).authorize_url()
55    }
56    fn token_url(&self) -> &str {
57        (**self).token_url()
58    }
59    fn user_info_url(&self) -> &str {
60        (**self).user_info_url()
61    }
62    fn default_scopes(&self) -> &'static [&'static str] {
63        (**self).default_scopes()
64    }
65    fn extract_user_info(&self, val: serde_json::Value) -> Result<UserInfo, serde_json::Error> {
66        (**self).extract_user_info(val)
67    }
68    fn additional_headers(&self) -> Vec<(String, String)> {
69        (**self).additional_headers()
70    }
71}