refresh_token_from_az_cli()
The refresh token of the account the Azure CLI is signed in as.
Usage
refresh_token_from_az_cli(
*,
username=None,
tenant_id=None,
config_dir=None,
)Reads the Azure CLI’s files without running az, so it also works where only the config directory is present (a mounted ~/.azure):
- The config directory is
config_dir, else$AZURE_CONFIG_DIR, else~/.azure. azureProfile.jsongives the current account: the default subscription’s user and tenant.usernameandtenant_idoverride them; with both given the profile is not read.msal_token_cache.jsonis loaded intomsal.SerializableTokenCache, which is only read, never written. The refresh token is the newest one issued to AZURE_CLI_CLIENT_ID for that user’shome_account_id. Refresh tokens carry no tenant, so the tenant comes from the profile.- When the username is several cache accounts (the same name in two clouds, or two home accounts), the one in the subscription’s cloud (
environmentName) and signed in to its tenant is taken. Where the profile cannot tell them apart (both overrides given, a custom cloud, a tenant none of them signed in to), the newest token wins.
An encrypted cache (msal_token_cache.bin, from az config set core.encrypt_token_cache=true) is not read.
Parameters
username: str | None = None-
The account to look up, matched case-insensitively; default the default subscription’s user.
tenant_id: str | None = None-
The tenant the token is for; default the default subscription’s tenant, or, with
username, that user’s default (else first) subscription’s tenant. config_dir: str | os.PathLike[str] | None = None-
The Azure CLI’s config directory; default
$AZURE_CONFIG_DIR, else~/.azure.~is expanded.
Returns
RefreshTokenInfo-
The token, AZURE_CLI_CLIENT_ID, the tenant and the username. Pass it to
spark_storage_conf(refresh_token=...)or RefreshTokenCredential.
Raises
RefreshTokenNotFoundError-
No profile, no default subscription, no cache or no matching token. The message names the file and the username, never a token.
UnsupportedPlatformError- On Windows, where the Azure CLI signs in through the Windows broker and encrypts its cache.
Notes
Uses msal.SerializableTokenCache to read and search the cache; the lookup (which file, which entry) is what dazapiz adds. Nothing is redeemed here: the token is returned as found, and may have expired if the CLI has not been used for a long time (run az login again).
Examples
from dazapiz import refresh_token_from_az_cli
info = refresh_token_from_az_cli()
info.username, info.tenant_id