Skip to main content

OAuth2ClientCredentials

Struct OAuth2ClientCredentials 

Source
pub struct OAuth2ClientCredentials { /* private fields */ }
Expand description

A TokenProvider implementing the OAuth 2.0 client credentials grant (RFC 6749 §4.4) with caching and proactive refresh.

  • Tokens are cached until shortly before expiry (with_refresh_leeway, default 30 s before expires_in elapses) and refreshed on demand.
  • Concurrent callers needing a refresh collapse into a single token request (single-flight).
  • The client secret is never logged, never echoed in errors, and redacted from Debug output.

Built on the crate’s own HTTP stack — no additional OAuth dependencies. With the default tls-rustls feature the token endpoint may be https:// (the norm); without it, only http:// endpoints are reachable and an https:// endpoint fails at construction with an actionable error.

Implementations§

Source§

impl OAuth2ClientCredentials

Source

pub fn new( token_url: impl Into<String>, client_id: impl Into<String>, client_secret: impl Into<String>, ) -> Self

Creates a provider for the given token endpoint and client credentials.

Source

pub fn from_agent_card( card: &AgentCard, scheme_name: &str, client_id: impl Into<String>, client_secret: impl Into<String>, ) -> ClientResult<Self>

Reads the token endpoint from an agent card’s OAuth 2.0 client-credentials flow.

scheme_name is the key in the card’s securitySchemes map. Request scopes with with_scopes — the card’s flow lists the scopes the agent offers (with descriptions); which of them to request is your decision.

§Errors

Returns ClientError::InvalidEndpoint when the scheme is missing, is not an OAuth 2.0 scheme, or has no client-credentials flow.

Source

pub async fn from_oidc_issuer( issuer: &str, client_id: impl Into<String>, client_secret: impl Into<String>, ) -> ClientResult<Self>

Discovers the token endpoint from an OIDC issuer (RFC 8414 / OIDC Discovery: {issuer}/.well-known/openid-configuration) and creates a provider for it.

§Errors

Returns a ClientError when the discovery document cannot be fetched or has no token_endpoint.

Source

pub fn with_scopes<I, S>(self, scopes: I) -> Self
where I: IntoIterator<Item = S>, S: Into<String>,

Sets the scopes to request (joined with spaces per RFC 6749 §3.3).

Source

pub fn with_audience(self, audience: impl Into<String>) -> Self

Sets the audience parameter (used by some authorization servers, e.g. Auth0, to select the target API).

Source

pub fn with_extra_param( self, key: impl Into<String>, value: impl Into<String>, ) -> Self

Adds an extra form parameter to the token request.

Source

pub const fn with_auth_style(self, style: TokenEndpointAuthStyle) -> Self

Sets how client credentials are presented (default: TokenEndpointAuthStyle::Basic).

Source

pub const fn with_refresh_leeway(self, leeway: Duration) -> Self

Sets how long before expiry a cached token is refreshed (default 30 s).

Source

pub const fn with_request_timeout(self, timeout: Duration) -> Self

Sets the token-request timeout (default 30 s).

Trait Implementations§

Source§

impl Debug for OAuth2ClientCredentials

Source§

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

Formats the value using the given formatter. Read more
Source§

impl TokenProvider for OAuth2ClientCredentials

Source§

fn access_token( &self, ) -> Pin<Box<dyn Future<Output = ClientResult<String>> + Send + '_>>

Returns a currently-valid access token. Read more

Auto Trait Implementations§

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> 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> IntoRequest<T> for T

Source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
Source§

impl<L> LayerExt<L> for L

Source§

fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>
where L: Layer<S>,

Applies the layer to a service and wraps it in Layered.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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