168 lines
6.5 KiB
SQL
168 lines
6.5 KiB
SQL
-- +goose Up
|
|
-- Identity storage: accounts, one-time email tokens, and server-side sessions.
|
|
-- Passwords, raw email tokens, and raw session tokens are never stored; only
|
|
-- an Argon2id PHC string and SHA-256 digests are. Every timestamp comes from
|
|
-- the application clock, so expiry checks and the stored deadlines use one
|
|
-- clock. Enum-like columns are text with CHECK constraints rather than
|
|
-- PostgreSQL enum types: a new value then changes one constraint in a later
|
|
-- migration, which can also be rolled back, while an enum value cannot be
|
|
-- removed and cannot be used in the transaction that adds it.
|
|
CREATE TABLE users (
|
|
-- UUIDv7 generated in Go.
|
|
id uuid NOT NULL,
|
|
-- Delivery form: trimmed ASCII, letter case as typed.
|
|
email text NOT NULL,
|
|
-- Comparison key computed in Go: the delivery form with A-Z lowercased.
|
|
email_normalized text NOT NULL,
|
|
-- Argon2id PHC string; never the password.
|
|
password_hash text NOT NULL,
|
|
scenario text NOT NULL,
|
|
onboarding_step text NOT NULL,
|
|
email_verified_at timestamptz,
|
|
-- Advanced on every registration of the unverified email. Email tokens
|
|
-- are bound to the version they were issued for.
|
|
registration_version bigint NOT NULL,
|
|
-- Time of the latest registration; the planned cleanup of unverified
|
|
-- accounts counts its deadline from here.
|
|
last_registered_at timestamptz NOT NULL,
|
|
-- Optimistic concurrency version, advanced on every update.
|
|
version bigint NOT NULL,
|
|
created_at timestamptz NOT NULL,
|
|
updated_at timestamptz NOT NULL,
|
|
CONSTRAINT users_pkey PRIMARY KEY (id),
|
|
CONSTRAINT users_email_normalized_key UNIQUE (email_normalized),
|
|
-- Addresses are ASCII until the mailer supports SMTPUTF8: one byte per
|
|
-- character in the UTF-8 database encoding.
|
|
CONSTRAINT users_email_ascii CHECK (
|
|
char_length(email) BETWEEN 3 AND 254
|
|
AND octet_length(email) = char_length(email)
|
|
),
|
|
-- translate() maps A-Z by code point, independent of the collation,
|
|
-- unlike lower().
|
|
CONSTRAINT users_email_normalized_rule CHECK (
|
|
email_normalized = translate(
|
|
email,
|
|
'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
|
|
'abcdefghijklmnopqrstuvwxyz'
|
|
)
|
|
),
|
|
CONSTRAINT users_password_hash_format CHECK (
|
|
char_length(password_hash) <= 512
|
|
AND password_hash
|
|
~ '^\$argon2id\$v=19\$m=[0-9]+,t=[0-9]+,p=[0-9]+\$[A-Za-z0-9+/]+\$[A-Za-z0-9+/]+$'
|
|
),
|
|
CONSTRAINT users_scenario_onboarding_step CHECK (
|
|
(
|
|
scenario = 'candidate'
|
|
AND onboarding_step IN (
|
|
'candidate_consents',
|
|
'candidate_profile',
|
|
'candidate_questionnaire',
|
|
'candidate_assessment',
|
|
'completed'
|
|
)
|
|
)
|
|
OR (
|
|
scenario = 'employer'
|
|
AND onboarding_step IN ('employer_company', 'completed')
|
|
)
|
|
),
|
|
CONSTRAINT users_registration_version_positive CHECK (
|
|
registration_version > 0
|
|
),
|
|
CONSTRAINT users_version_positive CHECK (version > 0),
|
|
CONSTRAINT users_timestamps_after_creation CHECK (
|
|
updated_at >= created_at
|
|
AND last_registered_at >= created_at
|
|
AND (email_verified_at IS NULL OR email_verified_at >= created_at)
|
|
)
|
|
);
|
|
|
|
-- Supports the planned cleanup of accounts that stay unverified after their
|
|
-- latest registration. Verified accounts are not indexed.
|
|
CREATE INDEX users_unverified_last_registered_at_idx
|
|
ON users (last_registered_at)
|
|
WHERE email_verified_at IS NULL;
|
|
|
|
CREATE TABLE email_tokens (
|
|
-- UUIDv7 generated in Go.
|
|
id uuid NOT NULL,
|
|
user_id uuid NOT NULL,
|
|
purpose text NOT NULL,
|
|
-- SHA-256 of the raw token as 32 raw bytes: half the size of hex text,
|
|
-- with an exact length check and no letter-case or encoding variants.
|
|
token_hash bytea NOT NULL,
|
|
registration_version bigint NOT NULL,
|
|
created_at timestamptz NOT NULL,
|
|
expires_at timestamptz NOT NULL,
|
|
consumed_at timestamptz,
|
|
-- Set when a re-registration or a resend replaces the token.
|
|
invalidated_at timestamptz,
|
|
CONSTRAINT email_tokens_pkey PRIMARY KEY (id),
|
|
CONSTRAINT email_tokens_token_hash_key UNIQUE (token_hash),
|
|
CONSTRAINT email_tokens_user_id_fkey FOREIGN KEY (user_id)
|
|
REFERENCES users (id) ON DELETE CASCADE,
|
|
CONSTRAINT email_tokens_purpose_known CHECK (
|
|
purpose IN ('email_verification')
|
|
),
|
|
CONSTRAINT email_tokens_token_hash_sha256 CHECK (
|
|
octet_length(token_hash) = 32
|
|
),
|
|
CONSTRAINT email_tokens_registration_version_positive CHECK (
|
|
registration_version > 0
|
|
),
|
|
CONSTRAINT email_tokens_expiry_after_creation CHECK (
|
|
expires_at > created_at
|
|
),
|
|
-- A token ends at most once: either consumed or invalidated.
|
|
CONSTRAINT email_tokens_single_outcome CHECK (
|
|
consumed_at IS NULL OR invalidated_at IS NULL
|
|
),
|
|
CONSTRAINT email_tokens_outcome_after_creation CHECK (
|
|
(consumed_at IS NULL OR consumed_at >= created_at)
|
|
AND (invalidated_at IS NULL OR invalidated_at >= created_at)
|
|
)
|
|
);
|
|
|
|
-- Serves invalidation of a user's tokens for one purpose and the cascade
|
|
-- delete of a user's tokens.
|
|
CREATE INDEX email_tokens_user_id_purpose_idx
|
|
ON email_tokens (user_id, purpose);
|
|
|
|
CREATE TABLE sessions (
|
|
-- UUIDv7 generated in Go; internal only, never sent to the client.
|
|
id uuid NOT NULL,
|
|
user_id uuid NOT NULL,
|
|
-- SHA-256 of the raw session token that the cookie carries.
|
|
token_hash bytea NOT NULL,
|
|
created_at timestamptz NOT NULL,
|
|
-- The idle timeout counts from here.
|
|
last_seen_at timestamptz NOT NULL,
|
|
-- Absolute deadline fixed at creation.
|
|
expires_at timestamptz NOT NULL,
|
|
revoked_at timestamptz,
|
|
CONSTRAINT sessions_pkey PRIMARY KEY (id),
|
|
CONSTRAINT sessions_token_hash_key UNIQUE (token_hash),
|
|
CONSTRAINT sessions_user_id_fkey FOREIGN KEY (user_id)
|
|
REFERENCES users (id) ON DELETE CASCADE,
|
|
CONSTRAINT sessions_token_hash_sha256 CHECK (
|
|
octet_length(token_hash) = 32
|
|
),
|
|
CONSTRAINT sessions_expiry_after_creation CHECK (expires_at > created_at),
|
|
CONSTRAINT sessions_last_seen_after_creation CHECK (
|
|
last_seen_at >= created_at
|
|
),
|
|
CONSTRAINT sessions_revoked_after_creation CHECK (
|
|
revoked_at IS NULL OR revoked_at >= created_at
|
|
)
|
|
);
|
|
|
|
-- Serves revocation of all of a user's sessions and the cascade delete.
|
|
CREATE INDEX sessions_user_id_idx
|
|
ON sessions (user_id);
|
|
|
|
-- +goose Down
|
|
DROP TABLE sessions;
|
|
DROP TABLE email_tokens;
|
|
DROP TABLE users;
|