Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/recovery-session-login-hooks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'seamless-auth-api': patch
---

A recovery session now runs the `post_login` and `pre_token` login hooks when its restriction lifts, after the user enrolls a passkey or authenticator app. The `pre_token` claims are kept on the session from then on. If a hook refuses, `POST /webauthn/register/finish` and `POST /totp/enroll/verify` answer 403 `login_denied` or 503 `login_hook_unavailable`, and the recovery session is revoked. The new factor is kept, so the user signs in with it, and that sign-in runs the hooks. Break-glass administrators still skip the hooks.
3 changes: 2 additions & 1 deletion docs/adr/0005-extensibility-callouts.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ The chain trigger updates the chain head row on every insert and holds that lock
- `pre_token` runs when a token is minted for a new session and on organization switch. It may add claims or deny. It is not called on refresh: its claims are kept on the session, so refresh never waits on an adopter's server.
- One hook per trigger. Timeout 2 seconds by default, 5 at most, one attempt.
- Failure mode per hook, `closed` by default. A timeout, a non-2xx, a body that does not parse, or claims over the budget fail the sign-in when closed and are ignored when open. Every invocation is audited.
- Break-glass sign-in and recovery sessions never call a hook, so a hook that is down cannot lock out the administrator who would fix it.
- Break-glass sign-in never calls a hook, so a hook that is down cannot lock out the administrator who would fix it.
- A recovery session is issued without calling a hook, so a hook that is down cannot stop a user from getting a factor back. Both hooks run when its restriction lifts after the user enrolls a factor, since that is when it becomes a full session. A refusal there revokes the session and keeps the new factor, so the next sign-in with it runs the hooks again.
- The call carries the user, method, organization, OpenID client, IP and user agent, never a token, code or secret. `reason` in the answer is audited and not shown; `userMessage` (200 characters) is.

### E6. Custom claims
Expand Down
28 changes: 28 additions & 0 deletions openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -35006,6 +35006,7 @@
"/totp/enroll/verify": {
"post": {
"summary": "Verify TOTP enrollment",
"description": "From a recovery session, enrolling lifts its restriction once the login hooks allow it. A hook can refuse: 403 `login_denied` with the hook's `message`, or 503 `login_hook_unavailable`. Either way the recovery session is revoked and the new factor is kept, so the user signs in with it.",
"tags": ["TOTP"],
"security": [{ "bearerAuth": [] }],
"requestBody": {
Expand Down Expand Up @@ -35145,6 +35146,19 @@
}
}
}
},
"503": {
"description": "HTTP 503",
"content": {
"application/json": {
"example": { "message": "string", "error": "string" },
"schema": {
"type": "object",
"properties": { "message": { "type": "string" }, "error": { "type": "string" } },
"required": ["error"]
}
}
}
}
}
}
Expand Down Expand Up @@ -36831,6 +36845,7 @@
"/webauthn/register/finish": {
"post": {
"summary": "Finish WebAuthn registration",
"description": "From a recovery session, enrolling lifts its restriction once the login hooks allow it. A hook can refuse: 403 `login_denied` with the hook's `message`, or 503 `login_hook_unavailable`. Either way the recovery session is revoked and the new factor is kept, so the user signs in with it.",
"tags": ["WebAuthn"],
"security": [{ "bearerAuth": [] }],
"requestBody": {
Expand Down Expand Up @@ -37039,6 +37054,19 @@
}
}
}
},
"503": {
"description": "HTTP 503",
"content": {
"application/json": {
"example": { "message": "string", "error": "string" },
"schema": {
"type": "object",
"properties": { "message": { "type": "string" }, "error": { "type": "string" } },
"required": ["error"]
}
}
}
}
}
}
Expand Down
12 changes: 11 additions & 1 deletion src/controllers/authentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,8 @@ export const refreshSession = async (req: Request, res: Response) => {
// the sign-in.
authMethods: session.authMethods ?? [],
authTime: session.authTime ?? null,
// The pre_token hook is not called on refresh, so its claims ride the chain.
// The pre_token hook is not called on refresh, so its claims ride the chain. A recovery
// session gets them when its restriction lifts, which is also when the hooks first run.
hookClaims: session.hookClaims ?? null,
parentSessionId: session.parentSessionId ?? null,
exchangeClientId: session.exchangeClientId ?? null,
Expand Down Expand Up @@ -820,6 +821,15 @@ export const refreshSession = async (req: Request, res: Response) => {
);
}

// A recovery lift that ran between reading this session and claiming its link updated
// the row being replaced, so the replacement takes the lifted state from it.
if (session.recovery === true) {
await session.reload();
if (session.recovery !== true) {
await newSession.update({ recovery: false, hookClaims: session.hookClaims ?? null });
}
}

// Read again because a revoke-all can land while this rotation is in flight. Its cutoff
// covers the session the rotation started from, so the replacement is refused whether
// or not the bulk update reached its row.
Expand Down
13 changes: 10 additions & 3 deletions src/controllers/totp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ import { AuthEventService } from '../services/authEventService.js';
import { breakGlassWaivers, recordBreakGlassSignIn } from '../services/breakGlassService.js';
import { rejectIfUserLocked } from '../services/lockoutPolicyService.js';
import { isPasskeyRequiredForUser } from '../services/loginPolicyService.js';
import { issueSessionAndRespond } from '../services/sessionIssuance.js';
import { liftRecoveryRestriction } from '../services/sessionService.js';
import {
issueSessionAndRespond,
liftRecoveryRestrictionAfterHooks,
} from '../services/sessionIssuance.js';
import {
getStepUpSignInTime,
recordStepUpVerification,
Expand Down Expand Up @@ -134,7 +136,12 @@ export const verifyCurrentTotpEnrollment = async (req: Request, res: Response) =

const authReq = req as AuthenticatedRequest;
if (authReq.recoverySession) {
await liftRecoveryRestriction(authReq.sessionId);
const refusal = await liftRecoveryRestrictionAfterHooks({
sessionId: authReq.sessionId,
user,
req,
});
if (refusal) return res.status(refusal.status).json(refusal.body);
}

return res.json({ message: 'Success' });
Expand Down
13 changes: 10 additions & 3 deletions src/controllers/webauthn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ import {
ensureMetadataServiceReady,
hasMetadataStatement,
} from '../services/metadataServiceBootstrap.js';
import { issueSessionAndRespond } from '../services/sessionIssuance.js';
import { liftRecoveryRestriction } from '../services/sessionService.js';
import {
issueSessionAndRespond,
liftRecoveryRestrictionAfterHooks,
} from '../services/sessionIssuance.js';
import { consumeChallenge, issueChallenge } from '../services/webauthnChallengeService.js';
import { AuthenticatedRequest } from '../types/types.js';
import getLogger from '../utils/logger.js';
Expand Down Expand Up @@ -414,7 +416,12 @@ const verifyWebAuthnRegistration = async (req: Request, res: Response) => {
}

if (authReq.recoverySession) {
await liftRecoveryRestriction(authReq.sessionId);
const refusal = await liftRecoveryRestrictionAfterHooks({
sessionId: authReq.sessionId,
user,
req,
});
if (refusal) return res.status(refusal.status).json(refusal.body);
}

// Enrollment is not a sign-in. The caller reached here with an access session, so
Expand Down
46 changes: 44 additions & 2 deletions src/generated/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33472,7 +33472,10 @@ export interface paths {
};
get?: never;
put?: never;
/** Verify TOTP enrollment */
/**
* Verify TOTP enrollment
* @description From a recovery session, enrolling lifts its restriction once the login hooks allow it. A hook can refuse: 403 `login_denied` with the hook's `message`, or 503 `login_hook_unavailable`. Either way the recovery session is revoked and the new factor is kept, so the user signs in with it.
*/
post: {
parameters: {
query?: never;
Expand Down Expand Up @@ -33617,6 +33620,24 @@ export interface paths {
};
};
};
/** @description HTTP 503 */
503: {
headers: {
[name: string]: unknown;
};
content: {
/**
* @example {
* "message": "string",
* "error": "string"
* }
*/
'application/json': {
message?: string;
error: string;
};
};
};
};
};
delete?: never;
Expand Down Expand Up @@ -35265,7 +35286,10 @@ export interface paths {
};
get?: never;
put?: never;
/** Finish WebAuthn registration */
/**
* Finish WebAuthn registration
* @description From a recovery session, enrolling lifts its restriction once the login hooks allow it. A hook can refuse: 403 `login_denied` with the hook's `message`, or 503 `login_hook_unavailable`. Either way the recovery session is revoked and the new factor is kept, so the user signs in with it.
*/
post: {
parameters: {
query?: never;
Expand Down Expand Up @@ -35458,6 +35482,24 @@ export interface paths {
};
};
};
/** @description HTTP 503 */
503: {
headers: {
[name: string]: unknown;
};
content: {
/**
* @example {
* "message": "string",
* "error": "string"
* }
*/
'application/json': {
message?: string;
error: string;
};
};
};
};
};
delete?: never;
Expand Down
7 changes: 7 additions & 0 deletions src/routes/totp.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ totpRouter.post(
auth: 'access',
allowRecoverySession: true,
summary: 'Verify TOTP enrollment',
description:
'From a recovery session, enrolling lifts its restriction once the login hooks allow it. A ' +
"hook can refuse: 403 `login_denied` with the hook's `message`, or 503 " +
'`login_hook_unavailable`. Either way the recovery session is revoked and the new factor is ' +
'kept, so the user signs in with it.',
tags: ['TOTP'],

schemas: {
Expand All @@ -88,6 +93,8 @@ totpRouter.post(
response: {
200: TotpVerifySuccessSchema,
401: ErrorSchema,
403: ErrorSchema,
503: ErrorSchema,
},
},
},
Expand Down
6 changes: 6 additions & 0 deletions src/routes/webauthn.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ webauthnRouter.post(
auth: 'access',
allowRecoverySession: true,
summary: 'Finish WebAuthn registration',
description:
'From a recovery session, enrolling lifts its restriction once the login hooks allow it. A ' +
"hook can refuse: 403 `login_denied` with the hook's `message`, or 503 " +
'`login_hook_unavailable`. Either way the recovery session is revoked and the new factor is ' +
'kept, so the user signs in with it.',
tags: ['WebAuthn'],

schemas: {
Expand All @@ -78,6 +83,7 @@ webauthnRouter.post(
401: ErrorSchema,
403: ErrorSchema,
500: ErrorSchema,
503: ErrorSchema,
},
},
},
Expand Down
Loading