@azure/msal-node-extensions

中文文档
5.6.1发布于 2026/11/07MITTypeScriptESM + CJS近 30 天下载 159.8万

npm (scoped) npm

原始使用文档
文档目录17 节
  1. About
  2. Cache Persistence
  3. Brokering
  4. Prerequisites
  5. Installation
  6. Usage - Cache Persistence
  7. Getting started
  8. Security boundary
  9. Usage - Brokering
  10. Build and Test
  11. Building the package locally
  12. Running Tests
  13. Samples
  14. Security Reporting
  15. License
  16. Contributing
  17. We Value and Adhere to the Microsoft Open Source Code of Co…

Microsoft Authentication Extensions for Node

npm (scoped) npm

Getting StartedAAD DocsLibrary ReferenceSamples
  1. About
  2. FAQ
  3. Changelog
  4. Prerequisites
  5. Installation
  6. Usage - Cache Persistence
  7. Usage - Brokering
  8. Build and Test
  9. Samples
  10. Security Reporting
  11. License
  12. Contributing
  13. Code of Conduct

About

The msal-node-extensions library offers optional features to enhance the capabilities of msal-node:

  • Secure mechanisms for client applications to perform cross-platform token cache serialization and persistence
  • An interface for acquiring tokens from the native token broker, enabling a higher level of security and SSO with other native applications

Cache Persistence

MSAL Node supports an in-memory cache by default and provides the ICachePlugin interface to perform cache serialization, but does not provide a default way of storing the token cache to disk. Microsoft authentication extensions for node is default implementation for persisting cache to disk across different platforms.

Supported platforms are Windows, Mac and Linux:

  • Windows - DPAPI is used for encryption.
  • MAC - The MAC KeyChain is used.
  • Linux - LibSecret is used for storing to "Secret Service".

Note: It is recommended to use this library for cache persistence support for Public client applications such as Desktop apps only. In web applications, this may lead to scale and performance issues. Web applications are recommended to persist the cache in session.

Brokering

When using the native broker, refresh tokens are bound to the device on which they are acquired on and are not accessible by msal-node or the application. This provides a higher level of security that cannot be achieved by msal-node alone. More information about token brokering can be found here

Prerequisites

The msal-node-extensions library ships with pre-compiled binaries.

Note: If you are planning to do local development on msal-node-extensions itself you may need to install some additional tools. node-gyp is used to compile addons for accessing system APIs. Installation requirements are listed on the node-gyp README

On linux, the library uses libsecret so you may need to install it. Depending on your distribution, you will need to run the following command:

  • Debian/Ubuntu: sudo apt-get install libsecret-1-dev
  • Red Hat-based: sudo yum install libsecret-devel
  • Arch Linux: sudo pacman -S libsecret

Installation

The msal-node-extensions package is available on NPM.

npm i @azure/msal-node-extensions --save

Usage - Cache Persistence

Getting started

Here is a code snippet on how to configure the token cache.

const { 
    DataProtectionScope,
    Environment,
    PersistenceCreator,
    PersistenceCachePlugin,
} = require("@azure/msal-node-extensions");

// You can use the helper functions provided through the Environment class to construct your cache path
// The helper functions provide consistent implementations across Windows, Mac and Linux.
const cachePath = path.join(Environment.getUserRootDirectory(), "./cache.json");

const persistenceConfiguration = {
    cachePath,
    dataProtectionScope: DataProtectionScope.CurrentUser,
    serviceName: "<SERVICE-NAME>",
    accountName: "<ACCOUNT-NAME>",
    usePlaintextFileOnLinux: false,
}

// The PersistenceCreator obfuscates a lot of the complexity by doing the following actions for you :-
// 1. Detects the environment the application is running on and initializes the right persistence instance for the environment.
// 2. Performs persistence validation for you.
// 3. Performs any fallbacks if necessary.
PersistenceCreator
.createPersistence(persistenceConfiguration)
.then(async (persistence) => {
    const publicClientConfig = {
        auth: {
            clientId: "<CLIENT-ID>",
            authority: "<AUTHORITY>",
        },

        // This hooks up the cross-platform cache into MSAL
        cache: {
            cachePlugin: new PersistenceCachePlugin(persistence)
        }
    };

    const pca = new msal.PublicClientApplication(publicClientConfig);
    
    // Use the public client application as required...
});

All the arguments for the persistence configuration are explained below:

Field NameDescriptionRequired For
cachePathThis is the path to the lock file the library uses to synchronize the reads and the writesWindows, Mac and Linux
dataProtectionScopeSpecifies the scope of the data protection on Windows either the current user or the local machine.Windows
serviceNameThis specifies the service name to be used on Mac and/or LinuxMac and Linux
accountNameThis specifies the account name to be used on Mac and/or LinuxMac and Linux
usePlaintextFileOnLinuxThis is a flag to default to plain text on linux if libsecret fails. Defaults to falseLinux

Security boundary

On Windows and Linux, the token cache is scoped to the user session, i.e. all applications running on behalf of the user can access the cache. Mac offers a more restricted scope, ensuring that only the application that created the cache can access it, and prompting the user if others apps want access.

Usage - Brokering

Enabling token brokering requires just one new configuration parameter:

import { PublicClientApplication, Configuration } from "@azure/msal-node";
import { NativeBrokerPlugin } from "@azure/msal-node-extensions";

const msalConfig: Configuration = {
    auth: {
        clientId: "your-client-id"
    },
    broker: {
        nativeBrokerPlugin: new NativeBrokerPlugin()
    }
};

const pca = new PublicClientApplication(msalConfig);

More detailed information can be found in the brokering documentation

Build and Test

If you intend to contribute to the library visit the contributing section for even more information on how.

Building the package locally

To build both the @azure/msal-node-extensions library and @azure/msal-common libraries, run the following commands:

// Install dev dependencies from root of repo
npm install
// Change to the msal-node-extensions package directory
cd lib/msal-node-extensions/
// Build msal-common and msal-node-extensions
npm run build:all

Running Tests

@azure/msal-node-extensions uses jest to run unit tests and coverage.

// To run tests
npm test

Samples

Security Reporting

If you find a security issue with our libraries or services please report it to [email protected] with as much detail as possible. Your submission may be eligible for a bounty through the Microsoft Bounty program. Please do not post security issues to GitHub Issues or any other public site. We will contact you shortly upon receiving the information. We encourage you to get notifications of when security incidents occur by visiting this page and subscribing to Security Advisory Alerts.

License

Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT License.

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.opensource.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., status check, 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.

We Value and Adhere to the Microsoft Open Source Code of Conduct

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/msal-node-extensions 共发布 135 个版本,首次发布于 2020/07/14;过去 12 个月发布了 41 个版本。

版本发布日期距今
5.6.1latest2026/11/07今天
5.6.02026/11/052 天前
5.5.42026/10/2811 天前
5.5.32026/10/2217 天前
5.5.22026/10/031 个月前
5.5.12026/09/161 个月前

其他发布通道:alpha → 5.0.0-alpha.0,beta → 5.0.0-beta.0,lts → 1.5.32。

关于 @azure/msal-node-extensions 的常见问题

如何安装 @azure/msal-node-extensions?

在项目目录运行 npm install @azure/msal-node-extensions,或使用 pnpm add / yarn add / bun add。

@azure/msal-node-extensions 支持 TypeScript 吗?

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

@azure/msal-node-extensions 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@azure/msal-node-extensions 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=20。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@azure/msal-node-extensions 可以商用吗?

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

@azure/msal-node-extensions 的下载量有多少?

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

@azure/msal-node-extensions 还在维护吗?

最新版本 5.6.1 发布于 2026/11/07(今天);至今共发布 135 个版本,其中过去 12 个月发布了 41 个;首次发布于 2020/07/14。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。