diff --git a/lib/keys/createApiKeyHandler.ts b/lib/keys/createApiKeyHandler.ts index 452e95834..22d6aa481 100644 --- a/lib/keys/createApiKeyHandler.ts +++ b/lib/keys/createApiKeyHandler.ts @@ -2,17 +2,14 @@ import { NextRequest, NextResponse } from "next/server"; import { getCorsHeaders } from "@/lib/networking/getCorsHeaders"; import { validateCreateApiKeyBody } from "@/lib/keys/validateCreateApiKeyBody"; import { getAuthenticatedAccountId } from "@/lib/auth/getAuthenticatedAccountId"; -import { createOrgApiKeysHandler } from "@/lib/keys/org/createOrgApiKeysHandler"; import { createKey } from "@/lib/keys/createKey"; /** - * Handler for creating a new API key. + * Handler for creating a new API key for the authenticated account. * Requires authentication via Bearer token in Authorization header. * * Body parameters: * - key_name (required): The name for the API key - * - organizationId (optional): If provided, creates a key for the organization - * after validating the authenticated account is a member. * * @param request - The request object containing the body with key_name. * @returns A NextResponse with the generated API key. @@ -32,14 +29,8 @@ export async function createApiKeyHandler(request: NextRequest): Promise { try { - const { searchParams } = new URL(request.url); - const organizationId = searchParams.get("organizationId") ?? undefined; - const accountIdOrError = await getAuthenticatedAccountId(request); if (accountIdOrError instanceof NextResponse) { return accountIdOrError; } const accountId = accountIdOrError; - // If organizationId is provided, delegate to org-specific handler - if (organizationId) { - return getOrgApiKeysHandler(accountId, organizationId); - } - - // Default: fetch keys for the authenticated account const { data, error } = await getApiKeys({ accountId }); if (error) { diff --git a/lib/keys/org/createOrgApiKeysHandler.ts b/lib/keys/org/createOrgApiKeysHandler.ts deleted file mode 100644 index c5f178d24..000000000 --- a/lib/keys/org/createOrgApiKeysHandler.ts +++ /dev/null @@ -1,28 +0,0 @@ -import { NextResponse } from "next/server"; -import { createKey } from "@/lib/keys/createKey"; -import { onlyOrgAccounts } from "./onlyOrgAccounts"; - -/** - * Internal helper to create an API key scoped to an organization. - * Validates that the authenticated account is a member of the organization - * before creating the key. - * - * @param accountId - The authenticated account ID - * @param organizationId - The organization ID to create the key for - * @param keyName - The name for the API key - * @returns NextResponse with the generated key or an error response - */ -export async function createOrgApiKeysHandler( - accountId: string, - organizationId: string, - keyName: string, -): Promise { - // Verify membership before creating org key - const membershipError = await onlyOrgAccounts(accountId, organizationId); - if (membershipError) { - return membershipError; - } - - // Create key owned by the organization - return createKey(organizationId, keyName); -} diff --git a/lib/keys/org/getOrgApiKeysHandler.ts b/lib/keys/org/getOrgApiKeysHandler.ts deleted file mode 100644 index 2d505452b..000000000 --- a/lib/keys/org/getOrgApiKeysHandler.ts +++ /dev/null @@ -1,50 +0,0 @@ -import { NextResponse } from "next/server"; -import { getCorsHeaders } from "@/lib/networking/getCorsHeaders"; -import { getApiKeys } from "@/lib/supabase/account_api_keys/getApiKeys"; -import { onlyOrgAccounts } from "./onlyOrgAccounts"; - -/** - * Internal helper to fetch API keys scoped to an organization. - * Validates that the authenticated account is a member of the organization - * before returning keys. - * - * @param accountId - The authenticated account ID - * @param organizationId - The organization ID whose keys are requested - * @returns NextResponse with keys or an error response - */ -export async function getOrgApiKeysHandler( - accountId: string, - organizationId: string, -): Promise { - // Verify membership before returning org keys - const membershipError = await onlyOrgAccounts(accountId, organizationId); - if (membershipError) { - return membershipError; - } - - const { data, error } = await getApiKeys({ accountId: organizationId }); - - if (error) { - console.error("Error fetching organization API keys:", error); - return NextResponse.json( - { - status: "error", - message: "Failed to fetch organization API keys", - }, - { - status: 500, - headers: getCorsHeaders(), - }, - ); - } - - return NextResponse.json( - { - keys: data || [], - }, - { - status: 200, - headers: getCorsHeaders(), - }, - ); -} diff --git a/lib/keys/org/onlyOrgAccounts.ts b/lib/keys/org/onlyOrgAccounts.ts deleted file mode 100644 index a8c1cf266..000000000 --- a/lib/keys/org/onlyOrgAccounts.ts +++ /dev/null @@ -1,36 +0,0 @@ -import { NextResponse } from "next/server"; -import { getCorsHeaders } from "@/lib/networking/getCorsHeaders"; -import { getAccountOrganizations } from "@/lib/supabase/account_organization_ids/getAccountOrganizations"; - -/** - * Validates that an account is a member of the specified organization. - * Returns an error response if validation fails, or null if validation passes. - * - * @param accountId - The account ID to validate - * @param organizationId - The organization ID to check membership for - * @returns NextResponse with 403 error if not a member, or null if member - */ -export async function onlyOrgAccounts( - accountId: string, - organizationId: string, -): Promise { - const orgMemberships = await getAccountOrganizations({ - accountId, - organizationId, - }); - - if (!orgMemberships.length) { - return NextResponse.json( - { - status: "error", - message: "Account is not a member of this organization", - }, - { - status: 403, - headers: getCorsHeaders(), - }, - ); - } - - return null; -} diff --git a/lib/keys/validateCreateApiKeyBody.ts b/lib/keys/validateCreateApiKeyBody.ts index 8f1136f87..51ad8cbab 100644 --- a/lib/keys/validateCreateApiKeyBody.ts +++ b/lib/keys/validateCreateApiKeyBody.ts @@ -4,7 +4,6 @@ import { z } from "zod"; export const createApiKeyBodySchema = z.object({ key_name: z.string().min(1, "key_name parameter is required"), - organizationId: z.string().optional(), }); export type CreateApiKeyBody = z.infer;