peppa/packages/core/src/access.ts
Асель 73966e7e99 PEP-16: only allowlisted people can log into Peppa
- platform.allowed_logins (default asel, 7601978, dana): allowlisted logins,
  project invitees and members, and local CLI accounts may log in
- enforced in the Gitea OAuth callback, on every web request (removal takes
  effect at once) and on every remote MCP request (401)
- CLI: peppa platform allowlist | allow <login> | deny <login>
- tests: one per spec scenario (69 e2e total, green)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 03:38:08 +05:00

34 lines
1.6 KiB
TypeScript

// Who may log into Peppa at all (web and remote MCP): an allowlist of Gitea logins, plus
// people invited to or members of a project, plus local accounts without Gitea (mock-…).
import { projectInvites, projectMembers, type Db } from "@peppa/db";
import { eq } from "drizzle-orm";
import { getSetting, setSetting } from "./settings.ts";
import type { User } from "./users.ts";
const ALLOWED = "platform.allowed_logins";
const DEFAULT_ALLOWED = ["asel", "7601978", "dana"];
export async function allowedLogins(db: Db): Promise<string[]> {
return (await getSetting<string[]>(db, ALLOWED)) ?? DEFAULT_ALLOWED;
}
export async function setAllowedLogins(db: Db, logins: string[]): Promise<void> {
await setSetting(db, ALLOWED, [...new Set(logins.map((l) => l.trim().toLowerCase()).filter(Boolean))].sort());
}
/** A Gitea login may log in if it is allowlisted or has a pending project invitation. */
export async function isLoginAllowed(db: Db, login: string): Promise<boolean> {
const l = login.toLowerCase();
if ((await allowedLogins(db)).includes(l)) return true;
const [invite] = await db.select().from(projectInvites).where(eq(projectInvites.login, l)).limit(1);
return !!invite;
}
/** An existing user keeps access while allowlisted, a project member, or a local account. */
export async function isUserAllowed(db: Db, user: User): Promise<boolean> {
if (user.giteaId < 0) return true; // local account (created by the CLI), can't come from Gitea OAuth
if (await isLoginAllowed(db, user.login)) return true;
const [m] = await db.select().from(projectMembers).where(eq(projectMembers.userId, user.id)).limit(1);
return !!m;
}