Files
0pt/app/api/0pt/sources/[sourceId]/authorization/route.ts
T

46 lines
2.1 KiB
TypeScript

/**
* app/api/0pt/sources/[sourceId]/authorization/route.ts
* ======================================================
* P060 "authorization": the recorded decision that 0pt may connect to this
* source at all — separate from rights (what the DATA may be used for). PUT
* is an upsert; a service identity as `granted_by` is rejected (400), because
* a source may not authorize itself.
*/
import { NextRequest, NextResponse } from "next/server";
import { parseBody, setSourceAuthorizationSchema } from "@/src/validation/0pt-schemas";
import { sourceRegistryService } from "@/src/services/source-registry";
import { siaxErrorResponse } from "@/src/http/siax-error-response";
import { assertTenantMatchesVerified } from "@/src/http/tenant-guard";
export async function GET(request: NextRequest, { params }: { params: Promise<{ sourceId: string }> }) {
try {
const { sourceId } = await params;
const tenantId = new URL(request.url).searchParams.get("tenant_id");
if (!tenantId) return NextResponse.json({ error: "tenant_id is required" }, { status: 400 });
const guard = assertTenantMatchesVerified(request, tenantId);
if (guard) return guard;
const auth = await sourceRegistryService.getAuthorization(sourceId, tenantId);
if (!auth) {
return NextResponse.json({ error: `source ${sourceId} has no authorization record` }, { status: 404 });
}
return NextResponse.json(auth);
} catch (error: unknown) {
return siaxErrorResponse(error);
}
}
export async function PUT(request: NextRequest, { params }: { params: Promise<{ sourceId: string }> }) {
try {
const { sourceId } = await params;
const parsed = parseBody(setSourceAuthorizationSchema, await request.json());
if (!parsed.ok) return NextResponse.json(parsed.body, { status: parsed.status });
const guard = assertTenantMatchesVerified(request, parsed.data.tenant_id);
if (guard) return guard;
return NextResponse.json(
await sourceRegistryService.setAuthorization({ ...parsed.data, source_id: sourceId }),
);
} catch (error: unknown) {
return siaxErrorResponse(error);
}
}