$ npm install @azure/identity
This library simplifies authentication against Azure Active Directory for Azure SDK libraries.
It provides a set of TokenCredential
implementations which can be passed into SDK libraries
to authenticate API requests. It supports token authentication using an Azure Active Directory service principal or managed identity.
Developers using Visual Studio Code can use the Azure Account Extension, to authenticate via the IDE. Applications using the DefaultAzureCredential
or the VisualStudioCodeCredential
can then use this account to authenticate calls in their application when running locally.
To authenticate in Visual Studio Code, first ensure the Azure Account Extension is installed. Once the extension is installed, press F1
to open the command palette and run the Azure: Sign In
command.
Applications using the AzureCliCredential
, rather directly or via the DefaultAzureCredential
, can use the Azure CLI account to authenticate calls in the application when running locally.
To authenticate with the Azure CLI users can run the command az login
. For users running on a system with a default web browser the azure cli will launch the browser to authenticate the user.
For systems without a default web browser, the az login
command will use the device code authentication flow. The user can also force the Azure ClI to use the device code flow rather than launching a browser by specifying the --use-device-code
argument.
Install Azure Identity with npm
:
npm install --save @azure/identity
If this is your first time using @azure/identity
or the Microsoft identity platform (Azure Active Directory), we recommend that you read Using @azure/identity
with Microsoft Identity Platform first. This document will give you a deeper understanding of the platform and how to configure your Azure account correctly.
A credential is a class which contains or can obtain the data needed for a service client to authenticate requests. Service clients across Azure SDK accept credentials when they are constructed, and service clients use those credentials to authenticate requests to the service.
The Azure Identity library focuses on OAuth authentication with Azure Active directory, and it offers a variety of credential classes capable of acquiring an AAD token to authenticate service requests. All of the credential classes in this library are implementations of the TokenCredential abstract class, and any of them can be used by to construct service clients capable of authenticating with a TokenCredential.
See Credential Classes.
The DefaultAzureCredential
is appropriate for most scenarios where the application is intended to ultimately be run in the Azure Cloud. This is because the DefaultAzureCredential
combines credentials commonly used to authenticate when deployed, with credentials used to authenticate in a development environment. The DefaultAzureCredential
will attempt to authenticate via the following mechanisms in order.
DefaultAzureCredential
will read account information specified via environment variables and use it to authenticate.DefaultAzureCredential
will authenticate with that account.DefaultAzureCredential
will authenticate with that account.az login
command, the DefaultAzureCredential
will authenticate with that account.DefaultAzureCredential
and EnvironmentCredential
are configured for service principal authentication with these environment variables:
variable name | value |
---|---|
AZURE_CLIENT_ID |
service principal's app id |
AZURE_TENANT_ID |
id of the principal's Azure Active Directory tenant |
AZURE_CLIENT_SECRET |
one of the service principal's client secrets (implies ClientSecretCredential ) |
AZURE_CLIENT_CERTIFICATE_PATH |
path to a PEM-encoded certificate file including private key (implies ClientCertificateCredential ) |
AZURE_USERNAME |
the username of a user in the tenant (implies UsernamePasswordCredential ) |
AZURE_PASSWORD |
the password of the user specified in AZURE_USERNAME |
DefaultAzureCredential
This example demonstrates authenticating the KeyClient
from the @azure/keyvault-keys client library using the DefaultAzureCredential
.
// The default credential first checks environment variables for configuration as described above.
// If environment configuration is incomplete, it will try managed identity.
const { KeyClient } = require("@azure/keyvault-keys");
const { DefaultAzureCredential } = require("@azure/identity");
// Azure SDK clients accept the credential as a parameter
const credential = new DefaultAzureCredential();
const client = new KeyClient(vaultUrl, credential);
const getResult = await client.getKey("MyKeyName");
DefaultAzureCredential
Many Azure hosts allow the assignment of a user assigned managed identity. This example demonstrates configuring the DefaultAzureCredential
to authenticate a user assigned identity when deployed to an azure host. It then authenticates a KeyClient
from the @azure/keyvault-keys client library with credential.
const { KeyClient } = require("@azure/keyvault-keys");
const { DefaultAzureCredential } = require("@azure/identity");
// when deployed to an azure host the default azure credential will authenticate the specified user assigned managed identity
var credential = new DefaultAzureCredential({ managedIdentityClientId: userAssignedClientId });
const client = new KeyClient(vaultUrl, credential);
ChainedTokenCredential
While the DefaultAzureCredential
is generally the quickest way to get started developing applications for Azure, more advanced users may want to customize the credentials considered when authenticating. The ChainedTokenCredential
enables users to combine multiple credential instances to define a customized chain of credentials. This example demonstrates creating a ChainedTokenCredential
which will attempt to authenticate using two differently configured instances of ClientSecretCredential
, to then authenticate the KeyClient
from the @azure/keyvault-keys:
const { ClientSecretCredential, ChainedTokenCredential } = require("@azure/identity");
// When an access token is requested, the chain will try each
// credential in order, stopping when one provides a token
const firstCredential = new ClientSecretCredential(tenantId, clientId, clientSecret);
const secondCredential = new ClientSecretCredential(tenantId, anotherClientId, anotherSecret);
const credentialChain = new ChainedTokenCredential(firstCredential, secondCredential);
// The chain can be used anywhere a credential is required
const { KeyClient } = require("@azure/keyvault-keys");
const client = new KeyClient(vaultUrl, credentialChain);
credential | usage |
---|---|
DefaultAzureCredential |
provides a simplified authentication experience to quickly start developing applications run in the Azure cloud |
ChainedTokenCredential |
allows users to define custom authentication flows composing multiple credentials |
EnvironmentCredential |
authenticates a service principal or user via credential information specified in environment variables |
ManagedIdentityCredential |
authenticates the managed identity of an azure resource |
credential | usage |
---|---|
ClientSecretCredential |
authenticates a service principal using a secret |
ClientCertificateCredential |
authenticates a service principal using a certificate |
credential | usage |
---|---|
InteractiveBrowserCredential |
interactively authenticates a user with the default system browser |
DeviceCodeCredential |
interactively authenticates a user on devices with limited UI |
UserPasswordCredential |
authenticates a user with a username and password |
AuthorizationCodeCredential |
authenticate a user with a previously obtained authorization code |
credential | usage |
---|---|
AzureCliCredential |
authenticate in a development environment with the Azure CLI |
VisualStudioCodeCredential |
authenticate in a development environment with Visual Studio Code |
Credentials raise AuthenticationError
when they fail to authenticate. This class has a message
field which describes why authentication failed. An AggregateAuthenticationError
will be raised by ChainedTokenCredential
with an errors
field containing an array of errors from each credential in the chain.
Enabling logging may help uncover useful information about failures. In order to see a log of HTTP requests and responses, set the AZURE_LOG_LEVEL
environment variable to info
. Alternatively, logging can be enabled at runtime by calling setLogLevel
in the @azure/logger
:
import { setLogLevel } from "@azure/logger";
setLogLevel("info");
Currently, the following client libraries support authenticating with TokenCredential
and the Azure Identity library. You can learn more about their use, and find additional documentation on use of these client libraries along samples with can be found in the links below.
API documentation for this library can be found on our documentation site.
If you encounter bugs or have suggestions, please open an issue.
If you'd like to contribute to this library, please read the contributing guide to learn more about how to build and test the code.
© 2010 - cnpmjs.org x YWFE | Home | YWFE