Files
TREK/server/tests/unit/nest/oidc-logout-replay.store.test.ts
Maurice 06aaddc34d feat(oidc): end TREK sessions when the identity provider signs a user out
OpenID Connect Back-Channel Logout 1.0: the provider calls
POST /api/auth/oidc/backchannel-logout with a logout token when a user signs
out there or an admin ends their session, and TREK ends the matching
sessions. Until now a session lived on for up to 30 days after access was
taken away at the provider.

- The token is checked as section 2.6 asks, through the verifier the login
  uses for the id_token (provider JWKS, issuer incl. a custom discovery URL,
  client id in aud), plus a recent iat, the logout event, a sub or sid, no
  nonce, an accepted typ and a jti not seen before (in-memory, bounded replay
  store; a jti whose sessions failed to end is released for the retry).
- The id_token's sid is stored on the session the SSO sign-in creates
  (nullable user_sessions.oidc_sid, both engines), and an app session handed
  off from that browser sign-in carries it too. A token with a sid ends those
  sessions only; a token with only a sub ends every session of the account.
- A valid token answers 200 with no-store also when it names nobody here; an
  invalid one 400 invalid_request, never 500, with the reason only in the
  server log. Public route with its allow-list entry, rate limited, audited
  as oidc.backchannel_logout.
- Always active: nothing changes until an admin enters the URL at the
  provider. Wiki: the URL, where it goes in Authentik, Keycloak and Pocket ID,
  and Keycloak's single token when an admin signs a user out.

Addresses discussion #3124.
2026-11-27 22:24:21 +01:00

65 lines
2.8 KiB
TypeScript

/**
* The in-memory store of logout tokens already acted on: a key is refused
* while its entry lives, accepted again once it expired or was released, and the map never
* holds more than its cap.
*/
import {
InMemoryOidcLogoutReplayStore,
OIDC_LOGOUT_REPLAY_MAX_ENTRIES,
} from '../../../src/nest/oidc/oidc-logout-replay.store';
import { describe, expect, it } from 'vitest';
describe('InMemoryOidcLogoutReplayStore', () => {
it('BCL-REPLAY-001: the first claim wins, a second one while the entry lives is a replay', async () => {
const store = new InMemoryOidcLogoutReplayStore();
expect(await store.claim('iss\nj1', 2000, 1000)).toBe(true);
expect(await store.claim('iss\nj1', 2000, 1500)).toBe(false);
expect(await store.claim('iss\nj2', 2000, 1500)).toBe(true);
});
it('BCL-REPLAY-002: an expired entry no longer refuses its key', async () => {
const store = new InMemoryOidcLogoutReplayStore();
expect(await store.claim('k', 2000, 1000)).toBe(true);
expect(await store.claim('k', 3000, 2000)).toBe(true);
expect(await store.claim('k', 3000, 2500)).toBe(false);
});
it('BCL-REPLAY-003: at the cap the expired entries go first', async () => {
const store = new InMemoryOidcLogoutReplayStore(3);
await store.claim('old', 1100, 1000);
await store.claim('live-a', 9000, 1000);
await store.claim('live-b', 9000, 1000);
expect(await store.claim('new', 9000, 2000)).toBe(true);
expect(store.size).toBe(3);
// The live ones are still held, the expired one made the room.
expect(await store.claim('live-a', 9000, 2000)).toBe(false);
expect(await store.claim('live-b', 9000, 2000)).toBe(false);
});
it('BCL-REPLAY-004: with nothing expired the oldest entry goes, so memory stays bounded', async () => {
const store = new InMemoryOidcLogoutReplayStore(2);
await store.claim('first', 9000, 1000);
await store.claim('second', 9000, 1000);
await store.claim('third', 9000, 1000);
expect(store.size).toBe(2);
expect(await store.claim('second', 9000, 1000)).toBe(false);
expect(await store.claim('third', 9000, 1000)).toBe(false);
});
it('BCL-REPLAY-005: the default cap is the documented one', () => {
expect(OIDC_LOGOUT_REPLAY_MAX_ENTRIES).toBe(10_000);
});
it('BCL-REPLAY-006: a released key is accepted again, and the other keys stay held', async () => {
const store = new InMemoryOidcLogoutReplayStore();
expect(await store.claim('a', 9000, 1000)).toBe(true);
expect(await store.claim('b', 9000, 1000)).toBe(true);
await store.release('a');
expect(store.size).toBe(1);
expect(await store.claim('a', 9000, 1500)).toBe(true);
expect(await store.claim('b', 9000, 1500)).toBe(false);
await expect(store.release('never-held')).resolves.toBeUndefined();
});
});