Enforce TOTP second factor on SSO login (#31)
SSO login minted a full session immediately, ignoring the account's totp_enabled flag — so a 2FA admin with a linked Google/Discord/OIDC identity could sign in without their authenticator code, silently downgrading the account to single-factor (the strength of the IdP login). The local password flow already gates on needsTotp(); SSO did not. Wire SSO through the same staged-TOTP gate: - ssoState: createTotpPending/verifyTotpPending + a short-lived httpOnly sso_totp cookie. The pending token carries stage:'totp' (session validation rejects it) + kind:'sso_totp' (scoped to the SSO endpoint) plus the resolved context (userId, provider, authMethod, returnTo). - sso.controller: finishLogin now stages the challenge and redirects to /admin/login?sso_totp=1 instead of creating a session when the account has TOTP on. New finishSsoTotp verifies the code (backoff + bot-scoring on failure, mirroring loginTotp) and only then mints the session. - sso.routes: POST /auth/sso/totp behind the same backoff/slow/limiter stack and code validation as the local TOTP endpoint. - client: AdminLogin detects ?sso_totp=1 and completes over fetch via api.ssoLoginTotp; the challenge never touches the URL or JS. Keeps the second factor httpOnly throughout, consistent with the SSO tx cookie. 12 new tests; full suite 106/106. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019rao86n5cXpwAyjdBFEshV
This commit is contained in:
@@ -19,6 +19,14 @@ const token = require('./token')
|
||||
const TX_COOKIE = 'sso_tx'
|
||||
const TX_TTL = '10m' // a login round-trip is quick; abandon after 10 minutes
|
||||
|
||||
// Second leg of an SSO login for an account that has TOTP enabled. The callback
|
||||
// authenticated the user with the IdP but must NOT bypass their second factor
|
||||
// (see issue #31), so instead of minting a session it stages this signed,
|
||||
// httpOnly cookie and routes the browser through the TOTP form — mirroring the
|
||||
// local password→TOTP gate. TTL matches the local challenge window.
|
||||
const TOTP_COOKIE = 'sso_totp'
|
||||
const TOTP_TTL = '5m'
|
||||
|
||||
// base64url of random bytes — used for the nonce and the PKCE verifier.
|
||||
function randomUrlSafe(bytes = 32) {
|
||||
return crypto.randomBytes(bytes).toString('base64url')
|
||||
@@ -56,4 +64,38 @@ function verifyTx(txToken, stateNonce) {
|
||||
return decoded
|
||||
}
|
||||
|
||||
module.exports = { TX_COOKIE, TX_TTL, createTx, verifyTx, codeChallengeFor, randomUrlSafe }
|
||||
// Stage the pending second factor for an SSO login. Carries the context the
|
||||
// callback already resolved (userId, provider, authMethod, returnTo) so that
|
||||
// presenting a valid code alone finishes the login. It is deliberately NOT a
|
||||
// session: `stage: 'totp'` makes session validation reject it (same marker the
|
||||
// local TOTP challenge uses), and `kind: 'sso_totp'` both reinforces that and
|
||||
// scopes it to the SSO completion endpoint.
|
||||
function createTotpPending({ userId, provider, authMethod, returnTo }) {
|
||||
return token.signToken(
|
||||
{ id: userId }, // subject only; identity is re-loaded fresh when the code is verified
|
||||
{ stage: 'totp', kind: 'sso_totp', provider, authMethod, returnTo },
|
||||
{ expiresIn: TOTP_TTL },
|
||||
)
|
||||
}
|
||||
|
||||
// Verify a pending-TOTP cookie. Returns the payload
|
||||
// ({ id, provider, authMethod, returnTo, ... }) or null if missing/expired/wrong-kind.
|
||||
function verifyTotpPending(pendingToken) {
|
||||
if (!pendingToken) return null
|
||||
const decoded = token.verifyToken(pendingToken)
|
||||
if (!decoded || decoded.stage !== 'totp' || decoded.kind !== 'sso_totp') return null
|
||||
return decoded
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
TX_COOKIE,
|
||||
TX_TTL,
|
||||
TOTP_COOKIE,
|
||||
TOTP_TTL,
|
||||
createTx,
|
||||
verifyTx,
|
||||
createTotpPending,
|
||||
verifyTotpPending,
|
||||
codeChallengeFor,
|
||||
randomUrlSafe,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user