% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/credential-azure-cli.R
\name{write_msal_token}
\alias{write_msal_token}
\title{Write an httr2 Token to the MSAL Token Cache}
\usage{
write_msal_token(token, cache_file = default_msal_token_cache())
}
\arguments{
\item{token}{An \code{\link[httr2:oauth_token]{httr2::oauth_token()}} object. Must contain \code{access_token},
\code{token_type}, and \code{.expires_at}. May optionally contain \code{refresh_token}
and \code{scope}. All cache fields (\code{home_account_id}, \code{tenant_id}, \code{username},
\code{client_id}, \code{scope}, \code{environment}) are derived from the JWT claims
(\code{oid}, \code{tid}, \code{upn}/\code{preferred_username}, \code{appid}/\code{azp}, \code{scp}/\code{scope},
\code{iss}) and the token object itself.}

\item{cache_file}{Path to the MSAL token cache JSON file. Defaults to
\code{\link[=default_msal_token_cache]{default_msal_token_cache()}}.}
}
\value{
Invisibly returns the path to the cache file.
}
\description{
Writes an \code{\link[httr2:oauth_token]{httr2::oauth_token()}} object into the MSAL token cache JSON file
(\code{msal_token_cache.json}) shared by the Azure SDK and Azure CLI. The
resulting entry is readable by other Azure tools (Python SDK, Azure CLI,
and the rest of this package via \code{\link[=az_cli_get_cached_token]{az_cli_get_cached_token()}}).
}
\details{
The function adds or overwrites \code{AccessToken}, \code{RefreshToken} (when the
token carries a refresh token), \code{Account}, and \code{AppMetadata} sections.
Existing entries for other accounts or clients are preserved.

The \code{home_account_id} follows the MSAL convention
\code{"<object_id>.<tenant_id>"} where \code{object_id} is the Azure AD OID of the
authenticated principal. Cache entry keys are built in the same format used
by the Azure CLI and MSAL Python:
\itemize{
\item AccessToken: \verb{<home_account_id>-<environment>-accesstoken-<client_id>-<realm>-<target>}
\item RefreshToken: \verb{<home_account_id>-<environment>-refreshtoken-<client_id>--}
\item Account: \verb{<home_account_id>-<environment>-<realm>}
\item AppMetadata: \verb{appmetadata-<environment>-<client_id>}
}
}
\seealso{
\code{\link[=az_cli_get_cached_token]{az_cli_get_cached_token()}}, \code{\link[httr2:oauth_token]{httr2::oauth_token()}}
}
