Files
TREK/server/tests/unit/db/user-sessions-oidc-sid-migration.test.ts
T
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

49 lines
2.1 KiB
TypeScript

/**
* `Migration20200101046510_a_session_knows_its_provider_session`: the nullable
* `user_sessions.oidc_sid` column and its index. Migrate to the step before
* it with a session already there, apply just this one, and check that the
* existing session comes through untouched with no sid.
*/
import { createMigrationOrm, migrateTo, pendingNames, rawExec, rawQuery } from '../../helpers/migration-step';
import { describe, expect, it } from 'vitest';
const TARGET = 'Migration20200101046510_a_session_knows_its_provider_session';
describe('user_sessions.oidc_sid migration', () => {
it('USERSESSSID-001: adds a nullable text column with its index and leaves existing sessions as they were', async () => {
const orm = await createMigrationOrm();
try {
const names = await pendingNames(orm);
const idx = names.indexOf(TARGET);
expect(idx).toBeGreaterThan(0);
const previous = names[idx - 1];
if (!previous) throw new Error('no step before the target');
await migrateTo(orm, previous);
await rawExec(
orm,
"INSERT INTO users (id, username, email, password_hash) VALUES (1, 'owner', 'owner@test', 'x')",
);
await rawExec(
orm,
"INSERT INTO user_sessions (id, user_id, expires_at, user_agent) VALUES ('s1', 1, '2030-01-01 00:00:00', 'Firefox')",
);
await migrateTo(orm, TARGET);
const columns = await rawQuery<{ name: string; type: string; notnull: number }>(
orm,
"SELECT name, type, \"notnull\" FROM pragma_table_info('user_sessions') WHERE name = 'oidc_sid'",
);
expect(columns).toEqual([{ name: 'oidc_sid', type: 'TEXT', notnull: 0 }]);
const indexes = await rawQuery<{ name: string }>(orm, "SELECT name FROM pragma_index_list('user_sessions')");
expect(indexes.map((i) => i.name)).toContain('idx_user_sessions_oidc_sid');
expect(
await rawQuery(orm, "SELECT id, user_id, user_agent, revoked_at, oidc_sid FROM user_sessions WHERE id = 's1'"),
).toEqual([{ id: 's1', user_id: 1, user_agent: 'Firefox', revoked_at: null, oidc_sid: null }]);
} finally {
await orm.close(true);
}
});
});