oauth2_client/resource_owner_password_credentials_grant/
provider_ext.rs

1use dyn_clone::{clone_trait_object, DynClone};
2pub use oauth2_core::access_token_request::BodyWithResourceOwnerPasswordCredentialsGrant;
3
4use crate::{
5    re_exports::{ClientId, ClientSecret, Map, Scope, Url, Value},
6    Provider,
7};
8
9//
10pub trait ProviderExtResourceOwnerPasswordCredentialsGrant: Provider + DynClone {
11    fn client_password_in_request_body(&self) -> bool {
12        false
13    }
14
15    fn scopes_default(&self) -> Option<Vec<<Self as Provider>::Scope>> {
16        None
17    }
18
19    fn access_token_request_body_extra(
20        &self,
21        _body: &BodyWithResourceOwnerPasswordCredentialsGrant<<Self as Provider>::Scope>,
22    ) -> Option<Result<Map<String, Value>, Box<dyn std::error::Error + Send + Sync + 'static>>>
23    {
24        None
25    }
26}
27
28clone_trait_object!(<SCOPE> ProviderExtResourceOwnerPasswordCredentialsGrant<Scope = SCOPE> where SCOPE: Scope + Clone);
29
30impl<SCOPE> core::fmt::Debug
31    for dyn ProviderExtResourceOwnerPasswordCredentialsGrant<Scope = SCOPE> + Send + Sync
32where
33    SCOPE: Scope,
34{
35    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
36        f.debug_struct("ProviderExtResourceOwnerPasswordCredentialsGrant")
37            .field("client_id", &self.client_id())
38            .field("token_endpoint_url", &self.token_endpoint_url().as_str())
39            .field("scopes_default", &self.scopes_default())
40            .finish()
41    }
42}
43
44//
45//
46//
47#[derive(Debug, Clone)]
48pub struct ProviderExtResourceOwnerPasswordCredentialsGrantStringScopeWrapper<P>
49where
50    P: ProviderExtResourceOwnerPasswordCredentialsGrant,
51{
52    inner: P,
53}
54
55impl<P> ProviderExtResourceOwnerPasswordCredentialsGrantStringScopeWrapper<P>
56where
57    P: ProviderExtResourceOwnerPasswordCredentialsGrant,
58{
59    pub fn new(provider: P) -> Self {
60        Self { inner: provider }
61    }
62}
63
64impl<P> Provider for ProviderExtResourceOwnerPasswordCredentialsGrantStringScopeWrapper<P>
65where
66    P: ProviderExtResourceOwnerPasswordCredentialsGrant + Clone,
67{
68    type Scope = String;
69
70    fn client_id(&self) -> Option<&ClientId> {
71        self.inner.client_id()
72    }
73
74    fn client_secret(&self) -> Option<&ClientSecret> {
75        self.inner.client_secret()
76    }
77
78    fn token_endpoint_url(&self) -> &Url {
79        self.inner.token_endpoint_url()
80    }
81
82    fn extra(&self) -> Option<Map<String, Value>> {
83        self.inner.extra()
84    }
85
86    // Note
87}
88
89impl<P> ProviderExtResourceOwnerPasswordCredentialsGrant
90    for ProviderExtResourceOwnerPasswordCredentialsGrantStringScopeWrapper<P>
91where
92    P: ProviderExtResourceOwnerPasswordCredentialsGrant + Clone,
93{
94    fn client_password_in_request_body(&self) -> bool {
95        self.inner.client_password_in_request_body()
96    }
97
98    fn scopes_default(&self) -> Option<Vec<<Self as Provider>::Scope>> {
99        self.inner
100            .scopes_default()
101            .map(|x| x.iter().map(|y| y.to_string()).collect())
102    }
103
104    fn access_token_request_body_extra(
105        &self,
106        body: &BodyWithResourceOwnerPasswordCredentialsGrant<<Self as Provider>::Scope>,
107    ) -> Option<Result<Map<String, Value>, Box<dyn std::error::Error + Send + Sync + 'static>>>
108    {
109        let body =
110            match BodyWithResourceOwnerPasswordCredentialsGrant::<<P as Provider>::Scope>::try_from_t_with_string(
111                body,
112            ) {
113                Ok(x) => x,
114                Err(err) => return Some(Err(Box::new(err))),
115            };
116
117        self.inner.access_token_request_body_extra(&body)
118    }
119
120    // Note
121}