Skip to content
Wallet and Entity Collection draft updates
Documentation menu

11.2. Consume one-time JWT identifiers atomically

Feature guides / 11. Operating a federation application · Example context

A wallet proof accepted at Car Rental Company must not work a second time, even when the repeated request reaches another replica. The same problem applies to client assertions at eduGAIN's protected endpoints.

How to use it

Configure replay on the host whose roles and endpoints consume the proof. Here Umeå University OP's initialized OP role claims the Request Object identifier while processing automatic registration. Replaying an otherwise valid Request Object fails through that operation. Core supplies the shared ReplayStore contract and development adapter; the application does not manually consume the same identifier again. Attestation proof processing.

import { Leaf } from "@oidfed/leaf";
import { OidcProviderRole, type OidcProviderRoleConfig } from "@oidfed/oidc";
import { MemoryFederationStorage, type CryptoContext, type FederationStorage, type TrustAnchorSet } from "@oidfed/core";

export async function umeaOpWithReplayProtection(
  crypto: CryptoContext, trustAnchors: TrustAnchorSet, opMetadata: Record<string, unknown>,
  onRegistration: NonNullable<OidcProviderRoleConfig["onRegistration"]>,
  storage: FederationStorage = new MemoryFederationStorage({ namespace: "https://op.umu.example", capabilities: ["replay"] }),
) {
  const op = new OidcProviderRole({
    onRegistration,
    metadata: { ...opMetadata, client_registration_types_supported: ["automatic", "explicit"] },
  });
  const umeaOp = await Leaf.create({
    entityId: "https://op.umu.example", authorityHints: ["https://umu.example"],
    crypto, trustAnchors, storage, metadata: { federation_entity: {} }, roles: [op],
  });
  return {
    umeaOp, op,
    acceptRequest: (jwt: string) => op.processAutomaticRegistration(jwt),
  };
}

Pass Umeå's actual OP capabilities and endpoints as opMetadata; the example focuses on the replay-store wiring. Use a shared durable adapter when several processes accept requests. Attached OIDC and Wallet roles inherit the host’s replay capability. Standalone proof operations receive the same capability through options.storage.

What your application owns

Use durable, shared atomic storage for a replicated receiver. Retain identifiers through the accepted expiry and skew window, and fail closed when the store is unavailable. Keep namespaces isolated. A plain get-then-set sequence is not atomic. Replicas and routes accepting the same proof use the same receiver namespace.

Share replay decisions across accepting workers

If Umeå accepts the same proof on two workers, an in-memory replay store on each worker cannot make the decision atomic across the deployment. Use shared state with an atomic consume operation for the acceptance boundary that must reject a repeated identifier.

Keep the stored replay lifetime aligned with the proof's acceptance window, including the configured clock policy. A failed write must not mean the proof was accepted without replay protection. Exercise two concurrent uses in a test and confirm that only the permitted first use succeeds.

How do OPs prevent Request Object replay attacks?

A Federation automatic-registration Request Object needs a jti and exp. OP-side processing checks expiry and claims the (issuer, audience, jti) tuple through the configured ReplayStore. The claim is retained until exp + clockSkewSeconds, covering the full acceptance window required by Federation §12.1.1.1. Use a shared atomic implementation across instances; the OP role requires this store for automatic registration.

Assertion verification and replay storage

verifyClientAssertion() requires exp and a non-empty jti; iat is optional. It checks the signature, issuer/subject equality, exact audience, expiry, and any supplied issuance time. Pass options.storage with the replay capability to atomically claim a verified assertion. Expiry is checked again after replay storage completes. Without it, the function verifies the JWT without recording its use. Authentication handlers must provide replay storage. These claims follow OIDC Core §9, referenced by Federation §8.8.

A ReplayStore claims an (issuer, audience, jti) tuple until expiry. Claims must be atomic across the processes serving one tenant. Use durable replay storage if the receiver accepts proofs across restarts. A single-process memory store also loses its claims when the process restarts.

The memory adapter keys claims by the complete JSON tuple [issuer, audience, jti]. If you supply its optional engine, it uses a SHA-256 digest and rechecks expiry after hashing. The default capacity is 10,000 unexpired entries. At capacity, useJti() rejects with ReplayStoreCapacityError and preserves every live entry. A replay or expired claim returns false; malformed claims reject with InvalidStorageRecordError. A storage failure must stop acceptance of the assertion or proof.

import { assertStorageCapabilities, MemoryFederationStorage, ReplayStoreCapacityError } from "@oidfed/core";
import type { JtiReplayClaim } from "@oidfed/core";

const storage = new MemoryFederationStorage({ namespace: "https://op.umu.example", capabilities: ["replay"], maxReplayEntries: 1000 });
assertStorageCapabilities(storage, ["replay"]);
const store = storage.replay;

// Call after verifying the assertion's signature and claims.
export async function recordVerifiedAssertion(claim: JtiReplayClaim): Promise<Response> {
  try {
    const isNew = await store.useJti(claim);
    return isNew
      ? new Response(null, { status: 204 })
      : new Response("Assertion already used or expired", { status: 401 });
  } catch (error) {
    if (error instanceof ReplayStoreCapacityError) {
      return new Response("Replay protection temporarily unavailable", { status: 503 });
    }
    throw error; // Let the host's error handler reject other storage failures.
  }
}

Here, 204 reports a successful replay-store claim only. The host continues its request processing only after that result; 401, 503, and rejected operations stop acceptance. Federation authentication helpers already contain replay-store failures and return an authentication error Result.

Next: 11.3. Bound discovery and control outbound requests

View source on GitHub