Expand description
Microsoft Entra ID (Azure AD) access tokens, using your HTTP client.
This crate builds the token request, parses the response, and caches the result per scope. It performs no I/O: you hand it a closure that runs one HTTP request with whatever client your process already has.
That is the whole design. azure_identity brings a transport, and a
process that already has hyper or reqwest and a TLS stack does not
want a second one to fetch a bearer token.
§Examples
use azure_token_lite::{Credential, TokenSource, ARM_SCOPE};
let credential = Credential::client_secret("tenant", "client", "secret");
let source = TokenSource::new(credential);
let token = source
.token(ARM_SCOPE, |request| async move {
// Perform `request` with your own client and return the body.
Ok::<_, std::io::Error>(
r#"{"access_token":"eyJ...","expires_in":3600}"#.to_string(),
)
})
.await?;
assert!(token.starts_with("eyJ"));§Four things that are easy to get wrong
A scope and a resource are spelled differently. The Entra v2
endpoint takes a scope ending in /.default; a managed identity
endpoint takes a resource without it. Sending one form to the other is
a 400 that says nothing useful. resource_of converts.
expires_in is a number from Entra and a string from the identity
endpoint. Accepting both is one line here and an hour of confusion
otherwise.
Different scopes are different tokens. A token for Resource Manager
sent to Log Analytics returns a 401 that looks exactly like a missing
role assignment. The cache is keyed by scope for that reason.
IMDS is not the same as IDENTITY_ENDPOINT. This crate implements
the latter only — see Credential::ManagedIdentity for why.
Structs§
- Token
- A parsed token response.
- Token
Request - One HTTP request for the caller to perform.
- Token
Source - Where tokens come from: one per process, shared by everything.
Enums§
- Credential
- How to authenticate.
- Error
- What went wrong.
- Method
- Which HTTP method a
TokenRequestneeds. - Token
Error - Why
TokenSource::tokenfailed.
Constants§
- ARM_
SCOPE - The Azure Resource Manager scope.
- CONTENT_
TYPE - The
Content-TypeaTokenRequestwith a body needs. - DEFAULT_
EXPIRY_ MARGIN - How long before a token’s stated expiry it is treated as expired.
- GRAPH_
SCOPE - The Microsoft Graph scope.
- IDENTITY_
API_ VERSION - The managed-identity API version this crate requests.
- IDENTITY_
ENDPOINT_ VARIABLE - The environment variable naming the managed-identity endpoint.
- IDENTITY_
HEADER_ VARIABLE - The environment variable carrying the managed-identity header secret.
- LOGS_
SCOPE - The Log Analytics scope.
Functions§
- form_
encode - Percent-encode a form value.
- parse_
response - Parse a token response body.
- resource_
of - The
resourcean identity endpoint wants, from a.defaultscope.