@axa-fr/oidc-client-service-worker

中文文档
7.29.8发布于 2026/10/15MITTypeScriptES Module零依赖构建来源已验证近 30 天下载 18.1万

OpenID Connect & OAuth authentication service worker

原始使用文档
文档目录5 节
  1. How it works
  2. Getting started
  3. Trusted domains
  4. Deployment and troubleshooting
  5. Protocol reference

@axa-fr/oidc-client-service-worker

The service worker used by @axa-fr/oidc-client and @axa-fr/react-oidc. It intercepts OIDC token responses, keeps access and refresh tokens in worker memory by default, and adds access tokens to requests that match your trusted-domain configuration.

Most applications should install one of the client packages rather than use this package directly. The clients handle worker registration and communication.

How it works

flowchart LR
    App["Browser application"] --> Client["OIDC client or React bindings"]
    Client <-->|"Messages and token placeholders"| Worker["OIDC service worker"]
    Worker <-->|"Token requests and responses"| Provider["OIDC provider"]
    App -->|"API request"| Worker
    Worker -->|"Access token on trusted requests"| API["Trusted API"]
    Config["OidcTrustedDomains.js"] --> Worker

The application can use token metadata without receiving the real access or refresh token. showAccessToken: true explicitly exposes the access token to application JavaScript while keeping the refresh token in the worker.

[!IMPORTANT] Token isolation does not prevent XSS or CSRF. Injected code can still make authenticated requests from the application. Use a restrictive Content Security Policy, prevent script injection, and enforce authorization on your APIs.

Getting started

  1. Install and configure the vanilla client or React bindings.

  2. Create your application's public-assets directory if it does not exist, then run the copy command for the client you installed:

    # Vanilla client
    node ./node_modules/@axa-fr/oidc-client/bin/copy-service-worker-files.mjs public
    
    # React — use this instead
    node ./node_modules/@axa-fr/react-oidc/bin/copy-service-worker-files.mjs public
    

    Replace public if your framework serves static files from another directory. The command overwrites OidcServiceWorker.js but preserves an existing OidcTrustedDomains.js.

  3. Replace the sample entries in public/OidcTrustedDomains.js with your provider and API URLs, as described below.

  4. Add these fields to your OIDC client configuration:

    const configuration = {
      ...oidcConfiguration,
      service_worker_relative_url: '/OidcServiceWorker.js',
      service_worker_only: true,
    };
    

    With service_worker_only: true, authentication requires worker support. Set it to false only if you accept fallback to JavaScript-accessible browser storage when the worker is unavailable.

  5. Add the appropriate copy command to your application's postinstall script. For example, for React:

    {
      "scripts": {
        "postinstall": "node ./node_modules/@axa-fr/react-oidc/bin/copy-service-worker-files.mjs public"
      }
    }
    

    Merge this with existing installation steps rather than replacing them. Deploy the copied worker with every client upgrade.

Trusted domains

Keep provider endpoints and API destinations separate:

const trustedDomains = {
  default: {
    oidcDomains: [/^https:\/\/identity\.example\.com(?:\/|$)/],
    accessTokenDomains: [/^https:\/\/api\.example\.com(?:\/|$)/],
  },
};

default is the default client configuration name. Add a matching entry for every named client or OidcProvider. These rules use regular-expression matching, including when rules are strings. Anchor patterns, escape hostname dots, and include a host or path boundary so that lookalike URLs do not match. Include the actual OIDC endpoint origins if your provider serves discovery, tokens, or user information from different hosts. Allow only the API destinations that should receive this configuration's access token; do not copy the demo allowlist into production.

Use oidcClient.fetchWithTokens(fetch) in vanilla applications or useOidcFetch() in React. In particular, allowMultiTabLogin: true requires the OIDC fetch wrapper: its placeholder identifies the tab whose token the worker must inject. A plain fetch cannot supply that information.

See the client's service-worker guide for additional options, including DPoP, access-token visibility, and request handling.

Deployment and troubleshooting

  • Serve both JavaScript files from your application's origin over HTTPS (localhost is supported for development).
  • The worker must control the pages that use it. Serving it at the origin root is the simplest setup; account for worker scope when hosting under a subpath.
  • Serve the files as JavaScript, not the HTML fallback used for SPA routes. Check the Network and Application panels in browser developer tools if registration fails.
  • Keep the worker version aligned with the client. Edit the trusted-domain file, not the generated OidcServiceWorker.js.
  • If API requests return 401, check the active configuration name, the matching accessTokenDomains entry, and whether you are using the OIDC fetch wrapper. The API must also accept the token's issuer, audience, and scopes.
  • After changing worker assets, check which worker version controls your page; an already-open tab may still be controlled by an older worker.

The FAQ covers broader security and integration questions.

Protocol reference

Most applications do not need to send worker messages directly. For custom integrations, PROTOCOL.md describes the versioned message envelope, payloads, token placeholders, storage keys, and compatibility guarantees.

Protocol constants and types are exported from @axa-fr/oidc-client-service-worker/protocol and re-exported by @axa-fr/oidc-client.

For source development, use the root contributor commands and the committed lockfile. Keep this package's Vitest and coverage versions aligned.

Back to the project overview

版本与发布记录

@axa-fr/oidc-client-service-worker 共发布 261 个版本,首次发布于 2023/07/23;过去 12 个月发布了 37 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
7.29.8latest2026/10/1523 天前
7.29.72026/10/1325 天前
7.29.62026/09/201 个月前
7.29.52026/09/191 个月前
7.29.42026/09/191 个月前
7.29.32026/09/131 个月前

关于 @axa-fr/oidc-client-service-worker 的常见问题

@axa-fr/oidc-client-service-worker 是什么?

OpenID Connect & OAuth authentication service worker

如何安装 @axa-fr/oidc-client-service-worker?

在项目目录运行 npm install @axa-fr/oidc-client-service-worker,或使用 pnpm add / yarn add / bun add。

@axa-fr/oidc-client-service-worker 支持 TypeScript 吗?

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

@axa-fr/oidc-client-service-worker 支持 ESM 还是 CommonJS?

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

@axa-fr/oidc-client-service-worker 可以商用吗?

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

@axa-fr/oidc-client-service-worker 的下载量有多少?

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

@axa-fr/oidc-client-service-worker 还在维护吗?

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