IWSN: "Połącz z IWSN" — a person of Intecion connects the site (contract 1.8.0)
- the state block on the e-mail tab (method IWSN): the button while the service offers the confirmation page
(connectAvailable) or before the site has enrolled; a click asks the site's server for the link
(POST /api/iapl/mail-relay/connect-link, admin only), which asks the service (POST /v1/connect, enrolling first),
checks that it begins with the service's own {base}/connect?, and hands it over without storing or logging it;
the browser opens it in a new tab with noopener noreferrer; the block asks /v1/status every 5 s for 10 min
- states: waiting for confirmation (button), waiting for approval (no page), connected, stopped; 503
connect-unavailable and 429 said in the admin's language; a load of the panel asks the service while the site waits
- dashboard alert for a waiting site (live); doctor mail-relay-connect (error) — replaces the pending warning and the
"not enrolled yet" note of mail-relay-state
- opt-out mail.relay.connectButton: false (no button, no route); building blocks requestRelayConnectLink,
isRelayConnectUrl, RELAY_CONNECT_LINK_PATH; the fake service implements /v1/connect, connectAvailable and
confirmConnect
- contract copy 1.8.0 (IWDL working tree, checksums match); docs, AGENT.md, setup / upgrade / debug skills, changelog
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
347e3bafa1
commit
2c0262367d
@@ -33,6 +33,8 @@ const refusal = (code: string, message: string, extra: Partial<RelayRefusal> = {
|
||||
|
||||
/** Codes this client makes up for what never reached the service. */
|
||||
export const LOCAL_CODES = {
|
||||
/** `/v1/connect` answered a link that does not begin with the service's own `{base}/connect?`: not opened. */
|
||||
badConnectUrl: 'client-bad-connect-url',
|
||||
disconnected: 'client-disconnected',
|
||||
/** A token that belongs to another site (the service says which host it is for). */
|
||||
foreignToken: 'client-foreign-token',
|
||||
@@ -358,6 +360,8 @@ export async function withRelayToken<T>(
|
||||
}
|
||||
|
||||
type StatusBody = {
|
||||
/** Since contract 1.8.0, only for a pending site: it can be confirmed through `/v1/connect`. */
|
||||
connectAvailable?: boolean
|
||||
host?: string
|
||||
limits?: NonNullable<RelayStored['service']>['limits']
|
||||
quota?: NonNullable<RelayStored['service']>['quota']
|
||||
@@ -398,6 +402,7 @@ export async function refreshRelayStatus(payload: BasePayload, options: Resolved
|
||||
hostChecked: true,
|
||||
service: {
|
||||
name: body.service?.name,
|
||||
...(body.connectAvailable === true ? { connectAvailable: true } : {}),
|
||||
contract: body.service?.contract,
|
||||
fetchedAt: new Date().toISOString(),
|
||||
limits: body.limits,
|
||||
@@ -410,6 +415,54 @@ export async function refreshRelayStatus(payload: BasePayload, options: Resolved
|
||||
return { stored }
|
||||
}
|
||||
|
||||
/** What "Połącz z IWSN" gets from the service (contract §2.1). Never logged, never stored. */
|
||||
export type ConnectLinkResult =
|
||||
| { connectUrl: string; expiresAt: string; ok: true; state: 'pending' }
|
||||
| { ok: true; state: 'active' }
|
||||
| RelayRefusal
|
||||
|
||||
/**
|
||||
* True when `connectUrl` is a link of the service this site uses: it begins with `{base}/connect?` and carries
|
||||
* the one `ref` the contract describes, nothing else. Anything else is a fault and is not opened.
|
||||
*/
|
||||
export function isRelayConnectUrl(connectUrl: unknown, base: string): connectUrl is string {
|
||||
if (typeof connectUrl !== 'string') {return false}
|
||||
const prefix = `${base.replace(/\/+$/, '')}/connect?`
|
||||
if (!connectUrl.startsWith(prefix)) {return false}
|
||||
return /^ref=[\w-]{43}$/.test(connectUrl.slice(prefix.length))
|
||||
}
|
||||
|
||||
/**
|
||||
* Asks the service for the link a person of Intecion confirms this waiting site with (contract §2.1,
|
||||
* `POST /v1/connect`) — only on an administrator's click, never by itself. A site without a token enrols first.
|
||||
* The link is returned to the caller as it came, after checking its beginning; it is not stored and not logged.
|
||||
* `active`: the site is confirmed already (the stored state says so from now on).
|
||||
*/
|
||||
export async function requestRelayConnectLink(payload: BasePayload, options: ResolvedRelayOptions): Promise<ConnectLinkResult> {
|
||||
const host = relayHost(payload, options)
|
||||
const answer = await withRelayToken<{ connectUrl?: unknown; expiresAt?: unknown; state?: unknown }>(
|
||||
payload,
|
||||
options,
|
||||
(token) => relayRequest({ fetchImpl: options.fetchImpl, method: 'POST', path: '/v1/connect', site: host, timeoutMs: options.timeoutMs, token, url: options.url }),
|
||||
{},
|
||||
// a click is a person asking: enrol first even when this site enrols by hand only
|
||||
{ enrol: options.enrol !== false },
|
||||
)
|
||||
if (!answer.ok) {
|
||||
if (answer.code === 'site-suspended') {await updateRelayStore(payload, options, (current) => ({ ...current, state: 'suspended' }))}
|
||||
return answer
|
||||
}
|
||||
if (answer.body.state === 'active') {
|
||||
await updateRelayStore(payload, options, (current) => ({ ...current, service: current.service ? { ...current.service, connectAvailable: undefined } : current.service, state: 'active' }))
|
||||
return { ok: true, state: 'active' }
|
||||
}
|
||||
if (answer.body.state === 'pending' && isRelayConnectUrl(answer.body.connectUrl, options.url) && typeof answer.body.expiresAt === 'string') {
|
||||
return { connectUrl: answer.body.connectUrl, expiresAt: answer.body.expiresAt, ok: true, state: 'pending' }
|
||||
}
|
||||
// (the link itself is not written anywhere: not in the message, not in the log)
|
||||
return refusal(LOCAL_CODES.badConnectUrl, 'The mail service answered a confirmation link that is not its own. It was not opened.')
|
||||
}
|
||||
|
||||
/**
|
||||
* Asks for a new token when the service said the present one is due (`token.rotateAfter`, 90 days).
|
||||
* The new token is stored at once; the previous one works for 24 hours more, so other processes of
|
||||
@@ -496,6 +549,8 @@ export async function setRelayToken(payload: BasePayload, token: string, options
|
||||
|
||||
/** What the panel and the doctor may know: never the token. */
|
||||
export type RelayStatus = {
|
||||
/** The service can confirm this waiting site through `/v1/connect` ("Połącz z IWSN"); from the last `/v1/status`. */
|
||||
connectAvailable?: boolean
|
||||
connected: boolean
|
||||
disconnected: boolean
|
||||
host: string
|
||||
@@ -518,6 +573,7 @@ export function describeRelayStore(payload: BasePayload, options: ResolvedRelayO
|
||||
return {
|
||||
connected: fromOption || (Boolean(stored.token) && !stored.disconnected),
|
||||
disconnected: Boolean(stored.disconnected),
|
||||
...(stored.state === 'pending' && stored.service?.connectAvailable ? { connectAvailable: true } : {}),
|
||||
...(stored.lastFailure ? { lastFailure: stored.lastFailure } : {}),
|
||||
...(stored.revokePending ? { revokePending: true } : {}),
|
||||
host: relayHost(payload, options) || stored.host || '',
|
||||
|
||||
Reference in New Issue
Block a user