v4: users sign in to one gerbeur instance with their account on another — topic instances, each with its own admins, domain and data, had no way to share accounts. Sign-in is federated over a subset of OpenID Connect, the authorization code flow with PKCE: each instance is an identity provider for its own users and a client for the others', and signs what it tells them with an Ed25519 key kept in federation_keys and published at /.well-known/jwks.json, beside its metadata at /.well-known/openid-configuration. Only sign-in is federated: an account from another instance is a local users row linked to (issuer, subject) in user_identities, with no password (password_hash ''), so sessions, roles, permissions, the socket and every foreign key work as before.

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>
This commit is contained in:
khannurien
2026-10-09 14:23:00 +00:00
parent 2d26d0aada
commit c0d03e9aaa
67 changed files with 5261 additions and 339 deletions

View File

@@ -0,0 +1,326 @@
import {
createUser,
db,
del,
type FakeInstance,
fakeInstance,
formWithFile,
get,
patch,
PNG,
post,
put,
stubNetwork,
test,
} from "../helpers.ts";
import { assert, assertEquals, assertMatch } from "@std/assert";
import type {
FederationLoginResponse,
FederationPeer,
} from "../../../shared/federation.ts";
import type { AuthResponse, SelfUser } from "../../../shared/user.ts";
const HOME = "https://home.test";
/** Puts `fake` behind the network, every other host answering 404. */
function network(...fakes: FakeInstance[]) {
return stubNetwork((url, init) => {
for (const fake of fakes) {
const res = fake.handle(url, init);
if (res) return res;
}
return new Response("", { status: 404 });
});
}
async function trust(issuer: string, status = "trusted") {
const admin = await createUser({ role: "admin" });
return await put<FederationPeer>("/api/federation/peers", {
auth: admin.auth,
json: { instance: issuer, status },
});
}
/** Signs in through `home` as `identity`, as the pages would. */
async function signIn(
home: FakeInstance,
identity: {
sub: string;
preferred_username: string;
email: string;
picture?: string;
},
inviteToken?: string,
) {
const start = await post<FederationLoginResponse>("/api/federation/logins", {
json: { instance: new URL(home.origin).host, inviteToken },
});
if (start.status !== 200) return start;
const authorizeUrl = start.data.authorizeUrl;
const code = home.issue(authorizeUrl, identity);
return await post<AuthResponse>("/api/federation/sessions", {
json: { code, state: new URL(authorizeUrl).searchParams.get("state") },
});
}
const ALICE = {
sub: "alice-id",
preferred_username: "alice",
email: "alice@home.example",
picture: `${HOME}/api/users/alice-id/avatar?v=1`,
};
test("federation: a trusted instance's user signs in as name@host, once made", async () => {
const home = await fakeInstance(HOME);
using net = network(home);
assertEquals((await trust("home.test")).data.name, "Fake home.test");
const start = await post<FederationLoginResponse>("/api/federation/logins", {
json: { instance: "home.test" },
});
const url = new URL(start.data.authorizeUrl);
assertEquals(url.origin + url.pathname, `${HOME}/authorize`);
assertEquals(url.searchParams.get("client_id"), "http://gerbeur.test");
assertEquals(
url.searchParams.get("redirect_uri"),
"http://gerbeur.test/federation/callback",
);
assertEquals(url.searchParams.get("code_challenge_method"), "S256");
const first = await signIn(home, ALICE);
const { user, token } = first.data as AuthResponse;
assertEquals(user.username, "alice@home.test");
assertEquals(user.email, "alice@home.example");
assertEquals(user.role, "user");
assertEquals(user.avatarMime, "image/png");
assert(net.calls.some((u) => u.pathname === "/api/users/alice-id/avatar"));
const me = await get<SelfUser>("/api/me", {
auth: { Authorization: `Bearer ${token}` },
});
assertEquals(me.data.id, user.id);
// The same identity signs in to the same account.
const again = await signIn(home, ALICE);
assertEquals((again.data as AuthResponse).user.id, user.id);
// It has no password here.
const login = await post("/api/sessions", {
json: { username: "alice@home.test", password: "" },
});
assertEquals(login.status, 401);
assertEquals(
(await get<SelfUser>("/api/users/@alice@home.test")).data.id,
user.id,
);
// The client encodes the handle's `@`.
assertEquals(
(await get<SelfUser>("/api/users/@alice%40home.test")).data.id,
user.id,
);
});
test("federation: a login works once, with the code the home made for it", async () => {
const home = await fakeInstance(HOME);
using _net = network(home);
await trust(HOME);
const start = await post<FederationLoginResponse>("/api/federation/logins", {
json: { instance: HOME },
});
const state = new URL(start.data.authorizeUrl).searchParams.get("state");
const wrong = await post("/api/federation/sessions", {
json: { code: "made-up", state },
});
assertEquals(wrong.status, 502);
// The failed try used the login up.
const code = home.issue(start.data.authorizeUrl, ALICE);
const replay = await post("/api/federation/sessions", {
json: { code, state },
});
assertEquals(replay.status, 400);
});
test("federation: an instance on neither list needs an invite, claimed on joining", async () => {
const home = await fakeInstance(HOME);
using _net = network(home);
const refused = await signIn(home, ALICE);
assertEquals(refused.status, 403);
const inviter = await createUser();
const { token } = (await post<{ token: string }>("/api/invites", {
auth: inviter.auth,
})).data;
const joined = await signIn(home, ALICE, token);
const { user } = joined.data as AuthResponse;
assertEquals(user.username, "alice@home.test");
const row = db.prepare(`SELECT invited_by FROM users WHERE id = ?;`).get(
user.id,
) as { invited_by: string };
assertEquals(row.invited_by, inviter.id);
assertEquals((await get(`/api/invites/${token}`)).status, 404);
// Once here, no invite is needed; another user of that instance still
// needs one.
assertEquals((await signIn(home, ALICE)).status, 200);
const bob = { ...ALICE, sub: "bob-id", preferred_username: "bob" };
assertEquals((await signIn(home, bob)).status, 403);
});
test("federation: blocking an instance refuses its users and ends their sessions", async () => {
const home = await fakeInstance(HOME);
using _net = network(home);
await trust(HOME);
const { token } = (await signIn(home, ALICE)).data as AuthResponse;
const auth = { Authorization: `Bearer ${token}` };
assertEquals((await get("/api/me", { auth })).status, 200);
await trust(HOME, "blocked");
assertEquals((await get("/api/me", { auth })).status, 401);
assertEquals((await signIn(home, ALICE)).status, 403);
const admin = await createUser({ role: "admin" });
const peers = await get<FederationPeer[]>("/api/federation/peers", {
auth: admin.auth,
});
assertEquals(peers.data.map((p) => p.status), ["blocked"]);
assertEquals((await get<FederationPeer[]>("/api/federation/peers")).data, []);
assertEquals(
(await del("/api/federation/peers/home.test", { auth: admin.auth }))
.status,
200,
);
assertEquals((await get("/api/me", { auth })).status, 200);
});
test("federation: only admins edit the peers, which must answer as gerbeurs", async () => {
const home = await fakeInstance(HOME);
using _net = network(home);
const user = await createUser();
const admin = await createUser({ role: "admin" });
assertEquals(
(await put("/api/federation/peers", {
auth: user.auth,
json: { instance: HOME, status: "trusted" },
})).status,
403,
);
for (const instance of ["elsewhere.test", "https://home.test/path"]) {
const res = await put("/api/federation/peers", {
auth: admin.auth,
json: { instance, status: "trusted" },
});
assert(res.status >= 400, instance);
}
assertEquals(
(await put("/api/federation/peers", {
auth: admin.auth,
json: { instance: "http://gerbeur.test", status: "trusted" },
})).status,
400,
);
});
test("federation: the home's events rename, update and sign out its users", async () => {
const home = await fakeInstance(HOME);
const other = await fakeInstance("https://other.test");
using _net = network(home, other);
await trust(HOME);
const { user, token } = (await signIn(home, ALICE)).data as AuthResponse;
const auth = { Authorization: `Bearer ${token}` };
const update = await home.event(ALICE.sub, {
type: "update",
claims: { ...ALICE, preferred_username: "alicia", email: "a@new.example" },
});
assertEquals(
(await post("/api/federation/events", { json: { event: update } }))
.status,
204,
);
const renamed = await get<SelfUser>("/api/me", { auth });
assertEquals(renamed.data.username, "alicia@home.test");
assertEquals(renamed.data.email, "a@new.example");
// The old handle still leads to them.
assertEquals(
(await get<SelfUser>("/api/users/@alice@home.test")).data.id,
user.id,
);
// Another instance can't speak for this one's users.
const forged = await other.event(ALICE.sub, { type: "revoke" });
assertEquals(
(await post("/api/federation/events", { json: { event: forged } }))
.status,
400,
);
// Revoking a little later than the token was issued, as it would be.
await new Promise((r) => setTimeout(r, 1100));
const revoke = await home.event(ALICE.sub, { type: "revoke" });
assertEquals(
(await post("/api/federation/events", { json: { event: revoke } }))
.status,
204,
);
assertEquals((await get("/api/me", { auth })).status, 401);
});
test("federation: an account from another instance changes its password, email, avatar and name there", async () => {
const home = await fakeInstance(HOME);
using _net = network(home);
await trust(HOME);
const { user, token } = (await signIn(home, ALICE)).data as AuthResponse;
const auth = { Authorization: `Bearer ${token}` };
const admin = await createUser({ role: "admin" });
assertEquals(
(await put("/api/me/password", {
auth,
json: { currentPassword: "", newPassword: "long-enough" },
})).status,
400,
);
assertEquals(
(await patch("/api/me", { auth, json: { email: "x@example.test" } }))
.status,
400,
);
assertEquals(
(await patch<SelfUser>("/api/me", { auth, json: { description: "hi" } }))
.data.description,
"hi",
);
assertEquals(
(await put("/api/me/avatar", { auth, form: formWithFile(PNG) })).status,
400,
);
assertEquals(
(await patch(`/api/users/${user.id}/username`, {
auth: admin.auth,
json: { username: "bob" },
})).status,
400,
);
});
test("federation: an email may be both a local account's and a remote one's", async () => {
const home = await fakeInstance(HOME);
using _net = network(home);
await trust(HOME);
const local = await createUser({ email: ALICE.email });
const res = await signIn(home, ALICE);
assertMatch((res.data as AuthResponse).user.username, /@home\.test$/);
// A reset by that address reaches the local account only.
const row = db.prepare(
`SELECT COUNT(*) AS n FROM users WHERE lower(email) = lower(?);`,
).get(ALICE.email) as { n: number };
assertEquals(row.n, 2);
assertEquals(
(await post("/api/password-resets", { json: { email: ALICE.email } }))
.status,
200,
);
const tokens = db.prepare(`SELECT user_id FROM password_reset_tokens;`)
.all() as { user_id: string }[];
assert(tokens.every((t) => t.user_id === local.id));
});