Skip to main content

Crate azure_token_lite

Crate azure_token_lite 

Source
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.
TokenRequest
One HTTP request for the caller to perform.
TokenSource
Where tokens come from: one per process, shared by everything.

Enums§

Credential
How to authenticate.
Error
What went wrong.
Method
Which HTTP method a TokenRequest needs.
TokenError
Why TokenSource::token failed.

Constants§

ARM_SCOPE
The Azure Resource Manager scope.
CONTENT_TYPE
The Content-Type a TokenRequest with 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 resource an identity endpoint wants, from a .default scope.