@equinor/fusion-framework-module-msal

中文文档
12.1.0发布于 2026/11/12ISCTypeScriptES Module近 30 天下载 1.3万

Microsoft Authentication Library (MSAL) integration module for Fusion Framework

原始使用文档
文档目录14 节
  1. Features
  2. Quick Start
  3. Documentation
  4. Configuration
  5. Required Settings
  6. Optional Settings
  7. Environment Variables
  8. Operational telemetry
  9. Testing
  10. Module Hoisting
  11. Additional Resources
  12. Official Documentation
  13. Learning Resources
  14. Support

@equinor/fusion-framework-module-msal

@equinor/fusion-framework-module-msal provides secure Azure AD authentication for browser applications using Microsoft's MSAL (Microsoft Authentication Library). Perfect for web applications, SPAs, and React apps that need to authenticate with Microsoft services.

Version: This package now uses MSAL Browser v4, providing the latest security improvements and features from Microsoft.

Features

  • Single Sign-On (SSO) support for Microsoft Azure AD and Azure AD B2C
  • Token Management with automatic refresh and secure caching
  • Module Hoisting for shared authentication state across application scopes
  • Silent Authentication for seamless user experience
  • Popup & Redirect Flows for different authentication scenarios
  • Zero Configuration with sensible defaults and optional customization
  • MSAL v4 Compatibility with v2 proxy layer for backward compatibility

Quick Start

pnpm add @equinor/fusion-framework-module-msal
import { enableMSAL, initialize, type IMsalProvider } from '@equinor/fusion-framework-module-msal';
import { ModulesConfigurator } from '@equinor/fusion-framework-module';

// 1. Configure the module
const configurator = new ModulesConfigurator();

enableMSAL(configurator, (builder) => {
  builder.setClientConfig({
    auth: {
      clientId: 'your-client-id',
      tenantId: 'your-tenant-id',
      redirectUri: 'https://your-app.com/callback'
    }
  });
  // With requiresAuth=true, the module will attempt automatic login during initialization
  // and await a valid authenticated account before initialization completes
  builder.setRequiresAuth(true);
});

// 2. Initialize the framework (auto-initializes auth provider)
const framework = await initialize(configurator);
const auth: IMsalProvider = framework.auth;

// 3. Optional: Handle authentication redirect manually (auto-called during initialization)
const redirectResult = await auth.handleRedirect();
if (redirectResult?.account) {
  console.log('Authenticated:', redirectResult.account.username);
}

// 4. Use authentication
// Option A: Token acquisition (v4 format - recommended)
const token = await auth.acquireAccessToken({ 
  request: { scopes: ['api://your-app-id/.default'] } 
});

// Option B: Legacy format (still supported via v2 proxy)
const legacyToken = await auth.acquireAccessToken({ 
  scopes: ['api://your-app-id/.default'] 
});

// Option C: Silent authentication with fallback
try {
  const result = await auth.login({ 
    request: { scopes: ['api://your-app-id/.default'] },
    silent: true  // Attempts SSO first
  });
} catch {
  // Fallback to interactive if silent fails
  await auth.login({ 
    request: { scopes: ['api://your-app-id/.default'] },
    behavior: 'popup'
  });
}

[!IMPORTANT] The @equinor/fusion-framework-app enables this package by default, so applications using the app package do not need to enable this module manually.

Documentation

GuideCovers
ConfigurationenableMSAL, builder methods, IMsalProvider, and type definitions
Backend-Issued Auth Code FlowSigning a user in from a backend-issued SPA auth code, without an interactive prompt
TestingThe /mock entry point: in-process authentication, deterministic tokens, and spying
Version ManagementVersion resolution, compatibility checking, and related errors
Migration v2 to v4Moving from MSAL Browser v2 to v4, including the compatibility proxy
TroubleshootingCommon failures and where to get help

Configuration

Required Settings

SettingDescriptionRequired
auth.clientIdAzure AD application client ID✅
auth.tenantIdAzure AD tenant ID✅
auth.redirectUriAuthentication callback URLOptional

Optional Settings

SettingDescriptionDefault
requiresAuthAuto-authenticate on initializationfalse
versionForce specific MSAL versionLatest

Environment Variables

# Required
AZURE_CLIENT_ID=your-client-id
AZURE_TENANT_ID=your-tenant-id

# Optional
AZURE_REDIRECT_URI=https://your-app.com/callback

Operational telemetry

When the telemetry module is available, the provider records safe terminal outcomes as fusion.msal.<operation>.duration metrics: acquire-token, login, logout, handle-redirect and exchange-auth-code. Outcomes are success, failure or cancelled; MSAL's user_cancelled error and a suppressed acquisition in MSAL's hidden iframe are cancellations. A failure adds one sanitized .failure exception, and a failed silent login before the interactive fallback adds a fusion.msal.silent-login-fallback warning. Tokens, scopes, accounts, usernames, login hints, auth codes and original errors are never recorded. Return values and thrown errors are unchanged. Token acquisition for a telemetry export request, marked with markTelemetryExportRequest, is not recorded, so an exporter using an authenticated Fusion HTTP client does not generate records to export.

These records replace the legacy module-msal.acquireToken measurement and the acquireToken-failed, logout.failed, handleRedirect.failed, initialize.auth-code-exchange-failed and login.silent-failed exceptions. Remaining legacy module-msal.* events no longer include username, requested or default scopes, or the logout redirectUri, and canLoginSilently is a boolean.

Testing

Import from @equinor/fusion-framework-module-msal/mock to authenticate in-process instead of against Entra ID. The real configurator, provider and schema validation still run — only the client that would contact Entra ID is substituted.

import { enableMsalMock } from '@equinor/fusion-framework-module-msal/mock';

enableMsalMock(configurator);

A user named Test User is signed in, and tokens are real JWTs minted in-process with a fixed issue time, so they are identical across runs and machines.

The entry point has no test-runner dependency, and ships no mocking API of its own — spying on a call is your test runner's job.

See Testing for choosing the signed-in user, signed-out behaviour and runner guidance, or @equinor/fusion-framework/mock to mock every framework boundary at once.

Module Hoisting

The module implements a hoisting pattern where the authentication provider is created once at the root level and shared across all sub-modules. This ensures consistent authentication state throughout your application while maintaining security and performance.

[!IMPORTANT] Configure the auth module only in the root Fusion Framework instance - Sub-instances will automatically inherit the authentication configuration from the parent.

Additional Resources

Official Documentation

Learning Resources

Support

版本与发布记录

@equinor/fusion-framework-module-msal 共发布 183 个版本,首次发布于 2022/01/14;过去 12 个月发布了 39 个版本。

版本发布日期距今
12.1.0latest2026/11/121 天前
12.1.0-next.1next2026/11/112 天前
12.1.0-next.02026/10/2816 天前
12.0.2-trusted-publishing-20261015-9dfe7602cd2a49a8a9df00d62c26e7304a82a85atrusted-publishing-202610152026/10/1629 天前
12.0.12026/10/121 个月前
12.0.02026/09/281 个月前

其他发布通道:apploader → 4.0.7-apploader-be21a6b60eb13eee8bf869e75002d8634e9c50a9,cli-search-index → 6.0.1-cli-search-index.0,msal-v5 → 7.0.0-msal-v5.1,trusted-publishing-20261015 → 12.0.2-trusted-publishing-20261015-9dfe7602cd2a49a8a9df00d62c26e7304a82a85a,next → 12.1.0-next.1。

关于 @equinor/fusion-framework-module-msal 的常见问题

@equinor/fusion-framework-module-msal 是什么?

Microsoft Authentication Library (MSAL) integration module for Fusion Framework

如何安装 @equinor/fusion-framework-module-msal?

在项目目录运行 npm install @equinor/fusion-framework-module-msal,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:zod(^4.0.0)、semver(^7.0.0)、typescript(>=5.0.0)、@equinor/fusion-framework-module(^6.2.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@equinor/fusion-framework-module-msal 支持 TypeScript 吗?

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

@equinor/fusion-framework-module-msal 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@equinor/fusion-framework-module-msal 可以商用吗?

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

@equinor/fusion-framework-module-msal 的下载量有多少?

根据 npm 官方下载统计,@equinor/fusion-framework-module-msal 在 2026/10/14 – 2026/11/12 这 30 天内被下载 13,463 次,日均约 449 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@equinor/fusion-framework-module-msal 还在维护吗?

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