import { type AgentMode, isMoreAutonomous, modeCap, type Role } from "@cloudhal/agent-runtime"; import type { JWK } from "jose"; import { StaffStore } from "../staff.ts"; import { GrantStore, newGrantId } from "./grants.ts"; import { createInstance } from "./instance.ts"; import { type ApiKeyRow, roleScopesHold, type VerifiedApiKey } from "./key-rows.ts"; import { oauthDeps } from "./oauth-deps.ts"; import { AGENT_KEY_RATE_LIMIT, AuthError, type AuthOptions, NOT_A_MEMBER_MESSAGE, } from "./types.ts"; export function authDeps(options: AuthOptions) { const now = options.now ?? (() => new Date()); const staffStore = new StaffStore(options.sql, now); const keyLimit = options.agentKeyRateLimit ?? AGENT_KEY_RATE_LIMIT; // Marks the server's own consent call to the provider (`AgentGrantService.consent`). const grants = new GrantStore(options.sql, now); const internal = { serverKey: `${newGrantId()}${newGrantId()}`, grants }; const instance = createInstance(options, staffStore, internal); const api = instance.api as unknown as { getJwks(): Promise<{ keys: JWK[] }>; verifyApiKey(input: { body: { key: string } }): Promise; createApiKey(input: { body: { name: string; userId: string; permissions: Record; metadata?: Record; }; }): Promise; updateApiKey(input: { body: { keyId: string; userId: string; metadata?: Record; permissions?: Record; }; }): Promise; createUser(input: { body: { email: string; password: string; name: string; data?: Record }; }): Promise<{ user: { id: string; name: string; email: string } }>; signInEmail(input: { body: { email: string; password: string }; returnHeaders: true; }): Promise<{ headers: Headers; response: unknown }>; }; type AdapterLike = { findMany(input: { model: string; where?: { field: string; value: unknown }[]; limit?: number; }): Promise; findOne(input: { model: string; where: { field: string; value: unknown }[]; }): Promise; delete(input: { model: string; where: { field: string; value: unknown }[] }): Promise; }; type AuthContextLike = { adapter: AdapterLike; password: { hash(password: string): Promise; verify(input: { hash: string; password: string }): Promise; }; internalAdapter: { deleteUserSessions(userId: string): Promise; findCredentialAccount(userId: string): Promise<{ password?: string | null } | null>; updatePassword(userId: string, hash: string): Promise; createAccount(input: Record): Promise; findUserByEmail( email: string, ): Promise<{ user: { id: string; name: string; email: string } } | null>; updateUser(userId: string, data: Record): Promise; }; }; const context = () => (instance as unknown as { $context: Promise }).$context; const keyById = async (id: string) => (await context()).adapter.findOne({ model: "apikey", where: [{ field: "id", value: id }], }); const keysOf = async (userId: string) => (await context()).adapter.findMany({ model: "apikey", where: [{ field: "referenceId", value: userId }], limit: 500, }); /** The signed-in person and their membership (refuses non-members). */ const caller = async (headers: Headers, message: string) => { const session = await instance.api.getSession({ headers }); if (!session) throw new AuthError(message, { status: 401, code: "unauthorized" }); const member = await staffStore.member(session.user.id); if (!member) throw new AuthError(NOT_A_MEMBER_MESSAGE, { status: 403, code: "not_a_member" }); return { userId: session.user.id, member }; }; /** One of the caller's own keys, or with `manage` any key when the role holds agents:manage. */ const keyFor = async (headers: Headers, id: string, manage: boolean) => { const who = await caller(headers, "sign in to change agent keys"); const key = await keyById(id); if (!key) throw new AuthError("unknown agent key", { status: 404, code: "not_found" }); if (key.referenceId === who.userId) return { who, key, own: true }; const canManage = manage && roleScopesHold(who.member.role, "agents:manage"); // Someone else's key is invisible without agents:manage (no existence oracle). if (!canManage) throw new AuthError("unknown agent key", { status: 404, code: "not_found" }); return { who, key, own: false }; }; const capProblem = (role: Role, mode: AgentMode) => { const cap = modeCap(role); if (isMoreAutonomous(mode, cap)) throw new AuthError(`your role allows keys up to ${cap.toUpperCase()} mode`, { code: "mode_above_role", }); }; const oauth = oauthDeps({ options, api, grants, staffStore, keyLimit, now }); return { now, sql: options.sql, secret: options.secret, ...oauth, staffStore, grants, internal, keyLimit, instance, api, context, keyById, keysOf, caller, keyFor, capProblem, }; } export type AuthDeps = ReturnType;