@axa-fr/oidc-client-service-worker
v7.30.5
Published
OpenID Connect & OAuth authentication service worker
Readme
@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
- Getting started
- Trusted domains
- Resource-specific token isolation
- Deployment and troubleshooting
- Protocol reference
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"] --> WorkerThe 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
Install and configure the vanilla client or React bindings.
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 publicReplace
publicif your framework serves static files from another directory. The command overwritesOidcServiceWorker.jsbut preserves an existingOidcTrustedDomains.js.Replace the sample entries in
public/OidcTrustedDomains.jswith your provider and API URLs, as described below.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 tofalseonly if you accept fallback to JavaScript-accessible browser storage when the worker is unavailable.Add the appropriate copy command to your application's
postinstallscript. 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.
Resource-specific token isolation
Use the local OpenIddict demos to test
two isolated audiences and DPoP nonce challenges end to end. Their local trust
rules activate only for a localhost worker explicitly selected with
provider=openiddict; the online trust rules do not gain localhost access.
Generated DPoP targets exclude query strings and fragments.
The clients' RFC 8707 resource API uses nested resource entries in the existing named/tab session, not separate OIDC clients. Access tokens, authorization state, PKCE, grant references, and DPoP state remain associated with the selected resource. Default and sibling access tokens are not replaced by acquisition or renewal. Additional authorization requires a fresh ID token for the same authenticated subject and transaction nonce. A resource refresh may omit an ID token even after its earlier ID token expires.
Configure resources in the client and allow both API origins in the worker:
const trustedDomains = {
default: {
oidcDomains: [/^https:\/\/issuer\.example\.com(?:\/|$)/],
accessTokenDomains: [
/^https:\/\/orders\.example\.com(?:\/|$)/,
/^https:\/\/inventory\.example\.com(?:\/|$)/,
],
},
};Use fetchForResource() or useOidcFetchForResource(), not automatic selection
by URL. A selected placeholder must match its exact resource context and both
the client URL mapping and worker trusted-domain rules. Managed API requests
without resource selection do not receive an arbitrary/default worker token,
including paths outside the permitted prefixes on registered API origins.
The standard provider user-info flow retains its default token handling.
Default renewal also retains its own token entry for single-resource grants.
The client sends a worker-only routing hint for explicit resource refreshes;
the worker verifies it against the OAuth resource and removes it before dispatch.
Resource tokens are always masked, including with showAccessToken: true.
Automatic API redirects are rejected.
Shared refresh rotation is retained centrally; independently authorized resources keep their own grants. Clearing a resource resets only its entry; clearing the base session removes all nested entries. Late responses cannot recreate cleared state. Logout can revoke resource tokens through the exact trusted revocation endpoint.
DPoP remains configured through this file's existing options. Resource API proofs use the selected real token and its bound key/nonce; the browser receives neither refresh tokens nor private keys from the resource API. API nonces are retained per resource separately from authorization-server nonces. API responses are not automatically retried.
Resource support requires the resourceTokenManagement capability returned by
protocol 1.1 workers. Copy and deploy the matching OidcServiceWorker.js with
each client upgrade. Older/unavailable workers cause explicit resource errors,
not browser-token fallback. Worker token memory is not durable: after loss,
restore or reauthenticate the original session before requesting resource
tokens. Resource entries cannot be reconstructed from stale placeholders.
See the core guide for provider/opaque-token limitations.
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
accessTokenDomainsentry, 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 with the root-pinned pnpm version. Keep this package's
Vitest and coverage versions aligned. Run pnpm audit --audit-level low at the
root to include development dependencies.
