-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Identity] More standard samples (#12800)
* [Identity] More standard samples * missing files * Feedback and other cleanups * removing device code and interactive samples because of user input * more info on how to set up the keyvault * apiref fix * using-azure-identity.md fix * no more tsconfig.samples.json * fixed version * wip * now it should work * formatting * Skipped the build samples step for now. Will log an issue. Skipped the build samples step for now. Will log an issue. * pnpm-lock after merge master * removing kv keys from package.json
- Loading branch information
Showing
23 changed files
with
485 additions
and
20 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
src/**/*.js | ||
samples/**/*.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
--- | ||
page_type: sample | ||
languages: | ||
- javascript | ||
products: | ||
- azure | ||
- azure-active-directory | ||
urlFragment: identity-javascript | ||
--- | ||
|
||
# Azure Identity library samples for JavaScript | ||
|
||
These sample programs show how to use the JavaScript client libraries for Azure Identity in some common scenarios. | ||
|
||
| **File Name** | **Description** | | ||
| ------------------------------------------------------ | --------------------------------------------------------------- | | ||
| [defaultAzureCredential.js][defaultAzureCredential] | Tries several authentications. The simplest way to use @azure/identity | | ||
| [clientSecretCredential.js][clientSecretCredential] | Authenticates with a client and a client's secret. | | ||
| [environmentCredential.js][environmentCredential] | Authenticates with a client and a client's secret sent through environment variables. | | ||
|
||
## Prerequisites | ||
|
||
The samples are compatible with Node.js >= 8.0.0. | ||
|
||
You need [an Azure subscription][freesub] and [an Azure Key Vault][azkeyvault] to run these sample programs. | ||
|
||
To create an AAD application: | ||
|
||
- Follow [Documentation to register a new application](https://docs.microsoft.com/azure/active-directory/develop/quickstart-register-app) in the Azure Active Directory (in the Azure portal). | ||
- Note down the `CLIENT_ID` and `TENANT_ID`. | ||
- In the "Certificates & Secrets" tab, create a secret and note that down. | ||
|
||
To allow your registered application to access your Key Vault | ||
|
||
- In the Azure portal, go to your Azure Key Vault. | ||
- In the left-side-navbar of your Azure Key Vault in the Azure portal, go to the `Access Policies` section, then click the `+ Add Access Policy` button. | ||
- In the `Add access policy` page, select all the permissions for Keys, Secrets and Certificates. | ||
- For the `Select principal` field, click on the `None selected`. A panel will appear at the right of the window. Search for your Azure Active Directory application, click the application on the search results, then click "Select" at the bottom. | ||
- Once your application is selected, click the "Add" button. | ||
- Click the `Save` button at the top of the Access Policies section of your Key Vault. | ||
- For more information on securing your Key Vault: [Learn more](https://docs.microsoft.com/azure/key-vault/general/secure-your-key-vault) | ||
|
||
Adapting the samples to run in the browser may require some additional consideration. For details, please see the [package README][package]. | ||
|
||
## Setup | ||
|
||
To run the samples using the published version of the package: | ||
|
||
1. Install the dependencies using `npm`: | ||
|
||
```bash | ||
npm install | ||
``` | ||
|
||
2. Edit the file `sample.env`, adding the correct credentials to access the Azure service and run the samples. Then rename the file from `sample.env` to just `.env`. The sample programs will read this file automatically. | ||
|
||
3. Run whichever samples you like (note that some samples may require additional setup, see the table above): | ||
|
||
```bash | ||
node helloWorld.js | ||
``` | ||
|
||
Alternatively, run a single sample with the correct environment variables set (step 2 is not required if you do this), for example (cross-platform): | ||
|
||
```bash | ||
npx cross-env KEYVAULT_NAME="<key vault name>" AZURE_TENANT_ID="<AAD tenant id>" AZURE_CLIENT_ID="<AAD client id>" AZURE_CLIENT_SECRET="<AAD client secret>" node environmentCredential.js | ||
``` | ||
|
||
## Next Steps | ||
|
||
Take a look at our [API Documentation][apiref] for more information about the APIs that are available in the clients. | ||
|
||
[defaultAzureCredential]: https://github.com/Azure/azure-sdk-for-js/blob/master/sdk/identity/identity/samples/javascript/defaultAzureCredential.js | ||
[clientSecretCredential]: https://github.com/Azure/azure-sdk-for-js/blob/master/sdk/identity/identity/samples/javascript/clientSecretCredential.js | ||
[environmentCredential]: https://github.com/Azure/azure-sdk-for-js/blob/master/sdk/identity/identity/samples/javascript/environmentCredential.js | ||
[apiref]: https://docs.microsoft.com/javascript/api/@azure/identity | ||
[azkeyvault]: https://docs.microsoft.com/azure/key-vault/quick-create-portal | ||
[freesub]: https://azure.microsoft.com/free/ | ||
[package]: https://github.com/Azure/azure-sdk-for-js/tree/master/sdk/identity/identity/README.md |
28 changes: 28 additions & 0 deletions
28
sdk/identity/identity/samples/javascript/clientSecretCredential.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
const { ClientSecretCredential } = require("@azure/identity"); | ||
const { KeyClient } = require("@azure/keyvault-keys"); | ||
|
||
// Load the .env file if it exists | ||
require("dotenv").config(); | ||
|
||
async function main() { | ||
const credential = new ClientSecretCredential( | ||
process.env.AZURE_TENANT_ID, // The tenant ID in Azure Active Directory | ||
process.env.AZURE_CLIENT_ID, // The application (client) ID registered in the AAD tenant | ||
process.env.AZURE_CLIENT_SECRET // The client secret for the registered application | ||
); | ||
|
||
const keyVaultUrl = `https://key-vault-name.vault.azure.net`; | ||
const client = new KeyClient(keyVaultUrl, credential); | ||
|
||
// Retrieving the properties of the existing keys in that specific Key Vault. | ||
console.log(await client.listPropertiesOfKeys().next()); | ||
} | ||
|
||
main().catch((err) => { | ||
console.log("error code: ", err.code); | ||
console.log("error message: ", err.message); | ||
console.log("error stack: ", err.stack); | ||
}); |
32 changes: 32 additions & 0 deletions
32
sdk/identity/identity/samples/javascript/defaultAzureCredential.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
const { DefaultAzureCredential } = require("@azure/identity"); | ||
const { KeyClient } = require("@azure/keyvault-keys"); | ||
|
||
// Load the .env file if it exists | ||
require("dotenv").config(); | ||
|
||
/** | ||
* 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. | ||
* | ||
* For more information, you may go to our readme: [link](https://github.com/Azure/azure-sdk-for-js/tree/master/sdk/identity/identity#defaultazurecredential) | ||
*/ | ||
|
||
async function main() { | ||
const credential = new DefaultAzureCredential(); | ||
|
||
const keyVaultUrl = `https://key-vault-name.vault.azure.net`; | ||
const client = new KeyClient(keyVaultUrl, credential); | ||
|
||
// Retrieving the properties of the existing keys in that specific Key Vault. | ||
console.log(await client.listPropertiesOfKeys().next()); | ||
} | ||
|
||
main().catch((err) => { | ||
console.log("error code: ", err.code); | ||
console.log("error message: ", err.message); | ||
console.log("error stack: ", err.stack); | ||
}); |
28 changes: 28 additions & 0 deletions
28
sdk/identity/identity/samples/javascript/environmentCredential.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
const { EnvironmentCredential } = require("@azure/identity"); | ||
const { KeyClient } = require("@azure/keyvault-keys"); | ||
|
||
// Load the .env file if it exists | ||
require("dotenv").config(); | ||
|
||
async function main() { | ||
// EnvironmentCredential expects the following three environment variables: | ||
// - AZURE_TENANT_ID: The tenant ID in Azure Active Directory | ||
// - AZURE_CLIENT_ID: The application (client) ID registered in the AAD tenant | ||
// - AZURE_CLIENT_SECRET: The client secret for the registered application | ||
const credential = new EnvironmentCredential(); | ||
|
||
const keyVaultUrl = `https://key-vault-name.vault.azure.net`; | ||
const client = new KeyClient(keyVaultUrl, credential); | ||
|
||
// Retrieving the properties of the existing keys in that specific Key Vault. | ||
console.log(await client.listPropertiesOfKeys().next()); | ||
} | ||
|
||
main().catch((err) => { | ||
console.log("error code: ", err.code); | ||
console.log("error message: ", err.message); | ||
console.log("error stack: ", err.stack); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{ | ||
"name": "azure-identity-samples-js", | ||
"private": true, | ||
"version": "0.1.0", | ||
"description": "Azure Identity client library samples for JavaScript", | ||
"engine": { | ||
"node": ">=8.0.0" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/Azure/azure-sdk-for-js.git" | ||
}, | ||
"keywords": [ | ||
"Azure", | ||
"Identity", | ||
"AAD", | ||
"Node.js", | ||
"JavaScript" | ||
], | ||
"author": "Microsoft Corporation", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/Azure/azure-sdk-for-js/issues" | ||
}, | ||
"homepage": "https://github.com/Azure/azure-sdk-for-js#readme", | ||
"sideEffects": false, | ||
"dependencies": { | ||
"@azure/identity": "latest", | ||
"@azure/keyvault-keys": "4.1.0", | ||
"dotenv": "^8.2.0" | ||
}, | ||
"devDependencies": { | ||
"rimraf": "^3.0.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# The name of the key vault to use in the samples. | ||
# Create a Key Vault in the Azure Portal and enter its URI (e.g. https://mytest.vault.azure.net/) here. | ||
KEYVAULT_URI=<key-vault-uri> | ||
|
||
# Used by some of the credentials to authenticate using Azure AD as a service principal for role-based authentication. | ||
AZURE_TENANT_ID=<AD tenant id or name> | ||
AZURE_CLIENT_ID=<ID of the user/service principal to authenticate as> | ||
AZURE_CLIENT_SECRET=<client secret used to authenticate to Azure AD> |
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.