import type { LegacyApiKeySimple } from "@toolctrl/db/api-keys.server"; import type { ServicePrincipal } from "../auth.js"; export const API_KEY_SCOPE_OPERATIONS = ["read", "create", "update", "delete"] as const; export type ApiKeyScopeOperation = (typeof API_KEY_SCOPE_OPERATIONS)[number]; export type StoredApiKeyPrincipal = { type: "api-key"; apiKey: LegacyApiKeySimple; }; export type ApiScopedPrincipal = ServicePrincipal | StoredApiKeyPrincipal; export type ApiScopeRequest = { tenantId: string; entityId: string; operation: ApiKeyScopeOperation; now?: Date; }; export type ApiScopeDenyReason = | "api_key_inactive" | "api_key_expired" | "tenant_deactivated" | "tenant_mismatch" | "entity_permission_missing" | "operation_permission_missing"; export type ApiScopeAllowed = { allowed: true; principalType: ApiScopedPrincipal["type"]; }; export type ApiTenantScopeAllowed = ApiScopeAllowed & { tenantId: string | null; }; export type ApiScopeDenied = { allowed: false; principalType: ApiScopedPrincipal["type"]; reason: ApiScopeDenyReason; code: "FORBIDDEN_API_KEY_SCOPE"; }; export type ApiScopeResult = ApiScopeAllowed | ApiScopeDenied; function deny(principalType: ApiScopedPrincipal["type"], reason: ApiScopeDenyReason): ApiScopeDenied { return { allowed: false, principalType, reason, code: "FORBIDDEN_API_KEY_SCOPE", }; } export function createStoredApiKeyPrincipal(apiKey: LegacyApiKeySimple): StoredApiKeyPrincipal { return { type: "api-key", apiKey, }; } export function authorizeApiScope(principal: ApiScopedPrincipal, request: ApiScopeRequest): ApiScopeResult { if (principal.type === "service-token") { return { allowed: true, principalType: principal.type, }; } const { apiKey } = principal; if (!apiKey.active) { return deny(principal.type, "api_key_inactive"); } if (apiKey.expires && apiKey.expires.getTime() <= (request.now ?? new Date()).getTime()) { return deny(principal.type, "api_key_expired"); } if (apiKey.tenant.deactivatedReason) { return deny(principal.type, "tenant_deactivated"); } if (apiKey.tenantId !== request.tenantId || apiKey.tenant.id !== request.tenantId) { return deny(principal.type, "tenant_mismatch"); } const entityPermission = apiKey.entities.find((entity) => entity.entityId === request.entityId); if (!entityPermission) { return deny(principal.type, "entity_permission_missing"); } if (!entityPermission[request.operation]) { return deny(principal.type, "operation_permission_missing"); } return { allowed: true, principalType: principal.type, }; } export function resolveApiTenantScope(principal: ApiScopedPrincipal, requestedTenantId: string | null, now = new Date()): ApiTenantScopeAllowed | ApiScopeDenied { if (principal.type === "service-token") { return { allowed: true, principalType: principal.type, tenantId: requestedTenantId, }; } const { apiKey } = principal; if (!apiKey.active) { return deny(principal.type, "api_key_inactive"); } if (apiKey.expires && apiKey.expires.getTime() <= now.getTime()) { return deny(principal.type, "api_key_expired"); } if (apiKey.tenant.deactivatedReason) { return deny(principal.type, "tenant_deactivated"); } if (apiKey.tenantId !== apiKey.tenant.id || (requestedTenantId && requestedTenantId !== apiKey.tenantId)) { return deny(principal.type, "tenant_mismatch"); } return { allowed: true, principalType: principal.type, tenantId: apiKey.tenantId, }; }