A remote user is name@host here, in users.username, in paths (/@alice@music.example) and in mentions, through one handle rule in shared/validation.ts that paths, MENTION_RE and Markdown all build on; Markdown undoes GFM's email autolink of a handle. Their name, email and avatar come from their home at each sign-in and by its signed events (federation_outbox, delivered by the federation job with backoff): a rename at home leaves the old handle an alias here, and a password change or a withdrawn grant ends their sessions here. Their password, email, avatar and name can't change here (assertLocalUser), nor can they sign in elsewhere from here. An admin keeps the trusted and blocked instances on /manage (federation:manage): a trusted instance's users sign in from a button on the login page, a blocked one's can't and are signed out at once, and any other's join with an invite from the register page. The /authorize page asks the user once per instance, and /settings lists the instances their account signs in to, each disconnectable; /login?next= brings a signed-out visitor back to /authorize. Emails stay unique among local accounts only, and migration 0024 adds the tables, makes that index partial and reserves authorize and federation as category slugs. remote/gerbeur-instance.ts is the only way to other instances: https and public addresses only, no redirects, no certificate fallback, unless GERBEUR_FEDERATION_ALLOW_PRIVATE lets two instances on one machine talk. 298 backend tests and 702 frontend tests pass, and deno check, deno fmt, deno lint, tsc, eslint, doc:lint and i18n:compile --strict are clean; checked end to end with two built instances on one machine: sign-in through the other's login and consent pages, consent skipped once granted and asked again after disconnecting, a rename and a password change at home reaching the other, and blocking; screenshots of the login, consent, profile, settings and manage pages at 1280 and 390px. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
74 lines
2.4 KiB
TypeScript
74 lines
2.4 KiB
TypeScript
/**
|
|
* Keeps the instance's Ed25519 key, which signs what it tells other instances
|
|
* in federated sign-in: the identity tokens of `identity-provider-service.ts`
|
|
* and the events of its outbox. The key is made the first time it's needed
|
|
* and stored in `federation_keys`, so it survives restarts; other instances
|
|
* fetch its public half from `GET /.well-known/jwks.json`.
|
|
*
|
|
* @module
|
|
*/
|
|
|
|
import type { JSONWebKeySet, JWK } from "@panva/jose";
|
|
import { db } from "../db/connection.ts";
|
|
import {
|
|
generateInstanceKey,
|
|
importInstanceKey,
|
|
type InstanceSigningKey,
|
|
} from "../lib/jwt.ts";
|
|
|
|
/**
|
|
* Holds the key once loaded. It is a promise so that two first requests in a
|
|
* row don't each make a key.
|
|
*/
|
|
let current: Promise<InstanceSigningKey> | null = null;
|
|
|
|
/**
|
|
* Returns the key the instance signs with now: the newest stored, or a new
|
|
* one, stored, when there is none.
|
|
*/
|
|
export function getSigningKey(): Promise<InstanceSigningKey> {
|
|
current ??= loadOrCreate().catch((err) => {
|
|
current = null;
|
|
throw err;
|
|
});
|
|
return current;
|
|
}
|
|
|
|
/** Reads the newest stored key, or makes and stores the first one. */
|
|
async function loadOrCreate(): Promise<InstanceSigningKey> {
|
|
const row = db.prepare(
|
|
`SELECT kid, private_jwk FROM federation_keys ORDER BY created_at DESC LIMIT 1;`,
|
|
).get() as { kid: string; private_jwk: string } | undefined;
|
|
if (row) {
|
|
return await importInstanceKey(row.kid, JSON.parse(row.private_jwk));
|
|
}
|
|
const { kid, privateJwk, publicJwk } = await generateInstanceKey();
|
|
db.prepare(
|
|
`INSERT INTO federation_keys (kid, private_jwk, public_jwk, created_at) VALUES (?, ?, ?, ?);`,
|
|
).run(
|
|
kid,
|
|
JSON.stringify(privateJwk),
|
|
JSON.stringify(publicJwk),
|
|
new Date().toISOString(),
|
|
);
|
|
return await importInstanceKey(kid, privateJwk);
|
|
}
|
|
|
|
/**
|
|
* Returns the public halves of every stored key, for
|
|
* `GET /.well-known/jwks.json`. It makes the first key when there is none, so
|
|
* that the set is never empty.
|
|
*/
|
|
export async function getPublicKeys(): Promise<JSONWebKeySet> {
|
|
await getSigningKey();
|
|
const rows = db.prepare(
|
|
`SELECT public_jwk FROM federation_keys ORDER BY created_at DESC;`,
|
|
).all() as { public_jwk: string }[];
|
|
return { keys: rows.map((r) => JSON.parse(r.public_jwk) as JWK) };
|
|
}
|
|
|
|
/** Forgets the loaded key, for the tests, which wipe the table between cases. */
|
|
export function resetSigningKey(): void {
|
|
current = null;
|
|
}
|