Files
HealthLog/src/lib/mcp/auth.ts
T
Marc-André Bombeck 9b0d5dc0b7 chore(release): v1.28.17 — dashboard edits stick; share scope honoured
Dashboard layout changes take effect immediately: the save now evicts the
locale-keyed home-screen snapshot it previously missed, so a re-ordered or
toggled tile set no longer reverts to the prior selection for the SWR window.

A share link built with a narrowed report-section selection now serves exactly
that selection. The stored selection is the grouped export shape; the shared
view read it through the flat parser, which dropped every grouped toggle and
fell back to the full defaults — an over-share against the owner's choice. The
view now folds the grouped shape down faithfully. Documents-only shares were
never affected.

Hardening: the shared-document download path now sanitises the filename
identically to the owner download path, and thumbnail generation re-checks the
pixel ceiling after decode so a malformed image that slips past the header
sniff still cannot exhaust the worker.
2026-07-10 12:36:35 +02:00

61 lines
2.3 KiB
TypeScript

/**
* MCP auth context.
*
* Resolves a pasted `hlk_<hex>` Bearer token to a session-narrowed identity for
* the MCP transports. Authentication is delegated to the canonical
* `resolveBearerToken` (`@/lib/auth/bearer`) — the SAME validation the HTTP edge
* runs — so revoked / expired / scope semantics are identical across both wires
* (REQ-T1, REQ-SEC-5).
*
* Security properties:
* - The id is narrowed to one `userId`; tools and resources feed it straight
* into the Prisma `where`, never accepting a caller-supplied user id.
* - The session is bound to `<user_id>:<token_id>` (REQ-SEC-11), never to a
* cookie session — the MCP wire carries no cookie.
* - Admin is unreachable by construction: `requireAdmin()` is cookie-only and
* this path never mints a cookie session, so no MCP context can elevate
* (REQ-SEC-7 / ADR-005). The guard test pins it.
*/
import { resolveBearerToken } from "@/lib/auth/bearer";
import { sessionBinding, tokenAllowsRead, tokenAllowsWrite } from "./scopes";
export interface McpAuthContext {
/** The single user this session acts as. Feeds every Prisma `where`. */
userId: string;
/** The presented `ApiToken` id (the token half of the binding). */
tokenId: string;
/** The token's granted scopes. */
scopes: string[];
/** `<user_id>:<token_id>` — the session binding (REQ-SEC-11). */
binding: string;
/** Whether this session may read (always true for a valid token). */
canRead: boolean;
/** Whether this session may write (requires `health:write`; gates the live write tools). */
canWrite: boolean;
}
/**
* Resolve a raw Bearer token into an `McpAuthContext`, or throw on an invalid /
* revoked / expired token. Reads require no special scope (the underlying reads
* are unscoped), so no `requiredPermission` is passed.
*/
export async function resolveMcpAuthContext(
rawToken: string,
): Promise<McpAuthContext> {
const trimmed = rawToken.trim();
if (!trimmed) {
throw new Error("Missing MCP Bearer token");
}
const { user, tokenId, permissions } = await resolveBearerToken(trimmed);
return {
userId: user.id,
tokenId,
scopes: permissions,
binding: sessionBinding(user.id, tokenId),
canRead: tokenAllowsRead(),
canWrite: tokenAllowsWrite(permissions),
};
}