feat(identity): register accounts and resend verification mail
This commit is contained in:
1 parent
5e202ac972
commit
4436f626a3
55 files changed
+3227
-88
No files matched your search
@@ -0,0 +1,84 @@
|
||||
-- +goose Up
|
||||
-- Replay records of anonymous commands that carry an Idempotency-Key, first
|
||||
-- of all the registration. A record is written in the same transaction as
|
||||
-- the effect it guards, so a record that is visible always belongs to a
|
||||
-- committed command: a running command is represented by the uncommitted row
|
||||
-- itself, on which a concurrent request with the same scope waits.
|
||||
--
|
||||
-- Nothing here identifies a person or reveals a credential. scope_hash is an
|
||||
-- HMAC-SHA-256 with a server secret over the operation, the key, and the
|
||||
-- normalized email, and fingerprint is an HMAC-SHA-256 with a server secret
|
||||
-- over the canonical request body; without the secret neither can be used to
|
||||
-- confirm an email address or to test a password offline.
|
||||
CREATE TABLE idempotency_records (
|
||||
scope_hash bytea NOT NULL,
|
||||
fingerprint bytea NOT NULL,
|
||||
created_at timestamptz NOT NULL,
|
||||
-- The key may be accepted as new once this time has passed.
|
||||
expires_at timestamptz NOT NULL,
|
||||
CONSTRAINT idempotency_records_pkey PRIMARY KEY (scope_hash),
|
||||
CONSTRAINT idempotency_records_scope_hash_sha256 CHECK (
|
||||
octet_length(scope_hash) = 32
|
||||
),
|
||||
CONSTRAINT idempotency_records_fingerprint_sha256 CHECK (
|
||||
octet_length(fingerprint) = 32
|
||||
),
|
||||
CONSTRAINT idempotency_records_expiry_after_creation CHECK (
|
||||
expires_at > created_at
|
||||
)
|
||||
);
|
||||
|
||||
-- Serves the bounded purge of expired records.
|
||||
CREATE INDEX idempotency_records_expires_at_idx
|
||||
ON idempotency_records (expires_at);
|
||||
|
||||
-- Pending credentials of a registration, bound to the verification token
|
||||
-- issued for it. A registration of an unverified address never changes the
|
||||
-- credentials of the account row: it issues a token that carries its own
|
||||
-- password hash and scenario. Confirming a token applies the credentials of
|
||||
-- that token, so whoever registered an address first or last cannot make the
|
||||
-- owner of the address adopt a password the owner did not choose. NULL for
|
||||
-- tokens that carry none (a resend copies them from the registration's
|
||||
-- latest token; password reset tokens carry none).
|
||||
ALTER TABLE email_tokens
|
||||
ADD COLUMN pending_password_hash text,
|
||||
ADD COLUMN pending_scenario text,
|
||||
ADD COLUMN pending_onboarding_step text,
|
||||
ADD CONSTRAINT email_tokens_pending_all_or_none CHECK (
|
||||
(pending_password_hash IS NULL) = (pending_scenario IS NULL)
|
||||
AND (pending_scenario IS NULL) = (pending_onboarding_step IS NULL)
|
||||
),
|
||||
ADD CONSTRAINT email_tokens_invalidated_keeps_no_pending CHECK (
|
||||
invalidated_at IS NULL OR pending_password_hash IS NULL
|
||||
),
|
||||
ADD CONSTRAINT email_tokens_pending_password_hash_format CHECK (
|
||||
pending_password_hash IS NULL
|
||||
OR (
|
||||
char_length(pending_password_hash) <= 512
|
||||
AND pending_password_hash
|
||||
~ '^\$argon2id\$v=19\$m=[1-9][0-9]{0,9},t=[1-9][0-9]{0,9},p=[1-9][0-9]{0,2}\$[A-Za-z0-9+/]+\$[A-Za-z0-9+/]+$'
|
||||
)
|
||||
),
|
||||
ADD CONSTRAINT email_tokens_pending_scenario_step CHECK (
|
||||
pending_scenario IS NULL
|
||||
OR (
|
||||
pending_scenario = 'candidate'
|
||||
AND pending_onboarding_step = 'candidate_consents'
|
||||
)
|
||||
OR (
|
||||
pending_scenario = 'employer'
|
||||
AND pending_onboarding_step = 'employer_company'
|
||||
)
|
||||
);
|
||||
|
||||
-- +goose Down
|
||||
ALTER TABLE email_tokens
|
||||
DROP CONSTRAINT email_tokens_pending_scenario_step,
|
||||
DROP CONSTRAINT email_tokens_invalidated_keeps_no_pending,
|
||||
DROP CONSTRAINT email_tokens_pending_password_hash_format,
|
||||
DROP CONSTRAINT email_tokens_pending_all_or_none,
|
||||
DROP COLUMN pending_onboarding_step,
|
||||
DROP COLUMN pending_scenario,
|
||||
DROP COLUMN pending_password_hash;
|
||||
|
||||
DROP TABLE idempotency_records;
|
||||
@@ -273,7 +273,10 @@ INSERT INTO email_tokens (
|
||||
token_hash,
|
||||
registration_version,
|
||||
created_at,
|
||||
expires_at
|
||||
expires_at,
|
||||
pending_password_hash,
|
||||
pending_scenario,
|
||||
pending_onboarding_step
|
||||
) VALUES (
|
||||
sqlc.arg(id),
|
||||
sqlc.arg(user_id),
|
||||
@@ -281,12 +284,92 @@ INSERT INTO email_tokens (
|
||||
sqlc.arg(token_hash),
|
||||
sqlc.arg(registration_version),
|
||||
sqlc.arg(created_at),
|
||||
sqlc.arg(expires_at)
|
||||
sqlc.arg(expires_at),
|
||||
sqlc.narg(pending_password_hash),
|
||||
sqlc.narg(pending_scenario),
|
||||
sqlc.narg(pending_onboarding_step)
|
||||
);
|
||||
|
||||
-- The pending credentials of the newest token of a registration, for a
|
||||
-- resend, which issues its token with the same credentials.
|
||||
-- name: GetLatestPendingRegistration :one
|
||||
SELECT
|
||||
pending_password_hash,
|
||||
pending_scenario,
|
||||
pending_onboarding_step
|
||||
FROM email_tokens
|
||||
WHERE
|
||||
user_id = sqlc.arg(user_id)
|
||||
AND purpose = sqlc.arg(purpose)
|
||||
AND registration_version = sqlc.arg(registration_version)
|
||||
AND pending_password_hash IS NOT NULL
|
||||
ORDER BY created_at DESC, id DESC
|
||||
LIMIT 1;
|
||||
|
||||
-- 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
|
||||
-- ends every earlier token, and deletes the registration C1 of the earlier
|
||||
-- registrant like UpdateUserRegistration does.
|
||||
-- name: AdvanceUserRegistration :one
|
||||
WITH updated AS (
|
||||
UPDATE users
|
||||
SET
|
||||
registration_version = registration_version + 1,
|
||||
last_registered_at = sqlc.arg(registered_at),
|
||||
updated_at = sqlc.arg(registered_at),
|
||||
version = version + 1
|
||||
WHERE
|
||||
users.id = sqlc.arg(id)
|
||||
AND users.version = sqlc.arg(expected_version)
|
||||
AND users.email_verified_at IS NULL
|
||||
RETURNING
|
||||
users.id,
|
||||
users.email,
|
||||
users.email_normalized,
|
||||
users.password_hash,
|
||||
users.scenario,
|
||||
users.onboarding_step,
|
||||
users.email_verified_at,
|
||||
users.registration_version,
|
||||
users.last_registered_at,
|
||||
users.version,
|
||||
users.created_at,
|
||||
users.updated_at
|
||||
),
|
||||
|
||||
discarded_consent AS (
|
||||
DELETE FROM consent_records
|
||||
USING updated
|
||||
WHERE
|
||||
consent_records.subject_id = updated.id
|
||||
AND consent_records.purpose = 'c1'
|
||||
AND consent_records.source = 'registration'
|
||||
)
|
||||
|
||||
SELECT
|
||||
updated.id,
|
||||
updated.email,
|
||||
updated.email_normalized,
|
||||
updated.password_hash,
|
||||
updated.scenario,
|
||||
updated.onboarding_step,
|
||||
updated.email_verified_at,
|
||||
updated.registration_version,
|
||||
updated.last_registered_at,
|
||||
updated.version,
|
||||
updated.created_at,
|
||||
updated.updated_at
|
||||
FROM updated;
|
||||
|
||||
-- name: InvalidateActiveEmailTokens :execrows
|
||||
UPDATE email_tokens
|
||||
SET invalidated_at = sqlc.arg(invalidated_at)
|
||||
SET
|
||||
invalidated_at = sqlc.arg(invalidated_at),
|
||||
-- An invalidated token keeps no password hash: it can never be confirmed.
|
||||
pending_password_hash = NULL,
|
||||
pending_scenario = NULL,
|
||||
pending_onboarding_step = NULL
|
||||
WHERE
|
||||
user_id = sqlc.arg(user_id)
|
||||
AND purpose = sqlc.arg(purpose)
|
||||
@@ -323,7 +406,10 @@ RETURNING
|
||||
token.created_at,
|
||||
token.expires_at,
|
||||
token.consumed_at,
|
||||
token.invalidated_at;
|
||||
token.invalidated_at,
|
||||
token.pending_password_hash,
|
||||
token.pending_scenario,
|
||||
token.pending_onboarding_step;
|
||||
|
||||
-- Explains why ConsumeEmailToken matched no row.
|
||||
-- name: GetEmailTokenState :one
|
||||
@@ -416,3 +502,52 @@ SET revoked_at = sqlc.arg(revoked_at)
|
||||
WHERE
|
||||
user_id = sqlc.arg(user_id)
|
||||
AND revoked_at IS NULL;
|
||||
|
||||
-- Stores the replay record of a command. A record whose retention has passed
|
||||
-- is replaced, so the key is accepted as new after the retention. A live
|
||||
-- record is left as it is and nothing is returned; the caller then reads it
|
||||
-- with GetLiveIdempotencyRecord. Concurrent callers with the same scope wait
|
||||
-- on the row of the first one until its transaction ends.
|
||||
-- name: ReserveIdempotencyRecord :one
|
||||
INSERT INTO idempotency_records (
|
||||
scope_hash,
|
||||
fingerprint,
|
||||
created_at,
|
||||
expires_at
|
||||
) VALUES (
|
||||
sqlc.arg(scope_hash),
|
||||
sqlc.arg(fingerprint),
|
||||
sqlc.arg(created_at),
|
||||
sqlc.arg(expires_at)
|
||||
)
|
||||
ON CONFLICT (scope_hash) DO UPDATE
|
||||
SET
|
||||
fingerprint = excluded.fingerprint,
|
||||
created_at = excluded.created_at,
|
||||
expires_at = excluded.expires_at
|
||||
WHERE idempotency_records.expires_at <= excluded.created_at
|
||||
RETURNING scope_hash, fingerprint, created_at, expires_at;
|
||||
|
||||
-- name: GetLiveIdempotencyRecord :one
|
||||
SELECT
|
||||
scope_hash,
|
||||
fingerprint,
|
||||
created_at,
|
||||
expires_at
|
||||
FROM idempotency_records
|
||||
WHERE
|
||||
scope_hash = sqlc.arg(scope_hash)
|
||||
AND expires_at > sqlc.arg(at);
|
||||
|
||||
-- Deletes at most batch_limit expired records. Locked rows are skipped, so
|
||||
-- the statement never waits for a concurrent reservation.
|
||||
-- name: PurgeExpiredIdempotencyRecords :execrows
|
||||
DELETE FROM idempotency_records
|
||||
WHERE scope_hash IN (
|
||||
SELECT stale.scope_hash
|
||||
FROM idempotency_records AS stale
|
||||
WHERE stale.expires_at <= sqlc.arg(at)
|
||||
ORDER BY stale.expires_at
|
||||
LIMIT sqlc.arg(batch_limit)
|
||||
FOR UPDATE SKIP LOCKED
|
||||
);
|
||||
Reference in new issue
Block a user