Mail relay: the access-key field is off by default (mail.relay.accessKeyField)

A token issued by hand is set by a developer: setRelayToken() from a script, or mail.relay.token.
The panel field and its route take a token only when the project switches the field on. Docs and
AGENT.md say when such a token is needed and how to set it; a token of another host is refused on
every way in.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
Radosław Smoliński
2026-11-25 22:25:34 +01:00
co-authored by Claude Fable 5.2
parent 1a11cefffa
commit edee3a321f
11 changed files with 87 additions and 21 deletions
+33 -7
View File
@@ -213,11 +213,35 @@ await runWithMailKind('account', () => sendWelcomeMails(payload, user))
`{ revoked }`: when the service could not be reached the site stops using the token at once, but the token
**still works at the service** — the panel says "not confirmed" and keeps it so that "Disconnect" can be pressed
again; only then is it revoked and forgotten.
- **A token of another site is refused.** The access-key field asks the service whose token it is before it stores
anything, and a token stored by a script is checked the same way before its first use (`/v1/status` names the
host): another site's token is not saved, not used and not revoked.
- **A token issued by hand** — for a site the service cannot call back (behind a firewall): Intecion issues one,
an admin enters it in the state block ("Enter an access key"), or a script calls `setRelayToken(payload, token)`.
- **A token of another site is refused, on every way in.** A token stored by a script is checked with the service
before its first use (`/v1/status` names the host), the panel's access-key field asks before it stores anything,
and every request names the site, so the service refuses a foreign token from `relay.token` too
(`host-mismatch`): another site's token is not saved, not used and not revoked.
- **A token issued by hand** — only for a site the service **cannot call back**: behind a firewall or basic auth, on
a private network, or one that cannot serve the confirm route. Every other site enrols by itself and needs none.
Intecion issues the token for the site's host; **a developer sets it**, one of three ways:
```ts
// 1. once, from a script (recommended): pnpm payload run scripts/set-mail-token.ts
// stored in payload-kv like an enrolled token; the plugin rotates it every 90 days
import config from '@payload-config'
import { setRelayToken } from '@intecion/ipal-kit'
import { getPayload } from 'payload'
const payload = await getPayload({ config })
await setRelayToken(payload, process.env.MAIL_TOKEN!) // pass it in the environment of this one run; never commit it
process.exit(0)
```
```ts
// 2. from the deployment's own secret, on every start: nothing is stored, the site never enrols,
// and replacing the token before it runs out is the project's job
iapl({ mail: { relay: { enrol: false, token: () => process.env.MY_MAIL_TOKEN } } })
```
3. In the panel, when the project switches the field on: `mail: { relay: { accessKeyField: true } }` adds
"Enter an access key" to the state block for admins. **Off by default** — without it the block has no such
field and its route takes no token (`403`).
**Such a site cannot heal by itself:** it never enrols on its own. When the service no longer knows its server
address (after a move, or a second server) or its token, the send reports `relay-refused` and the state block
says what to ask Intecion for — the new address added, or a new token.
@@ -241,14 +265,15 @@ Site integrations → E-mail sending starts with "How e-mails are sent" (compone
rendered by the tab's status banner — **no import-map entry of its own, so nothing to regenerate**): the
transport in effect; for the mail service the state (connected, waiting for approval, stopped, not connected and
why), the address mail leaves from, where replies go, what is left of the hour and the day, and a sender name the
service replaced. Buttons: **Check now**, **Connect again**, **Disconnect**, **Enter an access key**.
service replaced. Buttons: **Check now**, **Connect again**, **Disconnect** — and **Enter an access key** only
with `mail.relay.accessKeyField: true` ("The token" above).
Routes (all three need the `admin` role: not logged in → 401, other role → 403; none ever answers with a token):
| Route | What |
|---|---|
| `GET /api/iapl/mail-relay/status` | the transport and the stored state; `?refresh=1` asks the service |
| `POST /api/iapl/mail-relay/connect` | enrol now; with `{ token }` store a token issued by hand |
| `POST /api/iapl/mail-relay/connect` | enrol now; with `{ token }` store a token issued by hand — only with `accessKeyField: true`, otherwise `403` |
| `POST /api/iapl/mail-relay/disconnect` | give the token up |
| `POST /api/iapl/mail-relay/confirm` | **public** — the service's call-back. `404` and no change unless the site itself started an enrolment in the last 5 minutes; **refused** calls are rate-limited (10 a minute per address, `mail.relay.confirmRateLimit`) — a call that carries the nonce is never turned away by the limit, so junk from the same address cannot keep the site from enrolling |
@@ -308,6 +333,7 @@ message); "Connect again" asks at once. Other causes: `host-not-allowed` (not a
| `url` | `RELAY_BASE_URL` (`https://webservices.intecion.net/mail`) | another base address — a relay of your own that speaks the same contract |
| `host` | host of `serverURL` (`relayHost(payload, options)`) | the public host the service calls back |
| `enrol` | `'auto'` | `'manual'`: only "Connect again" or `enrolRelay()` enrols. `false`: never (the token comes from `token` or `setRelayToken`) |
| `accessKeyField` | `false` | `true` shows "Enter an access key" in the state block and lets its route store a hand-issued token an admin pastes |
| `token` | — | a token from elsewhere (`string` or a function): the stored token is not used and the site does not enrol |
| `senderName` | panel, then site name | a fixed name, or a function |
| `kind` | — | the kind of a message nobody marked |