feat(identity): confirm registration email atomically
This commit is contained in:
1 parent
8e7ce8d71c
commit
fade4e76e4
75 files changed
+4188
-403
No files matched your search
+101
-24
@@ -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
|
||||
|
||||
Reference in new issue
Block a user