feat(identity): confirm registration email atomically

This commit is contained in:
TakuyaYagam1 committed 2026-10-07 09:12:15 +03:00
1 parent 8e7ce8d71c
commit fade4e76e4
75 files changed
+4188 -403

No files matched your search

@@ -0,0 +1,192 @@
-- Bind the registration C1 to the registration whose pending credentials are
-- held by its verification token. Older registration code omits this column;
-- the insert trigger below fills it from the account row, which the
-- registration transaction updates before it grants C1.
-- +goose Up
ALTER TABLE consent_records
ADD COLUMN registration_version bigint;
-- Registration and re-registration delete the earlier registration C1 in
-- the same transaction that advances users.registration_version. The new C1
-- is granted after that update, so the current account version is the only
-- version an existing registration C1 can belong to. Refuse to guess if a
-- row predates the account's latest registration or its account is missing.
-- +goose StatementBegin
DO $$
BEGIN
IF EXISTS (
SELECT 1
FROM consent_records AS consent
LEFT JOIN users AS account ON account.id = consent.subject_id
WHERE consent.source = 'registration'
AND consent.purpose = 'c1'
AND (
account.id IS NULL
OR consent.granted_at < account.last_registered_at
)
) THEN
RAISE EXCEPTION 'registration C1 cannot be tied to the current account registration';
END IF;
IF EXISTS (
SELECT 1
FROM consent_records
WHERE source = 'registration' AND purpose = 'c1'
GROUP BY subject_id
HAVING count(*) FILTER (WHERE revoked_at IS NULL) > 1
) THEN
RAISE EXCEPTION 'account has more than one current registration C1';
END IF;
END;
$$;
-- +goose StatementEnd
-- Consent records are append-only. Temporarily remove the update guard only
-- for this one-time binding, then restore it with the new column protected.
DROP TRIGGER consent_records_refuse_change ON consent_records;
UPDATE consent_records AS consent
SET registration_version = account.registration_version
FROM users AS account
WHERE
consent.subject_id = account.id
AND consent.source = 'registration'
AND consent.purpose = 'c1';
ALTER TABLE consent_records
ADD CONSTRAINT consent_records_registration_version CHECK (
(
source = 'registration'
AND purpose = 'c1'
AND registration_version IS NOT NULL
AND registration_version > 0
)
OR (
source <> 'registration'
AND registration_version IS NULL
)
);
CREATE UNIQUE INDEX consent_records_registration_version_key
ON consent_records (subject_id, registration_version)
WHERE
source = 'registration'
AND purpose = 'c1'
AND revoked_at IS NULL;
-- The existing immutable trigger permits only a one-time revocation. It now
-- also protects registration_version from being changed with that stamp.
-- +goose StatementBegin
CREATE OR REPLACE FUNCTION consent_records_refuse_change() RETURNS trigger
LANGUAGE plpgsql AS $$
BEGIN
IF OLD.revoked_at IS NOT NULL
OR NEW.revoked_at IS NULL
OR (
NEW.id, NEW.subject_id, NEW.purpose, NEW.text_version,
NEW.text_hash, NEW.source, NEW.registration_version,
NEW.organization_id, NEW.organization_name, NEW.field_set,
NEW.invitation_id, NEW.invitation_terms_version, NEW.granted_at
) IS DISTINCT FROM (
OLD.id, OLD.subject_id, OLD.purpose, OLD.text_version,
OLD.text_hash, OLD.source, OLD.registration_version,
OLD.organization_id, OLD.organization_name, OLD.field_set,
OLD.invitation_id, OLD.invitation_terms_version, OLD.granted_at
) THEN
RAISE EXCEPTION USING
ERRCODE = 'integrity_constraint_violation',
CONSTRAINT = 'consent_records_immutable',
TABLE = 'consent_records',
MESSAGE = 'consent records are append-only: only an unrevoked record can be revoked, once';
END IF;
RETURN NEW;
END;
$$;
-- +goose StatementEnd
CREATE TRIGGER consent_records_refuse_change
BEFORE UPDATE ON consent_records
FOR EACH ROW EXECUTE FUNCTION consent_records_refuse_change();
-- Fills the registration version for an older application binary and checks
-- values supplied by the new binary. The registration transaction updates
-- the account before it inserts C1, so this is the current version even when
-- the account row is already locked by that transaction.
-- +goose StatementBegin
CREATE FUNCTION consent_records_bind_registration_version() RETURNS trigger
LANGUAGE plpgsql AS $$
DECLARE
current_version bigint;
BEGIN
IF NEW.source <> 'registration' OR NEW.purpose <> 'c1' THEN
RETURN NEW;
END IF;
SELECT registration_version
INTO current_version
FROM users
WHERE id = NEW.subject_id;
IF NOT FOUND THEN
RETURN NEW;
END IF;
IF NEW.registration_version IS NULL THEN
NEW.registration_version := current_version;
ELSIF NEW.registration_version <> current_version THEN
RAISE EXCEPTION USING
ERRCODE = 'check_violation',
CONSTRAINT = 'consent_records_registration_version_matches_user',
TABLE = 'consent_records',
MESSAGE = 'registration C1 must match the current account registration';
END IF;
RETURN NEW;
END;
$$;
-- +goose StatementEnd
CREATE TRIGGER consent_records_bind_registration_version
BEFORE INSERT ON consent_records
FOR EACH ROW EXECUTE FUNCTION consent_records_bind_registration_version();
-- +goose Down
DROP TRIGGER consent_records_bind_registration_version ON consent_records;
DROP FUNCTION consent_records_bind_registration_version();
DROP INDEX consent_records_registration_version_key;
DROP TRIGGER consent_records_refuse_change ON consent_records;
-- Restore the prior immutability check before removing its column.
-- +goose StatementBegin
CREATE OR REPLACE FUNCTION consent_records_refuse_change() RETURNS trigger
LANGUAGE plpgsql AS $$
BEGIN
IF OLD.revoked_at IS NOT NULL
OR NEW.revoked_at IS NULL
OR (
NEW.id, NEW.subject_id, NEW.purpose, NEW.text_version,
NEW.text_hash, NEW.source, NEW.organization_id,
NEW.organization_name, NEW.field_set,
NEW.invitation_id, NEW.invitation_terms_version, NEW.granted_at
) IS DISTINCT FROM (
OLD.id, OLD.subject_id, OLD.purpose, OLD.text_version,
OLD.text_hash, OLD.source, OLD.organization_id,
OLD.organization_name, OLD.field_set,
OLD.invitation_id, OLD.invitation_terms_version, OLD.granted_at
) THEN
RAISE EXCEPTION USING
ERRCODE = 'integrity_constraint_violation',
CONSTRAINT = 'consent_records_immutable',
TABLE = 'consent_records',
MESSAGE = 'consent records are append-only: only an unrevoked record can be revoked, once';
END IF;
RETURN NEW;
END;
$$;
-- +goose StatementEnd
CREATE TRIGGER consent_records_refuse_change
BEFORE UPDATE ON consent_records
FOR EACH ROW EXECUTE FUNCTION consent_records_refuse_change();
ALTER TABLE consent_records
DROP CONSTRAINT consent_records_registration_version,
DROP COLUMN registration_version;
+42
View File
@@ -39,6 +39,7 @@ INSERT INTO consent_records (
text_version,
text_hash,
source,
registration_version,
granted_at
) VALUES (
sqlc.arg(id),
@@ -47,6 +48,7 @@ INSERT INTO consent_records (
sqlc.arg(text_version),
sqlc.arg(text_hash),
sqlc.arg(source),
sqlc.narg(registration_version)::bigint,
sqlc.arg(granted_at)
)
ON CONFLICT (subject_id, purpose, text_version)
@@ -59,6 +61,7 @@ RETURNING
text_version,
text_hash,
source,
registration_version,
organization_id,
organization_name,
field_set,
@@ -93,6 +96,7 @@ INSERT INTO consent_records (
text_version,
text_hash,
source,
registration_version,
organization_id,
organization_name,
field_set,
@@ -106,6 +110,7 @@ INSERT INTO consent_records (
sqlc.arg(text_version),
sqlc.arg(text_hash),
'invitation_acceptance',
NULL,
sqlc.arg(organization_id),
sqlc.arg(organization_name),
sqlc.arg(field_set)::text [],
@@ -122,6 +127,7 @@ RETURNING
text_version,
text_hash,
source,
registration_version,
organization_id,
organization_name,
field_set,
@@ -140,6 +146,7 @@ SELECT
record.text_version,
record.text_hash,
record.source,
record.registration_version,
record.organization_id,
record.organization_name,
record.field_set,
@@ -177,6 +184,7 @@ SELECT
text_version,
text_hash,
source,
registration_version,
organization_id,
organization_name,
field_set,
@@ -202,6 +210,7 @@ SELECT
text_version,
text_hash,
source,
registration_version,
organization_id,
organization_name,
field_set,
@@ -220,6 +229,34 @@ WHERE
ORDER BY granted_at DESC, id DESC
LIMIT 1;
-- Locks the active registration C1 while its pending registration is applied.
-- name: LockRegistrationConsentRecord :one
SELECT
id,
subject_id,
purpose,
text_version,
text_hash,
source,
registration_version,
organization_id,
organization_name,
field_set,
invitation_id,
invitation_terms_version,
granted_at,
revoked_at,
revocation_source,
'active'::text AS state
FROM consent_records
WHERE
subject_id = sqlc.arg(subject_id)
AND purpose = 'c1'
AND source = 'registration'
AND registration_version = sqlc.arg(registration_version)
AND revoked_at IS NULL
FOR UPDATE;
-- name: ListCurrentContactConsentRecords :many
SELECT
id,
@@ -228,6 +265,7 @@ SELECT
text_version,
text_hash,
source,
registration_version,
organization_id,
organization_name,
field_set,
@@ -265,6 +303,7 @@ RETURNING
text_version,
text_hash,
source,
registration_version,
organization_id,
organization_name,
field_set,
@@ -294,6 +333,7 @@ RETURNING
text_version,
text_hash,
source,
registration_version,
organization_id,
organization_name,
field_set,
@@ -320,6 +360,7 @@ WITH ledger AS (
record.text_version,
record.text_hash,
record.source,
record.registration_version,
record.organization_id,
record.organization_name,
record.field_set,
@@ -354,6 +395,7 @@ SELECT
ledger.text_version,
ledger.text_hash,
ledger.source,
ledger.registration_version,
ledger.organization_id,
ledger.organization_name,
ledger.field_set,
+101 -24
View File
@@ -149,6 +149,36 @@ RETURNING
created_at,
updated_at;
-- Applies the registration carried by its token and verifies the address in
-- one optimistic update. The caller holds the token lock and checks the
-- matching C1 in the same transaction.
-- name: ConfirmUserEmail :one
UPDATE users
SET
password_hash = sqlc.arg(password_hash),
scenario = sqlc.arg(scenario),
onboarding_step = sqlc.arg(onboarding_step),
email_verified_at = sqlc.arg(verified_at),
updated_at = sqlc.arg(verified_at),
version = version + 1
WHERE
id = sqlc.arg(id)
AND registration_version = sqlc.arg(registration_version)
AND email_verified_at IS NULL
RETURNING
id,
email,
email_normalized,
password_hash,
scenario,
onboarding_step,
email_verified_at,
registration_version,
last_registered_at,
version,
created_at,
updated_at;
-- Deletes one batch of accounts that stayed unverified since before cutoff,
-- oldest first, with their tokens, sessions, mail requests, mail deliveries
-- (and through the deliveries their attempts), and consent records. The
@@ -306,6 +336,26 @@ WHERE
ORDER BY created_at DESC, id DESC
LIMIT 1;
-- Reads a token before password verification; Consume rechecks it in the
-- confirmation transaction and clears pending credentials when it succeeds.
-- name: GetEmailTokenByHash :one
SELECT
id,
user_id,
purpose,
token_hash,
registration_version,
created_at,
expires_at,
consumed_at,
invalidated_at,
pending_password_hash,
pending_scenario,
pending_onboarding_step
FROM email_tokens
WHERE token_hash = sqlc.arg(token_hash)
AND purpose = sqlc.arg(purpose);
-- 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
@@ -386,30 +436,57 @@ WHERE
-- returned registration version in the same transaction, rolled back when it
-- matches no row.
-- name: ConsumeEmailToken :one
UPDATE email_tokens AS token
SET consumed_at = sqlc.arg(consumed_at)
FROM users AS owner
WHERE
token.token_hash = sqlc.arg(token_hash)
AND token.purpose = sqlc.arg(purpose)
AND token.consumed_at IS NULL
AND token.invalidated_at IS NULL
AND token.expires_at > sqlc.arg(consumed_at)
AND owner.id = token.user_id
AND owner.registration_version = token.registration_version
RETURNING
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.pending_password_hash,
token.pending_scenario,
token.pending_onboarding_step;
WITH candidate AS MATERIALIZED (
SELECT
token.id,
token.user_id,
token.purpose,
token.token_hash,
token.registration_version,
token.created_at,
token.expires_at,
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
token.token_hash = sqlc.arg(token_hash)
AND token.purpose = sqlc.arg(purpose)
AND token.consumed_at IS NULL
AND token.invalidated_at IS NULL
AND token.expires_at > sqlc.arg(consumed_at)
AND owner.registration_version = token.registration_version
FOR UPDATE OF token
),
consumed AS (
UPDATE email_tokens AS token
SET
consumed_at = sqlc.arg(consumed_at),
pending_password_hash = NULL,
pending_scenario = NULL,
pending_onboarding_step = NULL
FROM candidate
WHERE token.id = candidate.id
RETURNING token.id, token.consumed_at, token.invalidated_at
)
SELECT
candidate.id,
candidate.user_id,
candidate.purpose,
candidate.token_hash,
candidate.registration_version,
candidate.created_at,
candidate.expires_at,
consumed.consumed_at,
consumed.invalidated_at,
candidate.pending_password_hash,
candidate.pending_scenario,
candidate.pending_onboarding_step
FROM candidate
INNER JOIN consumed ON consumed.id = candidate.id;
-- Explains why ConsumeEmailToken matched no row.
-- name: GetEmailTokenState :one