fix(integrations): support parsedBody in authenticateIntegrationRequest

This commit is contained in:
Marcos Fargas
2026-10-12 13:22:38 +02:00
parent b3936932d3
commit 9f1721c921
3 changed files with 9 additions and 5 deletions

View File

@@ -10,7 +10,7 @@ export async function POST(req: NextRequest) {
return NextResponse.json({ error: 'Invalid JSON body' }, { status: 400 }); return NextResponse.json({ error: 'Invalid JSON body' }, { status: 400 });
} }
const auth = await authenticateIntegrationRequest(req, { requireTenant: true }); const auth = await authenticateIntegrationRequest(req, { requireTenant: true, parsedBody: body });
if (!auth.ok) { if (!auth.ok) {
return auth.errorResponse!; return auth.errorResponse!;
} }

View File

@@ -10,7 +10,7 @@ export async function POST(req: NextRequest) {
return NextResponse.json({ error: 'Invalid JSON body' }, { status: 400 }); return NextResponse.json({ error: 'Invalid JSON body' }, { status: 400 });
} }
const auth = await authenticateIntegrationRequest(req, { requireTenant: true }); const auth = await authenticateIntegrationRequest(req, { requireTenant: true, parsedBody: body });
if (!auth.ok) { if (!auth.ok) {
return auth.errorResponse!; return auth.errorResponse!;
} }

View File

@@ -17,7 +17,7 @@ export interface IntegrationAuthResult {
*/ */
export async function authenticateIntegrationRequest( export async function authenticateIntegrationRequest(
req: NextRequest, req: NextRequest,
options: { requireTenant?: boolean } = { requireTenant: true } options: { requireTenant?: boolean; parsedBody?: any } = { requireTenant: true }
): Promise<IntegrationAuthResult> { ): Promise<IntegrationAuthResult> {
const apiKey = req.headers.get('x-api-key') || req.headers.get('X-Api-Key'); const apiKey = req.headers.get('x-api-key') || req.headers.get('X-Api-Key');
const n8nKey = process.env.N8N_API_KEY?.trim(); const n8nKey = process.env.N8N_API_KEY?.trim();
@@ -45,18 +45,22 @@ export async function authenticateIntegrationRequest(
return { ok: true, caller }; return { ok: true, caller };
} }
// Extraer tenant_id o client_id de query params o body // Extraer tenant_id o client_id de query params o parsedBody
let tenantId = let tenantId =
req.nextUrl.searchParams.get('tenant_id') || req.nextUrl.searchParams.get('tenant_id') ||
req.nextUrl.searchParams.get('client_id'); req.nextUrl.searchParams.get('client_id');
if (!tenantId && options.parsedBody) {
tenantId = options.parsedBody.tenant_id || options.parsedBody.client_id;
}
if (!tenantId && req.method !== 'GET') { if (!tenantId && req.method !== 'GET') {
try { try {
const cloned = req.clone(); const cloned = req.clone();
const body = await cloned.json(); const body = await cloned.json();
tenantId = body.tenant_id || body.client_id; tenantId = body.tenant_id || body.client_id;
} catch { } catch {
// Ignorar error al parsear body si no viene JSON // Ignorar
} }
} }