Skip to main content

TokenClient

Struct TokenClient 

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

An async OAuth 2.0 token endpoint client.

Construct one with TokenClient::builder. A single client should be reused across calls so the underlying connection pool is shared.

Implementations§

Source§

impl TokenClient

Source

pub fn builder() -> TokenClientBuilder

Returns a builder for configuring a TokenClient.

Source

pub async fn client_credentials( &self, scope: Option<&str>, ) -> Result<TokenResponse>

Performs the OAuth 2.0 client credentials grant (RFC 6749 §4.4, CC-001): POSTs grant_type=client_credentials to the token endpoint and returns the typed TokenResponse.

Authentication uses the configured ClientAuthMethod (client_secret_basic by default). When scope is Some, it is sent as a single space-delimited scope parameter (CC-005).

§Errors
Source

pub async fn exchange_code( &self, code: &str, redirect_uri: &str, code_verifier: Option<&str>, ) -> Result<TokenResponse>

Performs the OAuth 2.0 authorization code grant (RFC 6749 §4.1.3, ACG-001): POSTs grant_type=authorization_code with code, redirect_uri, and (when configured) client_id to the token endpoint and returns the typed TokenResponse.

When code_verifier is Some, it is validated against RFC 7636 §4.1 and sent as the code_verifier parameter (ACG-004). A configured client secret still authenticates the request via the configured method; a public client (no secret) is identified by client_id in the body.

§Errors
Source

pub async fn token_exchange( &self, request: &TokenExchangeRequest, ) -> Result<TokenResponse>

Performs the OAuth 2.0 token exchange grant (RFC 8693 §2.1, EXCH-001): POSTs grant_type=urn:ietf:params:oauth:grant-type:token-exchange with the parameters of request to the token endpoint and returns the typed TokenResponse, including the RFC 8693 §2.2 issued_token_type.

A TokenExchangeRequest carrying only a subject token requests an impersonation token; one that also carries an actor token requests a delegation token (RFC 8693 §1.1, EXCH-002). The type URIs are sent verbatim (RFC 8693 §3, EXCH-003), and resource / audience are sent as repeated parameters when more than one target is named (EXCH-004).

Authentication uses the configured ClientAuthMethod (client_secret_basic by default), exactly as for the other grants. The exchange owns its requested_token_type, resource, and audience parameters, so a TokenClientBuilder::extra_param carrying one of those keys is an error rather than something to drop: extra_param is client-wide, and quietly dropping an audience the caller set to pin every token to one API would relax that restriction on this grant alone.

use rs_identity_model::{TOKEN_TYPE_ACCESS_TOKEN, TokenClient, TokenExchangeRequest};

let client = TokenClient::builder()
    .client_id("my-client")
    .client_secret("my-secret")
    .token_endpoint("https://issuer.example.com/token")
    .build()?;
let issued = client
    .token_exchange(
        &TokenExchangeRequest::new("subject.tok", TOKEN_TYPE_ACCESS_TOKEN)
            .audience("https://api.example.com"),
    )
    .await?;
println!("issued a {:?}", issued.issued_token_type);
§Errors
  • IdentityError::Validation — request is missing a REQUIRED RFC 8693 §2.1 parameter, or a TokenClientBuilder::extra_param key names a parameter this grant owns (requested_token_type, resource, audience). No request is sent.
  • IdentityError::TokenEndpoint — a non-2xx OAuth error response (RFC 8693 §2.2.2, RFC 6749 §5.2, EXCH-006).
  • IdentityError::Http — a transport failure, a non-OAuth error body, or a 2xx body missing the REQUIRED access_token, token_type, or issued_token_type.
  • IdentityError::Deserialization — a 2xx body that is not a valid token response. An explicit JSON null falls here rather than under the missing-field errors above: #[serde(default)] fills in only an absent key, so "token_type": null fails to deserialize before any of those checks run.

Trait Implementations§

Source§

impl Debug for TokenClient

Source§

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

Formats the value using the given formatter. 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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + 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: Sized + 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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<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