Broker

Struct Broker 

Source
pub struct Broker<C, M>{
    pub http_client: Arc<C>,
    pub transport_mapper: Arc<M>,
    pub store: Arc<dyn BrokerStore>,
    pub descriptor: ProviderDescriptor,
    pub strategy: Arc<dyn ProviderStrategy>,
    pub client_id: String,
    pub client_secret: Option<String>,
    pub refresh_metrics: Arc<RefreshMetrics>,
    /* private fields */
}
Expand description

Coordinates OAuth 2.0 flows against a single provider descriptor.

The broker owns the HTTP client, token store, provider descriptor, and strategy references so individual flow implementations can focus on grant-specific logic (state + PKCE generation, code exchanges, refresh rotations, etc.). Client credentials are stored alongside the descriptor so client-auth methods can be applied consistently across token endpoints.

Fields§

§http_client: Arc<C>

HTTP client wrapper used for every outbound provider request.

§transport_mapper: Arc<M>

Mapper applied to transport-layer errors before surfacing them to callers.

§store: Arc<dyn BrokerStore>

Token store implementation that persists issued secrets.

§descriptor: ProviderDescriptor

Provider descriptor that defines OAuth endpoints and quirks.

§strategy: Arc<dyn ProviderStrategy>

Strategy responsible for provider-specific token request adjustments.

§client_id: String

OAuth 2.0 client identifier used in every grant.

§client_secret: Option<String>

Optional client secret for confidential authentication methods.

§refresh_metrics: Arc<RefreshMetrics>

Shared metrics recorder for refresh flow outcomes.

Implementations§

Source§

impl<C, M> Broker<C, M>

Source

pub fn start_authorization( &self, tenant: TenantId, principal: PrincipalId, scope: ScopeSet, redirect_uri: Url, ) -> Result<AuthorizationSession>

Generates an Authorization Code + PKCE session for the provided tenant context.

Calling this method verifies that the backing descriptor advertises authorization_code support, builds a cryptographically strong state, and attaches a PKCE verifier/challenge pair. The resulting AuthorizationSession exposes accessor methods that UI layers can use to embed the authorize URL in a link or form, while backend handlers can persist the tenant/principal/scope context alongside the opaque state for later validation.

The broker does not automatically persist the session — it is the caller’s responsibility to stash it (or the relevant fields) until the redirect round-trip completes so AuthorizationSession::validate_state can run before an exchange.

Source

pub async fn exchange_code( &self, session: AuthorizationSession, authorization_code: impl AsRef<str>, ) -> Result<TokenRecord>

Exchanges an authorization code + PKCE verifier for broker-managed tokens.

The AuthorizationSession generated by Broker::start_authorization carries the tenant/principal/scope context, redirect URI, and PKCE verifier needed to process the callback. Once the provider redirects back with a code, call this method with the original session and the returned code. Successful exchanges emit a TokenRecord that has already been written to the configured BrokerStore so subsequent fetches observe the latest secrets.

Source§

impl<C, M> Broker<C, M>

Source

pub async fn refresh_access_token( &self, request: CachedTokenRequest, ) -> Result<TokenRecord>

Refreshes the cached token family, performing CAS rotation + singleflight guards.

Source§

impl<C, M> Broker<C, M>

Source

pub async fn client_credentials( &self, request: CachedTokenRequest, ) -> Result<TokenRecord>

Performs the client_credentials grant with caching + singleflight guards.

Source§

impl<C, M> Broker<C, M>

Source

pub fn with_http_client( store: Arc<dyn BrokerStore>, descriptor: ProviderDescriptor, strategy: Arc<dyn ProviderStrategy>, client_id: impl Into<String>, http_client: impl Into<Arc<C>>, mapper: impl Into<Arc<M>>, ) -> Self

Creates a broker that reuses the caller-provided transport + mapper pair.

Source

pub fn with_client_secret(self, secret: impl Into<String>) -> Self

Sets or replaces the client secret used for confidential client auth modes.

Source§

impl Broker<ReqwestHttpClient, ReqwestTransportErrorMapper>

Source

pub fn new( store: Arc<dyn BrokerStore>, descriptor: ProviderDescriptor, strategy: Arc<dyn ProviderStrategy>, client_id: impl Into<String>, ) -> Self

Creates a new broker for the provided descriptor and client identifier.

The broker provisions its own reqwest-backed transport so callers do not need to pass HTTP handles explicitly. Use Broker::with_client_secret to attach a confidential client secret when the descriptor prefers client_secret_basic or client_secret_post.

Trait Implementations§

Source§

impl<C, M> Clone for Broker<C, M>

Source§

fn clone(&self) -> Broker<C, M>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<C, M> Debug for Broker<C, M>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<C, M> Freeze for Broker<C, M>
where C: ?Sized, M: ?Sized,

§

impl<C, M> !RefUnwindSafe for Broker<C, M>

§

impl<C, M> Send for Broker<C, M>
where C: ?Sized, M: ?Sized,

§

impl<C, M> Sync for Broker<C, M>
where C: ?Sized, M: ?Sized,

§

impl<C, M> Unpin for Broker<C, M>
where C: ?Sized, M: ?Sized,

§

impl<C, M> !UnwindSafe for Broker<C, M>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more