cauthy-auth 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{cauthy-C5_lopMQ.d.ts → cauthy--iqtLWNa.d.ts} +1 -1
- package/dist/{cauthy-mNADGlNl.d.mts → cauthy-Dg-DNIo9.d.mts} +1 -1
- package/dist/cli.js +6 -2
- package/dist/fastify.d.mts +3 -2
- package/dist/fastify.d.ts +3 -2
- package/dist/index.d.mts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.js +257 -220
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +257 -220
- package/dist/index.mjs.map +1 -1
- package/dist/react.d.mts +3 -2
- package/dist/react.d.ts +3 -2
- package/dist/{system-BWLddIt2.d.mts → system-CL3Q30FT.d.ts} +10 -0
- package/dist/{system-BWLddIt2.d.ts → system-CdvktS7x.d.mts} +10 -0
- package/dist/theme.d.mts +16 -3
- package/dist/theme.d.ts +16 -3
- package/dist/theme.js +32 -2
- package/dist/theme.js.map +1 -1
- package/dist/theme.mjs +31 -2
- package/dist/theme.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/cookies.ts","../src/seal.ts","../src/system.ts","../src/types.ts","../src/webhook.ts","../src/cauthy.ts","../src/theme.ts","../src/permissions.ts","../src/connect.ts"],"sourcesContent":["// Cookies, by hand: reading the Cookie header and writing Set-Cookie.\n//\n// A session can outgrow one cookie (browsers stop near 4 KB), so a value is\n// written in numbered parts — name.0, name.1 — and read back in order.\n\nconst PART = 3500\n\nexport function readCookies(header: string | null | undefined): Map<string, string> {\n const out = new Map<string, string>()\n for (const pair of (header ?? '').split(';')) {\n const i = pair.indexOf('=')\n if (i > 0) out.set(pair.slice(0, i).trim(), pair.slice(i + 1).trim())\n }\n return out\n}\n\n/** A value written in parts, or null when the cookie is not there. */\nexport function readParts(cookies: Map<string, string>, name: string): string | null {\n let value = ''\n for (let i = 0; cookies.has(`${name}.${i}`); i++) value += cookies.get(`${name}.${i}`)\n return value === '' ? null : value\n}\n\nexport interface CookieAttributes {\n secure: boolean\n maxAge: number // seconds; 0 deletes\n path?: string\n}\n\nfunction attributes(a: CookieAttributes): string {\n // httpOnly: script never reads it. SameSite=Lax: it rides the navigation\n // that comes back from Cauthy, and no cross-site form post.\n return `Path=${a.path ?? '/'}; HttpOnly; SameSite=Lax; Max-Age=${a.maxAge}${a.secure ? '; Secure' : ''}`\n}\n\nexport function writeCookie(name: string, value: string, a: CookieAttributes): string {\n return `${name}=${value}; ${attributes(a)}`\n}\n\n/**\n * The Set-Cookie headers that store a value in parts and delete whatever\n * parts an earlier, longer value left behind.\n */\nexport function writeParts(name: string, value: string, a: CookieAttributes, had: Map<string, string>): string[] {\n const out: string[] = []\n let n = 0\n for (let i = 0; i < value.length; i += PART) out.push(writeCookie(`${name}.${n++}`, value.slice(i, i + PART), a))\n for (let i = n; had.has(`${name}.${i}`); i++) out.push(writeCookie(`${name}.${i}`, '', { ...a, maxAge: 0 }))\n return out\n}\n\nexport function clearParts(name: string, a: CookieAttributes, had: Map<string, string>): string[] {\n const out: string[] = []\n for (let i = 0; had.has(`${name}.${i}`) || i === 0; i++) out.push(writeCookie(`${name}.${i}`, '', { ...a, maxAge: 0 }))\n return out\n}\n","// The session cookie's envelope: encrypted and authenticated with a key only\n// the server has, so the browser carries the session without being able to\n// read or change it.\n\nimport { CompactEncrypt, compactDecrypt } from 'jose'\n\nconst encoder = new TextEncoder()\nconst decoder = new TextDecoder()\n\n/** The 256-bit key a secret stands for. */\nexport async function keyOf(secret: string): Promise<Uint8Array> {\n return new Uint8Array(await crypto.subtle.digest('SHA-256', encoder.encode(secret)))\n}\n\nexport async function seal(value: unknown, key: Uint8Array): Promise<string> {\n return new CompactEncrypt(encoder.encode(JSON.stringify(value)))\n .setProtectedHeader({ alg: 'dir', enc: 'A256GCM' })\n .encrypt(key)\n}\n\n/** The value sealed, or null for anything that is not a cookie this key made. */\nexport async function unseal<T>(sealed: string, key: Uint8Array): Promise<T | null> {\n try {\n const { plaintext } = await compactDecrypt(sealed, key, {\n keyManagementAlgorithms: ['dir'],\n contentEncryptionAlgorithms: ['A256GCM'],\n })\n return JSON.parse(decoder.decode(plaintext)) as T\n } catch {\n return null\n }\n}\n","// The application acting on its own: creating its organizations, adding their\n// members, inviting people. No person's token is involved — the application\n// is somebody in Cauthy, and what it may touch is its own system's\n// organizations and nothing else.\n//\n// It proves who it is with the client's secret, or with a key it holds and\n// whose public half it publishes (`publicJwks`): then nothing secret is ever\n// shared with Cauthy.\n\nimport { exportJWK, importJWK, importPKCS8, SignJWT, type JWK, type KeyLike } from 'jose'\nimport { CauthyConfigError, type JsonValue } from './types'\n\nconst ASSERTION_TYPE = 'urn:ietf:params:oauth:client-assertion-type:jwt-bearer'\n/** Ask for a new token this long before the one held runs out. */\nconst SKEW_MS = 30_000\n\n/** The key an application signs its assertions with. */\nexport interface SystemKey {\n /** The private key: PKCS#8 PEM, or a JWK (as an object or its JSON). RSA, for RS256. */\n privateKey: string | JWK\n /** The id the public key is published under (`kid`). */\n keyId: string\n}\n\nexport interface SystemOptions {\n /** Cauthy's address. Default: `CAUTHY_ISSUER`. */\n issuer?: string\n /** The application's client id. Default: `CAUTHY_CLIENT_ID`. */\n clientId?: string\n /** Its secret. Default: `CAUTHY_CLIENT_SECRET`. Not needed with `key`. */\n clientSecret?: string\n /**\n * The key to prove who the application is with, instead of the secret.\n * Default: `CAUTHY_SYSTEM_KEY` (the private key) and `CAUTHY_SYSTEM_KEY_ID`.\n */\n key?: SystemKey\n fetch?: typeof fetch\n}\n\n/** Cauthy refused a request of the application's own. */\nexport class CauthyApiError extends Error {\n constructor(\n readonly status: number,\n /** Cauthy's code for what went wrong: `organization_not_found`, `other_system`, `outside_system_scope`… */\n readonly code: string,\n message: string,\n ) {\n super(`cauthy-auth: ${message}`)\n this.name = 'CauthyApiError'\n }\n}\n\n/** An organization of the application's system. Every moment is UTC. */\nexport interface Organization {\n id: string\n system: string\n name: string\n kind: string\n /** The id the application knows it by. */\n externalId?: string\n status: 'active' | 'archived'\n metadata: Record<string, JsonValue>\n members: number\n createdAt: string\n updatedAt: string\n}\n\nexport interface OrganizationMember {\n userId: string\n email: string\n name: string\n /** The account's status: a disabled member is still listed. */\n status: string\n role: string\n /** True when the address had no account: one was made and invited. */\n invited?: boolean\n}\n\n/** Who to add: an account by its id, or anybody by their address (invited when new). */\nexport type NewMember = { role: string; name?: string; locale?: string } & ({ userId: string; email?: undefined } | { email: string; userId?: undefined })\n\nexport interface CauthySystem {\n /** The application's own token, asked for when needed and kept until it is about to run out. */\n token(): Promise<string>\n /** Any route the application's token may call; answers what Cauthy put under `data`. */\n request<T = unknown>(method: string, path: string, body?: unknown): Promise<T>\n organizations: {\n list(query?: { q?: string; status?: 'active' | 'archived'; limit?: number; offset?: number }): Promise<{ items: Organization[]; total: number }>\n create(input: { name: string; kind?: string; externalId?: string; metadata?: Record<string, JsonValue> }): Promise<Organization>\n get(id: string): Promise<Organization>\n update(id: string, patch: { name?: string; kind?: string; status?: 'active' | 'archived'; metadata?: Record<string, JsonValue> }): Promise<Organization>\n /** Removes the organization and every membership of it, for good. */\n remove(id: string): Promise<void>\n }\n members: {\n list(organizationId: string): Promise<OrganizationMember[]>\n add(organizationId: string, member: NewMember): Promise<OrganizationMember>\n setRole(organizationId: string, userId: string, role: string): Promise<OrganizationMember>\n remove(organizationId: string, userId: string): Promise<void>\n }\n /** Invites a person who has no account yet; answers the account made for them. */\n invite(person: { email: string; name?: string; locale?: string }): Promise<{ id: string; email: string }>\n /** The public half of `key`, to serve at the address Cauthy was given (`jwks_uri`). Empty with a secret. */\n publicJwks(): Promise<{ keys: JWK[] }>\n}\n\nfunction env(name: string): string | undefined {\n return typeof process !== 'undefined' ? process.env?.[name] : undefined\n}\n\ninterface OrgWire {\n id: string; system: string; name: string; kind: string; external_id?: string; status: 'active' | 'archived'\n metadata: Record<string, JsonValue>; members: number; created_at: string; updated_at: string\n}\n\nfunction toOrganization(o: OrgWire): Organization {\n return { id: o.id, system: o.system, name: o.name, kind: o.kind, externalId: o.external_id, status: o.status,\n metadata: o.metadata ?? {}, members: o.members, createdAt: o.created_at, updatedAt: o.updated_at }\n}\n\ninterface MemberWire { user_id: string; email: string; name: string; status: string; role: string; invited?: boolean }\n\nfunction toMember(m: MemberWire): OrganizationMember {\n return { userId: m.user_id, email: m.email, name: m.name, status: m.status, role: m.role, ...(m.invited === undefined ? {} : { invited: m.invited }) }\n}\n\nfunction asJwk(k: SystemKey): JWK | undefined {\n if (typeof k.privateKey !== 'string') return k.privateKey\n return k.privateKey.includes('BEGIN') ? undefined : (JSON.parse(k.privateKey) as JWK)\n}\n\nasync function loadKey(k: SystemKey): Promise<KeyLike | Uint8Array> {\n const jwk = asJwk(k)\n // Extractable, so its public half can be published (publicJwks).\n return jwk ? importJWK(jwk, 'RS256') : importPKCS8(k.privateKey as string, 'RS256', { extractable: true })\n}\n\nexport function createSystem(given: SystemOptions = {}): CauthySystem {\n const issuer = (given.issuer ?? env('CAUTHY_ISSUER') ?? '').replace(/\\/+$/, '')\n const clientId = given.clientId ?? env('CAUTHY_CLIENT_ID') ?? ''\n const clientSecret = given.clientSecret ?? env('CAUTHY_CLIENT_SECRET') ?? ''\n const envKey = env('CAUTHY_SYSTEM_KEY')\n const key = given.key ?? (envKey ? { privateKey: envKey, keyId: env('CAUTHY_SYSTEM_KEY_ID') ?? '' } : undefined)\n const doFetch: typeof fetch = (...args) => (given.fetch ?? fetch)(...args)\n const tokenEndpoint = `${issuer}/v1/oauth/token`\n\n // Checked when it is first used, not when it is made: an application that\n // only signs people in never needs any of this.\n function configured(): void {\n if (!/^https?:\\/\\//.test(issuer)) throw new CauthyConfigError('issuer (CAUTHY_ISSUER) must be Cauthy\\'s address')\n if (clientId === '') throw new CauthyConfigError('clientId (CAUTHY_CLIENT_ID) is required')\n if (key ? key.keyId === '' : clientSecret === '') {\n throw new CauthyConfigError(key ? 'the key needs its id (CAUTHY_SYSTEM_KEY_ID)'\n : 'to act on its own the application needs its secret (CAUTHY_CLIENT_SECRET) or a key (CAUTHY_SYSTEM_KEY)')\n }\n }\n\n let imported: Promise<KeyLike | Uint8Array> | undefined\n const signingKey = () => (imported ??= loadKey(key as SystemKey))\n\n async function fail(res: Response, what: string): Promise<never> {\n const body = (await res.json().catch(() => null)) as { errors?: { code?: string; message?: string }[] } | null\n const first = body?.errors?.[0]\n throw new CauthyApiError(res.status, first?.code ?? 'error', `Cauthy refused ${what} (${res.status}): ${first?.message ?? res.statusText}`)\n }\n\n let held: { value: string; until: number } | undefined\n let asking: Promise<string> | undefined\n\n async function ask(): Promise<string> {\n configured()\n const form = new URLSearchParams({ grant_type: 'client_credentials', client_id: clientId })\n if (key) {\n const assertion = await new SignJWT({})\n .setProtectedHeader({ alg: 'RS256', kid: key.keyId })\n .setIssuer(clientId).setSubject(clientId).setAudience(tokenEndpoint)\n .setJti(crypto.randomUUID()).setIssuedAt().setExpirationTime('2m')\n .sign(await signingKey())\n form.set('client_assertion_type', ASSERTION_TYPE)\n form.set('client_assertion', assertion)\n } else {\n form.set('client_secret', clientSecret)\n }\n const res = await doFetch(tokenEndpoint, { method: 'POST', headers: { 'content-type': 'application/x-www-form-urlencoded' }, body: form })\n if (!res.ok) await fail(res, 'the application\\'s own token')\n const t = (await res.json()) as { access_token: string; expires_in: number }\n held = { value: t.access_token, until: Date.now() + t.expires_in * 1000 - SKEW_MS }\n return t.access_token\n }\n\n function token(): Promise<string> {\n if (held && Date.now() < held.until) return Promise.resolve(held.value)\n // One request, however many callers arrive while it is out.\n return (asking ??= ask().finally(() => { asking = undefined }))\n }\n\n async function call(method: string, path: string, body?: unknown): Promise<unknown> {\n const send = async () => doFetch(`${issuer}${path}`, {\n method,\n headers: { authorization: `Bearer ${await token()}`, ...(body === undefined ? {} : { 'content-type': 'application/json' }) },\n body: body === undefined ? undefined : JSON.stringify(body),\n })\n let res = await send()\n if (res.status === 401 && held) {\n // The token was taken back early (the client's keys or secret changed): once more, with a new one.\n held = undefined\n res = await send()\n }\n if (!res.ok) await fail(res, `${method} ${path}`)\n return res.status === 204 ? undefined : res.json()\n }\n\n const data = async <T>(method: string, path: string, body?: unknown) => ((await call(method, path, body)) as { data: T } | undefined)?.data as T\n const id = encodeURIComponent\n\n return {\n token,\n request: data,\n organizations: {\n async list(query = {}) {\n const q = new URLSearchParams()\n for (const [k, v] of Object.entries(query)) if (v !== undefined) q.set(k, String(v))\n const out = (await call('GET', `/v1/organizations${q.size ? `?${q}` : ''}`)) as { data: OrgWire[]; total_count: number }\n return { items: out.data.map(toOrganization), total: out.total_count }\n },\n async create(input) {\n return toOrganization(await data<OrgWire>('POST', '/v1/organizations',\n { name: input.name, kind: input.kind, external_id: input.externalId, metadata: input.metadata }))\n },\n async get(orgId) {\n return toOrganization(await data<OrgWire>('GET', `/v1/organizations/${id(orgId)}`))\n },\n async update(orgId, patch) {\n return toOrganization(await data<OrgWire>('PATCH', `/v1/organizations/${id(orgId)}`, patch))\n },\n async remove(orgId) {\n await call('DELETE', `/v1/organizations/${id(orgId)}`)\n },\n },\n members: {\n async list(orgId) {\n return (await data<MemberWire[]>('GET', `/v1/organizations/${id(orgId)}/members`)).map(toMember)\n },\n async add(orgId, m) {\n return toMember(await data<MemberWire>('POST', `/v1/organizations/${id(orgId)}/members`,\n { role: m.role, user_id: m.userId, email: m.email, name: m.name, locale: m.locale, client_id: clientId }))\n },\n async setRole(orgId, userId, role) {\n return toMember(await data<MemberWire>('PUT', `/v1/organizations/${id(orgId)}/members/${id(userId)}`, { role }))\n },\n async remove(orgId, userId) {\n await call('DELETE', `/v1/organizations/${id(orgId)}/members/${id(userId)}`)\n },\n },\n async invite(person) {\n // The invitation leads back to this application once it is accepted.\n const u = await data<{ id: string; email: string }>('POST', '/v1/users', { ...person, client_id: clientId })\n return { id: u.id, email: u.email }\n },\n async publicJwks() {\n if (!key) return { keys: [] }\n const { kty, n, e } = asJwk(key) ?? (await exportJWK(await signingKey()))\n return { keys: [{ kty, n, e, kid: key.keyId, alg: 'RS256', use: 'sig' }] }\n },\n }\n}\n","/** How an application is set up to sign its people in with Cauthy. */\nexport interface CauthyOptions {\n /** Cauthy's public address, e.g. `https://id.example.com`. Default: `CAUTHY_ISSUER`. */\n issuer?: string\n /** The application's OAuth client id. Default: `CAUTHY_CLIENT_ID`. */\n clientId?: string\n /** Its secret, when the client is confidential. Default: `CAUTHY_CLIENT_SECRET`. */\n clientSecret?: string\n /**\n * The key the session cookie is encrypted with: any string of 32 characters\n * or more, kept on the server. Default: `CAUTHY_SECRET`.\n */\n secret?: string\n /**\n * The system this application is, as its organizations are filed under in\n * Cauthy (`\"pondari\"`). When set, a session's organizations are that\n * system's. Default: `CAUTHY_SYSTEM`.\n */\n system?: string\n /** Where the routes are mounted. Default: `/api/auth`. */\n basePath?: string\n /**\n * The application's own public origin (`https://app.example.com`), for the\n * redirect URI. Default: the origin of each request, which is right unless\n * a proxy hides it.\n */\n baseURL?: string\n /** Where a person lands after signing in, when no `return_to` was asked. Default: `/`. */\n afterSignIn?: string\n /** Where a person lands after signing out. Default: `/`. */\n afterSignOut?: string\n /** Name of the session cookie. Default: `cauthy_session`. */\n cookieName?: string\n /**\n * A key the application proves who it is with when it acts on its own\n * (`auth.system`), instead of its secret. Default: `CAUTHY_SYSTEM_KEY` and\n * `CAUTHY_SYSTEM_KEY_ID`.\n */\n systemKey?: import('./system').SystemKey\n /** `fetch` to reach Cauthy with. Default: the global one. */\n fetch?: typeof fetch\n}\n\n/** The person signed in. Every moment is UTC. */\nexport interface SessionUser {\n id: string\n email: string\n name: string\n emailVerified: boolean\n /**\n * True when the account ran out of time to confirm its address: let it read\n * and refuse its writes.\n */\n restricted: boolean\n /** When an unconfirmed address stops being enough (ISO 8601, UTC), if there is a deadline. */\n emailVerifyBy?: string\n /** The language the person reads in (\"es\", \"en-US\"), when Cauthy knows it. */\n locale?: string\n}\n\n/** A value JSON can carry. What crosses from server to browser is typed as this, never `unknown`. */\nexport type JsonValue = string | number | boolean | null | JsonValue[] | { [key: string]: JsonValue }\n\n/** An organization the person belongs to, with their role in it. */\nexport interface SessionOrganization {\n id: string\n system: string\n name: string\n kind: string\n externalId?: string\n role: string\n /** What the system keeps about the organization, as it filed it in Cauthy. */\n metadata: Record<string, JsonValue>\n /**\n * What the person may do here, as \"resource:action\" entries (\"*\" is any):\n * their role's permissions, what it inherits, and anything given to them\n * apart from it. Absent when the system keeps no catalog of roles in Cauthy.\n * Check with `can(organization, resource, action)`.\n */\n permissions?: string[]\n /** What the person is denied here, whatever else allows it. */\n denied?: string[]\n}\n\n/** A signed-in person, as the server sees them. */\nexport interface Session {\n user: SessionUser\n /** The Cauthy tenant the account is of. */\n tenantId: string\n /** The roles the account holds in that tenant. */\n roles: string[]\n /**\n * Cauthy's access token for this person: send it as `Authorization: Bearer`\n * to another service that verifies Cauthy's tokens. Never give it to the browser.\n */\n accessToken: string\n /** When the access token expires and the session is checked with Cauthy again (ISO 8601, UTC). */\n expiresAt: string\n}\n\n/** What the browser is told of a session: who, and their organizations — never a token. */\nexport interface PublicSession {\n user: SessionUser\n tenantId: string\n roles: string[]\n organizations: SessionOrganization[]\n expiresAt: string\n}\n\n/** Why there is no session. */\nexport type SessionStatus =\n | 'authenticated'\n | 'unauthenticated' // nobody is signed in\n | 'expired' // signed in, but the session must be renewed first (see `Cauthy.resolve`)\n\nexport class CauthyConfigError extends Error {\n constructor(message: string) {\n super(`cauthy-auth: ${message}`)\n this.name = 'CauthyConfigError'\n }\n}\n","// Hearing from Cauthy: verifying a webhook delivery.\n//\n// Cauthy signs every delivery with its own key — the one its tokens are\n// signed with, published at /.well-known/jwks.json — so there is no secret to\n// receive, store or rotate. Verifying is one call:\n//\n// const event = await auth.webhook(request) // throws if it is not Cauthy's\n//\n// What is checked: the signature, against Cauthy's published keys; that the\n// delivery was meant for THIS endpoint (a signature anyone can verify must say\n// who it is for); that it is recent; and that the body is, byte for byte, the\n// one that was signed.\n\nimport { createRemoteJWKSet, jwtVerify, type JWTVerifyGetKey } from 'jose'\nimport type { JsonValue } from './types'\n\n/** Something that happened in Cauthy, as a subscriber is told. Moments are UTC. */\nexport interface CauthyEvent {\n /** This delivery's id: the same event delivered again carries the same one. */\n id: string\n /** What happened: \"organization.member_added\". */\n type: string\n tenantId: string\n createdAt: string\n /** The person it happened to, when it is about one. */\n userId?: string\n /** Who did it. */\n actorId?: string\n /** What the event says: for an organization, its id, system, external_id, role… */\n data: Record<string, JsonValue>\n}\n\nexport class CauthyWebhookError extends Error {\n constructor(readonly reason: string) {\n super(`cauthy-auth: webhook refused: ${reason}`)\n this.name = 'CauthyWebhookError'\n }\n}\n\nexport interface VerifyWebhookOptions {\n /** Cauthy's address. Default: `CAUTHY_ISSUER`. */\n issuer?: string\n /**\n * This endpoint's own public address, exactly as it is registered in Cauthy.\n * Default: the request's URL, which is right unless a proxy changes it.\n */\n url?: string\n /** Cauthy's keys. Default: fetched from the issuer and cached. */\n jwks?: JWTVerifyGetKey\n /** The moment to verify at. Default: now. */\n now?: Date\n}\n\nconst keys = new Map<string, JWTVerifyGetKey>()\n\nfunction keysOf(issuer: string): JWTVerifyGetKey {\n let set = keys.get(issuer)\n if (set === undefined) {\n set = createRemoteJWKSet(new URL(`${issuer}/.well-known/jwks.json`), { cooldownDuration: 30_000 })\n keys.set(issuer, set)\n }\n return set\n}\n\nconst hex = (s: unknown) => (typeof s === 'string' && s !== '' ? s : undefined)\n\n/**\n * Verifies a delivery and answers the event it carries. Throws\n * `CauthyWebhookError` for anything that is not a delivery Cauthy made for\n * this endpoint just now: answer it 401 and nothing else.\n */\nexport async function verifyWebhook(request: Request, options: VerifyWebhookOptions = {}): Promise<CauthyEvent> {\n const env = typeof process !== 'undefined' ? process.env?.CAUTHY_ISSUER : undefined\n const issuer = (options.issuer ?? env ?? '').replace(/\\/+$/, '')\n if (issuer === '') throw new CauthyWebhookError('issuer (CAUTHY_ISSUER) is not set')\n const signature = request.headers.get('x-cauthy-signature-jwt')\n if (!signature) throw new CauthyWebhookError('no signature')\n\n // The bytes as they arrived: what was signed is their hash, not a re-encoding.\n const raw = new Uint8Array(await request.arrayBuffer())\n const audience = options.url ?? request.url.split('?')[0]\n\n let claims: Record<string, unknown>\n try {\n const verified = await jwtVerify(signature, options.jwks ?? keysOf(issuer), {\n algorithms: ['RS256'],\n issuer,\n audience,\n typ: 'cauthy-webhook+jwt',\n requiredClaims: ['exp', 'iat', 'jti', 'body_sha256'],\n currentDate: options.now,\n clockTolerance: 30,\n })\n claims = verified.payload as Record<string, unknown>\n } catch (err) {\n throw new CauthyWebhookError(err instanceof Error ? err.message : 'bad signature')\n }\n\n // Not from the future either: a clock that far off is not Cauthy's.\n const now = Math.floor((options.now ?? new Date()).getTime() / 1000)\n if (typeof claims.iat !== 'number' || claims.iat > now + 30) throw new CauthyWebhookError('signed in the future')\n\n const digest = new Uint8Array(await crypto.subtle.digest('SHA-256', raw))\n let b64 = ''\n for (const b of digest) b64 += String.fromCharCode(b)\n const hash = btoa(b64).replace(/\\+/g, '-').replace(/\\//g, '_').replace(/=+$/, '')\n if (hash !== claims.body_sha256) throw new CauthyWebhookError('the body is not the one that was signed')\n\n let body: Record<string, any>\n try {\n body = JSON.parse(new TextDecoder().decode(raw))\n } catch {\n throw new CauthyWebhookError('the body is not JSON')\n }\n // The envelope and the signature must tell the same story.\n if (body.id !== claims.jti || body.event !== claims.evt || body.tenant_id !== claims.tid) {\n throw new CauthyWebhookError('the body and its signature disagree')\n }\n const inner = (body.data ?? {}) as Record<string, any>\n return {\n id: String(body.id),\n type: String(body.event),\n tenantId: String(body.tenant_id),\n createdAt: String(inner.created_at ?? body.created_at ?? ''),\n ...(hex(inner.user_id) ? { userId: inner.user_id } : {}),\n ...(hex(inner.actor_id) ? { actorId: inner.actor_id } : {}),\n data: (inner.data ?? {}) as Record<string, JsonValue>,\n }\n}\n","// Sign in with Cauthy, held by the application's own server.\n//\n// The application mounts one route (`handler`) and asks who is there\n// (`getSession`). Signing in, signing up, second factors, passkeys, recovering\n// a password and the account page are Cauthy's pages: this sends the browser\n// to them and keeps what comes back in an encrypted, httpOnly cookie. There is\n// no database and no screen to build.\n//\n// The session is Cauthy's short-lived access token plus the refresh token that\n// renews it. When the access token expires the session is renewed with Cauthy,\n// which is where an account disabled there, or signed out everywhere, stops\n// being signed in here — within minutes, with nothing to synchronise.\n\nimport { clearParts, readCookies, readParts, writeCookie, writeParts, type CookieAttributes } from './cookies'\nimport { keyOf, seal, unseal } from './seal'\nimport { createSystem, type CauthySystem } from './system'\nimport { verifyWebhook, type CauthyEvent, type VerifyWebhookOptions } from './webhook'\nimport {\n CauthyConfigError,\n type CauthyOptions,\n type JsonValue,\n type PublicSession,\n type Session,\n type SessionOrganization,\n type SessionStatus,\n type SessionUser,\n} from './types'\n\n/** What the session cookie holds. Short names: it rides every request. */\ninterface Stored {\n at: string // access token\n rt: string // refresh token\n exp: number // when the access token expires, epoch seconds\n u: SessionUser\n tid: string\n roles: string[]\n}\n\n/** What the sign-in in progress holds, between leaving for Cauthy and coming back. */\ninterface Flow {\n state: string\n verifier: string\n returnTo: string\n}\n\nconst SESSION_DAYS = 30\nconst FLOW_SECONDS = 600\n/** Renew this long before the access token runs out, so a request never carries one about to. */\nconst SKEW_SECONDS = 30\nconst ORGANIZATIONS_TTL_MS = 60_000\n\n/** What `resolve` answers: who is there, and the cookies that must go back. */\nexport interface Resolved {\n session: Session | null\n status: SessionStatus\n /** `Set-Cookie` values to send with the response. Empty when nothing changed. */\n cookies: string[]\n}\n\nexport interface Cauthy {\n /** Serves the routes under `basePath`: login, callback, session, refresh, logout, account. */\n handler(request: Request): Promise<Response>\n /**\n * Who is signed in, or null.\n *\n * Give it the response's headers and a session that needs renewing is\n * renewed on the spot, its new cookie added to them. Without them an\n * expired session reads as null (see `status`): renewing changes the\n * cookie, and a cookie nobody sends back is a session lost.\n */\n getSession(request: Request, responseHeaders?: Headers): Promise<Session | null>\n /** Whether somebody is signed in, nobody is, or the session must be renewed first. */\n status(request: Request): Promise<SessionStatus>\n /**\n * The session, renewed when it has to be, and the cookies to send back.\n * The caller MUST send `cookies` with its response.\n */\n resolve(request: Request): Promise<Resolved>\n /**\n * For a route only a signed-in person may see: the session, or the response\n * to give instead — a redirect to sign in (or to renew) for a page, a 401\n * for anything else.\n */\n protect(request: Request): Promise<{ session: Session; response?: undefined } | { session?: undefined; response: Response }>\n /** The organizations the person belongs to (of `system`, when set), with their role in each. */\n organizations(session: Session): Promise<SessionOrganization[]>\n /**\n * Verifies a webhook delivery from Cauthy and answers the event it carries;\n * throws `CauthyWebhookError` for anything else. No secret is involved: the\n * delivery is signed with Cauthy's own key.\n */\n webhook(request: Request, options?: Pick<VerifyWebhookOptions, 'jwks' | 'now' | 'url'>): Promise<CauthyEvent>\n /**\n * The application acting on its own, with no person's token: its\n * organizations, their members, invitations. Needs the client's secret, or\n * `systemKey`.\n */\n system: CauthySystem\n /** What the browser may be told of a session. */\n toPublic(session: Session): Promise<PublicSession>\n /** Paths of this application that start a sign-in, a sign-out, or open the account page. */\n urls: { signIn(returnTo?: string): string; signOut(): string; account(returnTo?: string): string; session: string }\n options: Readonly<Required<Pick<CauthyOptions, 'issuer' | 'clientId' | 'basePath' | 'afterSignIn' | 'afterSignOut' | 'cookieName'>>>\n}\n\nfunction env(name: string): string | undefined {\n return typeof process !== 'undefined' ? process.env?.[name] : undefined\n}\n\nfunction b64url(bytes: Uint8Array): string {\n let s = ''\n for (const b of bytes) s += String.fromCharCode(b)\n return btoa(s).replace(/\\+/g, '-').replace(/\\//g, '_').replace(/=+$/, '')\n}\n\nfunction random(bytes = 32): string {\n return b64url(crypto.getRandomValues(new Uint8Array(bytes)))\n}\n\n/** A path of this application, and nothing else: never another site, never `//host`. */\nfunction localPath(candidate: string | null | undefined, fallback: string): string {\n if (!candidate || !candidate.startsWith('/') || candidate.startsWith('//') || candidate.includes('\\\\')) return fallback\n return candidate\n}\n\nfunction json(body: unknown, status = 200, headers?: Headers): Response {\n const h = headers ?? new Headers()\n h.set('content-type', 'application/json; charset=utf-8')\n h.set('cache-control', 'no-store')\n return new Response(JSON.stringify(body), { status, headers: h })\n}\n\nfunction redirect(location: string, cookies: string[] = []): Response {\n const h = new Headers({ location, 'cache-control': 'no-store' })\n for (const c of cookies) h.append('set-cookie', c)\n return new Response(null, { status: 302, headers: h })\n}\n\nexport function createCauthy(given: CauthyOptions = {}): Cauthy {\n const issuer = (given.issuer ?? env('CAUTHY_ISSUER') ?? '').replace(/\\/+$/, '')\n const clientId = given.clientId ?? env('CAUTHY_CLIENT_ID') ?? ''\n const clientSecret = given.clientSecret ?? env('CAUTHY_CLIENT_SECRET')\n const secret = given.secret ?? env('CAUTHY_SECRET') ?? ''\n const system = given.system ?? env('CAUTHY_SYSTEM') ?? ''\n if (!/^https?:\\/\\//.test(issuer)) throw new CauthyConfigError('issuer (CAUTHY_ISSUER) must be Cauthy\\'s address, e.g. https://id.example.com')\n if (clientId === '') throw new CauthyConfigError('clientId (CAUTHY_CLIENT_ID) is required')\n if (secret.length < 32) throw new CauthyConfigError('secret (CAUTHY_SECRET) must be at least 32 characters: it encrypts the session cookie')\n\n const basePath = '/' + (given.basePath ?? '/api/auth').replace(/^\\/+|\\/+$/g, '')\n const afterSignIn = localPath(given.afterSignIn, '/')\n const afterSignOut = localPath(given.afterSignOut, '/')\n const cookieName = given.cookieName ?? 'cauthy_session'\n const flowCookie = `${cookieName}_flow`\n const doFetch: typeof fetch = (...args) => (given.fetch ?? fetch)(...args)\n const key = keyOf(secret)\n\n const originOf = (request: Request) => (given.baseURL ?? new URL(request.url).origin).replace(/\\/+$/, '')\n const attrs = (request: Request, maxAge: number): CookieAttributes => ({ secure: originOf(request).startsWith('https://'), maxAge })\n\n // ── Talking to Cauthy ────────────────────────────────────────────────────\n\n async function token(form: Record<string, string>): Promise<{ ok: true; body: any } | { ok: false; ended: boolean }> {\n const body = new URLSearchParams({ client_id: clientId, ...form })\n if (clientSecret) body.set('client_secret', clientSecret)\n try {\n const res = await doFetch(`${issuer}/v1/oauth/token`, {\n method: 'POST',\n headers: { 'content-type': 'application/x-www-form-urlencoded', accept: 'application/json' },\n body,\n })\n if (res.ok) return { ok: true, body: await res.json() }\n // 4xx is Cauthy's answer: the grant is no good. Anything else is a\n // failure to ask, and says nothing about the session.\n return { ok: false, ended: res.status >= 400 && res.status < 500 }\n } catch {\n return { ok: false, ended: false }\n }\n }\n\n async function userinfo(accessToken: string): Promise<{ u: SessionUser; tid: string; roles: string[] } | null> {\n try {\n const res = await doFetch(`${issuer}/v1/oauth/userinfo`, { headers: { authorization: `Bearer ${accessToken}` } })\n if (!res.ok) return null\n const b = (await res.json()) as Record<string, any>\n return {\n tid: String(b.tid ?? ''),\n roles: Array.isArray(b.roles) ? b.roles.filter((r: unknown) => typeof r === 'string') : [],\n u: {\n id: String(b.sub ?? ''),\n email: String(b.email ?? ''),\n name: String(b.name ?? ''),\n emailVerified: b.email_verified === true,\n restricted: b.restricted === true,\n ...(typeof b.email_verify_by === 'string' ? { emailVerifyBy: b.email_verify_by } : {}),\n ...(typeof b.locale === 'string' && b.locale !== '' ? { locale: b.locale } : {}),\n },\n }\n } catch {\n return null\n }\n }\n\n /** What Cauthy's answer to a token request becomes in the cookie. */\n async function stored(body: any): Promise<Stored | null> {\n if (typeof body?.access_token !== 'string' || typeof body?.refresh_token !== 'string') return null\n const who = await userinfo(body.access_token)\n if (who === null || who.u.id === '') return null\n const lifetime = typeof body.expires_in === 'number' && body.expires_in > 0 ? body.expires_in : 300\n return { at: body.access_token, rt: body.refresh_token, exp: Math.floor(Date.now() / 1000) + lifetime, ...who }\n }\n\n function toSession(s: Stored): Session {\n return { user: s.u, tenantId: s.tid, roles: s.roles, accessToken: s.at, expiresAt: new Date(s.exp * 1000).toISOString() }\n }\n\n // ── The cookie ───────────────────────────────────────────────────────────\n\n async function read(request: Request): Promise<{ had: Map<string, string>; value: Stored | null; present: boolean }> {\n const had = readCookies(request.headers.get('cookie'))\n const raw = readParts(had, cookieName)\n if (raw === null) return { had, value: null, present: false }\n const value = await unseal<Stored>(raw, await key)\n const sound = value !== null && typeof value.at === 'string' && typeof value.rt === 'string' && typeof value.exp === 'number'\n return { had, value: sound ? value : null, present: true }\n }\n\n async function write(request: Request, s: Stored, had: Map<string, string>): Promise<string[]> {\n return writeParts(cookieName, await seal(s, await key), attrs(request, SESSION_DAYS * 86_400), had)\n }\n\n const fresh = (s: Stored) => s.exp - Date.now() / 1000 > SKEW_SECONDS\n\n // One renewal per refresh token at a time in this process: requests that\n // arrive together share it. (Across processes Cauthy gives a few seconds'\n // grace to the same token, for the same reason.)\n const renewing = new Map<string, Promise<Stored | 'ended' | 'unreachable'>>()\n\n function renew(was: Stored): Promise<Stored | 'ended' | 'unreachable'> {\n let running = renewing.get(was.rt)\n if (running === undefined) {\n const rt = was.rt\n running = (async (): Promise<Stored | 'ended' | 'unreachable'> => {\n const res = await token({ grant_type: 'refresh_token', refresh_token: rt })\n if (!res.ok) return res.ended ? 'ended' : 'unreachable'\n const next = await stored(res.body)\n if (next !== null) return next\n // Renewed, but who it is could not be read just now. The new tokens\n // are kept all the same — the old refresh token is spent, and losing\n // its successor would lose the session — with what was known before.\n if (typeof res.body?.access_token !== 'string' || typeof res.body?.refresh_token !== 'string') return 'unreachable'\n const lifetime = typeof res.body.expires_in === 'number' && res.body.expires_in > 0 ? res.body.expires_in : 300\n return { ...was, at: res.body.access_token, rt: res.body.refresh_token, exp: Math.floor(Date.now() / 1000) + lifetime }\n })()\n renewing.set(rt, running)\n // A renewal that worked is remembered for a moment, for requests still\n // arriving with the old cookie; one that could not ask is tried again\n // by the next request.\n void running.then((result) => {\n if (result === 'unreachable') return renewing.delete(rt)\n const timer = setTimeout(() => renewing.delete(rt), 5_000)\n ;(timer as { unref?: () => void }).unref?.()\n })\n }\n return running\n }\n\n async function resolve(request: Request): Promise<Resolved> {\n const { had, value, present } = await read(request)\n if (value === null) {\n // A cookie this key did not make (a rotated secret, a tampered value) is cleared.\n return { session: null, status: 'unauthenticated', cookies: present ? clearParts(cookieName, attrs(request, 0), had) : [] }\n }\n if (fresh(value)) return { session: toSession(value), status: 'authenticated', cookies: [] }\n\n const renewed = await renew(value)\n if (renewed === 'ended') {\n return { session: null, status: 'unauthenticated', cookies: clearParts(cookieName, attrs(request, 0), had) }\n }\n // Cauthy could not be asked: the cookie stays as it is, and the next\n // request tries again. Nobody is let in on an expired token meanwhile.\n if (renewed === 'unreachable') return { session: null, status: 'expired', cookies: [] }\n return { session: toSession(renewed), status: 'authenticated', cookies: await write(request, renewed, had) }\n }\n\n async function getSession(request: Request, responseHeaders?: Headers): Promise<Session | null> {\n if (responseHeaders === undefined) {\n const { value } = await read(request)\n return value !== null && fresh(value) ? toSession(value) : null\n }\n const r = await resolve(request)\n for (const c of r.cookies) responseHeaders.append('set-cookie', c)\n return r.session\n }\n\n async function status(request: Request): Promise<SessionStatus> {\n const { value } = await read(request)\n if (value === null) return 'unauthenticated'\n return fresh(value) ? 'authenticated' : 'expired'\n }\n\n // ── Organizations ────────────────────────────────────────────────────────\n\n const orgCache = new Map<string, { at: number; value: SessionOrganization[] }>()\n\n async function organizations(session: Session): Promise<SessionOrganization[]> {\n const hit = orgCache.get(session.accessToken)\n if (hit && Date.now() - hit.at < ORGANIZATIONS_TTL_MS) return hit.value\n const url = `${issuer}/v1/oauth/organizations${system ? `?system=${encodeURIComponent(system)}` : ''}`\n const res = await doFetch(url, { headers: { authorization: `Bearer ${session.accessToken}` } })\n if (!res.ok) throw new Error(`cauthy-auth: reading organizations: ${res.status}`)\n const body = (await res.json()) as { data?: Record<string, any>[] }\n const value = (body.data ?? []).map(\n (o): SessionOrganization => ({\n id: String(o.id),\n system: String(o.system ?? ''),\n name: String(o.name ?? ''),\n kind: String(o.kind ?? ''),\n role: String(o.role ?? ''),\n metadata: (o.metadata as Record<string, JsonValue>) ?? {},\n ...(o.external_id ? { externalId: String(o.external_id) } : {}),\n ...(Array.isArray(o.permissions) ? { permissions: o.permissions.map(String), denied: Array.isArray(o.denied) ? o.denied.map(String) : [] } : {}),\n }),\n )\n if (orgCache.size > 5_000) orgCache.clear()\n orgCache.set(session.accessToken, { at: Date.now(), value })\n return value\n }\n\n async function toPublic(session: Session): Promise<PublicSession> {\n let orgs: SessionOrganization[] = []\n try {\n orgs = await organizations(session)\n } catch {\n // The person is signed in all the same; their organizations come back on the next read.\n }\n return { user: session.user, tenantId: session.tenantId, roles: session.roles, organizations: orgs, expiresAt: session.expiresAt }\n }\n\n // ── Routes ───────────────────────────────────────────────────────────────\n\n const urls = {\n signIn: (returnTo?: string) => `${basePath}/login${returnTo ? `?return_to=${encodeURIComponent(returnTo)}` : ''}`,\n signOut: () => `${basePath}/logout`,\n account: (returnTo?: string) => `${basePath}/account${returnTo ? `?return_to=${encodeURIComponent(returnTo)}` : ''}`,\n session: `${basePath}/session`,\n }\n\n async function login(request: Request, url: URL): Promise<Response> {\n const flow: Flow = { state: random(), verifier: random(48), returnTo: localPath(url.searchParams.get('return_to'), afterSignIn) }\n const challenge = b64url(new Uint8Array(await crypto.subtle.digest('SHA-256', new TextEncoder().encode(flow.verifier))))\n const q = new URLSearchParams({\n client_id: clientId,\n redirect_uri: `${originOf(request)}${basePath}/callback`,\n response_type: 'code',\n state: flow.state,\n code_challenge: challenge,\n code_challenge_method: 'S256',\n })\n // What the application may ask of the sign-in page: to ask again, and a language.\n const prompt = url.searchParams.get('prompt')\n if (prompt === 'login' || prompt === 'none') q.set('prompt', prompt)\n const locale = url.searchParams.get('locale') ?? url.searchParams.get('ui_locales')\n if (locale && /^[a-zA-Z-]{2,10}$/.test(locale)) q.set('ui_locales', locale)\n return redirect(`${issuer}/v1/oauth/authorize?${q}`, [\n writeCookie(flowCookie, await seal(flow, await key), attrs(request, FLOW_SECONDS)),\n ])\n }\n\n function failed(request: Request, reason: string): Response {\n const h = new Headers({ 'content-type': 'text/plain; charset=utf-8', 'cache-control': 'no-store' })\n h.append('set-cookie', writeCookie(flowCookie, '', attrs(request, 0)))\n return new Response(`Sign-in could not be completed (${reason}). Go back and try again.`, { status: 400, headers: h })\n }\n\n async function callback(request: Request, url: URL): Promise<Response> {\n const had = readCookies(request.headers.get('cookie'))\n const sealed = had.get(flowCookie)\n const flow = sealed ? await unseal<Flow>(sealed, await key) : null\n // The answer must be to a question THIS browser asked: without that, a\n // stranger's link could sign a person in to the stranger's account.\n if (flow === null || url.searchParams.get('state') !== flow.state) return failed(request, 'state_mismatch')\n const error = url.searchParams.get('error')\n if (error !== null) {\n // prompt=none and nobody signed in: not an error, just \"nobody\".\n if (error === 'login_required') return redirect(flow.returnTo, [writeCookie(flowCookie, '', attrs(request, 0))])\n return failed(request, error.slice(0, 60))\n }\n const code = url.searchParams.get('code')\n if (!code) return failed(request, 'no_code')\n const res = await token({\n grant_type: 'authorization_code',\n code,\n redirect_uri: `${originOf(request)}${basePath}/callback`,\n code_verifier: flow.verifier,\n })\n const s = res.ok ? await stored(res.body) : null\n if (s === null) return failed(request, 'exchange_failed')\n return redirect(flow.returnTo, [...(await write(request, s, had)), writeCookie(flowCookie, '', attrs(request, 0))])\n }\n\n async function logout(request: Request): Promise<Response> {\n const { had, value } = await read(request)\n if (value !== null) {\n // Best effort: the session is over here whether or not Cauthy heard.\n const body = new URLSearchParams({ client_id: clientId, token: value.rt })\n if (clientSecret) body.set('client_secret', clientSecret)\n await doFetch(`${issuer}/v1/oauth/revoke`, {\n method: 'POST',\n headers: { 'content-type': 'application/x-www-form-urlencoded' },\n body,\n }).catch(() => undefined)\n orgCache.delete(value.at)\n }\n // Then through Cauthy, which ends its own session in this browser too:\n // otherwise the next \"sign in\" would walk straight back in.\n const q = new URLSearchParams({ client_id: clientId, post_logout_redirect_uri: `${originOf(request)}${afterSignOut}` })\n return redirect(`${issuer}/v1/oauth/logout?${q}`, clearParts(cookieName, attrs(request, 0), had))\n }\n\n async function handler(request: Request): Promise<Response> {\n const url = new URL(request.url)\n if (url.pathname !== basePath && !url.pathname.startsWith(`${basePath}/`)) return new Response('Not found', { status: 404 })\n const route = url.pathname.slice(basePath.length).replace(/\\/+$/, '') || '/'\n const get = request.method === 'GET' || request.method === 'HEAD'\n\n if (route === '/login' && get) return login(request, url)\n if (route === '/callback' && get) return callback(request, url)\n if (route === '/logout' && (get || request.method === 'POST')) return logout(request)\n\n if (route === '/session' && get) {\n const r = await resolve(request)\n const h = new Headers()\n for (const c of r.cookies) h.append('set-cookie', c)\n return json({ session: r.session ? await toPublic(r.session) : null, status: r.status }, 200, h)\n }\n\n // For a page that found its session expired: renew, and go back to it.\n if (route === '/refresh' && get) {\n const r = await resolve(request)\n const back = localPath(url.searchParams.get('return_to'), afterSignIn)\n if (r.status === 'unauthenticated') return redirect(urls.signIn(back), r.cookies)\n if (r.status === 'expired') return new Response('Sign-in is unavailable right now. Try again in a moment.', { status: 503 })\n return redirect(back, r.cookies)\n }\n\n if (route === '/account' && get) {\n const q = new URLSearchParams({ client_id: clientId })\n const back = localPath(url.searchParams.get('return_to'), '')\n if (back !== '') q.set('return_to', `${originOf(request)}${back}`)\n const locale = url.searchParams.get('locale')\n if (locale && /^[a-zA-Z-]{2,10}$/.test(locale)) q.set('ui_locales', locale)\n return redirect(`${issuer}/v1/oauth/account?${q}`)\n }\n return new Response('Not found', { status: 404 })\n }\n\n async function protect(request: Request) {\n const { value } = await read(request)\n if (value !== null && fresh(value)) return { session: toSession(value) }\n const url = new URL(request.url)\n const state = value === null ? 'none' : 'expired'\n // A page is sent to sign in, or to renew and come back. Anything else —\n // a fetch, a form post — is told, and its caller decides.\n const page = request.method === 'GET' && (request.headers.get('sec-fetch-mode') ?? 'navigate') === 'navigate' &&\n (request.headers.get('accept') ?? 'text/html').includes('text/html')\n if (!page) {\n return { response: json({ error: 'unauthenticated', session: state }, 401, new Headers({ 'x-cauthy-session': state })) }\n }\n const back = url.pathname + url.search\n const to = value === null ? urls.signIn(back) : `${basePath}/refresh?return_to=${encodeURIComponent(back)}`\n return { response: redirect(to) }\n }\n\n // The endpoint's public address: the application's origin when it was\n // given (a proxy may hide it from the request), with the request's path.\n const webhook: Cauthy['webhook'] = (request, o = {}) =>\n verifyWebhook(request, { issuer, url: o.url ?? `${originOf(request)}${new URL(request.url).pathname}`, jwks: o.jwks, now: o.now })\n\n return {\n handler,\n webhook,\n system: createSystem({ issuer, clientId, clientSecret, key: given.systemKey, fetch: given.fetch }),\n getSession,\n status,\n resolve,\n protect,\n organizations,\n toPublic,\n urls,\n options: { issuer, clientId, basePath, afterSignIn, afterSignOut, cookieName },\n }\n}\n","// An application's look, for Cauthy's hosted pages.\n//\n// Cauthy's pages — sign-in, sign-up, the account page — can take the colours,\n// corners, typeface and logo of the application that sends its people there,\n// so signing in reads as a page of the application. A theme is a short list of\n// values, which Cauthy checks one by one; it is never CSS.\n//\n// An application built with Stackmyth already has those values, as its\n// `--sm-*` tokens. `themeFromStackmyth` reads them out of its stylesheets.\n\n/** How an application looks on Cauthy's hosted pages. Every value is optional. */\nexport interface CauthyTheme {\n /** The page. Colours are `#rrggbb` or `rgba(r, g, b, a)`. */\n background?: string\n /** The card, and inputs. */\n surface?: string\n /** Headings and what is typed. */\n text?: string\n /** Paragraphs, labels, small print. */\n textMuted?: string\n border?: string\n /** The main button, and what is written on it. */\n primary?: string\n primaryText?: string\n /** Links, and the focus ring. */\n accent?: string\n error?: string\n errorBackground?: string\n success?: string\n successBackground?: string\n /** Corners of cards and inputs, in pixels (0–40). */\n radius?: number\n /** Corners of buttons, in pixels (0–40). */\n buttonRadius?: number\n /** A CSS font-family list. Cauthy loads no font files: put a system font after the application's own. */\n fontFamily?: string\n /** An image shown where \"Cauthy\" is, over https. */\n logoUrl?: string\n}\n\nexport interface StackmythThemeOptions {\n /**\n * The named theme to read, when the stylesheet keeps several under\n * `[data-theme=…]` (`\"vennet\"`). Without it only what applies to every\n * page (`:root`) is read.\n */\n theme?: string\n /** Which of the application's two modes to take. Default: `light`. */\n mode?: 'light' | 'dark'\n logoUrl?: string\n}\n\nexport interface ImportedTheme {\n theme: CauthyTheme\n /** Tokens that were there and could not be carried over, and why. */\n warnings: string[]\n}\n\nconst MAX_RADIUS = 40\n\n// ── Reading custom properties out of CSS ───────────────────────────────────\n\ninterface Declaration {\n name: string\n value: string\n /** The selectors and at-rules it sits inside, outermost first. */\n within: string[]\n}\n\nfunction declarations(css: string): Declaration[] {\n const text = css.replace(/\\/\\*[\\s\\S]*?\\*\\//g, '')\n const out: Declaration[] = []\n const within: string[] = []\n let chunk = ''\n let quote = ''\n let depth = 0 // parentheses, so a `;` inside url(…) does not end anything\n const flush = () => {\n const m = /^\\s*(--[\\w-]+)\\s*:\\s*([\\s\\S]+?)\\s*$/.exec(chunk)\n if (m) out.push({ name: m[1], value: m[2].replace(/\\s+/g, ' ').replace(/\\s*!important$/, ''), within: [...within] })\n chunk = ''\n }\n for (const ch of text) {\n if (quote) {\n chunk += ch\n if (ch === quote) quote = ''\n continue\n }\n if (ch === '\"' || ch === \"'\") {\n quote = ch\n chunk += ch\n } else if (ch === '(') {\n depth++\n chunk += ch\n } else if (ch === ')') {\n depth = Math.max(0, depth - 1)\n chunk += ch\n } else if (ch === '{' && depth === 0) {\n within.push(chunk.trim().replace(/\\s+/g, ' '))\n chunk = ''\n } else if (ch === '}' && depth === 0) {\n flush()\n within.pop()\n } else if (ch === ';' && depth === 0) {\n flush()\n } else {\n chunk += ch\n }\n }\n return out\n}\n\nconst DARK = /prefers-color-scheme:\\s*dark|data-(mode|theme|color-scheme)=[\"']?dark|\\.dark\\b/i\nconst LIGHT = /prefers-color-scheme:\\s*light|data-(mode|theme|color-scheme)=[\"']?light|\\.light\\b/i\nconst NAMED = /data-theme=[\"']?([\\w-]+)/i\n\n/** Whether a declaration applies to the page being themed. */\nfunction applies(d: Declaration, o: Required<Pick<StackmythThemeOptions, 'mode'>> & { theme?: string }): boolean {\n for (const scope of d.within) {\n if (o.mode === 'light' && DARK.test(scope)) return false\n if (o.mode === 'dark' && LIGHT.test(scope)) return false\n const named = NAMED.exec(scope)\n if (named && !/^(dark|light)$/i.test(named[1]) && named[1] !== o.theme) return false\n // Component-level rules (`.btn { --sm-… }`) are not the page's theme.\n if (!scope.startsWith('@') && !/:root|^html\\b|^body\\b|\\[data-|^\\*$|:host/.test(scope)) return false\n }\n return true\n}\n\n// ── Values ─────────────────────────────────────────────────────────────────\n\nfunction resolve(value: string, vars: Map<string, string>, seen = 0): string | null {\n if (seen > 16) return null\n let unresolved = false\n // Innermost var() first: fallbacks may hold another var().\n let out = value\n for (let guard = 0; guard < 32 && /var\\(/.test(out); guard++) {\n out = out.replace(/var\\(\\s*(--[\\w-]+)\\s*(?:,\\s*([^()]*(?:\\([^()]*\\)[^()]*)*))?\\)/, (_m, name: string, fallback?: string) => {\n const known = vars.get(name)\n const next = known !== undefined ? resolve(known, vars, seen + 1) : fallback !== undefined ? resolve(fallback.trim(), vars, seen + 1) : null\n if (next === null) unresolved = true\n return next ?? ''\n })\n if (unresolved) return null\n }\n return /var\\(/.test(out) ? null : out.trim()\n}\n\nconst clamp01 = (n: number) => Math.min(1, Math.max(0, n))\nconst hex2 = (n: number) => Math.round(clamp01(n) * 255).toString(16).padStart(2, '0')\nconst gamma = (c: number) => (c <= 0.0031308 ? 12.92 * c : 1.055 * Math.pow(c, 1 / 2.4) - 0.055)\n\nfunction out(r: number, g: number, b: number, a = 1): string {\n if (a >= 1) return `#${hex2(r)}${hex2(g)}${hex2(b)}`\n return `rgba(${Math.round(clamp01(r) * 255)}, ${Math.round(clamp01(g) * 255)}, ${Math.round(clamp01(b) * 255)}, ${Math.round(clamp01(a) * 1000) / 1000})`\n}\n\nconst alpha = (s: string | undefined) => (s === undefined ? 1 : s.endsWith('%') ? parseFloat(s) / 100 : parseFloat(s))\n\nconst KEYWORDS: Record<string, string> = { white: '#ffffff', black: '#000000' }\n\n/** A colour as Cauthy takes it — `#rrggbb` or `rgba(…)` — or null when it is none it can read. */\nexport function toColor(value: string): string | null {\n const v = value.trim().toLowerCase()\n if (KEYWORDS[v]) return KEYWORDS[v]\n if (/^#([0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})$/.test(v)) return v\n let m = /^rgba?\\(\\s*([\\d.]+%?)[\\s,]+([\\d.]+%?)[\\s,]+([\\d.]+%?)(?:\\s*[,/]\\s*([\\d.]+%?))?\\s*\\)$/.exec(v)\n if (m) {\n const ch = (s: string) => (s.endsWith('%') ? parseFloat(s) / 100 : parseFloat(s) / 255)\n return out(ch(m[1]), ch(m[2]), ch(m[3]), alpha(m[4]))\n }\n m = /^hsla?\\(\\s*([\\d.]+)(?:deg)?[\\s,]+([\\d.]+)%[\\s,]+([\\d.]+)%(?:\\s*[,/]\\s*([\\d.]+%?))?\\s*\\)$/.exec(v)\n if (m) {\n const h = ((parseFloat(m[1]) % 360) + 360) % 360\n const s = parseFloat(m[2]) / 100\n const l = parseFloat(m[3]) / 100\n const f = (n: number) => {\n const k = (n + h / 30) % 12\n return l - s * Math.min(l, 1 - l) * Math.max(-1, Math.min(k - 3, 9 - k, 1))\n }\n return out(f(0), f(8), f(4), alpha(m[4]))\n }\n m = /^oklch\\(\\s*([\\d.]+%?)\\s+([\\d.]+%?)\\s+([\\d.]+)(?:deg)?(?:\\s*\\/\\s*([\\d.]+%?))?\\s*\\)$/.exec(v)\n if (m) {\n const L = m[1].endsWith('%') ? parseFloat(m[1]) / 100 : parseFloat(m[1])\n const C = m[2].endsWith('%') ? (parseFloat(m[2]) / 100) * 0.4 : parseFloat(m[2])\n const H = (parseFloat(m[3]) * Math.PI) / 180\n const a = C * Math.cos(H)\n const b = C * Math.sin(H)\n const l = Math.pow(L + 0.3963377774 * a + 0.2158037573 * b, 3)\n const mm = Math.pow(L - 0.1055613458 * a - 0.0638541728 * b, 3)\n const s = Math.pow(L - 0.0894841775 * a - 1.291485548 * b, 3)\n return out(\n gamma(4.0767416621 * l - 3.3077115913 * mm + 0.2309699292 * s),\n gamma(-1.2684380046 * l + 2.6097574011 * mm - 0.3413193965 * s),\n gamma(-0.0041960863 * l - 0.7034186147 * mm + 1.707614701 * s),\n alpha(m[4]),\n )\n }\n return null\n}\n\n/** A length in pixels, within what Cauthy takes, or null. */\nfunction toRadius(value: string): number | null {\n const m = /^([\\d.]+)(px|rem|em)?$/.exec(value.trim())\n if (!m) return null\n const px = parseFloat(m[1]) * (m[2] === 'rem' || m[2] === 'em' ? 16 : 1)\n return Number.isFinite(px) ? Math.min(MAX_RADIUS, Math.max(0, Math.round(px))) : null\n}\n\n/** A font list as Cauthy takes it, or null. */\nfunction toFont(value: string): string | null {\n const v = value.trim()\n return /^[A-Za-z0-9 ,'\"_-]{1,200}$/.test(v) && (v.match(/\"/g)?.length ?? 0) % 2 === 0 && (v.match(/'/g)?.length ?? 0) % 2 === 0 ? v : null\n}\n\n// ── Stackmyth ──────────────────────────────────────────────────────────────\n\n/** Which Stackmyth token each value of a theme is read from, first found wins. */\nconst COLORS: [keyof CauthyTheme, string[]][] = [\n ['background', ['--sm-bg-surface', '--sm-surface']],\n ['surface', ['--sm-bg-surface-raised', '--sm-surface-raised']],\n ['text', ['--sm-text-primary', '--sm-surface-text']],\n ['textMuted', ['--sm-text-secondary']],\n ['border', ['--sm-border-default', '--sm-surface-border']],\n ['primary', ['--sm-primary', '--sm-bg-interactive']],\n ['primaryText', ['--sm-primary-text', '--sm-text-inverse']],\n ['accent', ['--sm-text-brand', '--sm-border-focus', '--sm-accent']],\n ['error', ['--sm-error-soft-text', '--sm-error']],\n ['errorBackground', ['--sm-error-soft']],\n ['success', ['--sm-success-soft-text', '--sm-success']],\n ['successBackground', ['--sm-success-soft']],\n]\n\n/**\n * Reads an application's Stackmyth theme out of its stylesheets.\n *\n * Give it the CSS that declares the tokens, in the order the application\n * loads it — Stackmyth's own (`@stackmyth/core/core.vars.css`, or a theme\n * file) and then the application's brand file that re-points them. Tokens\n * that point at the application's own variables are followed. The\n * application's dark mode is left out unless asked for.\n */\nexport function themeFromStackmyth(css: string | string[], options: StackmythThemeOptions = {}): ImportedTheme {\n const o = { mode: options.mode ?? 'light', theme: options.theme }\n const vars = new Map<string, string>()\n for (const sheet of Array.isArray(css) ? css : [css]) {\n for (const d of declarations(sheet)) if (applies(d, o)) vars.set(d.name, d.value)\n }\n const theme: CauthyTheme = {}\n const warnings: string[] = []\n\n const read = <T>(tokens: string[], to: (v: string) => T | null, what: string): T | undefined => {\n for (const token of tokens) {\n const raw = vars.get(token)\n if (raw === undefined) continue\n const value = resolve(raw, vars)\n const got = value === null ? null : to(value)\n if (got !== null) return got\n warnings.push(`${token}: \"${raw}\" is not ${what} Cauthy can take`)\n }\n return undefined\n }\n\n for (const [field, tokens] of COLORS) {\n const c = read(tokens, toColor, 'a colour')\n if (c !== undefined) (theme as Record<string, unknown>)[field] = c\n }\n const radius = read(['--sm-radius-container', '--sm-radius-md'], toRadius, 'a length')\n if (radius !== undefined) theme.radius = radius\n const button = read(['--sm-radius-interactive', '--sm-radius-sm'], toRadius, 'a length')\n if (button !== undefined) theme.buttonRadius = button\n const font = read(['--sm-font-family'], toFont, 'a font list')\n if (font !== undefined) theme.fontFamily = font\n if (options.logoUrl) theme.logoUrl = options.logoUrl\n\n if (vars.size === 0) warnings.push('no custom properties were found: is this the stylesheet that declares the --sm-* tokens?')\n else if (![...vars.keys()].some((k) => k.startsWith('--sm-'))) warnings.push('no --sm-* token was found in these stylesheets')\n return { theme, warnings }\n}\n\n// ── Sending it to Cauthy ───────────────────────────────────────────────────\n\n/** A theme as Cauthy's API names its values. */\nexport function toWire(theme: CauthyTheme): Record<string, string | number> {\n const names: Record<keyof CauthyTheme, string> = {\n background: 'background', surface: 'surface', text: 'text', textMuted: 'text_muted', border: 'border',\n primary: 'primary', primaryText: 'primary_text', accent: 'accent', error: 'error', errorBackground: 'error_background',\n success: 'success', successBackground: 'success_background', radius: 'radius', buttonRadius: 'button_radius',\n fontFamily: 'font_family', logoUrl: 'logo_url',\n }\n const wire: Record<string, string | number> = {}\n for (const [k, v] of Object.entries(theme)) if (v !== undefined && v !== '') wire[names[k as keyof CauthyTheme]] = v as string | number\n return wire\n}\n\nexport interface PushThemeOptions {\n /** Cauthy's address. Default: `CAUTHY_ISSUER`. */\n issuer?: string\n /** The application's client id. Default: `CAUTHY_CLIENT_ID`. */\n clientId?: string\n /** The workspace's slug. Default: `CAUTHY_TENANT`. */\n tenant?: string\n /**\n * A personal access token with `oauth_clients:manage`. Default:\n * `CAUTHY_TOKEN`. It belongs in the deploy pipeline, never in the application.\n */\n token?: string\n theme: CauthyTheme\n fetch?: typeof fetch\n}\n\n/**\n * Gives the application its look on Cauthy's pages. It replaces the theme: a\n * value left out goes back to Cauthy's own. Run it when the brand changes —\n * from a script or the deploy — not on every request.\n */\nexport async function pushTheme(o: PushThemeOptions): Promise<void> {\n const env = (n: string) => (typeof process !== 'undefined' ? process.env?.[n] : undefined)\n const issuer = (o.issuer ?? env('CAUTHY_ISSUER') ?? '').replace(/\\/+$/, '')\n const clientId = o.clientId ?? env('CAUTHY_CLIENT_ID') ?? ''\n const tenant = o.tenant ?? env('CAUTHY_TENANT') ?? ''\n const token = o.token ?? env('CAUTHY_TOKEN') ?? ''\n for (const [name, v] of Object.entries({ CAUTHY_ISSUER: issuer, CAUTHY_CLIENT_ID: clientId, CAUTHY_TENANT: tenant, CAUTHY_TOKEN: token })) {\n if (v === '') throw new Error(`cauthy-auth: ${name} is required to push a theme`)\n }\n const res = await (o.fetch ?? fetch)(`${issuer}/v1/oauth/clients/${encodeURIComponent(clientId)}/theme`, {\n method: 'PUT',\n headers: { authorization: `Bearer ${token}`, 'x-tenant-key': tenant, 'content-type': 'application/json' },\n body: JSON.stringify(toWire(o.theme)),\n })\n if (!res.ok) {\n const body = (await res.json().catch(() => null)) as { errors?: { message?: string }[] } | null\n throw new Error(`cauthy-auth: Cauthy refused the theme (${res.status}): ${body?.errors?.[0]?.message ?? res.statusText}`)\n }\n}\n","// What a person may do in an organization.\n//\n// When a system keeps its catalog of roles in Cauthy, each of a person's\n// organizations arrives with what their role — and anything given or denied to\n// them apart from it — lets them do. Checking is a lookup in that list: no\n// call to Cauthy, on the server or in the browser.\n\n/** The part of an organization that says what its member may do. */\nexport interface Permitted {\n /** \"resource:action\" entries the person is allowed; \"*\" is any. Absent when the system keeps no catalog in Cauthy. */\n permissions?: string[]\n /** Entries the person is denied whatever else allows them. */\n denied?: string[]\n}\n\nfunction matches(entry: string, resource: string, action: string): boolean {\n // The resource may itself hold a colon: the action is what follows the last one.\n const i = entry.lastIndexOf(':')\n if (i < 0) return false\n const r = entry.slice(0, i)\n const a = entry.slice(i + 1)\n return (r === '*' || r === resource) && (a === '*' || a === action)\n}\n\n/**\n * Whether the person may do an action on a resource in an organization:\n * allowed by some entry, and denied by none. A denial wins.\n *\n * An organization of a system that keeps no catalog in Cauthy carries no\n * permissions, and the answer is `false`: decide by its `role` instead.\n *\n * if (!can(organization, 'invoicing', 'write')) return forbidden()\n */\nexport function can(organization: Permitted | null | undefined, resource: string, action: string): boolean {\n if (!organization?.permissions) return false\n if ((organization.denied ?? []).some((d) => matches(d, resource, action))) return false\n return organization.permissions.some((p) => matches(p, resource, action))\n}\n\n// ── A system's catalog of roles ────────────────────────────────────────────\n\n/** Something a role may do: an action on a resource, in organizations of a kind (\"*\", the default, is every kind). */\nexport interface PolicyPermission {\n kind?: string\n resource: string\n action: string\n}\n\nexport interface PolicyRole {\n role: string\n name?: string\n description?: string\n /** Roles whose permissions this one has as well. */\n inherits?: string[]\n permissions?: PolicyPermission[]\n}\n\nexport interface PushPolicyOptions {\n /** The system whose catalog this is. Default: `CAUTHY_SYSTEM`. */\n system?: string\n roles: PolicyRole[]\n /** Cauthy's address. Default: `CAUTHY_ISSUER`. */\n issuer?: string\n /** The workspace's slug. Default: `CAUTHY_TENANT`. */\n tenant?: string\n /** A personal access token with `organizations:manage`. Default: `CAUTHY_TOKEN`. */\n token?: string\n fetch?: typeof fetch\n}\n\n/**\n * Gives Cauthy a system's catalog of roles, replacing the one it has. It is\n * the way to load a first catalog from what a system already had; from then on\n * the catalog is Cauthy's, edited in its dashboard, and pushing again would\n * overwrite what was edited there.\n */\nexport async function pushPolicy(o: PushPolicyOptions): Promise<void> {\n const env = (n: string) => (typeof process !== 'undefined' ? process.env?.[n] : undefined)\n const issuer = (o.issuer ?? env('CAUTHY_ISSUER') ?? '').replace(/\\/+$/, '')\n const system = o.system ?? env('CAUTHY_SYSTEM') ?? ''\n const tenant = o.tenant ?? env('CAUTHY_TENANT') ?? ''\n const token = o.token ?? env('CAUTHY_TOKEN') ?? ''\n for (const [name, v] of Object.entries({ CAUTHY_ISSUER: issuer, CAUTHY_SYSTEM: system, CAUTHY_TENANT: tenant, CAUTHY_TOKEN: token })) {\n if (v === '') throw new Error(`cauthy-auth: ${name} is required to push a policy`)\n }\n const res = await (o.fetch ?? fetch)(`${issuer}/v1/systems/${encodeURIComponent(system)}/policy`, {\n method: 'PUT',\n headers: { authorization: `Bearer ${token}`, 'x-tenant-key': tenant, 'content-type': 'application/json' },\n body: JSON.stringify({ roles: o.roles }),\n })\n if (!res.ok) {\n const body = (await res.json().catch(() => null)) as { errors?: { message?: string }[] } | null\n throw new Error(`cauthy-auth: Cauthy refused the policy (${res.status}): ${body?.errors?.[0]?.message ?? res.statusText}`)\n }\n}\n","// Connecting an application to Cauthy in one call: its client, where its\n// people come back to, where it hears events, and its own identity. It is what\n// `cauthy-auth init` runs, and what Cauthy's dashboard does under \"Connect a\n// system\".\n\nexport interface ConnectOptions {\n /** The system's name, as its organizations are filed under: lowercase, digits, - or _. */\n system: string\n /** The application's public address: `https://app.example.com`, or `http://localhost:3000` while developing. */\n appUrl: string\n /** Shown to people on Cauthy's pages. Default: the system's name. */\n name?: string\n /** Where the application serves the return from a sign-in. Default: `/api/auth/callback`. */\n callbackPath?: string\n /** An https address of the application that receives Cauthy's events. */\n eventsUrl?: string\n /** The events to receive. Default: all of them. */\n events?: string[]\n /** Where the application publishes its keys, to prove itself without a secret. */\n jwksUri?: string\n /** Cauthy's address. Default: `CAUTHY_ISSUER`. */\n issuer?: string\n /** The workspace's slug. Default: `CAUTHY_TENANT`. */\n tenant?: string\n /** A personal access token with `systems:connect`. Default: `CAUTHY_TOKEN`. */\n token?: string\n fetch?: typeof fetch\n}\n\nexport interface Connected {\n system: string\n clientId: string\n /** Shown this once: Cauthy keeps only its hash. */\n clientSecret: string\n redirectUris: string[]\n webhook: { id: string; url: string; events: string[] } | null\n /**\n * Everything the application sets: what Cauthy answered, plus CAUTHY_SECRET,\n * the key of the session cookie, generated here and known to nobody else.\n */\n env: Record<string, string>\n}\n\nfunction sessionSecret(): string {\n const bytes = crypto.getRandomValues(new Uint8Array(32))\n let s = ''\n for (const b of bytes) s += String.fromCharCode(b)\n return btoa(s).replace(/\\+/g, '-').replace(/\\//g, '_').replace(/=+$/, '')\n}\n\nexport async function connectSystem(o: ConnectOptions): Promise<Connected> {\n const env = (n: string) => (typeof process !== 'undefined' ? process.env?.[n] : undefined)\n const issuer = (o.issuer ?? env('CAUTHY_ISSUER') ?? '').replace(/\\/+$/, '')\n const tenant = o.tenant ?? env('CAUTHY_TENANT') ?? ''\n const token = o.token ?? env('CAUTHY_TOKEN') ?? ''\n for (const [name, v] of Object.entries({ CAUTHY_ISSUER: issuer, CAUTHY_TENANT: tenant, CAUTHY_TOKEN: token })) {\n if (v === '') throw new Error(`cauthy-auth: ${name} is required to connect a system`)\n }\n const res = await (o.fetch ?? fetch)(`${issuer}/v1/connect`, {\n method: 'POST',\n headers: { authorization: `Bearer ${token}`, 'x-tenant-key': tenant, 'content-type': 'application/json' },\n body: JSON.stringify({ system: o.system, app_url: o.appUrl, name: o.name, callback_path: o.callbackPath,\n events_url: o.eventsUrl, events: o.events, jwks_uri: o.jwksUri }),\n })\n const body = (await res.json().catch(() => null)) as {\n data?: { system: string; client_secret: string; client: { client_id: string; redirect_uris: string[] }\n webhook: Connected['webhook']; env: Record<string, string> }\n errors?: { field?: string; message?: string }[]\n } | null\n if (!res.ok || !body?.data) {\n const why = (body?.errors ?? []).map((e) => e.message).filter(Boolean).join('; ') || res.statusText\n throw new Error(`cauthy-auth: Cauthy did not connect the system (${res.status}): ${why}`)\n }\n const d = body.data\n return { system: d.system, clientId: d.client.client_id, clientSecret: d.client_secret, redirectUris: d.client.redirect_uris,\n webhook: d.webhook, env: { ...d.env, CAUTHY_SECRET: sessionSecret() } }\n}\n\n/** The variables as the lines of an env file. */\nexport function envFile(env: Record<string, string>): string {\n return Object.entries(env).map(([k, v]) => `${k}=${v}`).join('\\n') + '\\n'\n}\n"],"mappings":";AAKA,IAAM,OAAO;AAEN,SAAS,YAAY,QAAwD;AAClF,QAAMA,OAAM,oBAAI,IAAoB;AACpC,aAAW,SAAS,UAAU,IAAI,MAAM,GAAG,GAAG;AAC5C,UAAM,IAAI,KAAK,QAAQ,GAAG;AAC1B,QAAI,IAAI,EAAG,CAAAA,KAAI,IAAI,KAAK,MAAM,GAAG,CAAC,EAAE,KAAK,GAAG,KAAK,MAAM,IAAI,CAAC,EAAE,KAAK,CAAC;AAAA,EACtE;AACA,SAAOA;AACT;AAGO,SAAS,UAAU,SAA8B,MAA6B;AACnF,MAAI,QAAQ;AACZ,WAAS,IAAI,GAAG,QAAQ,IAAI,GAAG,IAAI,IAAI,CAAC,EAAE,GAAG,IAAK,UAAS,QAAQ,IAAI,GAAG,IAAI,IAAI,CAAC,EAAE;AACrF,SAAO,UAAU,KAAK,OAAO;AAC/B;AAQA,SAAS,WAAW,GAA6B;AAG/C,SAAO,QAAQ,EAAE,QAAQ,GAAG,qCAAqC,EAAE,MAAM,GAAG,EAAE,SAAS,aAAa,EAAE;AACxG;AAEO,SAAS,YAAY,MAAc,OAAe,GAA6B;AACpF,SAAO,GAAG,IAAI,IAAI,KAAK,KAAK,WAAW,CAAC,CAAC;AAC3C;AAMO,SAAS,WAAW,MAAc,OAAe,GAAqB,KAAoC;AAC/G,QAAMA,OAAgB,CAAC;AACvB,MAAI,IAAI;AACR,WAAS,IAAI,GAAG,IAAI,MAAM,QAAQ,KAAK,KAAM,CAAAA,KAAI,KAAK,YAAY,GAAG,IAAI,IAAI,GAAG,IAAI,MAAM,MAAM,GAAG,IAAI,IAAI,GAAG,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AAA,MAAW,QAAQ,EAAE;AAAA,MAAQ,UAAU,EAAE;AAAA,IAAQ,CAAC;AAAA,EACpE,CAAC;AACD,QAAM,OAAQ,MAAM,IAAI,KAAK,EAAE,MAAM,MAAM,IAAI;AAK/C,MAAI,CAAC,IAAI,MAAM,CAAC,MAAM,MAAM;AAC1B,UAAM,OAAO,MAAM,UAAU,CAAC,GAAG,IAAI,CAAC,MAAM,EAAE,OAAO,EAAE,OAAO,OAAO,EAAE,KAAK,IAAI,KAAK,IAAI;AACzF,UAAM,IAAI,MAAM,mDAAmD,IAAI,MAAM,MAAM,GAAG,EAAE;AAAA,EAC1F;AACA,QAAM,IAAI,KAAK;AACf,SAAO;AAAA,IAAE,QAAQ,EAAE;AAAA,IAAQ,UAAU,EAAE,OAAO;AAAA,IAAW,cAAc,EAAE;AAAA,IAAe,cAAc,EAAE,OAAO;AAAA,IAC7G,SAAS,EAAE;AAAA,IAAS,KAAK,EAAE,GAAG,EAAE,KAAK,eAAe,cAAc,EAAE;AAAA,EAAE;AAC1E;","names":["out","out","env","env","resolve","out","env","env","env"]}
|
|
1
|
+
{"version":3,"sources":["../src/cookies.ts","../src/seal.ts","../src/system.ts","../src/theme.ts","../src/types.ts","../src/webhook.ts","../src/cauthy.ts","../src/permissions.ts","../src/connect.ts"],"sourcesContent":["// Cookies, by hand: reading the Cookie header and writing Set-Cookie.\n//\n// A session can outgrow one cookie (browsers stop near 4 KB), so a value is\n// written in numbered parts — name.0, name.1 — and read back in order.\n\nconst PART = 3500\n\nexport function readCookies(header: string | null | undefined): Map<string, string> {\n const out = new Map<string, string>()\n for (const pair of (header ?? '').split(';')) {\n const i = pair.indexOf('=')\n if (i > 0) out.set(pair.slice(0, i).trim(), pair.slice(i + 1).trim())\n }\n return out\n}\n\n/** A value written in parts, or null when the cookie is not there. */\nexport function readParts(cookies: Map<string, string>, name: string): string | null {\n let value = ''\n for (let i = 0; cookies.has(`${name}.${i}`); i++) value += cookies.get(`${name}.${i}`)\n return value === '' ? null : value\n}\n\nexport interface CookieAttributes {\n secure: boolean\n maxAge: number // seconds; 0 deletes\n path?: string\n}\n\nfunction attributes(a: CookieAttributes): string {\n // httpOnly: script never reads it. SameSite=Lax: it rides the navigation\n // that comes back from Cauthy, and no cross-site form post.\n return `Path=${a.path ?? '/'}; HttpOnly; SameSite=Lax; Max-Age=${a.maxAge}${a.secure ? '; Secure' : ''}`\n}\n\nexport function writeCookie(name: string, value: string, a: CookieAttributes): string {\n return `${name}=${value}; ${attributes(a)}`\n}\n\n/**\n * The Set-Cookie headers that store a value in parts and delete whatever\n * parts an earlier, longer value left behind.\n */\nexport function writeParts(name: string, value: string, a: CookieAttributes, had: Map<string, string>): string[] {\n const out: string[] = []\n let n = 0\n for (let i = 0; i < value.length; i += PART) out.push(writeCookie(`${name}.${n++}`, value.slice(i, i + PART), a))\n for (let i = n; had.has(`${name}.${i}`); i++) out.push(writeCookie(`${name}.${i}`, '', { ...a, maxAge: 0 }))\n return out\n}\n\nexport function clearParts(name: string, a: CookieAttributes, had: Map<string, string>): string[] {\n const out: string[] = []\n for (let i = 0; had.has(`${name}.${i}`) || i === 0; i++) out.push(writeCookie(`${name}.${i}`, '', { ...a, maxAge: 0 }))\n return out\n}\n","// The session cookie's envelope: encrypted and authenticated with a key only\n// the server has, so the browser carries the session without being able to\n// read or change it.\n\nimport { CompactEncrypt, compactDecrypt } from 'jose'\n\nconst encoder = new TextEncoder()\nconst decoder = new TextDecoder()\n\n/** The 256-bit key a secret stands for. */\nexport async function keyOf(secret: string): Promise<Uint8Array> {\n return new Uint8Array(await crypto.subtle.digest('SHA-256', encoder.encode(secret)))\n}\n\nexport async function seal(value: unknown, key: Uint8Array): Promise<string> {\n return new CompactEncrypt(encoder.encode(JSON.stringify(value)))\n .setProtectedHeader({ alg: 'dir', enc: 'A256GCM' })\n .encrypt(key)\n}\n\n/** The value sealed, or null for anything that is not a cookie this key made. */\nexport async function unseal<T>(sealed: string, key: Uint8Array): Promise<T | null> {\n try {\n const { plaintext } = await compactDecrypt(sealed, key, {\n keyManagementAlgorithms: ['dir'],\n contentEncryptionAlgorithms: ['A256GCM'],\n })\n return JSON.parse(decoder.decode(plaintext)) as T\n } catch {\n return null\n }\n}\n","// The application acting on its own: creating its organizations, adding their\n// members, inviting people. No person's token is involved — the application\n// is somebody in Cauthy, and what it may touch is its own system's\n// organizations and nothing else.\n//\n// It proves who it is with the client's secret, or with a key it holds and\n// whose public half it publishes (`publicJwks`): then nothing secret is ever\n// shared with Cauthy.\n\nimport { exportJWK, importJWK, importPKCS8, SignJWT, type JWK, type KeyLike } from 'jose'\nimport { fromWire, toWire, type CauthyTheme } from './theme'\nimport { CauthyConfigError, type JsonValue } from './types'\n\nconst ASSERTION_TYPE = 'urn:ietf:params:oauth:client-assertion-type:jwt-bearer'\n/** Ask for a new token this long before the one held runs out. */\nconst SKEW_MS = 30_000\n\n/** The key an application signs its assertions with. */\nexport interface SystemKey {\n /** The private key: PKCS#8 PEM, or a JWK (as an object or its JSON). RSA, for RS256. */\n privateKey: string | JWK\n /** The id the public key is published under (`kid`). */\n keyId: string\n}\n\nexport interface SystemOptions {\n /** Cauthy's address. Default: `CAUTHY_ISSUER`. */\n issuer?: string\n /** The application's client id. Default: `CAUTHY_CLIENT_ID`. */\n clientId?: string\n /** Its secret. Default: `CAUTHY_CLIENT_SECRET`. Not needed with `key`. */\n clientSecret?: string\n /**\n * The key to prove who the application is with, instead of the secret.\n * Default: `CAUTHY_SYSTEM_KEY` (the private key) and `CAUTHY_SYSTEM_KEY_ID`.\n */\n key?: SystemKey\n fetch?: typeof fetch\n}\n\n/** Cauthy refused a request of the application's own. */\nexport class CauthyApiError extends Error {\n constructor(\n readonly status: number,\n /** Cauthy's code for what went wrong: `organization_not_found`, `other_system`, `outside_system_scope`… */\n readonly code: string,\n message: string,\n ) {\n super(`cauthy-auth: ${message}`)\n this.name = 'CauthyApiError'\n }\n}\n\n/** An organization of the application's system. Every moment is UTC. */\nexport interface Organization {\n id: string\n system: string\n name: string\n kind: string\n /** The id the application knows it by. */\n externalId?: string\n status: 'active' | 'archived'\n metadata: Record<string, JsonValue>\n members: number\n createdAt: string\n updatedAt: string\n}\n\nexport interface OrganizationMember {\n userId: string\n email: string\n name: string\n /** The account's status: a disabled member is still listed. */\n status: string\n role: string\n /** True when the address had no account: one was made and invited. */\n invited?: boolean\n}\n\n/** Who to add: an account by its id, or anybody by their address (invited when new). */\nexport type NewMember = { role: string; name?: string; locale?: string } & ({ userId: string; email?: undefined } | { email: string; userId?: undefined })\n\nexport interface CauthySystem {\n /** The application's own token, asked for when needed and kept until it is about to run out. */\n token(): Promise<string>\n /** Any route the application's token may call; answers what Cauthy put under `data`. */\n request<T = unknown>(method: string, path: string, body?: unknown): Promise<T>\n organizations: {\n list(query?: { q?: string; status?: 'active' | 'archived'; limit?: number; offset?: number }): Promise<{ items: Organization[]; total: number }>\n create(input: { name: string; kind?: string; externalId?: string; metadata?: Record<string, JsonValue> }): Promise<Organization>\n get(id: string): Promise<Organization>\n update(id: string, patch: { name?: string; kind?: string; status?: 'active' | 'archived'; metadata?: Record<string, JsonValue> }): Promise<Organization>\n /** Removes the organization and every membership of it, for good. */\n remove(id: string): Promise<void>\n }\n members: {\n list(organizationId: string): Promise<OrganizationMember[]>\n add(organizationId: string, member: NewMember): Promise<OrganizationMember>\n setRole(organizationId: string, userId: string, role: string): Promise<OrganizationMember>\n remove(organizationId: string, userId: string): Promise<void>\n }\n /** Invites a person who has no account yet; answers the account made for them. */\n invite(person: { email: string; name?: string; locale?: string }): Promise<{ id: string; email: string }>\n /**\n * How the application looks on Cauthy's pages: read it, set it, or take it\n * back to Cauthy's own with `null`. Of its own sign-in client — this one,\n * unless another is named (a system whose identity is a separate client).\n */\n theme: {\n get(clientId?: string): Promise<CauthyTheme>\n set(theme: CauthyTheme | null, clientId?: string): Promise<void>\n }\n /** The public half of `key`, to serve at the address Cauthy was given (`jwks_uri`). Empty with a secret. */\n publicJwks(): Promise<{ keys: JWK[] }>\n}\n\nfunction env(name: string): string | undefined {\n return typeof process !== 'undefined' ? process.env?.[name] : undefined\n}\n\ninterface OrgWire {\n id: string; system: string; name: string; kind: string; external_id?: string; status: 'active' | 'archived'\n metadata: Record<string, JsonValue>; members: number; created_at: string; updated_at: string\n}\n\nfunction toOrganization(o: OrgWire): Organization {\n return { id: o.id, system: o.system, name: o.name, kind: o.kind, externalId: o.external_id, status: o.status,\n metadata: o.metadata ?? {}, members: o.members, createdAt: o.created_at, updatedAt: o.updated_at }\n}\n\ninterface MemberWire { user_id: string; email: string; name: string; status: string; role: string; invited?: boolean }\n\nfunction toMember(m: MemberWire): OrganizationMember {\n return { userId: m.user_id, email: m.email, name: m.name, status: m.status, role: m.role, ...(m.invited === undefined ? {} : { invited: m.invited }) }\n}\n\nfunction asJwk(k: SystemKey): JWK | undefined {\n if (typeof k.privateKey !== 'string') return k.privateKey\n return k.privateKey.includes('BEGIN') ? undefined : (JSON.parse(k.privateKey) as JWK)\n}\n\nasync function loadKey(k: SystemKey): Promise<KeyLike | Uint8Array> {\n const jwk = asJwk(k)\n // Extractable, so its public half can be published (publicJwks).\n return jwk ? importJWK(jwk, 'RS256') : importPKCS8(k.privateKey as string, 'RS256', { extractable: true })\n}\n\nexport function createSystem(given: SystemOptions = {}): CauthySystem {\n const issuer = (given.issuer ?? env('CAUTHY_ISSUER') ?? '').replace(/\\/+$/, '')\n const clientId = given.clientId ?? env('CAUTHY_CLIENT_ID') ?? ''\n const clientSecret = given.clientSecret ?? env('CAUTHY_CLIENT_SECRET') ?? ''\n const envKey = env('CAUTHY_SYSTEM_KEY')\n const key = given.key ?? (envKey ? { privateKey: envKey, keyId: env('CAUTHY_SYSTEM_KEY_ID') ?? '' } : undefined)\n const doFetch: typeof fetch = (...args) => (given.fetch ?? fetch)(...args)\n const tokenEndpoint = `${issuer}/v1/oauth/token`\n\n // Checked when it is first used, not when it is made: an application that\n // only signs people in never needs any of this.\n function configured(): void {\n if (!/^https?:\\/\\//.test(issuer)) throw new CauthyConfigError('issuer (CAUTHY_ISSUER) must be Cauthy\\'s address')\n if (clientId === '') throw new CauthyConfigError('clientId (CAUTHY_CLIENT_ID) is required')\n if (key ? key.keyId === '' : clientSecret === '') {\n throw new CauthyConfigError(key ? 'the key needs its id (CAUTHY_SYSTEM_KEY_ID)'\n : 'to act on its own the application needs its secret (CAUTHY_CLIENT_SECRET) or a key (CAUTHY_SYSTEM_KEY)')\n }\n }\n\n let imported: Promise<KeyLike | Uint8Array> | undefined\n const signingKey = () => (imported ??= loadKey(key as SystemKey))\n\n async function fail(res: Response, what: string): Promise<never> {\n const body = (await res.json().catch(() => null)) as { errors?: { code?: string; message?: string }[] } | null\n const first = body?.errors?.[0]\n throw new CauthyApiError(res.status, first?.code ?? 'error', `Cauthy refused ${what} (${res.status}): ${first?.message ?? res.statusText}`)\n }\n\n let held: { value: string; until: number } | undefined\n let asking: Promise<string> | undefined\n\n async function ask(): Promise<string> {\n configured()\n const form = new URLSearchParams({ grant_type: 'client_credentials', client_id: clientId })\n if (key) {\n const assertion = await new SignJWT({})\n .setProtectedHeader({ alg: 'RS256', kid: key.keyId })\n .setIssuer(clientId).setSubject(clientId).setAudience(tokenEndpoint)\n .setJti(crypto.randomUUID()).setIssuedAt().setExpirationTime('2m')\n .sign(await signingKey())\n form.set('client_assertion_type', ASSERTION_TYPE)\n form.set('client_assertion', assertion)\n } else {\n form.set('client_secret', clientSecret)\n }\n const res = await doFetch(tokenEndpoint, { method: 'POST', headers: { 'content-type': 'application/x-www-form-urlencoded' }, body: form })\n if (!res.ok) await fail(res, 'the application\\'s own token')\n const t = (await res.json()) as { access_token: string; expires_in: number }\n held = { value: t.access_token, until: Date.now() + t.expires_in * 1000 - SKEW_MS }\n return t.access_token\n }\n\n function token(): Promise<string> {\n if (held && Date.now() < held.until) return Promise.resolve(held.value)\n // One request, however many callers arrive while it is out.\n return (asking ??= ask().finally(() => { asking = undefined }))\n }\n\n async function call(method: string, path: string, body?: unknown): Promise<unknown> {\n const send = async () => doFetch(`${issuer}${path}`, {\n method,\n headers: { authorization: `Bearer ${await token()}`, ...(body === undefined ? {} : { 'content-type': 'application/json' }) },\n body: body === undefined ? undefined : JSON.stringify(body),\n })\n let res = await send()\n if (res.status === 401 && held) {\n // The token was taken back early (the client's keys or secret changed): once more, with a new one.\n held = undefined\n res = await send()\n }\n if (!res.ok) await fail(res, `${method} ${path}`)\n return res.status === 204 ? undefined : res.json()\n }\n\n const data = async <T>(method: string, path: string, body?: unknown) => ((await call(method, path, body)) as { data: T } | undefined)?.data as T\n const id = encodeURIComponent\n\n return {\n token,\n request: data,\n organizations: {\n async list(query = {}) {\n const q = new URLSearchParams()\n for (const [k, v] of Object.entries(query)) if (v !== undefined) q.set(k, String(v))\n const out = (await call('GET', `/v1/organizations${q.size ? `?${q}` : ''}`)) as { data: OrgWire[]; total_count: number }\n return { items: out.data.map(toOrganization), total: out.total_count }\n },\n async create(input) {\n return toOrganization(await data<OrgWire>('POST', '/v1/organizations',\n { name: input.name, kind: input.kind, external_id: input.externalId, metadata: input.metadata }))\n },\n async get(orgId) {\n return toOrganization(await data<OrgWire>('GET', `/v1/organizations/${id(orgId)}`))\n },\n async update(orgId, patch) {\n return toOrganization(await data<OrgWire>('PATCH', `/v1/organizations/${id(orgId)}`, patch))\n },\n async remove(orgId) {\n await call('DELETE', `/v1/organizations/${id(orgId)}`)\n },\n },\n members: {\n async list(orgId) {\n return (await data<MemberWire[]>('GET', `/v1/organizations/${id(orgId)}/members`)).map(toMember)\n },\n async add(orgId, m) {\n return toMember(await data<MemberWire>('POST', `/v1/organizations/${id(orgId)}/members`,\n { role: m.role, user_id: m.userId, email: m.email, name: m.name, locale: m.locale, client_id: clientId }))\n },\n async setRole(orgId, userId, role) {\n return toMember(await data<MemberWire>('PUT', `/v1/organizations/${id(orgId)}/members/${id(userId)}`, { role }))\n },\n async remove(orgId, userId) {\n await call('DELETE', `/v1/organizations/${id(orgId)}/members/${id(userId)}`)\n },\n },\n async invite(person) {\n // The invitation leads back to this application once it is accepted.\n const u = await data<{ id: string; email: string }>('POST', '/v1/users', { ...person, client_id: clientId })\n return { id: u.id, email: u.email }\n },\n theme: {\n async get(of = clientId) {\n return fromWire(await data<Record<string, unknown>>('GET', `/v1/oauth/clients/${id(of)}/theme`))\n },\n async set(theme, of = clientId) {\n if (theme === null) await call('DELETE', `/v1/oauth/clients/${id(of)}/theme`)\n else await call('PUT', `/v1/oauth/clients/${id(of)}/theme`, toWire(theme))\n },\n },\n async publicJwks() {\n if (!key) return { keys: [] }\n const { kty, n, e } = asJwk(key) ?? (await exportJWK(await signingKey()))\n return { keys: [{ kty, n, e, kid: key.keyId, alg: 'RS256', use: 'sig' }] }\n },\n }\n}\n","// An application's look, for Cauthy's hosted pages.\n//\n// Cauthy's pages — sign-in, sign-up, the account page — can take the colours,\n// corners, typeface and logo of the application that sends its people there,\n// so signing in reads as a page of the application. A theme is a short list of\n// values, which Cauthy checks one by one; it is never CSS.\n//\n// An application built with Stackmyth already has those values, as its\n// `--sm-*` tokens. `themeFromStackmyth` reads them out of its stylesheets.\n\n/** How an application looks on Cauthy's hosted pages. Every value is optional. */\n/** One font file the application serves: a family in one weight (or a range) and one style. */\nexport interface ThemeFont {\n family: string\n /** An https address of a `.woff2` file. It must answer with `Access-Control-Allow-Origin` for Cauthy's origin. */\n url: string\n /** `400`, or `\"100 900\"` for a variable font. Default: 400. */\n weight?: number | string\n style?: 'normal' | 'italic'\n}\n\nexport interface CauthyTheme {\n /** The page. Colours are `#rrggbb` or `rgba(r, g, b, a)`. */\n background?: string\n /** The card, and inputs. */\n surface?: string\n /** Headings and what is typed. */\n text?: string\n /** Paragraphs, labels, small print. */\n textMuted?: string\n border?: string\n /** The main button, and what is written on it. */\n primary?: string\n primaryText?: string\n /** Links, and the focus ring. */\n accent?: string\n error?: string\n errorBackground?: string\n success?: string\n successBackground?: string\n /** Corners of cards and inputs, in pixels (0–40). */\n radius?: number\n /** Corners of buttons, in pixels (0–40). */\n buttonRadius?: number\n /** A CSS font-family list: the application's typeface first, a system font after it. */\n fontFamily?: string\n /** The files of the typefaces `fontFamily` names, for people who do not have them installed. */\n fonts?: ThemeFont[]\n /** An image shown where \"Cauthy\" is, over https. */\n logoUrl?: string\n}\n\nexport interface StackmythThemeOptions {\n /**\n * The named theme to read, when the stylesheet keeps several under\n * `[data-theme=…]` (`\"vennet\"`). Without it only what applies to every\n * page (`:root`) is read.\n */\n theme?: string\n /** Which of the application's two modes to take. Default: `light`. */\n mode?: 'light' | 'dark'\n logoUrl?: string\n}\n\nexport interface ImportedTheme {\n theme: CauthyTheme\n /** Tokens that were there and could not be carried over, and why. */\n warnings: string[]\n}\n\nconst MAX_RADIUS = 40\n\n// ── Reading custom properties out of CSS ───────────────────────────────────\n\ninterface Declaration {\n name: string\n value: string\n /** The selectors and at-rules it sits inside, outermost first. */\n within: string[]\n}\n\nfunction declarations(css: string): Declaration[] {\n const text = css.replace(/\\/\\*[\\s\\S]*?\\*\\//g, '')\n const out: Declaration[] = []\n const within: string[] = []\n let chunk = ''\n let quote = ''\n let depth = 0 // parentheses, so a `;` inside url(…) does not end anything\n const flush = () => {\n const m = /^\\s*(--[\\w-]+)\\s*:\\s*([\\s\\S]+?)\\s*$/.exec(chunk)\n if (m) out.push({ name: m[1], value: m[2].replace(/\\s+/g, ' ').replace(/\\s*!important$/, ''), within: [...within] })\n chunk = ''\n }\n for (const ch of text) {\n if (quote) {\n chunk += ch\n if (ch === quote) quote = ''\n continue\n }\n if (ch === '\"' || ch === \"'\") {\n quote = ch\n chunk += ch\n } else if (ch === '(') {\n depth++\n chunk += ch\n } else if (ch === ')') {\n depth = Math.max(0, depth - 1)\n chunk += ch\n } else if (ch === '{' && depth === 0) {\n within.push(chunk.trim().replace(/\\s+/g, ' '))\n chunk = ''\n } else if (ch === '}' && depth === 0) {\n flush()\n within.pop()\n } else if (ch === ';' && depth === 0) {\n flush()\n } else {\n chunk += ch\n }\n }\n return out\n}\n\nconst DARK = /prefers-color-scheme:\\s*dark|data-(mode|theme|color-scheme)=[\"']?dark|\\.dark\\b/i\nconst LIGHT = /prefers-color-scheme:\\s*light|data-(mode|theme|color-scheme)=[\"']?light|\\.light\\b/i\nconst NAMED = /data-theme=[\"']?([\\w-]+)/i\n\n/** Whether a declaration applies to the page being themed. */\nfunction applies(d: Declaration, o: Required<Pick<StackmythThemeOptions, 'mode'>> & { theme?: string }): boolean {\n for (const scope of d.within) {\n if (o.mode === 'light' && DARK.test(scope)) return false\n if (o.mode === 'dark' && LIGHT.test(scope)) return false\n const named = NAMED.exec(scope)\n if (named && !/^(dark|light)$/i.test(named[1]) && named[1] !== o.theme) return false\n // Component-level rules (`.btn { --sm-… }`) are not the page's theme.\n if (!scope.startsWith('@') && !/:root|^html\\b|^body\\b|\\[data-|^\\*$|:host/.test(scope)) return false\n }\n return true\n}\n\n// ── Values ─────────────────────────────────────────────────────────────────\n\nfunction resolve(value: string, vars: Map<string, string>, seen = 0): string | null {\n if (seen > 16) return null\n let unresolved = false\n // Innermost var() first: fallbacks may hold another var().\n let out = value\n for (let guard = 0; guard < 32 && /var\\(/.test(out); guard++) {\n out = out.replace(/var\\(\\s*(--[\\w-]+)\\s*(?:,\\s*([^()]*(?:\\([^()]*\\)[^()]*)*))?\\)/, (_m, name: string, fallback?: string) => {\n const known = vars.get(name)\n const next = known !== undefined ? resolve(known, vars, seen + 1) : fallback !== undefined ? resolve(fallback.trim(), vars, seen + 1) : null\n if (next === null) unresolved = true\n return next ?? ''\n })\n if (unresolved) return null\n }\n return /var\\(/.test(out) ? null : out.trim()\n}\n\nconst clamp01 = (n: number) => Math.min(1, Math.max(0, n))\nconst hex2 = (n: number) => Math.round(clamp01(n) * 255).toString(16).padStart(2, '0')\nconst gamma = (c: number) => (c <= 0.0031308 ? 12.92 * c : 1.055 * Math.pow(c, 1 / 2.4) - 0.055)\n\nfunction out(r: number, g: number, b: number, a = 1): string {\n if (a >= 1) return `#${hex2(r)}${hex2(g)}${hex2(b)}`\n return `rgba(${Math.round(clamp01(r) * 255)}, ${Math.round(clamp01(g) * 255)}, ${Math.round(clamp01(b) * 255)}, ${Math.round(clamp01(a) * 1000) / 1000})`\n}\n\nconst alpha = (s: string | undefined) => (s === undefined ? 1 : s.endsWith('%') ? parseFloat(s) / 100 : parseFloat(s))\n\nconst KEYWORDS: Record<string, string> = { white: '#ffffff', black: '#000000' }\n\n/** A colour as Cauthy takes it — `#rrggbb` or `rgba(…)` — or null when it is none it can read. */\nexport function toColor(value: string): string | null {\n const v = value.trim().toLowerCase()\n if (KEYWORDS[v]) return KEYWORDS[v]\n if (/^#([0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})$/.test(v)) return v\n let m = /^rgba?\\(\\s*([\\d.]+%?)[\\s,]+([\\d.]+%?)[\\s,]+([\\d.]+%?)(?:\\s*[,/]\\s*([\\d.]+%?))?\\s*\\)$/.exec(v)\n if (m) {\n const ch = (s: string) => (s.endsWith('%') ? parseFloat(s) / 100 : parseFloat(s) / 255)\n return out(ch(m[1]), ch(m[2]), ch(m[3]), alpha(m[4]))\n }\n m = /^hsla?\\(\\s*([\\d.]+)(?:deg)?[\\s,]+([\\d.]+)%[\\s,]+([\\d.]+)%(?:\\s*[,/]\\s*([\\d.]+%?))?\\s*\\)$/.exec(v)\n if (m) {\n const h = ((parseFloat(m[1]) % 360) + 360) % 360\n const s = parseFloat(m[2]) / 100\n const l = parseFloat(m[3]) / 100\n const f = (n: number) => {\n const k = (n + h / 30) % 12\n return l - s * Math.min(l, 1 - l) * Math.max(-1, Math.min(k - 3, 9 - k, 1))\n }\n return out(f(0), f(8), f(4), alpha(m[4]))\n }\n m = /^oklch\\(\\s*([\\d.]+%?)\\s+([\\d.]+%?)\\s+([\\d.]+)(?:deg)?(?:\\s*\\/\\s*([\\d.]+%?))?\\s*\\)$/.exec(v)\n if (m) {\n const L = m[1].endsWith('%') ? parseFloat(m[1]) / 100 : parseFloat(m[1])\n const C = m[2].endsWith('%') ? (parseFloat(m[2]) / 100) * 0.4 : parseFloat(m[2])\n const H = (parseFloat(m[3]) * Math.PI) / 180\n const a = C * Math.cos(H)\n const b = C * Math.sin(H)\n const l = Math.pow(L + 0.3963377774 * a + 0.2158037573 * b, 3)\n const mm = Math.pow(L - 0.1055613458 * a - 0.0638541728 * b, 3)\n const s = Math.pow(L - 0.0894841775 * a - 1.291485548 * b, 3)\n return out(\n gamma(4.0767416621 * l - 3.3077115913 * mm + 0.2309699292 * s),\n gamma(-1.2684380046 * l + 2.6097574011 * mm - 0.3413193965 * s),\n gamma(-0.0041960863 * l - 0.7034186147 * mm + 1.707614701 * s),\n alpha(m[4]),\n )\n }\n return null\n}\n\n/** A length in pixels, within what Cauthy takes, or null. */\nfunction toRadius(value: string): number | null {\n const m = /^([\\d.]+)(px|rem|em)?$/.exec(value.trim())\n if (!m) return null\n const px = parseFloat(m[1]) * (m[2] === 'rem' || m[2] === 'em' ? 16 : 1)\n return Number.isFinite(px) ? Math.min(MAX_RADIUS, Math.max(0, Math.round(px))) : null\n}\n\n/** A font list as Cauthy takes it, or null. */\nfunction toFont(value: string): string | null {\n const v = value.trim()\n return /^[A-Za-z0-9 ,'\"_-]{1,200}$/.test(v) && (v.match(/\"/g)?.length ?? 0) % 2 === 0 && (v.match(/'/g)?.length ?? 0) % 2 === 0 ? v : null\n}\n\n// ── Stackmyth ──────────────────────────────────────────────────────────────\n\n/** Which Stackmyth token each value of a theme is read from, first found wins. */\nconst COLORS: [keyof CauthyTheme, string[]][] = [\n ['background', ['--sm-bg-surface', '--sm-surface']],\n ['surface', ['--sm-bg-surface-raised', '--sm-surface-raised']],\n ['text', ['--sm-text-primary', '--sm-surface-text']],\n ['textMuted', ['--sm-text-secondary']],\n ['border', ['--sm-border-default', '--sm-surface-border']],\n ['primary', ['--sm-primary', '--sm-bg-interactive']],\n ['primaryText', ['--sm-primary-text', '--sm-text-inverse']],\n ['accent', ['--sm-text-brand', '--sm-border-focus', '--sm-accent']],\n ['error', ['--sm-error-soft-text', '--sm-error']],\n ['errorBackground', ['--sm-error-soft']],\n ['success', ['--sm-success-soft-text', '--sm-success']],\n ['successBackground', ['--sm-success-soft']],\n]\n\n/**\n * Reads an application's Stackmyth theme out of its stylesheets.\n *\n * Give it the CSS that declares the tokens, in the order the application\n * loads it — Stackmyth's own (`@stackmyth/core/core.vars.css`, or a theme\n * file) and then the application's brand file that re-points them. Tokens\n * that point at the application's own variables are followed. The\n * application's dark mode is left out unless asked for.\n */\nexport function themeFromStackmyth(css: string | string[], options: StackmythThemeOptions = {}): ImportedTheme {\n const o = { mode: options.mode ?? 'light', theme: options.theme }\n const vars = new Map<string, string>()\n for (const sheet of Array.isArray(css) ? css : [css]) {\n for (const d of declarations(sheet)) if (applies(d, o)) vars.set(d.name, d.value)\n }\n const theme: CauthyTheme = {}\n const warnings: string[] = []\n\n const read = <T>(tokens: string[], to: (v: string) => T | null, what: string): T | undefined => {\n for (const token of tokens) {\n const raw = vars.get(token)\n if (raw === undefined) continue\n const value = resolve(raw, vars)\n const got = value === null ? null : to(value)\n if (got !== null) return got\n warnings.push(`${token}: \"${raw}\" is not ${what} Cauthy can take`)\n }\n return undefined\n }\n\n for (const [field, tokens] of COLORS) {\n const c = read(tokens, toColor, 'a colour')\n if (c !== undefined) (theme as Record<string, unknown>)[field] = c\n }\n const radius = read(['--sm-radius-container', '--sm-radius-md'], toRadius, 'a length')\n if (radius !== undefined) theme.radius = radius\n const button = read(['--sm-radius-interactive', '--sm-radius-sm'], toRadius, 'a length')\n if (button !== undefined) theme.buttonRadius = button\n const font = read(['--sm-font-family'], toFont, 'a font list')\n if (font !== undefined) theme.fontFamily = font\n if (options.logoUrl) theme.logoUrl = options.logoUrl\n\n if (vars.size === 0) warnings.push('no custom properties were found: is this the stylesheet that declares the --sm-* tokens?')\n else if (![...vars.keys()].some((k) => k.startsWith('--sm-'))) warnings.push('no --sm-* token was found in these stylesheets')\n return { theme, warnings }\n}\n\n// ── Sending it to Cauthy ───────────────────────────────────────────────────\n\n/** A theme as Cauthy's API names its values. */\nexport function toWire(theme: CauthyTheme): Record<string, string | number | ThemeFont[]> {\n const names: Record<keyof CauthyTheme, string> = {\n background: 'background', surface: 'surface', text: 'text', textMuted: 'text_muted', border: 'border',\n primary: 'primary', primaryText: 'primary_text', accent: 'accent', error: 'error', errorBackground: 'error_background',\n success: 'success', successBackground: 'success_background', radius: 'radius', buttonRadius: 'button_radius',\n fontFamily: 'font_family', logoUrl: 'logo_url', fonts: 'fonts',\n }\n const wire: Record<string, string | number | ThemeFont[]> = {}\n for (const [k, v] of Object.entries(theme)) {\n if (v === undefined || v === '' || (Array.isArray(v) && v.length === 0)) continue\n wire[names[k as keyof CauthyTheme]] = v as string | number | ThemeFont[]\n }\n return wire\n}\n\n/** A theme as Cauthy answers it, in this package's names. */\nexport function fromWire(wire: Record<string, unknown>): CauthyTheme {\n const names: Record<string, keyof CauthyTheme> = {\n background: 'background', surface: 'surface', text: 'text', text_muted: 'textMuted', border: 'border',\n primary: 'primary', primary_text: 'primaryText', accent: 'accent', error: 'error', error_background: 'errorBackground',\n success: 'success', success_background: 'successBackground', radius: 'radius', button_radius: 'buttonRadius',\n font_family: 'fontFamily', logo_url: 'logoUrl', fonts: 'fonts',\n }\n const theme: Record<string, unknown> = {}\n for (const [k, v] of Object.entries(wire ?? {})) if (names[k] && v !== undefined && v !== null && v !== '') theme[names[k]] = v\n return theme as CauthyTheme\n}\n\nexport interface PushThemeOptions {\n /** Cauthy's address. Default: `CAUTHY_ISSUER`. */\n issuer?: string\n /** The application's client id. Default: `CAUTHY_CLIENT_ID`. */\n clientId?: string\n /** The workspace's slug. Default: `CAUTHY_TENANT`. */\n tenant?: string\n /**\n * A personal access token with `oauth_clients:manage`. Default:\n * `CAUTHY_TOKEN`. It belongs in the deploy pipeline, never in the application.\n */\n token?: string\n theme: CauthyTheme\n fetch?: typeof fetch\n}\n\n/**\n * Gives the application its look on Cauthy's pages. It replaces the theme: a\n * value left out goes back to Cauthy's own. Run it when the brand changes —\n * from a script or the deploy — not on every request.\n */\nexport async function pushTheme(o: PushThemeOptions): Promise<void> {\n const env = (n: string) => (typeof process !== 'undefined' ? process.env?.[n] : undefined)\n const issuer = (o.issuer ?? env('CAUTHY_ISSUER') ?? '').replace(/\\/+$/, '')\n const clientId = o.clientId ?? env('CAUTHY_CLIENT_ID') ?? ''\n const tenant = o.tenant ?? env('CAUTHY_TENANT') ?? ''\n const token = o.token ?? env('CAUTHY_TOKEN') ?? ''\n for (const [name, v] of Object.entries({ CAUTHY_ISSUER: issuer, CAUTHY_CLIENT_ID: clientId, CAUTHY_TENANT: tenant, CAUTHY_TOKEN: token })) {\n if (v === '') throw new Error(`cauthy-auth: ${name} is required to push a theme`)\n }\n const res = await (o.fetch ?? fetch)(`${issuer}/v1/oauth/clients/${encodeURIComponent(clientId)}/theme`, {\n method: 'PUT',\n headers: { authorization: `Bearer ${token}`, 'x-tenant-key': tenant, 'content-type': 'application/json' },\n body: JSON.stringify(toWire(o.theme)),\n })\n if (!res.ok) {\n const body = (await res.json().catch(() => null)) as { errors?: { message?: string }[] } | null\n throw new Error(`cauthy-auth: Cauthy refused the theme (${res.status}): ${body?.errors?.[0]?.message ?? res.statusText}`)\n }\n}\n","/** How an application is set up to sign its people in with Cauthy. */\nexport interface CauthyOptions {\n /** Cauthy's public address, e.g. `https://id.example.com`. Default: `CAUTHY_ISSUER`. */\n issuer?: string\n /** The application's OAuth client id. Default: `CAUTHY_CLIENT_ID`. */\n clientId?: string\n /** Its secret, when the client is confidential. Default: `CAUTHY_CLIENT_SECRET`. */\n clientSecret?: string\n /**\n * The key the session cookie is encrypted with: any string of 32 characters\n * or more, kept on the server. Default: `CAUTHY_SECRET`.\n */\n secret?: string\n /**\n * The system this application is, as its organizations are filed under in\n * Cauthy (`\"pondari\"`). When set, a session's organizations are that\n * system's. Default: `CAUTHY_SYSTEM`.\n */\n system?: string\n /** Where the routes are mounted. Default: `/api/auth`. */\n basePath?: string\n /**\n * The application's own public origin (`https://app.example.com`), for the\n * redirect URI. Default: the origin of each request, which is right unless\n * a proxy hides it.\n */\n baseURL?: string\n /** Where a person lands after signing in, when no `return_to` was asked. Default: `/`. */\n afterSignIn?: string\n /** Where a person lands after signing out. Default: `/`. */\n afterSignOut?: string\n /** Name of the session cookie. Default: `cauthy_session`. */\n cookieName?: string\n /**\n * A key the application proves who it is with when it acts on its own\n * (`auth.system`), instead of its secret. Default: `CAUTHY_SYSTEM_KEY` and\n * `CAUTHY_SYSTEM_KEY_ID`.\n */\n systemKey?: import('./system').SystemKey\n /** `fetch` to reach Cauthy with. Default: the global one. */\n fetch?: typeof fetch\n}\n\n/** The person signed in. Every moment is UTC. */\nexport interface SessionUser {\n id: string\n email: string\n name: string\n emailVerified: boolean\n /**\n * True when the account ran out of time to confirm its address: let it read\n * and refuse its writes.\n */\n restricted: boolean\n /** When an unconfirmed address stops being enough (ISO 8601, UTC), if there is a deadline. */\n emailVerifyBy?: string\n /** The language the person reads in (\"es\", \"en-US\"), when Cauthy knows it. */\n locale?: string\n}\n\n/** A value JSON can carry. What crosses from server to browser is typed as this, never `unknown`. */\nexport type JsonValue = string | number | boolean | null | JsonValue[] | { [key: string]: JsonValue }\n\n/** An organization the person belongs to, with their role in it. */\nexport interface SessionOrganization {\n id: string\n system: string\n name: string\n kind: string\n externalId?: string\n role: string\n /** What the system keeps about the organization, as it filed it in Cauthy. */\n metadata: Record<string, JsonValue>\n /**\n * What the person may do here, as \"resource:action\" entries (\"*\" is any):\n * their role's permissions, what it inherits, and anything given to them\n * apart from it. Absent when the system keeps no catalog of roles in Cauthy.\n * Check with `can(organization, resource, action)`.\n */\n permissions?: string[]\n /** What the person is denied here, whatever else allows it. */\n denied?: string[]\n}\n\n/** A signed-in person, as the server sees them. */\nexport interface Session {\n user: SessionUser\n /** The Cauthy tenant the account is of. */\n tenantId: string\n /** The roles the account holds in that tenant. */\n roles: string[]\n /**\n * Cauthy's access token for this person: send it as `Authorization: Bearer`\n * to another service that verifies Cauthy's tokens. Never give it to the browser.\n */\n accessToken: string\n /** When the access token expires and the session is checked with Cauthy again (ISO 8601, UTC). */\n expiresAt: string\n}\n\n/** What the browser is told of a session: who, and their organizations — never a token. */\nexport interface PublicSession {\n user: SessionUser\n tenantId: string\n roles: string[]\n organizations: SessionOrganization[]\n expiresAt: string\n}\n\n/** Why there is no session. */\nexport type SessionStatus =\n | 'authenticated'\n | 'unauthenticated' // nobody is signed in\n | 'expired' // signed in, but the session must be renewed first (see `Cauthy.resolve`)\n\nexport class CauthyConfigError extends Error {\n constructor(message: string) {\n super(`cauthy-auth: ${message}`)\n this.name = 'CauthyConfigError'\n }\n}\n","// Hearing from Cauthy: verifying a webhook delivery.\n//\n// Cauthy signs every delivery with its own key — the one its tokens are\n// signed with, published at /.well-known/jwks.json — so there is no secret to\n// receive, store or rotate. Verifying is one call:\n//\n// const event = await auth.webhook(request) // throws if it is not Cauthy's\n//\n// What is checked: the signature, against Cauthy's published keys; that the\n// delivery was meant for THIS endpoint (a signature anyone can verify must say\n// who it is for); that it is recent; and that the body is, byte for byte, the\n// one that was signed.\n\nimport { createRemoteJWKSet, jwtVerify, type JWTVerifyGetKey } from 'jose'\nimport type { JsonValue } from './types'\n\n/** Something that happened in Cauthy, as a subscriber is told. Moments are UTC. */\nexport interface CauthyEvent {\n /** This delivery's id: the same event delivered again carries the same one. */\n id: string\n /** What happened: \"organization.member_added\". */\n type: string\n tenantId: string\n createdAt: string\n /** The person it happened to, when it is about one. */\n userId?: string\n /** Who did it. */\n actorId?: string\n /** What the event says: for an organization, its id, system, external_id, role… */\n data: Record<string, JsonValue>\n}\n\nexport class CauthyWebhookError extends Error {\n constructor(readonly reason: string) {\n super(`cauthy-auth: webhook refused: ${reason}`)\n this.name = 'CauthyWebhookError'\n }\n}\n\nexport interface VerifyWebhookOptions {\n /** Cauthy's address. Default: `CAUTHY_ISSUER`. */\n issuer?: string\n /**\n * This endpoint's own public address, exactly as it is registered in Cauthy.\n * Default: the request's URL, which is right unless a proxy changes it.\n */\n url?: string\n /** Cauthy's keys. Default: fetched from the issuer and cached. */\n jwks?: JWTVerifyGetKey\n /** The moment to verify at. Default: now. */\n now?: Date\n}\n\nconst keys = new Map<string, JWTVerifyGetKey>()\n\nfunction keysOf(issuer: string): JWTVerifyGetKey {\n let set = keys.get(issuer)\n if (set === undefined) {\n set = createRemoteJWKSet(new URL(`${issuer}/.well-known/jwks.json`), { cooldownDuration: 30_000 })\n keys.set(issuer, set)\n }\n return set\n}\n\nconst hex = (s: unknown) => (typeof s === 'string' && s !== '' ? s : undefined)\n\n/**\n * Verifies a delivery and answers the event it carries. Throws\n * `CauthyWebhookError` for anything that is not a delivery Cauthy made for\n * this endpoint just now: answer it 401 and nothing else.\n */\nexport async function verifyWebhook(request: Request, options: VerifyWebhookOptions = {}): Promise<CauthyEvent> {\n const env = typeof process !== 'undefined' ? process.env?.CAUTHY_ISSUER : undefined\n const issuer = (options.issuer ?? env ?? '').replace(/\\/+$/, '')\n if (issuer === '') throw new CauthyWebhookError('issuer (CAUTHY_ISSUER) is not set')\n const signature = request.headers.get('x-cauthy-signature-jwt')\n if (!signature) throw new CauthyWebhookError('no signature')\n\n // The bytes as they arrived: what was signed is their hash, not a re-encoding.\n const raw = new Uint8Array(await request.arrayBuffer())\n const audience = options.url ?? request.url.split('?')[0]\n\n let claims: Record<string, unknown>\n try {\n const verified = await jwtVerify(signature, options.jwks ?? keysOf(issuer), {\n algorithms: ['RS256'],\n issuer,\n audience,\n typ: 'cauthy-webhook+jwt',\n requiredClaims: ['exp', 'iat', 'jti', 'body_sha256'],\n currentDate: options.now,\n clockTolerance: 30,\n })\n claims = verified.payload as Record<string, unknown>\n } catch (err) {\n throw new CauthyWebhookError(err instanceof Error ? err.message : 'bad signature')\n }\n\n // Not from the future either: a clock that far off is not Cauthy's.\n const now = Math.floor((options.now ?? new Date()).getTime() / 1000)\n if (typeof claims.iat !== 'number' || claims.iat > now + 30) throw new CauthyWebhookError('signed in the future')\n\n const digest = new Uint8Array(await crypto.subtle.digest('SHA-256', raw))\n let b64 = ''\n for (const b of digest) b64 += String.fromCharCode(b)\n const hash = btoa(b64).replace(/\\+/g, '-').replace(/\\//g, '_').replace(/=+$/, '')\n if (hash !== claims.body_sha256) throw new CauthyWebhookError('the body is not the one that was signed')\n\n let body: Record<string, any>\n try {\n body = JSON.parse(new TextDecoder().decode(raw))\n } catch {\n throw new CauthyWebhookError('the body is not JSON')\n }\n // The envelope and the signature must tell the same story.\n if (body.id !== claims.jti || body.event !== claims.evt || body.tenant_id !== claims.tid) {\n throw new CauthyWebhookError('the body and its signature disagree')\n }\n const inner = (body.data ?? {}) as Record<string, any>\n return {\n id: String(body.id),\n type: String(body.event),\n tenantId: String(body.tenant_id),\n createdAt: String(inner.created_at ?? body.created_at ?? ''),\n ...(hex(inner.user_id) ? { userId: inner.user_id } : {}),\n ...(hex(inner.actor_id) ? { actorId: inner.actor_id } : {}),\n data: (inner.data ?? {}) as Record<string, JsonValue>,\n }\n}\n","// Sign in with Cauthy, held by the application's own server.\n//\n// The application mounts one route (`handler`) and asks who is there\n// (`getSession`). Signing in, signing up, second factors, passkeys, recovering\n// a password and the account page are Cauthy's pages: this sends the browser\n// to them and keeps what comes back in an encrypted, httpOnly cookie. There is\n// no database and no screen to build.\n//\n// The session is Cauthy's short-lived access token plus the refresh token that\n// renews it. When the access token expires the session is renewed with Cauthy,\n// which is where an account disabled there, or signed out everywhere, stops\n// being signed in here — within minutes, with nothing to synchronise.\n\nimport { clearParts, readCookies, readParts, writeCookie, writeParts, type CookieAttributes } from './cookies'\nimport { keyOf, seal, unseal } from './seal'\nimport { createSystem, type CauthySystem } from './system'\nimport { verifyWebhook, type CauthyEvent, type VerifyWebhookOptions } from './webhook'\nimport {\n CauthyConfigError,\n type CauthyOptions,\n type JsonValue,\n type PublicSession,\n type Session,\n type SessionOrganization,\n type SessionStatus,\n type SessionUser,\n} from './types'\n\n/** What the session cookie holds. Short names: it rides every request. */\ninterface Stored {\n at: string // access token\n rt: string // refresh token\n exp: number // when the access token expires, epoch seconds\n u: SessionUser\n tid: string\n roles: string[]\n}\n\n/** What the sign-in in progress holds, between leaving for Cauthy and coming back. */\ninterface Flow {\n state: string\n verifier: string\n returnTo: string\n}\n\nconst SESSION_DAYS = 30\nconst FLOW_SECONDS = 600\n/** Renew this long before the access token runs out, so a request never carries one about to. */\nconst SKEW_SECONDS = 30\nconst ORGANIZATIONS_TTL_MS = 60_000\n\n/** What `resolve` answers: who is there, and the cookies that must go back. */\nexport interface Resolved {\n session: Session | null\n status: SessionStatus\n /** `Set-Cookie` values to send with the response. Empty when nothing changed. */\n cookies: string[]\n}\n\nexport interface Cauthy {\n /** Serves the routes under `basePath`: login, callback, session, refresh, logout, account. */\n handler(request: Request): Promise<Response>\n /**\n * Who is signed in, or null.\n *\n * Give it the response's headers and a session that needs renewing is\n * renewed on the spot, its new cookie added to them. Without them an\n * expired session reads as null (see `status`): renewing changes the\n * cookie, and a cookie nobody sends back is a session lost.\n */\n getSession(request: Request, responseHeaders?: Headers): Promise<Session | null>\n /** Whether somebody is signed in, nobody is, or the session must be renewed first. */\n status(request: Request): Promise<SessionStatus>\n /**\n * The session, renewed when it has to be, and the cookies to send back.\n * The caller MUST send `cookies` with its response.\n */\n resolve(request: Request): Promise<Resolved>\n /**\n * For a route only a signed-in person may see: the session, or the response\n * to give instead — a redirect to sign in (or to renew) for a page, a 401\n * for anything else.\n */\n protect(request: Request): Promise<{ session: Session; response?: undefined } | { session?: undefined; response: Response }>\n /** The organizations the person belongs to (of `system`, when set), with their role in each. */\n organizations(session: Session): Promise<SessionOrganization[]>\n /**\n * Verifies a webhook delivery from Cauthy and answers the event it carries;\n * throws `CauthyWebhookError` for anything else. No secret is involved: the\n * delivery is signed with Cauthy's own key.\n */\n webhook(request: Request, options?: Pick<VerifyWebhookOptions, 'jwks' | 'now' | 'url'>): Promise<CauthyEvent>\n /**\n * The application acting on its own, with no person's token: its\n * organizations, their members, invitations. Needs the client's secret, or\n * `systemKey`.\n */\n system: CauthySystem\n /** What the browser may be told of a session. */\n toPublic(session: Session): Promise<PublicSession>\n /** Paths of this application that start a sign-in, a sign-out, or open the account page. */\n urls: { signIn(returnTo?: string): string; signOut(): string; account(returnTo?: string): string; session: string }\n options: Readonly<Required<Pick<CauthyOptions, 'issuer' | 'clientId' | 'basePath' | 'afterSignIn' | 'afterSignOut' | 'cookieName'>>>\n}\n\nfunction env(name: string): string | undefined {\n return typeof process !== 'undefined' ? process.env?.[name] : undefined\n}\n\nfunction b64url(bytes: Uint8Array): string {\n let s = ''\n for (const b of bytes) s += String.fromCharCode(b)\n return btoa(s).replace(/\\+/g, '-').replace(/\\//g, '_').replace(/=+$/, '')\n}\n\nfunction random(bytes = 32): string {\n return b64url(crypto.getRandomValues(new Uint8Array(bytes)))\n}\n\n/** A path of this application, and nothing else: never another site, never `//host`. */\nfunction localPath(candidate: string | null | undefined, fallback: string): string {\n if (!candidate || !candidate.startsWith('/') || candidate.startsWith('//') || candidate.includes('\\\\')) return fallback\n return candidate\n}\n\nfunction json(body: unknown, status = 200, headers?: Headers): Response {\n const h = headers ?? new Headers()\n h.set('content-type', 'application/json; charset=utf-8')\n h.set('cache-control', 'no-store')\n return new Response(JSON.stringify(body), { status, headers: h })\n}\n\nfunction redirect(location: string, cookies: string[] = []): Response {\n const h = new Headers({ location, 'cache-control': 'no-store' })\n for (const c of cookies) h.append('set-cookie', c)\n return new Response(null, { status: 302, headers: h })\n}\n\nexport function createCauthy(given: CauthyOptions = {}): Cauthy {\n const issuer = (given.issuer ?? env('CAUTHY_ISSUER') ?? '').replace(/\\/+$/, '')\n const clientId = given.clientId ?? env('CAUTHY_CLIENT_ID') ?? ''\n const clientSecret = given.clientSecret ?? env('CAUTHY_CLIENT_SECRET')\n const secret = given.secret ?? env('CAUTHY_SECRET') ?? ''\n const system = given.system ?? env('CAUTHY_SYSTEM') ?? ''\n if (!/^https?:\\/\\//.test(issuer)) throw new CauthyConfigError('issuer (CAUTHY_ISSUER) must be Cauthy\\'s address, e.g. https://id.example.com')\n if (clientId === '') throw new CauthyConfigError('clientId (CAUTHY_CLIENT_ID) is required')\n if (secret.length < 32) throw new CauthyConfigError('secret (CAUTHY_SECRET) must be at least 32 characters: it encrypts the session cookie')\n\n const basePath = '/' + (given.basePath ?? '/api/auth').replace(/^\\/+|\\/+$/g, '')\n const afterSignIn = localPath(given.afterSignIn, '/')\n const afterSignOut = localPath(given.afterSignOut, '/')\n const cookieName = given.cookieName ?? 'cauthy_session'\n const flowCookie = `${cookieName}_flow`\n const doFetch: typeof fetch = (...args) => (given.fetch ?? fetch)(...args)\n const key = keyOf(secret)\n\n const originOf = (request: Request) => (given.baseURL ?? new URL(request.url).origin).replace(/\\/+$/, '')\n const attrs = (request: Request, maxAge: number): CookieAttributes => ({ secure: originOf(request).startsWith('https://'), maxAge })\n\n // ── Talking to Cauthy ────────────────────────────────────────────────────\n\n async function token(form: Record<string, string>): Promise<{ ok: true; body: any } | { ok: false; ended: boolean }> {\n const body = new URLSearchParams({ client_id: clientId, ...form })\n if (clientSecret) body.set('client_secret', clientSecret)\n try {\n const res = await doFetch(`${issuer}/v1/oauth/token`, {\n method: 'POST',\n headers: { 'content-type': 'application/x-www-form-urlencoded', accept: 'application/json' },\n body,\n })\n if (res.ok) return { ok: true, body: await res.json() }\n // 4xx is Cauthy's answer: the grant is no good. Anything else is a\n // failure to ask, and says nothing about the session.\n return { ok: false, ended: res.status >= 400 && res.status < 500 }\n } catch {\n return { ok: false, ended: false }\n }\n }\n\n async function userinfo(accessToken: string): Promise<{ u: SessionUser; tid: string; roles: string[] } | null> {\n try {\n const res = await doFetch(`${issuer}/v1/oauth/userinfo`, { headers: { authorization: `Bearer ${accessToken}` } })\n if (!res.ok) return null\n const b = (await res.json()) as Record<string, any>\n return {\n tid: String(b.tid ?? ''),\n roles: Array.isArray(b.roles) ? b.roles.filter((r: unknown) => typeof r === 'string') : [],\n u: {\n id: String(b.sub ?? ''),\n email: String(b.email ?? ''),\n name: String(b.name ?? ''),\n emailVerified: b.email_verified === true,\n restricted: b.restricted === true,\n ...(typeof b.email_verify_by === 'string' ? { emailVerifyBy: b.email_verify_by } : {}),\n ...(typeof b.locale === 'string' && b.locale !== '' ? { locale: b.locale } : {}),\n },\n }\n } catch {\n return null\n }\n }\n\n /** What Cauthy's answer to a token request becomes in the cookie. */\n async function stored(body: any): Promise<Stored | null> {\n if (typeof body?.access_token !== 'string' || typeof body?.refresh_token !== 'string') return null\n const who = await userinfo(body.access_token)\n if (who === null || who.u.id === '') return null\n const lifetime = typeof body.expires_in === 'number' && body.expires_in > 0 ? body.expires_in : 300\n return { at: body.access_token, rt: body.refresh_token, exp: Math.floor(Date.now() / 1000) + lifetime, ...who }\n }\n\n function toSession(s: Stored): Session {\n return { user: s.u, tenantId: s.tid, roles: s.roles, accessToken: s.at, expiresAt: new Date(s.exp * 1000).toISOString() }\n }\n\n // ── The cookie ───────────────────────────────────────────────────────────\n\n async function read(request: Request): Promise<{ had: Map<string, string>; value: Stored | null; present: boolean }> {\n const had = readCookies(request.headers.get('cookie'))\n const raw = readParts(had, cookieName)\n if (raw === null) return { had, value: null, present: false }\n const value = await unseal<Stored>(raw, await key)\n const sound = value !== null && typeof value.at === 'string' && typeof value.rt === 'string' && typeof value.exp === 'number'\n return { had, value: sound ? value : null, present: true }\n }\n\n async function write(request: Request, s: Stored, had: Map<string, string>): Promise<string[]> {\n return writeParts(cookieName, await seal(s, await key), attrs(request, SESSION_DAYS * 86_400), had)\n }\n\n const fresh = (s: Stored) => s.exp - Date.now() / 1000 > SKEW_SECONDS\n\n // One renewal per refresh token at a time in this process: requests that\n // arrive together share it. (Across processes Cauthy gives a few seconds'\n // grace to the same token, for the same reason.)\n const renewing = new Map<string, Promise<Stored | 'ended' | 'unreachable'>>()\n\n function renew(was: Stored): Promise<Stored | 'ended' | 'unreachable'> {\n let running = renewing.get(was.rt)\n if (running === undefined) {\n const rt = was.rt\n running = (async (): Promise<Stored | 'ended' | 'unreachable'> => {\n const res = await token({ grant_type: 'refresh_token', refresh_token: rt })\n if (!res.ok) return res.ended ? 'ended' : 'unreachable'\n const next = await stored(res.body)\n if (next !== null) return next\n // Renewed, but who it is could not be read just now. The new tokens\n // are kept all the same — the old refresh token is spent, and losing\n // its successor would lose the session — with what was known before.\n if (typeof res.body?.access_token !== 'string' || typeof res.body?.refresh_token !== 'string') return 'unreachable'\n const lifetime = typeof res.body.expires_in === 'number' && res.body.expires_in > 0 ? res.body.expires_in : 300\n return { ...was, at: res.body.access_token, rt: res.body.refresh_token, exp: Math.floor(Date.now() / 1000) + lifetime }\n })()\n renewing.set(rt, running)\n // A renewal that worked is remembered for a moment, for requests still\n // arriving with the old cookie; one that could not ask is tried again\n // by the next request.\n void running.then((result) => {\n if (result === 'unreachable') return renewing.delete(rt)\n const timer = setTimeout(() => renewing.delete(rt), 5_000)\n ;(timer as { unref?: () => void }).unref?.()\n })\n }\n return running\n }\n\n async function resolve(request: Request): Promise<Resolved> {\n const { had, value, present } = await read(request)\n if (value === null) {\n // A cookie this key did not make (a rotated secret, a tampered value) is cleared.\n return { session: null, status: 'unauthenticated', cookies: present ? clearParts(cookieName, attrs(request, 0), had) : [] }\n }\n if (fresh(value)) return { session: toSession(value), status: 'authenticated', cookies: [] }\n\n const renewed = await renew(value)\n if (renewed === 'ended') {\n return { session: null, status: 'unauthenticated', cookies: clearParts(cookieName, attrs(request, 0), had) }\n }\n // Cauthy could not be asked: the cookie stays as it is, and the next\n // request tries again. Nobody is let in on an expired token meanwhile.\n if (renewed === 'unreachable') return { session: null, status: 'expired', cookies: [] }\n return { session: toSession(renewed), status: 'authenticated', cookies: await write(request, renewed, had) }\n }\n\n async function getSession(request: Request, responseHeaders?: Headers): Promise<Session | null> {\n if (responseHeaders === undefined) {\n const { value } = await read(request)\n return value !== null && fresh(value) ? toSession(value) : null\n }\n const r = await resolve(request)\n for (const c of r.cookies) responseHeaders.append('set-cookie', c)\n return r.session\n }\n\n async function status(request: Request): Promise<SessionStatus> {\n const { value } = await read(request)\n if (value === null) return 'unauthenticated'\n return fresh(value) ? 'authenticated' : 'expired'\n }\n\n // ── Organizations ────────────────────────────────────────────────────────\n\n const orgCache = new Map<string, { at: number; value: SessionOrganization[] }>()\n\n async function organizations(session: Session): Promise<SessionOrganization[]> {\n const hit = orgCache.get(session.accessToken)\n if (hit && Date.now() - hit.at < ORGANIZATIONS_TTL_MS) return hit.value\n const url = `${issuer}/v1/oauth/organizations${system ? `?system=${encodeURIComponent(system)}` : ''}`\n const res = await doFetch(url, { headers: { authorization: `Bearer ${session.accessToken}` } })\n if (!res.ok) throw new Error(`cauthy-auth: reading organizations: ${res.status}`)\n const body = (await res.json()) as { data?: Record<string, any>[] }\n const value = (body.data ?? []).map(\n (o): SessionOrganization => ({\n id: String(o.id),\n system: String(o.system ?? ''),\n name: String(o.name ?? ''),\n kind: String(o.kind ?? ''),\n role: String(o.role ?? ''),\n metadata: (o.metadata as Record<string, JsonValue>) ?? {},\n ...(o.external_id ? { externalId: String(o.external_id) } : {}),\n ...(Array.isArray(o.permissions) ? { permissions: o.permissions.map(String), denied: Array.isArray(o.denied) ? o.denied.map(String) : [] } : {}),\n }),\n )\n if (orgCache.size > 5_000) orgCache.clear()\n orgCache.set(session.accessToken, { at: Date.now(), value })\n return value\n }\n\n async function toPublic(session: Session): Promise<PublicSession> {\n let orgs: SessionOrganization[] = []\n try {\n orgs = await organizations(session)\n } catch {\n // The person is signed in all the same; their organizations come back on the next read.\n }\n return { user: session.user, tenantId: session.tenantId, roles: session.roles, organizations: orgs, expiresAt: session.expiresAt }\n }\n\n // ── Routes ───────────────────────────────────────────────────────────────\n\n const urls = {\n signIn: (returnTo?: string) => `${basePath}/login${returnTo ? `?return_to=${encodeURIComponent(returnTo)}` : ''}`,\n signOut: () => `${basePath}/logout`,\n account: (returnTo?: string) => `${basePath}/account${returnTo ? `?return_to=${encodeURIComponent(returnTo)}` : ''}`,\n session: `${basePath}/session`,\n }\n\n async function login(request: Request, url: URL): Promise<Response> {\n const flow: Flow = { state: random(), verifier: random(48), returnTo: localPath(url.searchParams.get('return_to'), afterSignIn) }\n const challenge = b64url(new Uint8Array(await crypto.subtle.digest('SHA-256', new TextEncoder().encode(flow.verifier))))\n const q = new URLSearchParams({\n client_id: clientId,\n redirect_uri: `${originOf(request)}${basePath}/callback`,\n response_type: 'code',\n state: flow.state,\n code_challenge: challenge,\n code_challenge_method: 'S256',\n })\n // What the application may ask of the sign-in page: to ask again, and a language.\n const prompt = url.searchParams.get('prompt')\n if (prompt === 'login' || prompt === 'none') q.set('prompt', prompt)\n const locale = url.searchParams.get('locale') ?? url.searchParams.get('ui_locales')\n if (locale && /^[a-zA-Z-]{2,10}$/.test(locale)) q.set('ui_locales', locale)\n return redirect(`${issuer}/v1/oauth/authorize?${q}`, [\n writeCookie(flowCookie, await seal(flow, await key), attrs(request, FLOW_SECONDS)),\n ])\n }\n\n function failed(request: Request, reason: string): Response {\n const h = new Headers({ 'content-type': 'text/plain; charset=utf-8', 'cache-control': 'no-store' })\n h.append('set-cookie', writeCookie(flowCookie, '', attrs(request, 0)))\n return new Response(`Sign-in could not be completed (${reason}). Go back and try again.`, { status: 400, headers: h })\n }\n\n async function callback(request: Request, url: URL): Promise<Response> {\n const had = readCookies(request.headers.get('cookie'))\n const sealed = had.get(flowCookie)\n const flow = sealed ? await unseal<Flow>(sealed, await key) : null\n // The answer must be to a question THIS browser asked: without that, a\n // stranger's link could sign a person in to the stranger's account.\n if (flow === null || url.searchParams.get('state') !== flow.state) return failed(request, 'state_mismatch')\n const error = url.searchParams.get('error')\n if (error !== null) {\n // prompt=none and nobody signed in: not an error, just \"nobody\".\n if (error === 'login_required') return redirect(flow.returnTo, [writeCookie(flowCookie, '', attrs(request, 0))])\n return failed(request, error.slice(0, 60))\n }\n const code = url.searchParams.get('code')\n if (!code) return failed(request, 'no_code')\n const res = await token({\n grant_type: 'authorization_code',\n code,\n redirect_uri: `${originOf(request)}${basePath}/callback`,\n code_verifier: flow.verifier,\n })\n const s = res.ok ? await stored(res.body) : null\n if (s === null) return failed(request, 'exchange_failed')\n return redirect(flow.returnTo, [...(await write(request, s, had)), writeCookie(flowCookie, '', attrs(request, 0))])\n }\n\n async function logout(request: Request): Promise<Response> {\n const { had, value } = await read(request)\n if (value !== null) {\n // Best effort: the session is over here whether or not Cauthy heard.\n const body = new URLSearchParams({ client_id: clientId, token: value.rt })\n if (clientSecret) body.set('client_secret', clientSecret)\n await doFetch(`${issuer}/v1/oauth/revoke`, {\n method: 'POST',\n headers: { 'content-type': 'application/x-www-form-urlencoded' },\n body,\n }).catch(() => undefined)\n orgCache.delete(value.at)\n }\n // Then through Cauthy, which ends its own session in this browser too:\n // otherwise the next \"sign in\" would walk straight back in.\n const q = new URLSearchParams({ client_id: clientId, post_logout_redirect_uri: `${originOf(request)}${afterSignOut}` })\n return redirect(`${issuer}/v1/oauth/logout?${q}`, clearParts(cookieName, attrs(request, 0), had))\n }\n\n async function handler(request: Request): Promise<Response> {\n const url = new URL(request.url)\n if (url.pathname !== basePath && !url.pathname.startsWith(`${basePath}/`)) return new Response('Not found', { status: 404 })\n const route = url.pathname.slice(basePath.length).replace(/\\/+$/, '') || '/'\n const get = request.method === 'GET' || request.method === 'HEAD'\n\n if (route === '/login' && get) return login(request, url)\n if (route === '/callback' && get) return callback(request, url)\n if (route === '/logout' && (get || request.method === 'POST')) return logout(request)\n\n if (route === '/session' && get) {\n const r = await resolve(request)\n const h = new Headers()\n for (const c of r.cookies) h.append('set-cookie', c)\n return json({ session: r.session ? await toPublic(r.session) : null, status: r.status }, 200, h)\n }\n\n // For a page that found its session expired: renew, and go back to it.\n if (route === '/refresh' && get) {\n const r = await resolve(request)\n const back = localPath(url.searchParams.get('return_to'), afterSignIn)\n if (r.status === 'unauthenticated') return redirect(urls.signIn(back), r.cookies)\n if (r.status === 'expired') return new Response('Sign-in is unavailable right now. Try again in a moment.', { status: 503 })\n return redirect(back, r.cookies)\n }\n\n if (route === '/account' && get) {\n const q = new URLSearchParams({ client_id: clientId })\n const back = localPath(url.searchParams.get('return_to'), '')\n if (back !== '') q.set('return_to', `${originOf(request)}${back}`)\n const locale = url.searchParams.get('locale')\n if (locale && /^[a-zA-Z-]{2,10}$/.test(locale)) q.set('ui_locales', locale)\n return redirect(`${issuer}/v1/oauth/account?${q}`)\n }\n return new Response('Not found', { status: 404 })\n }\n\n async function protect(request: Request) {\n const { value } = await read(request)\n if (value !== null && fresh(value)) return { session: toSession(value) }\n const url = new URL(request.url)\n const state = value === null ? 'none' : 'expired'\n // A page is sent to sign in, or to renew and come back. Anything else —\n // a fetch, a form post — is told, and its caller decides.\n const page = request.method === 'GET' && (request.headers.get('sec-fetch-mode') ?? 'navigate') === 'navigate' &&\n (request.headers.get('accept') ?? 'text/html').includes('text/html')\n if (!page) {\n return { response: json({ error: 'unauthenticated', session: state }, 401, new Headers({ 'x-cauthy-session': state })) }\n }\n const back = url.pathname + url.search\n const to = value === null ? urls.signIn(back) : `${basePath}/refresh?return_to=${encodeURIComponent(back)}`\n return { response: redirect(to) }\n }\n\n // The endpoint's public address: the application's origin when it was\n // given (a proxy may hide it from the request), with the request's path.\n const webhook: Cauthy['webhook'] = (request, o = {}) =>\n verifyWebhook(request, { issuer, url: o.url ?? `${originOf(request)}${new URL(request.url).pathname}`, jwks: o.jwks, now: o.now })\n\n return {\n handler,\n webhook,\n system: createSystem({ issuer, clientId, clientSecret, key: given.systemKey, fetch: given.fetch }),\n getSession,\n status,\n resolve,\n protect,\n organizations,\n toPublic,\n urls,\n options: { issuer, clientId, basePath, afterSignIn, afterSignOut, cookieName },\n }\n}\n","// What a person may do in an organization.\n//\n// When a system keeps its catalog of roles in Cauthy, each of a person's\n// organizations arrives with what their role — and anything given or denied to\n// them apart from it — lets them do. Checking is a lookup in that list: no\n// call to Cauthy, on the server or in the browser.\n\n/** The part of an organization that says what its member may do. */\nexport interface Permitted {\n /** \"resource:action\" entries the person is allowed; \"*\" is any. Absent when the system keeps no catalog in Cauthy. */\n permissions?: string[]\n /** Entries the person is denied whatever else allows them. */\n denied?: string[]\n}\n\nfunction matches(entry: string, resource: string, action: string): boolean {\n // The resource may itself hold a colon: the action is what follows the last one.\n const i = entry.lastIndexOf(':')\n if (i < 0) return false\n const r = entry.slice(0, i)\n const a = entry.slice(i + 1)\n return (r === '*' || r === resource) && (a === '*' || a === action)\n}\n\n/**\n * Whether the person may do an action on a resource in an organization:\n * allowed by some entry, and denied by none. A denial wins.\n *\n * An organization of a system that keeps no catalog in Cauthy carries no\n * permissions, and the answer is `false`: decide by its `role` instead.\n *\n * if (!can(organization, 'invoicing', 'write')) return forbidden()\n */\nexport function can(organization: Permitted | null | undefined, resource: string, action: string): boolean {\n if (!organization?.permissions) return false\n if ((organization.denied ?? []).some((d) => matches(d, resource, action))) return false\n return organization.permissions.some((p) => matches(p, resource, action))\n}\n\n// ── A system's catalog of roles ────────────────────────────────────────────\n\n/** Something a role may do: an action on a resource, in organizations of a kind (\"*\", the default, is every kind). */\nexport interface PolicyPermission {\n kind?: string\n resource: string\n action: string\n}\n\nexport interface PolicyRole {\n role: string\n name?: string\n description?: string\n /** Roles whose permissions this one has as well. */\n inherits?: string[]\n permissions?: PolicyPermission[]\n}\n\nexport interface PushPolicyOptions {\n /** The system whose catalog this is. Default: `CAUTHY_SYSTEM`. */\n system?: string\n roles: PolicyRole[]\n /** Cauthy's address. Default: `CAUTHY_ISSUER`. */\n issuer?: string\n /** The workspace's slug. Default: `CAUTHY_TENANT`. */\n tenant?: string\n /** A personal access token with `organizations:manage`. Default: `CAUTHY_TOKEN`. */\n token?: string\n fetch?: typeof fetch\n}\n\n/**\n * Gives Cauthy a system's catalog of roles, replacing the one it has. It is\n * the way to load a first catalog from what a system already had; from then on\n * the catalog is Cauthy's, edited in its dashboard, and pushing again would\n * overwrite what was edited there.\n */\nexport async function pushPolicy(o: PushPolicyOptions): Promise<void> {\n const env = (n: string) => (typeof process !== 'undefined' ? process.env?.[n] : undefined)\n const issuer = (o.issuer ?? env('CAUTHY_ISSUER') ?? '').replace(/\\/+$/, '')\n const system = o.system ?? env('CAUTHY_SYSTEM') ?? ''\n const tenant = o.tenant ?? env('CAUTHY_TENANT') ?? ''\n const token = o.token ?? env('CAUTHY_TOKEN') ?? ''\n for (const [name, v] of Object.entries({ CAUTHY_ISSUER: issuer, CAUTHY_SYSTEM: system, CAUTHY_TENANT: tenant, CAUTHY_TOKEN: token })) {\n if (v === '') throw new Error(`cauthy-auth: ${name} is required to push a policy`)\n }\n const res = await (o.fetch ?? fetch)(`${issuer}/v1/systems/${encodeURIComponent(system)}/policy`, {\n method: 'PUT',\n headers: { authorization: `Bearer ${token}`, 'x-tenant-key': tenant, 'content-type': 'application/json' },\n body: JSON.stringify({ roles: o.roles }),\n })\n if (!res.ok) {\n const body = (await res.json().catch(() => null)) as { errors?: { message?: string }[] } | null\n throw new Error(`cauthy-auth: Cauthy refused the policy (${res.status}): ${body?.errors?.[0]?.message ?? res.statusText}`)\n }\n}\n","// Connecting an application to Cauthy in one call: its client, where its\n// people come back to, where it hears events, and its own identity. It is what\n// `cauthy-auth init` runs, and what Cauthy's dashboard does under \"Connect a\n// system\".\n\nexport interface ConnectOptions {\n /** The system's name, as its organizations are filed under: lowercase, digits, - or _. */\n system: string\n /** The application's public address: `https://app.example.com`, or `http://localhost:3000` while developing. */\n appUrl: string\n /** Shown to people on Cauthy's pages. Default: the system's name. */\n name?: string\n /** Where the application serves the return from a sign-in. Default: `/api/auth/callback`. */\n callbackPath?: string\n /** An https address of the application that receives Cauthy's events. */\n eventsUrl?: string\n /** The events to receive. Default: all of them. */\n events?: string[]\n /** Where the application publishes its keys, to prove itself without a secret. */\n jwksUri?: string\n /** Cauthy's address. Default: `CAUTHY_ISSUER`. */\n issuer?: string\n /** The workspace's slug. Default: `CAUTHY_TENANT`. */\n tenant?: string\n /** A personal access token with `systems:connect`. Default: `CAUTHY_TOKEN`. */\n token?: string\n fetch?: typeof fetch\n}\n\nexport interface Connected {\n system: string\n clientId: string\n /** Shown this once: Cauthy keeps only its hash. */\n clientSecret: string\n redirectUris: string[]\n webhook: { id: string; url: string; events: string[] } | null\n /**\n * Everything the application sets: what Cauthy answered, plus CAUTHY_SECRET,\n * the key of the session cookie, generated here and known to nobody else.\n */\n env: Record<string, string>\n}\n\nfunction sessionSecret(): string {\n const bytes = crypto.getRandomValues(new Uint8Array(32))\n let s = ''\n for (const b of bytes) s += String.fromCharCode(b)\n return btoa(s).replace(/\\+/g, '-').replace(/\\//g, '_').replace(/=+$/, '')\n}\n\nexport async function connectSystem(o: ConnectOptions): Promise<Connected> {\n const env = (n: string) => (typeof process !== 'undefined' ? process.env?.[n] : undefined)\n const issuer = (o.issuer ?? env('CAUTHY_ISSUER') ?? '').replace(/\\/+$/, '')\n const tenant = o.tenant ?? env('CAUTHY_TENANT') ?? ''\n const token = o.token ?? env('CAUTHY_TOKEN') ?? ''\n for (const [name, v] of Object.entries({ CAUTHY_ISSUER: issuer, CAUTHY_TENANT: tenant, CAUTHY_TOKEN: token })) {\n if (v === '') throw new Error(`cauthy-auth: ${name} is required to connect a system`)\n }\n const res = await (o.fetch ?? fetch)(`${issuer}/v1/connect`, {\n method: 'POST',\n headers: { authorization: `Bearer ${token}`, 'x-tenant-key': tenant, 'content-type': 'application/json' },\n body: JSON.stringify({ system: o.system, app_url: o.appUrl, name: o.name, callback_path: o.callbackPath,\n events_url: o.eventsUrl, events: o.events, jwks_uri: o.jwksUri }),\n })\n const body = (await res.json().catch(() => null)) as {\n data?: { system: string; client_secret: string; client: { client_id: string; redirect_uris: string[] }\n webhook: Connected['webhook']; env: Record<string, string> }\n errors?: { field?: string; message?: string }[]\n } | null\n if (!res.ok || !body?.data) {\n const why = (body?.errors ?? []).map((e) => e.message).filter(Boolean).join('; ') || res.statusText\n throw new Error(`cauthy-auth: Cauthy did not connect the system (${res.status}): ${why}`)\n }\n const d = body.data\n return { system: d.system, clientId: d.client.client_id, clientSecret: d.client_secret, redirectUris: d.client.redirect_uris,\n webhook: d.webhook, env: { ...d.env, CAUTHY_SECRET: sessionSecret() } }\n}\n\n/** The variables as the lines of an env file. */\nexport function envFile(env: Record<string, string>): string {\n return Object.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AG,EAAE;AAAA,EAC1F;AACA,QAAM,IAAI,KAAK;AACf,SAAO;AAAA,IAAE,QAAQ,EAAE;AAAA,IAAQ,UAAU,EAAE,OAAO;AAAA,IAAW,cAAc,EAAE;AAAA,IAAe,cAAc,EAAE,OAAO;AAAA,IAC7G,SAAS,EAAE;AAAA,IAAS,KAAK,EAAE,GAAG,EAAE,KAAK,eAAe,cAAc,EAAE;AAAA,EAAE;AAC1E;","names":["out","out","env","out","env","env","resolve","env","env"]}
|
package/dist/react.d.mts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import * as react from 'react';
|
|
2
2
|
import { ReactNode } from 'react';
|
|
3
|
-
import { P as PublicSession } from './system-
|
|
4
|
-
export { J as JsonValue, e as SessionOrganization, g as SessionUser } from './system-
|
|
3
|
+
import { P as PublicSession } from './system-CdvktS7x.mjs';
|
|
4
|
+
export { J as JsonValue, e as SessionOrganization, g as SessionUser } from './system-CdvktS7x.mjs';
|
|
5
5
|
export { d as can } from './permissions-B-a3eN0h.mjs';
|
|
6
6
|
import 'jose';
|
|
7
|
+
import './theme.mjs';
|
|
7
8
|
|
|
8
9
|
interface SessionContext {
|
|
9
10
|
/** The signed-in person and their organizations, or null. */
|
package/dist/react.d.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import * as react from 'react';
|
|
2
2
|
import { ReactNode } from 'react';
|
|
3
|
-
import { P as PublicSession } from './system-
|
|
4
|
-
export { J as JsonValue, e as SessionOrganization, g as SessionUser } from './system-
|
|
3
|
+
import { P as PublicSession } from './system-CL3Q30FT.js';
|
|
4
|
+
export { J as JsonValue, e as SessionOrganization, g as SessionUser } from './system-CL3Q30FT.js';
|
|
5
5
|
export { d as can } from './permissions-B-a3eN0h.js';
|
|
6
6
|
import 'jose';
|
|
7
|
+
import './theme.js';
|
|
7
8
|
|
|
8
9
|
interface SessionContext {
|
|
9
10
|
/** The signed-in person and their organizations, or null. */
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { JWK } from 'jose';
|
|
2
|
+
import { CauthyTheme } from './theme.js';
|
|
2
3
|
|
|
3
4
|
/** How an application is set up to sign its people in with Cauthy. */
|
|
4
5
|
interface CauthyOptions {
|
|
@@ -223,6 +224,15 @@ interface CauthySystem {
|
|
|
223
224
|
id: string;
|
|
224
225
|
email: string;
|
|
225
226
|
}>;
|
|
227
|
+
/**
|
|
228
|
+
* How the application looks on Cauthy's pages: read it, set it, or take it
|
|
229
|
+
* back to Cauthy's own with `null`. Of its own sign-in client — this one,
|
|
230
|
+
* unless another is named (a system whose identity is a separate client).
|
|
231
|
+
*/
|
|
232
|
+
theme: {
|
|
233
|
+
get(clientId?: string): Promise<CauthyTheme>;
|
|
234
|
+
set(theme: CauthyTheme | null, clientId?: string): Promise<void>;
|
|
235
|
+
};
|
|
226
236
|
/** The public half of `key`, to serve at the address Cauthy was given (`jwks_uri`). Empty with a secret. */
|
|
227
237
|
publicJwks(): Promise<{
|
|
228
238
|
keys: JWK[];
|