oauth2_client/jwt_authorization_grant/
provider_ext.rs1use dyn_clone::{clone_trait_object, DynClone};
2pub use oauth2_core::access_token_request::BodyWithJwtAuthorizationGrant;
3
4use crate::{
5 re_exports::{Body, ClientId, ClientSecret, Map, Request, Scope, Url, Value},
6 Provider,
7};
8
9pub trait ProviderExtJwtAuthorizationGrant: Provider + DynClone {
11 fn assertion(&self) -> &str;
12
13 fn scopes_default(&self) -> Option<Vec<<Self as Provider>::Scope>> {
14 None
15 }
16
17 fn access_token_request_body_extra(
18 &self,
19 _body: &BodyWithJwtAuthorizationGrant<<Self as Provider>::Scope>,
20 ) -> Option<Result<Map<String, Value>, Box<dyn std::error::Error + Send + Sync + 'static>>>
21 {
22 None
23 }
24
25 fn access_token_request_url_modifying(&self, _url: &mut Url) {}
26
27 fn access_token_request_modifying(&self, _request: &mut Request<Body>) {}
28}
29
30clone_trait_object!(<SCOPE> ProviderExtJwtAuthorizationGrant<Scope = SCOPE> where SCOPE: Scope + Clone);
31
32impl<SCOPE> core::fmt::Debug for dyn ProviderExtJwtAuthorizationGrant<Scope = SCOPE> + Send + Sync
33where
34 SCOPE: Scope,
35{
36 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
37 f.debug_struct("ProviderExtJwtAuthorizationGrant")
38 .field("client_id", &self.client_id())
39 .field("token_endpoint_url", &self.token_endpoint_url().as_str())
40 .field("scopes_default", &self.scopes_default())
41 .finish()
42 }
43}
44
45#[derive(Debug, Clone)]
49pub struct ProviderExtJwtAuthorizationGrantStringScopeWrapper<P>
50where
51 P: ProviderExtJwtAuthorizationGrant,
52{
53 inner: P,
54}
55
56impl<P> ProviderExtJwtAuthorizationGrantStringScopeWrapper<P>
57where
58 P: ProviderExtJwtAuthorizationGrant,
59{
60 pub fn new(provider: P) -> Self {
61 Self { inner: provider }
62 }
63}
64
65impl<P> Provider for ProviderExtJwtAuthorizationGrantStringScopeWrapper<P>
66where
67 P: ProviderExtJwtAuthorizationGrant + Clone,
68{
69 type Scope = String;
70
71 fn client_id(&self) -> Option<&ClientId> {
72 self.inner.client_id()
73 }
74
75 fn client_secret(&self) -> Option<&ClientSecret> {
76 self.inner.client_secret()
77 }
78
79 fn token_endpoint_url(&self) -> &Url {
80 self.inner.token_endpoint_url()
81 }
82
83 fn extra(&self) -> Option<Map<String, Value>> {
84 self.inner.extra()
85 }
86
87 }
89
90impl<P> ProviderExtJwtAuthorizationGrant for ProviderExtJwtAuthorizationGrantStringScopeWrapper<P>
91where
92 P: ProviderExtJwtAuthorizationGrant + Clone,
93{
94 fn assertion(&self) -> &str {
95 self.inner.assertion()
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: &BodyWithJwtAuthorizationGrant<<Self as Provider>::Scope>,
107 ) -> Option<Result<Map<String, Value>, Box<dyn std::error::Error + Send + Sync + 'static>>>
108 {
109 let body =
110 match BodyWithJwtAuthorizationGrant::<<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 fn access_token_request_url_modifying(&self, url: &mut Url) {
121 self.inner.access_token_request_url_modifying(url)
122 }
123
124 fn access_token_request_modifying(&self, request: &mut Request<Body>) {
125 self.inner.access_token_request_modifying(request)
126 }
127 }