mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-04 18:04:18 +00:00
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.
This commit is contained in:
@@ -1,4 +1,11 @@
|
||||
import { oidcCallbackQuerySchema, oidcExchangeQuerySchema, oidcLoginQuerySchema } from './oidc.schema';
|
||||
import {
|
||||
OIDC_LOGOUT_TOKEN_MAX_LENGTH,
|
||||
oidcBackchannelLogoutErrorSchema,
|
||||
oidcBackchannelLogoutRequestSchema,
|
||||
oidcCallbackQuerySchema,
|
||||
oidcExchangeQuerySchema,
|
||||
oidcLoginQuerySchema,
|
||||
} from './oidc.schema';
|
||||
|
||||
import { describe, it, expect } from 'vitest';
|
||||
|
||||
@@ -31,3 +38,32 @@ describe('oidcExchangeQuerySchema', () => {
|
||||
expect(oidcExchangeQuerySchema.safeParse({}).success).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('oidcBackchannelLogoutRequestSchema', () => {
|
||||
it('requires a non-empty logout_token string', () => {
|
||||
expect(oidcBackchannelLogoutRequestSchema.safeParse({ logout_token: 'a.b.c' }).success).toBe(true);
|
||||
expect(oidcBackchannelLogoutRequestSchema.safeParse({}).success).toBe(false);
|
||||
expect(oidcBackchannelLogoutRequestSchema.safeParse({ logout_token: '' }).success).toBe(false);
|
||||
});
|
||||
|
||||
it('refuses a token that is not one string (a repeated form field parses to an array)', () => {
|
||||
expect(oidcBackchannelLogoutRequestSchema.safeParse({ logout_token: ['a', 'b'] }).success).toBe(false);
|
||||
expect(oidcBackchannelLogoutRequestSchema.safeParse({ logout_token: 5 }).success).toBe(false);
|
||||
});
|
||||
|
||||
it('refuses a token past the length cap', () => {
|
||||
const long = 'a'.repeat(OIDC_LOGOUT_TOKEN_MAX_LENGTH + 1);
|
||||
expect(oidcBackchannelLogoutRequestSchema.safeParse({ logout_token: long }).success).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('oidcBackchannelLogoutErrorSchema', () => {
|
||||
it('describes the spec 2.8 error body', () => {
|
||||
expect(
|
||||
oidcBackchannelLogoutErrorSchema.safeParse({ error: 'invalid_request', error_description: 'x' }).success,
|
||||
).toBe(true);
|
||||
expect(oidcBackchannelLogoutErrorSchema.safeParse({ error: 'server_error', error_description: 'x' }).success).toBe(
|
||||
false,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -28,3 +28,36 @@ export const oidcExchangeQuerySchema = z.object({
|
||||
code: z.string(),
|
||||
});
|
||||
export type OidcExchangeQuery = z.infer<typeof oidcExchangeQuerySchema>;
|
||||
|
||||
/**
|
||||
* The longest logout token accepted. A signed logout token holds a handful of
|
||||
* short claims and is well under 2 KB; the cap only keeps a stranger's body
|
||||
* from reaching the JWT parser at the size of the global body limit.
|
||||
*/
|
||||
export const OIDC_LOGOUT_TOKEN_MAX_LENGTH = 16_384;
|
||||
|
||||
/**
|
||||
* POST /api/auth/oidc/backchannel-logout, OpenID Connect Back-Channel Logout 1.0
|
||||
* section 2.5: the identity provider posts an `application/x-www-form-urlencoded`
|
||||
* body with one parameter, the signed `logout_token`. Everything else in the body
|
||||
* is ignored, as the spec leaves room for nothing else.
|
||||
*/
|
||||
export const oidcBackchannelLogoutRequestSchema = z.object({
|
||||
logout_token: z.string().min(1).max(OIDC_LOGOUT_TOKEN_MAX_LENGTH),
|
||||
});
|
||||
export type OidcBackchannelLogoutRequest = z.infer<typeof oidcBackchannelLogoutRequestSchema>;
|
||||
|
||||
/**
|
||||
* The 200 answer (section 2.8). The spec asks for no body; the route answers an
|
||||
* empty JSON object, also when no account or session matched the token, so the
|
||||
* provider cannot tell which subjects have a TREK account.
|
||||
*/
|
||||
export const oidcBackchannelLogoutResponseSchema = z.object({});
|
||||
export type OidcBackchannelLogoutResponse = z.infer<typeof oidcBackchannelLogoutResponseSchema>;
|
||||
|
||||
/** The 400 answer (section 2.8) for a missing or invalid logout token. */
|
||||
export const oidcBackchannelLogoutErrorSchema = z.object({
|
||||
error: z.literal('invalid_request'),
|
||||
error_description: z.string(),
|
||||
});
|
||||
export type OidcBackchannelLogoutError = z.infer<typeof oidcBackchannelLogoutErrorSchema>;
|
||||
|
||||
Reference in New Issue
Block a user