node.js based authentication library for Azure with type definitions
Перейти к файлу
Xinxing Liu 43c52f9420 Fix callback not calling issue in applicationTokenCredentialsBase.ts 2019-08-22 12:06:13 -07:00
.github Remove daschult from CODEOWNERS 2019-08-02 13:21:54 -07:00
.scripts update min version of dependencies in the package (#68) 2019-07-23 10:59:30 -07:00
.vscode update min version of dependencies in the package (#68) 2019-07-23 10:59:30 -07:00
lib Fix callback not calling issue in applicationTokenCredentialsBase.ts 2019-08-22 12:06:13 -07:00
samples Add support for client_id, object_id and ms_res_id query parameters for VmMSI and fixed a bug in AzureCliCredentials (#59) 2019-05-20 11:16:03 -07:00
test Add support for client_id, object_id and ms_res_id query parameters for VmMSI and fixed a bug in AzureCliCredentials (#59) 2019-05-20 11:16:03 -07:00
.gitattributes initial commit 2017-09-08 11:25:16 -07:00
.gitignore Implemented ServicePrincipal login with certificate (#53) 2019-05-06 13:22:16 -07:00
Changelog.md Fix bug preventing any tenants from being discovered (#76) 2019-08-16 12:37:27 -07:00
LICENSE Initial commit 2017-09-06 16:29:08 -07:00
README.md update min version of dependencies in the package (#68) 2019-07-23 10:59:30 -07:00
azure-pipelines.yml Add support for client_id, object_id and ms_res_id query parameters for VmMSI and fixed a bug in AzureCliCredentials (#59) 2019-05-20 11:16:03 -07:00
package.json Fix bug preventing any tenants from being discovered (#76) 2019-08-16 12:37:27 -07:00
rollup.config.js Add rollup 2018-11-14 15:33:27 -08:00
sample.env Implemented ServicePrincipal login with certificate (#53) 2019-05-06 13:22:16 -07:00
tsconfig.json Implemented ServicePrincipal login with certificate (#53) 2019-05-06 13:22:16 -07:00
tslint.json Remove Travis configuration and add no-return-await TS lint rule (#33) 2018-11-12 13:46:57 -08:00

README.md

ms-rest-nodeauth Build Status

This library provides different node.js based authentication mechanisms for services in Azure. It also contains rich type definitions thereby providing a good TypeScript experience. All the authentication methods support callbacks as well as promises. If they are called within an async method in your application then you can use the async/await pattern as well.

Example

username/password based login

import * as msRestNodeAuth from "@azure/ms-rest-nodeauth";

const username = process.env["AZURE_USERNAME"];
const password = process.env["AZURE_PASSWORD"];

msRestNodeAuth.loginWithUsernamePasswordWithAuthResponse(username, password).then((authres) => {
  console.dir(authres, { depth: null })
}).catch((err) => {
  console.log(err);
});

service-principal and secret based login

import * as msRestNodeAuth from "@azure/ms-rest-nodeauth";

const clientId = process.env["CLIENT_ID"];
const secret = process.env["APPLICATION_SECRET"];
const tenantId = process.env["DOMAIN"];

msRestNodeAuth.loginWithServicePrincipalSecretWithAuthResponse(clientId, secret, tenantId).then((authres) => {
  console.dir(authres, { depth: null })
}).catch((err) => {
  console.log(err);
});

service-principal and certificate based login by providing an ABSOLUTE file path to the .pem file

import * as msRestNodeAuth from "@azure/ms-rest-nodeauth";

const clientId = process.env["CLIENT_ID"];
const tenantId = process.env["DOMAIN"];

msRestNodeAuth.loginWithServicePrincipalCertificateWithAuthResponse(clientId, "/Users/user1/foo.pem", tenantId).then((authres) => {
  console.dir(authres, { depth: null })
}).catch((err) => {
  console.log(err);
});

service-principal and certificate based login by providing the certificate and private key (contents of the .pem file)

import * as msRestNodeAuth from "@azure/ms-rest-nodeauth";

const clientId = process.env["CLIENT_ID"];
const tenantId = process.env["DOMAIN"];
const certificate = 
`
-----BEGIN PRIVATE KEY-----
xxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxx
-----END PRIVATE KEY-----
-----BEGIN CERTIFICATE-----
yyyyyyyyyyyyyyyyy
yyyyyyyyyyyyyyyyy
-----END CERTIFICATE-----
`;

msRestNodeAuth.loginWithServicePrincipalCertificateWithAuthResponse(clientId, certificate, tenantId).then((authres) => {
  console.dir(authres, { depth: null })
}).catch((err) => {
  console.log(err);
});

interactive/device-code flow login

import * as msRestNodeAuth from "@azure/ms-rest-nodeauth";

msRestNodeAuth.interactiveLoginWithAuthResponse().then((authres) => {
  console.dir(authres, { depth: null })
}).catch((err) => {
  console.log(err);
});

service-principal authentication from auth file on disk

Before using this method please install az cli from https://github.com/Azure/azure-cli/releases. Then execute az ad sp create-for-rbac --sdk-auth > ${yourFilename.json}.

If you want to create the sp for a different cloud/environment then please execute:

  1. az cloud list

  2. az cloud set –n

  3. az ad sp create-for-rbac --sdk-auth > auth.json // create sp with secret.

       OR
    

    az ad sp create-for-rbac --create-cert --sdk-auth > auth.json // create sp with certificate. If the service principal is already created then login with service principal info:

  4. az login --service-principal -u <clientId> -p <clientSecret> -t <tenantId>

  5. az account show --sdk-auth > auth.json

import * as msRestNodeAuth from "@azure/ms-rest-nodeauth";

const options: msRestNodeAuth.LoginWithAuthFileOptions = {
  filePath: "<file path to auth file>",
}
msRestNodeAuth.loginWithAuthFileWithAuthResponse(options).then((authRes) => {
  console.log(authRes);
  console.log(process.env["AZURE_SUBSCRIPTION_ID"]);
}).catch((err) => {
  console.log(err);
});

MSI (Managed Service Identity) based login from a virtual machine created in Azure.

import * as msRestNodeAuth from "@azure/ms-rest-nodeauth";

const options: msRestNodeAuth.MSIVmOptions = {
  port: 50342;
}

msRestNodeAuth.loginWithVmMSI(options).then((msiTokenRes) => {
  console.log(msiTokenRes);
}).catch((err) => {
  console.log(err);
});

MSI (Managed Service Identity) based login from an AppService or Azure Function created in Azure.

import * as msRestNodeAuth from "@azure/ms-rest-nodeauth";

const options: msRestNodeAuth.MSIAppServiceOptions = {
  msiEndpoint: "http://127.0.0.1:41741/MSI/token/";
}

msRestNodeAuth.loginWithAppServiceMSI(options).then((msiTokenRes) => {
  console.log(msiTokenRes);
}).catch((err) => {
  console.log(err);
});

Getting credentials via Azure CLI.

Pre-requisite

  • Install azure-cli. For more information see here.
  • Login via az login
  • Detailed sample over here.
import { AzureCliCredentials } from "@azure/ms-rest-nodeauth";
import { ServiceClient, RequestPrepareOptions } from "@azure/ms-rest-js";

async function main(): Promise<void> {
  try {
    // Please make sure you have logged in via Azure CLI `az login` before executing this script.
    const creds = await AzureCliCredentials.create();
    const client = new ServiceClient(creds);
    console.log(">>> Subscription associated with the access token: '%s'.",
      creds.tokenInfo.subscription);

    const request: RequestPrepareOptions = {
      url: getUrl(creds.subscriptionInfo.id),
      method: "GET"
    };
    console.log(">>> Request url: '%s'.", request.url);

    const res = await client.sendRequest(request);
    console.log("List of resource groups from subscriptionId '%s': \n%O",
      creds.subscriptionInfo.id, res.parsedBody);

    // Let us change the subscriptionId, which should trigger refreshing the access token.
    const subscriptions = await AzureCliCredentials.listAllSubscriptions();
    creds.subscriptionInfo = subscriptions[1];

    console.log(">>> The new subscription id associated with the credential object is: '%s'.",
      creds.subscriptionInfo.id);
    request.url = getUrl(creds.subscriptionInfo.id);
    console.log(">>> Request url: '%s'.", request.url);

    const res2 = await client.sendRequest(request);
    console.log("List of resource groups from subscriptionId '%s': \n%O",
      creds.subscriptionInfo.id, res2.parsedBody);

    console.log(">>> Subscription associated with the access token: '%s'.",
      creds.tokenInfo.subscription);
  } catch (err) {
    console.log(err);
  }
}

function getUrl(subscriptionId: string): string {
  return `https://management.azure.com/subscriptions/${subscriptionId}/resourcegroups?api-version=2018-05-01`;
}

main();

Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.microsoft.com.

When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.