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 beforeexpires_inelapses) 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
Debugoutput.
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
impl OAuth2ClientCredentials
Sourcepub fn new(
token_url: impl Into<String>,
client_id: impl Into<String>,
client_secret: impl Into<String>,
) -> Self
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.
Sourcepub fn from_agent_card(
card: &AgentCard,
scheme_name: &str,
client_id: impl Into<String>,
client_secret: impl Into<String>,
) -> ClientResult<Self>
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.
Sourcepub async fn from_oidc_issuer(
issuer: &str,
client_id: impl Into<String>,
client_secret: impl Into<String>,
) -> ClientResult<Self>
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.
Sourcepub fn with_scopes<I, S>(self, scopes: I) -> Self
pub fn with_scopes<I, S>(self, scopes: I) -> Self
Sets the scopes to request (joined with spaces per RFC 6749 §3.3).
Sourcepub fn with_audience(self, audience: impl Into<String>) -> Self
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).
Sourcepub fn with_extra_param(
self,
key: impl Into<String>,
value: impl Into<String>,
) -> Self
pub fn with_extra_param( self, key: impl Into<String>, value: impl Into<String>, ) -> Self
Adds an extra form parameter to the token request.
Sourcepub const fn with_auth_style(self, style: TokenEndpointAuthStyle) -> Self
pub const fn with_auth_style(self, style: TokenEndpointAuthStyle) -> Self
Sets how client credentials are presented (default:
TokenEndpointAuthStyle::Basic).
Sourcepub const fn with_refresh_leeway(self, leeway: Duration) -> Self
pub const fn with_refresh_leeway(self, leeway: Duration) -> Self
Sets how long before expiry a cached token is refreshed (default 30 s).
Sourcepub const fn with_request_timeout(self, timeout: Duration) -> Self
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
impl Debug for OAuth2ClientCredentials
Source§impl TokenProvider for OAuth2ClientCredentials
impl TokenProvider for OAuth2ClientCredentials
Source§fn access_token(
&self,
) -> Pin<Box<dyn Future<Output = ClientResult<String>> + Send + '_>>
fn access_token( &self, ) -> Pin<Box<dyn Future<Output = ClientResult<String>> + Send + '_>>
Auto Trait Implementations§
impl !Freeze for OAuth2ClientCredentials
impl !RefUnwindSafe for OAuth2ClientCredentials
impl !UnwindSafe for OAuth2ClientCredentials
impl Send for OAuth2ClientCredentials
impl Sync for OAuth2ClientCredentials
impl Unpin for OAuth2ClientCredentials
impl UnsafeUnpin for OAuth2ClientCredentials
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request