railyard/apps/mail/crates/mail-auth/migrations/20261112000001_auth_init.sql
caelum0x 9429ff20da feat(mail): Scribase Mail Rust platform: sending core, accounts, auth, SMTP submission, Supabase hook, deliverability, TS SDK, docs
Clean-room Rust rewrite (written without reference to useSend source).
Crates: mail-types, mail-core, mail-server, mail-auth, mail-submit,
mail-hooks, mail-deliverability; sdks/ts (@scribase/mail); docs/.
Each verified by running locally (see plans/scribase-mail-rust-2026-11-12.md).
mail-mta, mail-inbound, mail-oci are still in progress and not in this commit.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-11-13 01:03:09 +03:00

54 lines
2.1 KiB
SQL

-- mail-auth: passwordless auth tables. All tables are prefixed auth_.
CREATE TABLE IF NOT EXISTS auth_projects (
id UUID PRIMARY KEY,
slug TEXT NOT NULL UNIQUE,
app_name TEXT NOT NULL,
from_address TEXT NOT NULL,
jwt_audience TEXT NOT NULL,
allowed_redirect_origins TEXT[] NOT NULL DEFAULT '{}',
subject_overrides JSONB NOT NULL DEFAULT '{}'::jsonb,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE TABLE IF NOT EXISTS auth_users (
id UUID PRIMARY KEY,
project_id UUID NOT NULL REFERENCES auth_projects(id) ON DELETE CASCADE,
email TEXT NOT NULL,
email_verified_at TIMESTAMPTZ,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
last_login_at TIMESTAMPTZ,
UNIQUE (project_id, email)
);
-- One table for every purpose: magic_link, otp, email_verify, password_reset.
-- token_hash is SHA-256 of the secret; the secret itself is never stored.
CREATE TABLE IF NOT EXISTS auth_tokens (
id UUID PRIMARY KEY,
project_id UUID NOT NULL REFERENCES auth_projects(id) ON DELETE CASCADE,
purpose TEXT NOT NULL,
email TEXT NOT NULL,
token_hash BYTEA NOT NULL UNIQUE,
redirect_to TEXT,
attempts INTEGER NOT NULL DEFAULT 0,
max_attempts INTEGER NOT NULL DEFAULT 1,
expires_at TIMESTAMPTZ NOT NULL,
used_at TIMESTAMPTZ,
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE INDEX IF NOT EXISTS auth_tokens_active_idx
ON auth_tokens (project_id, email, purpose, created_at DESC)
WHERE used_at IS NULL;
CREATE INDEX IF NOT EXISTS auth_tokens_expires_idx ON auth_tokens (expires_at);
-- Fixed hourly windows. bucket is "email:<project_id>:<email>" or "ip:<ip>".
CREATE TABLE IF NOT EXISTS auth_rate_limits (
bucket TEXT NOT NULL,
window_start TIMESTAMPTZ NOT NULL,
count INTEGER NOT NULL DEFAULT 0,
PRIMARY KEY (bucket, window_start)
);