Package Exports
- @azure/identity
- @azure/identity/dist-esm/src/credentials/authorizationCodeCredential.browser.js
- @azure/identity/dist-esm/src/credentials/authorizationCodeCredential.js
- @azure/identity/dist-esm/src/credentials/azureCliCredential.browser.js
- @azure/identity/dist-esm/src/credentials/azureCliCredential.js
- @azure/identity/dist-esm/src/credentials/azurePowerShellCredential.browser.js
- @azure/identity/dist-esm/src/credentials/azurePowerShellCredential.js
- @azure/identity/dist-esm/src/credentials/clientCertificateCredential.browser.js
- @azure/identity/dist-esm/src/credentials/clientCertificateCredential.js
- @azure/identity/dist-esm/src/credentials/clientSecretCredential.browser.js
- @azure/identity/dist-esm/src/credentials/clientSecretCredential.js
- @azure/identity/dist-esm/src/credentials/defaultAzureCredential.browser.js
- @azure/identity/dist-esm/src/credentials/defaultAzureCredential.js
- @azure/identity/dist-esm/src/credentials/deviceCodeCredential.browser.js
- @azure/identity/dist-esm/src/credentials/deviceCodeCredential.js
- @azure/identity/dist-esm/src/credentials/environmentCredential.browser.js
- @azure/identity/dist-esm/src/credentials/environmentCredential.js
- @azure/identity/dist-esm/src/credentials/interactiveBrowserCredential.browser.js
- @azure/identity/dist-esm/src/credentials/interactiveBrowserCredential.js
- @azure/identity/dist-esm/src/credentials/managedIdentityCredential/index.browser.js
- @azure/identity/dist-esm/src/credentials/managedIdentityCredential/index.js
- @azure/identity/dist-esm/src/credentials/usernamePasswordCredential.browser.js
- @azure/identity/dist-esm/src/credentials/usernamePasswordCredential.js
- @azure/identity/dist-esm/src/credentials/visualStudioCodeCredential.browser.js
- @azure/identity/dist-esm/src/credentials/visualStudioCodeCredential.js
- @azure/identity/dist-esm/src/extensions/consumer.browser.js
- @azure/identity/dist-esm/src/extensions/consumer.js
- @azure/identity/dist-esm/src/util/authHostEnv.browser.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@azure/identity) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Azure Identity client library for JavaScript
The Azure Identity library provides Azure Active Directory token authentication support across the Azure SDK. It provides a set of TokenCredential implementations which can be used to construct Azure SDK clients which support AAD token authentication.
You can find examples for these various credentials in the Azure Identity Examples Page
Key links:
Getting started
Install the package
Install Azure Identity with npm
:
npm install --save @azure/identity
Prerequisites
- An Azure subscription.
- Optional: The Azure CLI and/or Azure PowerShell can also be useful for authenticating in a development environment and managing account roles.
Supported Node.js versions
This version of @azure/identity
supports stable (even numbered) versions of Node.js starting from v10. While it may run in Node.js v8, no support is guaranteed.
Note: If your application runs on Node.js v8 or lower, we strongly recommend you to upgrade your Node.js version to latest stable version or pin your
@azure/identity
dependency to version 1.1.0.
Authenticate the client in development environment
While we recommend using managed identity or service principal authentication in your production application, it is typical for a developer to use their own account for authenticating calls to Azure services when debugging and executing code locally. There are several developer tools which can be used to perform this authentication in your development environment.
Authenticating via the Azure CLI
Applications using the AzureCliCredential
, whether 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.
Authenticating via Azure PowerShell
Applications using the AzurePowerShellCredential
, whether directly or via the DefaultAzureCredential
, can use the account connected to Azure PowerShell to authenticate calls in the application when running locally.
To authenticate with Azure PowerShell users can run the Connect-AzAccount
cmdlet. By default, ike the Azure CLI, Connect-AzAccount
will launch the default web browser to authenticate a user account.
If interactive authentication cannot be supported in the session, then the -UseDeviceAuthentication
argument will force the cmdlet to use a device code authentication flow instead, similar to the corresponding option in the Azure CLI credential.
Authenticate the client in browsers
To authenticate Azure SDKs within web browsers, we currently offer the InteractiveBrowserCredential
, which can be set to use redirection or popups to complete the authentication flow. It is necessary to create an Azure App Registration in the portal for your web application first.
Key concepts
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.
Credentials
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.
DefaultAzureCredential
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.
Note:
DefaultAzureCredential
is intended to simplify getting started with the SDK by handling common scenarios with reasonable default behaviors. Developers who want more control or whose scenario isn't served by the default settings should use other credential types.
If used from NodeJS, the DefaultAzureCredential
will attempt to authenticate via the following mechanisms in order:
- Environment - The
DefaultAzureCredential
will read account information specified via environment variables and use it to authenticate. - Managed Identity - If the application is deployed to an Azure host with Managed Identity enabled, the
DefaultAzureCredential
will authenticate with that account. - Azure CLI - If the developer has authenticated an account via the Azure CLI
az login
command, theDefaultAzureCredential
will authenticate with that account. - Azure PowerShell - If the developer has authenticated using the Azure PowerShell module
Connect-AzAccount
command, theDefaultAzureCredential
will authenticate with that account.
Extensions
Azure Identity for JavaScript provides an extension API that allows us to provide certain functionality through separate extension packages. The @azure/identity
package exports a top-level function (useIdentityExtension
) that can be used to enable an extension, and we provide two extension packages:
@azure/identity-cache-persistence
, which provides persistent token caching in Node.js using a native secure storage system provided by your operating system. This extension allows cachedaccess_token
values to persist across sessions, meaning that an interactive login flow does not need to be repeated as long as a cached token is available.@azure/identity-vscode
, which provides the dependencies ofVisualStudioCodeCredential
and enables it. Without this extension, theVisualStudioCodeCredential
in this package will throw aCredentialUnavailableError
. The extension provides the underlying implementation of this credential, enabling it for use both on its own and as part of theDefaultAzureCredential
described above.
Environment Variables
DefaultAzureCredential
and EnvironmentCredential
can be configured with environment variables. Each type of authentication requires values for specific variables:
Service principal with secret
variable name | value |
---|---|
AZURE_CLIENT_ID |
id of an Azure Active Directory application |
AZURE_TENANT_ID |
id of the application's Azure Active Directory tenant |
AZURE_CLIENT_SECRET |
one of the application's client secrets |
Service principal with certificate
variable name | value |
---|---|
AZURE_CLIENT_ID |
id of an Azure Active Directory application |
AZURE_TENANT_ID |
id of the application's Azure Active Directory tenant |
AZURE_CLIENT_CERTIFICATE_PATH |
path to a PEM-encoded certificate file including private key (without password protection) |
Username and password
variable name | value |
---|---|
AZURE_CLIENT_ID |
id of an Azure Active Directory application |
AZURE_USERNAME |
a username (usually an email address) |
AZURE_PASSWORD |
that user's password |
Configuration is attempted in the above order. For example, if values for a client secret and certificate are both present, the client secret will be used.
Examples
You can find more examples of using various credentials in Azure Identity Examples Page
Authenticating with the 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.
// Azure Key Vault service to use
const { KeyClient } = require("@azure/keyvault-keys");
// Azure authentication library to access Azure Key Vault
const { DefaultAzureCredential } = require("@azure/identity");
// Azure SDK clients accept the credential as a parameter
const credential = new DefaultAzureCredential();
// Create authenticated client
const client = new KeyClient(vaultUrl, credential);
Specifying a user assigned managed identity with the DefaultAzureCredential
A relatively common scenario involves authenticating using a user assigned managed identity for an Azure resource. Explore the example on Authenticating a user assigned managed identity with DefaultAzureCredential to see how this is made a relatively straightforward task that can be configured using environment variables or in code.
Define a custom authentication flow with the 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);
Managed Identity Support
The Managed identity authentication is supported via either the DefaultAzureCredential
or the ManagedIdentityCredential
credential classes directly for the following Azure hosts:
- Azure Virtual Machines
- Azure App Service
- Azure Kubernetes Service
- Azure Cloud Shell
- Azure Arc
- Azure Service Fabric
For examples of how to use managed identity for authentication please refer to the examples
Credential Classes
Authenticating Azure Hosted Applications
credential | usage | example |
---|---|---|
DefaultAzureCredential |
Provides a simplified authentication experience to quickly start developing applications run in the Azure cloud. | example |
ChainedTokenCredential |
Allows users to define custom authentication flows composing multiple credentials. | example |
EnvironmentCredential |
Authenticates a service principal or user via credential information specified in environment variables. | example |
ManagedIdentityCredential |
Authenticates the managed identity of an Azure resource. | example |
Authenticating Service Principals
credential | usage | example | reference |
---|---|---|---|
ClientSecretCredential |
Authenticates a service principal using a secret. | example | Service principal authentication |
ClientCertificateCredential |
Authenticates a service principal using a certificate. | example | Service principal authentication |
Authenticating Users
credential | usage | example | reference |
---|---|---|---|
InteractiveBrowserCredential |
Interactively authenticates a user with the default system browser. Read more about how this happens here. | example | OAuth2 authentication code |
DeviceCodeCredential |
Interactively authenticates a user on devices with limited UI. | example | Device code authentication |
UsernamePasswordCredential |
Authenticates a user with a username and password. | example | Username + password authentication |
AuthorizationCodeCredential |
Authenticate a user with a previously obtained authorization code. | example | OAuth2 authentication code |
Authenticating via Development Tools
credential | usage | example | reference |
---|---|---|---|
AzureCliCredential |
Authenticate in a development environment with the Azure CLI. | example | Azure CLI authentication |
AzurePowerShellCredential |
Authenticate in a development environment using Azure PowerShell. | example | Azure PowerShell authentication |
Troubleshooting
Error Handling
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.
Logging
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");
Next steps
Read the documentation
API documentation for this library can be found on our documentation site.
Provide Feedback
If you encounter bugs or have suggestions, please open an issue.
Contributing
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.