@azure/ms-rest-nodeauth

中文文档
3.1.1发布于 2021/12/16MITTypeScriptCommonJS近 30 天下载 54.8万

Azure Authentication library in node.js with type definitions.

此软件包已被标记为弃用

This package has been deprecated in favor of the new identity packages from [Azure SDK for JavaScript](https://github.com/azure/azure-sdk-for-js).

原始使用文档
文档目录11 节
  1. Known issue with proxy support
  2. Example
  3. username/password based login
  4. service-principal and secret based login
  5. interactive/device-code flow login
  6. service-principal authentication from auth file on disk
  7. MSI (Managed Service Identity) based login from a virtual m…
  8. MSI (Managed Service Identity) based login from an AppServi…
  9. Getting credentials via Azure CLI.
  10. Authenticating with an existing token
  11. Contributing

ms-rest-nodeauth Build Status

The @azure/ms-rest-nodeauth package provides different authentication mechanisms meant to be used with a select set of packages from the Azure SDK for JavaScript and TypeScript.

There is no active development for @azure/ms-rest-nodeauth other than critical bug fixes. Please refer to the below table to find suggested alternatives for each Azure service for which you have been using credentials from @azure/ms-rest-nodeauth.

Azure servicePackageAlternative to @azure/ms-rest-nodeauth
Azure Resource ManagerPackages with names starting with @azure/arm-As of December 2021, new major versions of all packages targetting the Azure Resource Manager will drop support for credentials from @azure/ms-rest-nodeauth. Refer to the migration guide to use @azure/identity instead.
Application Insights@azure/applicationinsights-queryNot planned as this package will be deprecated in favor of @azure/monitor-query in early 2022.
Application Insights@azure/loganalyticsNot planned as this package will be deprecated in favor of @azure/monitor-query in early 2022.
Batch@azure/batchNo alternatives yet
Graph@azure/graphNot planned as this service will be deprecated in June 2022.
Service Fabric@azure/servicefabricNot planned as there is no active development for this package.
Storage Data Lake@azure/storage-datalakeNot planned as there is no active development for this package.

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.

Things to consider when using personal accounts:

When using personal accounts, the domain property in the options passed to the authentication methods is mandatory and should be set to the tenant Id. If this property is not set, the credentials created by the authentication methods will not be able to access any of the resources of the personal account. For that same reason, the list of subscriptions expected in the return value of these methods will be empty.

You can get the tenant Id from Azure portal or the Azure CLI. If you need to fetch the tenant Id programmatically, follow the below steps:

  • Use any of the authentication methods without setting the domain to get a credential. - Call the buildTenantLists(credential) method by sending that same credential as the first parameter to get the list of all tenants in your account.

You can now use any of the authentication methods and pass in the tenant Id or use the setDomain() method on the existing credential to change the tenant it uses to create the tokens.

Known issue with proxy support

This package uses adal-node which has moved to axios from request as the package of choice for httpRequests since npm support for request is discontinued. However it was noticed later that axios does not support proxies. The adal-node package is taking only security changes and will not be fixing this issue. Therefore, we recommend using https-proxy-agent as suggested in the issue linked in case you need proxies for your application. For example:

// paste this code to support proxy environment
// reference:
// https://github.com/axios/axios/issues/2072#issuecomment-567473812
const HttpsProxyAgent = require('https-proxy-agent');
const axios = require('axios')
axios.defaults.proxy = false
axios.defaults.httpsAgent = new HttpsProxyAgent('http://MY_PROXY:8888')

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.

The code below works for both system managed and user-assigned managed identities. You can leave the options empty if you want to use system managed identity. If you want to use the user-assigned managed identity, you must at least provide the clientId in the options. If your VM has multiple user-assigned managed identities, you must include objectId and identityId in the options as well.

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

const options: msRestNodeAuth.MSIVmOptions = {
  // The objectId of the managed identity you would like the token for.
  // Required, if your VM has multiple user-assigned managed identities.
  //
  //     objectId: "your-managed-identity-object-id",
  //
  // The clientId of the managed identity you would like the token for.
  // Required, if your VM has any user-assigned managed identity.
  //
  //     clientId: "your-managed-identity-client-id",
  //
  // The `Azure Resource ID` of the managed identity you would like the token for.
  // Required, if your VM has multiple user-assigned managed identities.
  //
  //     identityId: "your-managed-identity-identity-id",
  //
};

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.

The code below works for both system managed and user-assigned managed identities. You can leave the options empty if you want to use system managed identity. If you want to use the user-assigned managed identity, you must at least provide the clientId in the options.

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

const options: msRestNodeAuth.MSIAppServiceOptions = {
  // The clientId of the managed identity you would like the token for.
  // Required, if your app service has user-assigned managed identities.
  //
  //     clientId: "your-managed-identity-client-id"
  //
};

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();

Authenticating with an existing token

If you have acquired a valid Azure Active Directory token from another source, you can use it to authenticate with Azure SDK libraries using the following code snippet:

const { HttpHeaders } = require("@azure/ms-rest-js");

function getCredentialForToken(accessToken) {
  return {
    signRequest: (request) => {
      if (!request.headers) request.headers = new HttpHeaders();
      request.headers.set("Authorization", `Bearer ${accessToken}`);
      return Promise.resolve(request);
    }
  };
}

const creds = getCredentialForToken("your existing token");

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 [email protected] with any additional questions or comments.

版本与发布记录

@azure/ms-rest-nodeauth 共发布 33 个版本,首次发布于 2018/11/13;过去 12 个月发布了 0 个版本。

版本发布日期距今
3.1.1latest2021/12/164 年前
3.1.02021/09/225 年前
3.0.102021/05/215 年前
3.0.92021/04/015 年前
3.0.82021/03/245 年前
3.0.72021/02/245 年前

关于 @azure/ms-rest-nodeauth 的常见问题

@azure/ms-rest-nodeauth 是什么?

Azure Authentication library in node.js with type definitions.注意:该包已被作者标记为弃用——This package has been deprecated in favor of the new identity packages from [Azure SDK for JavaScript](https://github.com/azure/azure-sdk-for-js).

如何安装 @azure/ms-rest-nodeauth?

在项目目录运行 npm install @azure/ms-rest-nodeauth,或使用 pnpm add / yarn add / bun add。

@azure/ms-rest-nodeauth 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@azure/ms-rest-nodeauth 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@azure/ms-rest-nodeauth 可以商用吗?

@azure/ms-rest-nodeauth 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@azure/ms-rest-nodeauth 的下载量有多少?

根据 npm 官方下载统计,@azure/ms-rest-nodeauth 在 2026/10/08 – 2026/11/06 这 30 天内被下载 547,544 次,日均约 18,251 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@azure/ms-rest-nodeauth 还在维护吗?

最新版本 3.1.1 发布于 2021/12/16(4 年前);至今共发布 33 个版本,其中过去 12 个月发布了 0 个;首次发布于 2018/11/13。作者已将其标记为弃用,新项目建议寻找替代方案。