feat(identity): register accounts and resend verification mail

This commit is contained in:
shira committed 2026-10-06 23:59:51 +03:00
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;