1pub mod auth_code_pkce;
4pub mod common;
5pub mod refresh;
6
7mod client_credentials;
8
9pub use auth_code_pkce::*;
10pub use common::*;
11pub use refresh::*;
12
13use crate::{
15 _prelude::*,
16 http::TokenHttpClient,
17 oauth::TransportErrorMapper,
18 provider::{ProviderDescriptor, ProviderStrategy},
19 store::{BrokerStore, StoreKey},
20};
21#[cfg(feature = "reqwest")]
22use crate::{http::ReqwestHttpClient, oauth::ReqwestTransportErrorMapper};
23
24#[cfg(feature = "reqwest")]
25pub type ReqwestBroker = Broker<ReqwestHttpClient, ReqwestTransportErrorMapper>;
27
28#[derive(Clone)]
36pub struct Broker<C, M>
37where
38 C: ?Sized + TokenHttpClient,
39 M: ?Sized + TransportErrorMapper<C::TransportError>,
40{
41 pub http_client: Arc<C>,
43 pub transport_mapper: Arc<M>,
45 pub store: Arc<dyn BrokerStore>,
47 pub descriptor: ProviderDescriptor,
49 pub strategy: Arc<dyn ProviderStrategy>,
51 pub client_id: String,
53 pub client_secret: Option<String>,
55 pub refresh_metrics: Arc<RefreshMetrics>,
57 flow_guards: Arc<Mutex<HashMap<StoreKey, Arc<AsyncMutex<()>>>>>,
58}
59impl<C, M> Broker<C, M>
60where
61 C: ?Sized + TokenHttpClient,
62 M: ?Sized + TransportErrorMapper<C::TransportError>,
63{
64 pub fn with_http_client(
66 store: Arc<dyn BrokerStore>,
67 descriptor: ProviderDescriptor,
68 strategy: Arc<dyn ProviderStrategy>,
69 client_id: impl Into<String>,
70 http_client: impl Into<Arc<C>>,
71 mapper: impl Into<Arc<M>>,
72 ) -> Self {
73 Self {
74 http_client: http_client.into(),
75 transport_mapper: mapper.into(),
76 store,
77 descriptor,
78 strategy,
79 client_id: client_id.into(),
80 client_secret: None,
81 flow_guards: Default::default(),
82 refresh_metrics: Default::default(),
83 }
84 }
85
86 pub fn with_client_secret(mut self, secret: impl Into<String>) -> Self {
88 self.client_secret = Some(secret.into());
89
90 self
91 }
92}
93#[cfg(feature = "reqwest")]
94impl Broker<ReqwestHttpClient, ReqwestTransportErrorMapper> {
95 pub fn new(
102 store: Arc<dyn BrokerStore>,
103 descriptor: ProviderDescriptor,
104 strategy: Arc<dyn ProviderStrategy>,
105 client_id: impl Into<String>,
106 ) -> Self {
107 Self::with_http_client(
108 store,
109 descriptor,
110 strategy,
111 client_id,
112 ReqwestHttpClient::default(),
113 Arc::new(ReqwestTransportErrorMapper),
114 )
115 }
116}
117impl<C, M> Debug for Broker<C, M>
118where
119 C: ?Sized + TokenHttpClient,
120 M: ?Sized + TransportErrorMapper<C::TransportError>,
121{
122 fn fmt(&self, f: &mut Formatter) -> FmtResult {
123 f.debug_struct("Broker")
124 .field("descriptor", &self.descriptor)
125 .field("client_id", &self.client_id)
126 .field("client_secret_set", &self.client_secret.is_some())
127 .finish()
128 }
129}