Skip to main content

openauth_oauth/oauth2/
provider.rs

1use std::future::Future;
2use std::pin::Pin;
3
4use serde_json::Value;
5use url::Url;
6
7use super::authorization_url::AuthorizationUrlRequest;
8use super::error::OAuthError;
9use super::tokens::{OAuth2Tokens, OAuth2UserInfo, ProviderOptions};
10
11/// Minimal OAuth provider metadata.
12#[derive(Debug, Clone, PartialEq, Eq)]
13pub struct OAuthProviderMetadata {
14    id: String,
15    name: String,
16}
17
18impl OAuthProviderMetadata {
19    pub fn new(id: impl Into<String>, name: impl Into<String>) -> Self {
20        Self {
21            id: id.into(),
22            name: name.into(),
23        }
24    }
25
26    pub fn id(&self) -> &str {
27        &self.id
28    }
29
30    pub fn name(&self) -> &str {
31        &self.name
32    }
33}
34
35/// Minimal public contract shared by OAuth provider implementations.
36pub trait OAuthProviderContract {
37    fn id(&self) -> &str;
38    fn name(&self) -> &str;
39}
40
41impl OAuthProviderContract for OAuthProviderMetadata {
42    fn id(&self) -> &str {
43        self.id()
44    }
45
46    fn name(&self) -> &str {
47        self.name()
48    }
49}
50
51pub type SocialProviderFuture<'a, T> =
52    Pin<Box<dyn Future<Output = Result<T, OAuthError>> + Send + 'a>>;
53
54#[derive(Debug, Clone, Default, PartialEq, Eq)]
55pub struct SocialAuthorizationUrlRequest {
56    pub state: String,
57    pub redirect_uri: String,
58    pub code_verifier: Option<String>,
59    pub scopes: Vec<String>,
60    pub login_hint: Option<String>,
61}
62
63impl SocialAuthorizationUrlRequest {
64    pub fn into_authorization_url_request(
65        self,
66        id: impl Into<String>,
67        options: ProviderOptions,
68        authorization_endpoint: impl Into<String>,
69        scopes: Vec<String>,
70    ) -> AuthorizationUrlRequest {
71        AuthorizationUrlRequest {
72            id: id.into(),
73            options,
74            authorization_endpoint: authorization_endpoint.into(),
75            redirect_uri: self.redirect_uri,
76            state: self.state,
77            code_verifier: self.code_verifier,
78            scopes,
79            login_hint: self.login_hint,
80            ..AuthorizationUrlRequest::default()
81        }
82    }
83}
84
85#[derive(Debug, Clone, Default, PartialEq, Eq)]
86pub struct SocialAuthorizationCodeRequest {
87    pub code: String,
88    pub code_verifier: Option<String>,
89    pub redirect_uri: String,
90    pub device_id: Option<String>,
91}
92
93#[derive(Debug, Clone, Default, PartialEq)]
94pub struct SocialIdTokenRequest {
95    pub token: String,
96    pub nonce: Option<String>,
97    pub access_token: Option<String>,
98    pub refresh_token: Option<String>,
99    pub scopes: Vec<String>,
100    pub provider_user: Option<Value>,
101}
102
103pub trait SocialOAuthProvider: Send + Sync + 'static {
104    fn id(&self) -> &str;
105    fn name(&self) -> &str;
106    fn provider_options(&self) -> ProviderOptions;
107
108    fn create_authorization_url(
109        &self,
110        input: SocialAuthorizationUrlRequest,
111    ) -> Result<Url, OAuthError>;
112
113    fn validate_authorization_code(
114        &self,
115        input: SocialAuthorizationCodeRequest,
116    ) -> SocialProviderFuture<'_, OAuth2Tokens>;
117
118    fn get_user_info(
119        &self,
120        tokens: OAuth2Tokens,
121        provider_user: Option<Value>,
122    ) -> SocialProviderFuture<'_, Option<OAuth2UserInfo>>;
123
124    fn verify_id_token(&self, _input: SocialIdTokenRequest) -> SocialProviderFuture<'_, bool> {
125        Box::pin(async { Ok(false) })
126    }
127
128    fn refresh_access_token(
129        &self,
130        _refresh_token: String,
131    ) -> SocialProviderFuture<'_, OAuth2Tokens> {
132        Box::pin(async move {
133            Err(OAuthError::InvalidResponse(
134                "provider does not support refresh tokens".to_owned(),
135            ))
136        })
137    }
138
139    fn revoke_token(&self, _token: String) -> SocialProviderFuture<'_, ()> {
140        Box::pin(async move {
141            Err(OAuthError::InvalidResponse(
142                "provider does not support token revocation".to_owned(),
143            ))
144        })
145    }
146}