feat(identity): confirm the email address with a six-digit code from the message

This commit is contained in:
shira committed 2026-10-10 00:52:46 +03:00
1 parent c94434cd09
commit d365a05c96
87 files changed
+4764 -224

No files matched your search

@@ -0,0 +1,70 @@
-- +goose Up
-- Email verification codes: a six-digit code that travels with each
-- verification message next to the link. The table keeps only a keyed digest
-- of the code, its expiry, and the count of failed attempts. The count is
-- advanced by a single conditional UPDATE, so concurrent wrong attempts cannot
-- exceed the limit and none is lost. Tokens issued before this migration carry
-- no code and keep working by link only.
--
-- The three columns are set together for a token that carries a code. The
-- application sets the code expiry to the earlier of the token expiry and the
-- code lifetime, and checks both at every use.
ALTER TABLE email_tokens
ADD COLUMN code_hash bytea,
ADD COLUMN code_expires_at timestamptz,
ADD COLUMN code_failed_attempts smallint NOT NULL DEFAULT 0,
ADD CONSTRAINT email_tokens_code_pair CHECK (
(code_hash IS NULL) = (code_expires_at IS NULL)
),
ADD CONSTRAINT email_tokens_code_hash_length CHECK (
code_hash IS NULL OR octet_length(code_hash) = 32
),
ADD CONSTRAINT email_tokens_code_expiry CHECK (
code_expires_at IS NULL OR code_expires_at > created_at
),
ADD CONSTRAINT email_tokens_code_purpose CHECK (
code_hash IS NULL OR purpose = 'email_verification'
),
ADD CONSTRAINT email_tokens_code_attempts_range CHECK (
code_failed_attempts BETWEEN 0 AND 5
);
COMMENT ON COLUMN email_tokens.code_hash IS
'HMAC-SHA-256 of the six-digit verification code under a server-side key; NULL when the token carries no code or the code has ended. The code itself is never stored.';
COMMENT ON COLUMN email_tokens.code_expires_at IS
'End of the code validity: the earlier of the token expiry and the code lifetime. NULL together with code_hash.';
COMMENT ON COLUMN email_tokens.code_failed_attempts IS
'Wrong code or password attempts counted against the open code; the token is invalidated when the count reaches the limit of five.';
-- +goose Down
-- A rollback removes the codes, so it refuses while a code is still usable.
-- A code expires within its 30-minute lifetime, so waiting for expiry is enough.
-- +goose StatementBegin
DO $$
BEGIN
IF EXISTS (
SELECT 1
FROM email_tokens
WHERE code_hash IS NOT NULL
AND consumed_at IS NULL
AND invalidated_at IS NULL
AND code_expires_at > now()
) THEN
RAISE EXCEPTION USING
ERRCODE = 'object_not_in_prerequisite_state',
MESSAGE = 'email verification codes are still usable',
HINT = 'Wait until the open codes expire, then roll back again.';
END IF;
END
$$;
-- +goose StatementEnd
ALTER TABLE email_tokens
DROP CONSTRAINT email_tokens_code_attempts_range,
DROP CONSTRAINT email_tokens_code_purpose,
DROP CONSTRAINT email_tokens_code_expiry,
DROP CONSTRAINT email_tokens_code_hash_length,
DROP CONSTRAINT email_tokens_code_pair,
DROP COLUMN code_failed_attempts,
DROP COLUMN code_expires_at,
DROP COLUMN code_hash;
+91 -3
View File
@@ -304,6 +304,8 @@ INSERT INTO email_tokens (
registration_version,
created_at,
expires_at,
code_hash,
code_expires_at,
pending_password_hash,
pending_scenario,
pending_onboarding_step
@@ -315,6 +317,8 @@ INSERT INTO email_tokens (
sqlc.arg(registration_version),
sqlc.arg(created_at),
sqlc.arg(expires_at),
sqlc.narg(code_hash),
sqlc.narg(code_expires_at),
sqlc.narg(pending_password_hash),
sqlc.narg(pending_scenario),
sqlc.narg(pending_onboarding_step)
@@ -349,6 +353,9 @@ SELECT
expires_at,
consumed_at,
invalidated_at,
code_hash,
code_expires_at,
code_failed_attempts,
pending_password_hash,
pending_scenario,
pending_onboarding_step
@@ -356,6 +363,83 @@ FROM email_tokens
WHERE token_hash = sqlc.arg(token_hash)
AND purpose = sqlc.arg(purpose);
-- Reads the newest open verification token that carries a code for the
-- unverified account with this normalized address. The registration version
-- must be current, so a code of an earlier registration never matches. The
-- row is read before the code is compared; the consumption rechecks it.
-- name: GetOpenEmailCodeByAddress :one
SELECT
token.id,
token.user_id,
token.purpose,
token.token_hash,
token.registration_version,
token.created_at,
token.expires_at,
token.consumed_at,
token.invalidated_at,
token.code_hash,
token.code_expires_at,
token.code_failed_attempts,
token.pending_password_hash,
token.pending_scenario,
token.pending_onboarding_step
FROM email_tokens AS token
INNER JOIN users AS owner ON owner.id = token.user_id
WHERE
owner.email_normalized = sqlc.arg(email_normalized)
AND owner.email_verified_at IS NULL
AND owner.registration_version = token.registration_version
AND token.purpose = sqlc.arg(purpose)
AND token.consumed_at IS NULL
AND token.invalidated_at IS NULL
AND token.code_hash IS NOT NULL
AND token.pending_password_hash IS NOT NULL
ORDER BY token.created_at DESC, token.id DESC
LIMIT 1;
-- Counts one failed code or password attempt against an open code. The
-- single UPDATE reads and writes the counter under the row lock, so concurrent
-- failures serialize and none is lost. The attempt that reaches the limit
-- invalidates the token and drops its code and pending credentials, so the
-- code and the link both stop working. A token that is no longer open matches
-- no row and is left unchanged.
-- name: RecordFailedEmailCodeAttempt :one
UPDATE email_tokens
SET
code_failed_attempts = code_failed_attempts + 1,
invalidated_at = CASE
WHEN code_failed_attempts + 1 >= sqlc.arg(max_attempts) THEN sqlc.arg(attempted_at)
ELSE invalidated_at
END,
code_hash = CASE
WHEN code_failed_attempts + 1 >= sqlc.arg(max_attempts) THEN NULL
ELSE code_hash
END,
code_expires_at = CASE
WHEN code_failed_attempts + 1 >= sqlc.arg(max_attempts) THEN NULL
ELSE code_expires_at
END,
pending_password_hash = CASE
WHEN code_failed_attempts + 1 >= sqlc.arg(max_attempts) THEN NULL
ELSE pending_password_hash
END,
pending_scenario = CASE
WHEN code_failed_attempts + 1 >= sqlc.arg(max_attempts) THEN NULL
ELSE pending_scenario
END,
pending_onboarding_step = CASE
WHEN code_failed_attempts + 1 >= sqlc.arg(max_attempts) THEN NULL
ELSE pending_onboarding_step
END
WHERE
id = sqlc.arg(id)
AND consumed_at IS NULL
AND invalidated_at IS NULL
AND code_hash IS NOT NULL
AND code_failed_attempts < sqlc.arg(max_attempts)
RETURNING code_failed_attempts >= sqlc.arg(max_attempts) AS burned;
-- Starts a new registration of an unverified account without touching its
-- credentials, scenario, or onboarding step: the new registration carries
-- them on its verification token. Advances the registration version, which
@@ -416,10 +500,12 @@ FROM updated;
UPDATE email_tokens
SET
invalidated_at = sqlc.arg(invalidated_at),
-- An invalidated token keeps no password hash: it can never be confirmed.
-- An invalidated token keeps no password hash or code: it can never be confirmed.
pending_password_hash = NULL,
pending_scenario = NULL,
pending_onboarding_step = NULL
pending_onboarding_step = NULL,
code_hash = NULL,
code_expires_at = NULL
WHERE
user_id = sqlc.arg(user_id)
AND purpose = sqlc.arg(purpose)
@@ -466,7 +552,9 @@ consumed AS (
consumed_at = sqlc.arg(consumed_at),
pending_password_hash = NULL,
pending_scenario = NULL,
pending_onboarding_step = NULL
pending_onboarding_step = NULL,
code_hash = NULL,
code_expires_at = NULL
FROM candidate
WHERE token.id = candidate.id
RETURNING token.id, token.consumed_at, token.invalidated_at