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

63 lines
2.8 KiB
Markdown

# mail-auth
Passwordless login for apps on Scribase Mail: magic links, 6-digit email codes,
email verification and password-reset links, all ending in a short-lived EdDSA
session JWT that apps verify against `/.well-known/jwks.json` (or by calling
`POST /auth/token/verify`).
Independent implementation, written without reference to useSend source.
## Endpoints
| Method | Path | Purpose |
| --- | --- | --- |
| POST | `/auth/magic-link` | `{email, redirect_to, project}` → sends a 15-minute single-use link. Always 200. |
| POST | `/auth/email-verification` | Same body; sends a confirm-your-email link. |
| POST | `/auth/password-reset` | Same body; sends a reset link. The resulting JWT carries `purpose: "password_reset"`. |
| GET | `/auth/verify?token=` | Consumes a link token. Redirects to `redirect_to#access_token=...` or returns JSON when `Accept: application/json`. |
| POST | `/auth/otp` | `{email, project}` → sends a 6-digit code, 10-minute TTL, 5 attempts. |
| POST | `/auth/otp/verify` | `{email, project, code}` → `{access_token, ...}`. |
| POST | `/auth/token/verify` | `{token}` → claims, for apps without a JWT library. |
| GET | `/.well-known/jwks.json` | Ed25519 public key (OKP / EdDSA). |
| GET/POST | `/auth/admin/projects` | List / create projects. `Authorization: Bearer $AUTH_ADMIN_TOKEN`. |
| GET/PATCH | `/auth/admin/projects/{id}` | Read / update a project. |
Rate limits: 5 requests per hour per email per project, 30 per hour per IP
(Postgres counters, fixed hourly windows) → `429` with `Retry-After`.
## Environment
| Variable | Default | Notes |
| --- | --- | --- |
| `AUTH_SIGNING_KEY` | generated in dev | 32-byte Ed25519 seed, base64url (hex also accepted). Required when `AUTH_ENV=production`. |
| `AUTH_ADMIN_TOKEN` | unset → admin API returns 503 | Bearer token for project admin endpoints. |
| `AUTH_PUBLIC_BASE_URL` | `http://127.0.0.1:8791` | Used to build the links in emails. |
| `AUTH_ISSUER` | `scribase-mail-auth` | JWT `iss`. |
| `AUTH_MAGIC_LINK_TTL_SECS` | `900` | |
| `AUTH_OTP_TTL_SECS` | `600` | |
| `AUTH_JWT_TTL_SECS` | `3600` | |
| `AUTH_OTP_MAX_ATTEMPTS` | `5` | |
| `AUTH_EMAIL_RATE_LIMIT` | `5` | per email per project per hour |
| `AUTH_IP_RATE_LIMIT` | `30` | per IP per hour |
| `AUTH_TRUST_PROXY` | `false` | when `true`, read the client IP from `X-Forwarded-For`. |
## Run the dev server
```sh
createdb mail_dev_auth
AUTH_ADMIN_TOKEN=dev-admin cargo run -p mail-auth --example dev_server
```
Mail is written to `/tmp/mail-auth-outbox/<id>.eml` instead of being sent.
## Embedding
```rust
let state = mail_auth::AuthState::new(pool, sender, keys, config);
mail_auth::migrate(&pool).await?;
let app = other_router.merge(mail_auth::router(state));
```
All tables are prefixed `auth_`. Migrations use a timestamp version namespace
and `ignore_missing`, so they can share `_sqlx_migrations` with `mail-core`.