Skip to main content

RefreshTokenCredential

A TokenCredential that redeems a refresh token for access tokens.

Usage

Source

RefreshTokenCredential(
    refresh_token=None,
    *,
    authority_host=None,
)

Use it wherever the Azure SDK, ApiClient or dazapiz.duckdb.set_credential_secret() takes a credential, to act as the user an az login (or any other sign-in) refresh token belongs to. Access tokens are kept in memory, per tenant and scope set, until five minutes before they expire. When Entra rotates the refresh token, the new one is kept and returned by get_refresh_token(). Thread-safe.

Parameters

refresh_token: str | SupportsRefreshToken | None = None

A refresh token string, a SupportsRefreshToken (such as refresh_token_from_az_cli()), or None for $AZURE_REFRESH_TOKEN. A string is redeemed with $AZURE_CLIENT_ID, else AZURE_CLI_CLIENT_ID, in $AZURE_TENANT_ID, else "common"; a SupportsRefreshToken brings its own client ID and tenant.

authority_host: str | None = None
The Entra authority host, with or without https://; default $AZURE_AUTHORITY_HOST, else the public cloud (login.microsoftonline.com).

Raises

RefreshTokenNotFoundError

refresh_token is None and $AZURE_REFRESH_TOKEN is unset.

TypeError

refresh_token is of another type.

ValueError
refresh_token is an empty string.

Notes

Wraps msal.PublicClientApplication.acquire_token_by_refresh_token(), one MSAL app per tenant. azure-identity has no credential that takes a refresh token. repr() shows the client ID, tenant and username, never a token.

Examples

from azure.storage.filedatalake import DataLakeServiceClient
from dazapiz import RefreshTokenCredential, refresh_token_from_az_cli

credential = RefreshTokenCredential(refresh_token_from_az_cli())
lake = DataLakeServiceClient("https://myaccount.dfs.core.windows.net", credential)

Methods

Name Description
get_token() An access token for scopes, from memory or by redeeming the refresh token.
get_refresh_token() The current refresh token: the one given, or the latest rotated one.

get_token()

An access token for scopes, from memory or by redeeming the refresh token.

Usage

Source

get_token(*scopes, claims=None, tenant_id=None, **kwargs)

The TokenCredential protocol’s method, which the Azure SDK calls.

Parameters
*scopes: str

One or more scopes, such as "https://storage.azure.com/.default".

claims: str | None = None

A claims challenge from a resource; skips the in-memory token and is sent to Entra.

tenant_id: str | None = None

Request the token from this tenant instead of the refresh token’s own.

**kwargs: Any
Accepted for the protocol and ignored.
Returns
azure.core.credentials.AccessToken
The token and its expires_on (seconds since the epoch).
Raises
ValueError

No scope was given.

azure.core.exceptions.ClientAuthenticationError
Entra refused the refresh token (expired, revoked, or for another client); the message carries MSAL’s error, never the token.

get_refresh_token()

The current refresh token: the one given, or the latest rotated one.

Usage

Source

get_refresh_token()
Returns
RefreshTokenInfo
The refresh token with its client ID, tenant and username.