caelum0x 219a58e855 feat(mail): wire one mail-server process — relay pool default, SMTP submit, Supabase hook, inbound ingest, OCI onboarding
- mail-server spawns SMTP submission + HTTPS inbound ingest, mounts Supabase hook router
- RelayPool is the default transport; relay rejections mark email failed without suppressing recipient
- inbound DSN/ARF -> bounced/complained + suppression + webhooks
- domains API returns Resend-style records incl. OCI DKIM; OCI onboarding gated by OCI_ONBOARDING=on
- OCI suppression mirror sync; docs: test mode uses sk_test_

Verified end to end locally against a fake relay (API + SMTP send, DKIM valid, bounce -> suppressed -> 403, complaint, Supabase hook, 7 signed webhooks).

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

64 lines
3.6 KiB
Markdown

# mail-hooks
The Supabase Auth **Send Email Hook** endpoint for Scribase Mail. Supabase
calls `POST /hooks/supabase/{slug}` with `{ user, email_data }` signed per
Standard Webhooks; we verify the signature and timestamp, render the
template for `email_action_type`, build the verify link and send through
mail-core on behalf of the project's account.
Independent implementation, written without reference to useSend source.
Licence: MIT. User guide: [`SUPABASE.md`](./SUPABASE.md).
## Endpoints
| Method + path | Purpose | Auth |
|---|---|---|
| `POST /hooks/supabase/{slug}` | the hook Supabase calls | `webhook-id` / `webhook-timestamp` / `webhook-signature` |
| `POST /hooks/projects` | create a project; returns `hook_secret` (`v1,whsec_...`) and `endpoint_path` | `Bearer sk_` (full access) |
| `GET /hooks/projects`, `GET/PATCH/DELETE /hooks/projects/{id}` | manage projects of the key's account | `Bearer sk_` |
| `GET /hooks/projects/{id}/deliveries?limit=` | recent hook calls (status, email ids, errors) | `Bearer sk_` |
| `GET /hooks/projects/{id}/preview/{email_action_type}` | render a template with sample data, no send | `Bearer sk_` |
Create body: `{ "name", "from", "app_name", "slug"?, "hook_secret"?, "site_url"?, "templates"? }`.
Errors use Supabase's hook shape `{ "error": { "http_code": 401, "message": "..." } }`.
## Behaviour
- Signature: `HMAC-SHA256(secret, "{webhook-id}.{webhook-timestamp}.{body}")`,
base64, compared constant-time against every `v1,` entry in
`webhook-signature`. Secret accepted as `v1,whsec_<b64>`, `whsec_<b64>` or
bare base64. Timestamp outside `HOOKS_TIMESTAMP_TOLERANCE_SECS` (300) → 401.
- Replay: `(project, webhook-id)` is unique in `hook_deliveries`; a repeated
id returns `200 {}` without sending again.
- Verify link, per Supabase's docs:
`{site_url}/auth/v1/verify?token={token_hash}&type={email_action_type}&redirect_to={redirect_to}`.
A project `site_url` overrides the payload's.
- `email_change`: with both token pairs present (Secure Email Change) two
emails go out: to `user.email` using `token` + `token_hash_new`, and to
`user.new_email` using `token_new` + `token_hash` (field names crossed, as
documented). With one pair, one email to the new address.
- Built-in templates (HTML + text, no emoji): `signup`, `invite`, `magiclink`,
`recovery`, `email_change` (both directions), `reauthentication` (code),
`email` (code), and the seven `*_notification` types. Unknown types get a
generic notice.
- Custom templates per project and action type: `{"magiclink": {"subject",
"html", "text"}}` with `{{confirmation_url}}`, `{{token}}`, `{{token_hash}}`,
`{{new_token}}`, `{{email}}`, `{{new_email}}`, `{{old_email}}`,
`{{site_url}}`, `{{redirect_to}}`, `{{app_name}}`, `{{email_action_type}}`.
Supabase's names (`{{ .ConfirmationURL }}`, `{{ .Token }}`, `{{ .SiteURL }}`,
...) are accepted too, so existing templates paste over.
- Emails are tagged `source=supabase_hook` and `email_action_type=<type>`.
## Run
```sh
export DATABASE_URL=postgres://localhost/mail_dev_core # same DB as mail-server (run it first)
export HOOKS_BIND_ADDR=127.0.0.1:8793 DEV_TRUST_UNVERIFIED_DOMAINS=on
cargo run -p mail-hooks
```
Embed (what mail-server does): `mail_hooks::migrate(&pool).await?; app.merge(mail_hooks::router(HookState::new(store, core_cfg, HooksConfig::from_env())))`.
Admin keys are resolved with `mail_core::authenticate_api_key`; sends go through `mail_core::CoreSender::for_account` (the same queue as `POST /emails`).
Tables are `hook_projects` and `hook_deliveries` (idempotent SQL in
`migrations/`, applied by `migrate`, independent of `_sqlx_migrations`).