npm 中文文档 npm 中文文档
指南
npmjs.com (opens new window)
指南
npmjs.com (opens new window)
  • 快速入门

    • npm 是什么?
    • npm 安装和更新
    • npm 防止权限错误
    • npm package.json 文件
    • npm 安装包
    • npm 更新包
    • npm 卸载包
    • npm 创建 Node.js 模块
    • npm 发布和更新包
    • npm 使用语义化版本
    • npm 使用 Dist-tags 标记包
    • npm 包和模块的了解
  • 命令行
  • 配置 npm

azure-devops-npm-auth


Uses the OAuth 2 device code flow to authenticate against the Azure DevOps artifact private registry.

Why? 🤔


Microsoft provides the vsts-npm-auth package for this task but sadly, it's not cross-platform and doesn't automatically handle token refresh.

There's also better-vsts-npm-auth which solves these issues but requires manual setup (not ideal for a dev team) and authentication through a web app, which in my opinion isn't the best flow to use in the command line.

The azure-devops-npm-authsolves all these problems mainly by using the OAuth 2 device code flow.  Once authenticated, access and refresh tokens are then stored in the user's personal .npmrc file, keeping them secure and out of any code repository.

Installation 💪


Simply run npm i azure-devops-npm-auth --save-dev.

Usage 🤷‍♂️


First, add a pre-installation script to your package.json file like so:

  1. ``` js
  2.   "scripts": {
  3.     "preinstall": "azure-devops-npm-auth"
  4.     ...
  5.   },
  6. ```

Then, setup the project repository in the .npmrc file as documented in the Azure DevOps npm feed connection page:

When installing packages using npm i, the preinstallation script will be executed and ask you to login using a device code:

Follow the instructions to login and authenticate npm to the Azure DevOps private feed.  The following installation should be able to use the refresh tokenand automate the task of authenticating:

Advanced Usage 🧙‍♂️


If you want to use your own Azure Active Directory application, it's possible to specify the client_id and tenant_id arguments:

  1. ``` js
  2.   "scripts": {
  3.     "preinstall": "azure-devops-npm-auth --client_id='xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' --tenant_id='xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'"
  4.     ...
  5.   },
  6. ```

Note: If your Azure Active Directory application is configured to be multitenant, tenant_id can also be common (is the default; Work and school accounts or personal Microsoft accounts), consumers (personal Microsoft accounts) or organizations (work and school accounts).

When creating your own Azure Active Direction application, under the authentication section, you need the configure it to be a public application:

You also need to add the required API permissions to have 'Azure DevOps user_impersonation ' and 'Microsoft Graph User.Read ':

register-azure-devops-npm-auth.ps1


To automate the creation of the above AD app registration in yourAD tenant:

Ensure you have installed azure-cli
Open a powershell core prompt (minimum version 6.1)
Run ./register-azure-devops-npm-auth.ps1 -Login -InfA Continue

This will open your default browser where you will need to login to Azure with credentials that has the Azure AD 'Global administrator' RBAC role

Continuous integration


To disable authentication within CI environments add the --ci flag which skips authentication when the TF_BUILD environment variable is set (which is automatically set in Azure DevOps build pipelines):

  1. ``` js
  2.   "scripts": {
  3.     "preinstall": "azure-devops-npm-auth --ci"
  4.     ...
  5.   },
  6. ```

It's also possible to specify a custom environment variable:

  1. ``` js
  2.   "scripts": {
  3.     "preinstall": "azure-devops-npm-auth --ci=MY_CUSTOM_VARIABLE"
  4.     ...
  5.   },
  6. ```

Project Base Path


You can pass in a path to customize the directory to look in for the project's .npmrc file. The default value is the current working directory:

  1. ``` js
  2.   "scripts": {
  3.     "preinstall": "azure-devops-npm-auth --project_base_path=./configs"
  4.     ...
  5.   },
  6. ```

Note: this is the path to the directory that contains the .npmrc file, meaning you do notneed to specify the .npmrc in the path.

Special Thanks 👏


I have to give thanks to the author(s) of better-vsts-npm-auth which was a big inspiration of mine for this project.  Also, thanks to openid-client for simplifying the process of integrating the OAuth device code flow to the code.

License 👩‍⚖️


Copyright © 2022, GSoft inc. This code is licensed under the Apache License, Version 2.0. You may obtain a copy of this license at https://github.com/gsoft-inc/gsoft-license/blob/master/LICENSE.
Last Updated: 2023-05-15 10:22:02