Azure DevOps Authentication CLI Wrapper
The `azureauth` package serves as a Node.js wrapper for the `microsoft-authentication-cli`, designed to streamline Azure Active Directory (AAD) authentication processes within Node.js applications and npm scripts. Its primary utility lies in automating the download and management of the underlying `azureauth` CLI executable, ensuring it's scoped locally to `./node_modules/.bin`. This unique approach allows for multiple versions of the AzureAuth CLI to coexist on a single machine, mitigating versioning conflicts. As of version 0.14.0, the package is in active pre-1.0 development, indicating an iterative release cadence with potential API changes. It is particularly useful for scenarios requiring Personal Access Token (PAT) generation for Azure DevOps (ADO) feeds, offering a cross-platform alternative to Windows-only tools like `vsts-npm-auth`. While `@azure/identity` provides a comprehensive SDK for general Azure authentication, `azureauth` specifically targets CLI-driven AAD authentication tasks.
Common errors
-
'azureauth' is not recognized as an internal or external command, operable program or batch file.
cause The `azureauth` CLI executable is typically installed into `node_modules/.bin`. This error occurs when attempting to run it directly from the system PATH without using `npm exec`, `npx`, or npm scripts.fixAlways execute the `azureauth` CLI wrapper via `npx azureauth`, `npm exec azureauth`, or by defining it in your `package.json` scripts (e.g., `"scripts": { "authcli": "azureauth --version" }` and then `npm run authcli`). -
code E401 npm ERR! Unable to authenticate, need: Bearer authorization.
cause This error commonly occurs when attempting to access private npm feeds (e.g., Azure Artifacts) without a valid or current authentication token. The existing token in your `.npmrc` might be expired or invalid.fixRun `ado-npm-auth` (which uses `azureauth` internally) or use the `adoPat` function programmatically to refresh your `.npmrc` credentials. For `ado-npm-auth`, you might run `npx ado-npm-auth` or specify a config file: `npx ado-npm-auth -c .npmrc`. -
Error: User canceled authentication or timed out.
cause During an interactive authentication flow initiated by `adoPat` or the CLI, the user either closed the browser window/dialog without completing authentication or failed to authenticate within the allotted time.fixEnsure the user is ready to complete the authentication prompt in their browser promptly. Check network connectivity and proxy settings that might interfere with the browser redirect or token exchange. Increase `timeout` if available in the API options for slower environments.
Warnings
- breaking As a pre-1.0 package (v0.14.0), API surfaces may change frequently without adhering to strict semantic versioning, potentially introducing breaking changes in minor releases.
- gotcha The package wraps an external CLI executable. Ensure the host system's environment allows execution of downloaded binaries, especially in restricted CI/CD environments or hardened systems. Issues can arise from proxy settings or security policies blocking downloads/execution.
- gotcha Azure authentication, especially for Azure DevOps, often involves Multi-Factor Authentication (MFA) or Conditional Access policies. Non-interactive logins may fail with errors like '50126' or '53003' if MFA is required but cannot be performed.
- deprecated The underlying `microsoft-authentication-cli` and related Azure authentication mechanisms are continuously updated. Older token types (e.g., 'classic tokens') may be deprecated, and more granular, time-limited tokens with 2FA requirements are becoming standard.
Install
-
npm install azureauth -
yarn add azureauth -
pnpm add azureauth
Imports
- adoPat
const { adoPat } = require('azureauth');import { adoPat } from 'azureauth'; - runAzureAuthCli
import { runAzureAuthCli } from 'azureauth';import { runAzureAuthCli } from 'azureauth/lib/cli'; - AzureAuthOptions
import { AzureAuthOptions } from 'azureauth';import { type AzureAuthOptions } from 'azureauth';
Quickstart
import { adoPat } from 'azureauth';
async function getAzureDevOpsPat() {
try {
const pat = await adoPat({
displayName: 'my-cli-auth-pat',
organization: process.env.AZDO_ORG_URL ?? 'https://dev.azure.com/myorganization',
promptHint: 'Please authenticate in your browser',
scope: ['vso.packaging_write'], // Example scope
});
console.log('Successfully generated ADO PAT.');
// In a real application, securely store or use the PAT
// console.log('PAT:', pat); // Do not log PAT to console in production!
return pat;
} catch (error) {
console.error('Failed to get ADO PAT:', error);
throw error;
}
}
getAzureDevOpsPat()
.then(() => console.log('ADO PAT process completed.'))
.catch(() => console.error('Exiting due to PAT generation failure.'));