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
impl TokenClient
Sourcepub fn builder() -> TokenClientBuilder
pub fn builder() -> TokenClientBuilder
Returns a builder for configuring a TokenClient.
Sourcepub async fn client_credentials(
&self,
scope: Option<&str>,
) -> Result<TokenResponse>
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
IdentityError::TokenEndpoint— a non-2xx OAuth error response (CC-004).IdentityError::Http— a transport failure or non-OAuth error body.IdentityError::Deserialization— a 2xx body that is not a valid token response.
Sourcepub async fn exchange_code(
&self,
code: &str,
redirect_uri: &str,
code_verifier: Option<&str>,
) -> Result<TokenResponse>
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
IdentityError::Validation— acode_verifieroutside the 43–128 unreserved-character range.IdentityError::TokenEndpoint— a non-2xx OAuth error response (ACG-005).IdentityError::Http/IdentityError::Deserialization— as forclient_credentials.
Sourcepub async fn token_exchange(
&self,
request: &TokenExchangeRequest,
) -> Result<TokenResponse>
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—requestis missing a REQUIRED RFC 8693 §2.1 parameter, or aTokenClientBuilder::extra_paramkey 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 REQUIREDaccess_token,token_type, orissued_token_type.IdentityError::Deserialization— a 2xx body that is not a valid token response. An explicit JSONnullfalls here rather than under the missing-field errors above:#[serde(default)]fills in only an absent key, so"token_type": nullfails to deserialize before any of those checks run.