import { Request, Response, NextFunction } from "express"; import { z } from "zod"; import { db, userOrgs, virtualApiKeyResources, virtualApiKeys } from "@server/db"; import response from "@server/lib/response"; import HttpCode from "@server/types/HttpCode"; import createHttpError from "http-errors"; import logger from "@server/logger"; import { fromError } from "zod-validation-error"; import { OpenAPITags, registry } from "@server/openApi"; import { and, eq } from "drizzle-orm"; import { createDate, TimeSpan } from "oslo"; import { assertManualKeyResourcesInOrg, replaceVirtualApiKeyResources, toPublicVirtualApiKey } from "@server/lib/virtualApiKey"; import type { CreateOrEditVirtualApiKeyResponse } from "@server/routers/virtualApiKey/types"; import { updateVirtualApiKeyBodySchema } from "@server/routers/virtualApiKey/validation"; const paramsSchema = z.strictObject({ virtualApiKeyId: z.string().nonempty() }); registry.registerPath({ method: "post", path: "/virtual-api-key/{virtualApiKeyId}", description: "Update a manual virtual API key metadata and resource assignment.", tags: [OpenAPITags.VirtualApiKey], request: { params: paramsSchema, body: { content: { "application/json": { schema: updateVirtualApiKeyBodySchema } } } }, responses: { 200: { description: "Successful response" } } }); export async function updateVirtualApiKey( req: Request, res: Response, next: NextFunction ): Promise { try { const parsedParams = paramsSchema.safeParse(req.params); if (!parsedParams.success) { return next( createHttpError( HttpCode.BAD_REQUEST, fromError(parsedParams.error).toString() ) ); } const parsedBody = updateVirtualApiKeyBodySchema.safeParse(req.body); if (!parsedBody.success) { return next( createHttpError( HttpCode.BAD_REQUEST, fromError(parsedBody.error).toString() ) ); } const { virtualApiKeyId } = parsedParams.data; const body = parsedBody.data; const [existing] = req.virtualApiKey && req.virtualApiKey.virtualApiKeyId === virtualApiKeyId ? [req.virtualApiKey] : await db .select() .from(virtualApiKeys) .where( eq(virtualApiKeys.virtualApiKeyId, virtualApiKeyId) ) .limit(1); if (!existing || existing.kind !== "manual") { return next( createHttpError( HttpCode.NOT_FOUND, `Virtual API key with ID ${virtualApiKeyId} not found` ) ); } if (body.userId) { const [membership] = await db .select() .from(userOrgs) .where( and( eq(userOrgs.userId, body.userId), eq(userOrgs.orgId, existing.orgId) ) ) .limit(1); if (!membership) { return next( createHttpError( HttpCode.BAD_REQUEST, "User is not a member of this organization" ) ); } } const nextAllResources = body.allResources !== undefined ? body.allResources : existing.allResources; let nextResourceIds: number[] | undefined; if (nextAllResources) { nextResourceIds = []; } else if (body.resourceIds !== undefined) { nextResourceIds = body.resourceIds; } if (nextResourceIds !== undefined) { const resourceCheck = await assertManualKeyResourcesInOrg({ allResources: nextAllResources, resourceIds: nextResourceIds, orgId: existing.orgId }); if (!resourceCheck.ok) { return next( createHttpError(HttpCode.BAD_REQUEST, resourceCheck.message) ); } } const updates: Partial = {}; if (body.name !== undefined) { updates.name = body.name; } if (body.description !== undefined) { updates.description = body.description; } if (body.userId !== undefined) { updates.userId = body.userId; } if (body.allResources !== undefined) { updates.allResources = body.allResources; } if (body.validForSeconds !== undefined) { updates.expiresAt = body.validForSeconds === null ? null : createDate( new TimeSpan(body.validForSeconds, "s") ).getTime(); } const updated = await db.transaction(async (trx) => { let row = existing; if (Object.keys(updates).length > 0) { const [updatedRow] = await trx .update(virtualApiKeys) .set(updates) .where(eq(virtualApiKeys.virtualApiKeyId, virtualApiKeyId)) .returning(); row = updatedRow; } if (nextResourceIds !== undefined) { await replaceVirtualApiKeyResources( trx, virtualApiKeyId, nextResourceIds ); } return row; }); const resourceRows = await db .select({ resourceId: virtualApiKeyResources.resourceId }) .from(virtualApiKeyResources) .where(eq(virtualApiKeyResources.virtualApiKeyId, virtualApiKeyId)); return response(res, { data: { virtualApiKey: { ...toPublicVirtualApiKey(updated), resourceIds: resourceRows.map((row) => row.resourceId) } }, success: true, error: false, message: "Virtual API key updated successfully", status: HttpCode.OK }); } catch (error) { logger.error(error); return next( createHttpError(HttpCode.INTERNAL_SERVER_ERROR, "An error occurred") ); } }