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::{ReqwestHttpClient, TokenHttpClient},
17 oauth::{ReqwestTransportErrorMapper, TransportErrorMapper},
18 provider::{ProviderDescriptor, ProviderStrategy},
19 store::{BrokerStore, StoreKey},
20};
21
22#[derive(Clone)]
31pub struct Broker<C = ReqwestHttpClient, M = ReqwestTransportErrorMapper>
32where
33 C: ?Sized + TokenHttpClient,
34 M: ?Sized + TransportErrorMapper<C::TransportError>,
35{
36 pub http_client: Arc<C>,
38 pub transport_mapper: Arc<M>,
40 pub store: Arc<dyn BrokerStore>,
42 pub descriptor: ProviderDescriptor,
44 pub strategy: Arc<dyn ProviderStrategy>,
46 pub client_id: String,
48 pub client_secret: Option<String>,
50 pub refresh_metrics: Arc<RefreshMetrics>,
52 flow_guards: Arc<Mutex<HashMap<StoreKey, Arc<AsyncMutex<()>>>>>,
53}
54impl Broker {
55 pub fn new(
62 store: Arc<dyn BrokerStore>,
63 descriptor: ProviderDescriptor,
64 strategy: Arc<dyn ProviderStrategy>,
65 client_id: impl Into<String>,
66 ) -> Self {
67 Self::with_http_client(
68 store,
69 descriptor,
70 strategy,
71 client_id,
72 ReqwestHttpClient::default(),
73 Arc::new(ReqwestTransportErrorMapper),
74 )
75 }
76}
77impl<C, M> Broker<C, M>
78where
79 C: ?Sized + TokenHttpClient,
80 M: ?Sized + TransportErrorMapper<C::TransportError>,
81{
82 pub fn with_http_client(
84 store: Arc<dyn BrokerStore>,
85 descriptor: ProviderDescriptor,
86 strategy: Arc<dyn ProviderStrategy>,
87 client_id: impl Into<String>,
88 http_client: impl Into<Arc<C>>,
89 mapper: impl Into<Arc<M>>,
90 ) -> Self {
91 Self {
92 http_client: http_client.into(),
93 transport_mapper: mapper.into(),
94 store,
95 descriptor,
96 strategy,
97 client_id: client_id.into(),
98 client_secret: None,
99 flow_guards: Arc::new(Mutex::new(HashMap::new())),
100 refresh_metrics: Arc::new(RefreshMetrics::default()),
101 }
102 }
103
104 pub fn with_client_secret(mut self, secret: impl Into<String>) -> Self {
106 self.client_secret = Some(secret.into());
107
108 self
109 }
110}
111
112impl<C, M> Debug for Broker<C, M>
113where
114 C: ?Sized + TokenHttpClient,
115 M: ?Sized + TransportErrorMapper<C::TransportError>,
116{
117 fn fmt(&self, f: &mut Formatter) -> FmtResult {
118 f.debug_struct("Broker")
119 .field("descriptor", &self.descriptor)
120 .field("client_id", &self.client_id)
121 .field("client_secret_set", &self.client_secret.is_some())
122 .finish()
123 }
124}