mirror of
https://github.com/simstudioai/sim.git
synced 2026-04-06 03:00:16 -04:00
64 lines
1.4 KiB
TypeScript
64 lines
1.4 KiB
TypeScript
import { NextRequest } from 'next/server'
|
|
import { getWorkflowById } from '@/lib/workflows'
|
|
|
|
export interface ValidationResult {
|
|
error?: { message: string; status: number }
|
|
workflow?: any
|
|
}
|
|
|
|
export async function validateWorkflowAccess(
|
|
request: NextRequest,
|
|
workflowId: string,
|
|
requireDeployment = true
|
|
): Promise<ValidationResult> {
|
|
try {
|
|
const workflow = await getWorkflowById(workflowId)
|
|
if (!workflow) {
|
|
return {
|
|
error: {
|
|
message: 'Workflow not found',
|
|
status: 404,
|
|
},
|
|
}
|
|
}
|
|
|
|
if (requireDeployment) {
|
|
if (!workflow.isDeployed) {
|
|
return {
|
|
error: {
|
|
message: 'Workflow is not deployed',
|
|
status: 403,
|
|
},
|
|
}
|
|
}
|
|
|
|
// API key authentication
|
|
let apiKey = null
|
|
for (const [key, value] of request.headers.entries()) {
|
|
if (key.toLowerCase() === 'x-api-key' && value) {
|
|
apiKey = value
|
|
break
|
|
}
|
|
}
|
|
|
|
if (!apiKey || !workflow.apiKey || apiKey !== workflow.apiKey) {
|
|
return {
|
|
error: {
|
|
message: 'Unauthorized',
|
|
status: 401,
|
|
},
|
|
}
|
|
}
|
|
}
|
|
return { workflow }
|
|
} catch (error) {
|
|
console.error('Validation error:', error)
|
|
return {
|
|
error: {
|
|
message: 'Internal server error',
|
|
status: 500,
|
|
},
|
|
}
|
|
}
|
|
}
|