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