@@ -2,6 +2,7 @@ import { eq } from 'drizzle-orm'
22import { type NextRequest , NextResponse } from 'next/server'
33import { z } from 'zod'
44import { getSession } from '@/lib/auth'
5+ import { verifyInternalToken } from '@/lib/auth/internal'
56import { createLogger } from '@/lib/logs/console-logger'
67import { getUserEntityPermissions , hasAdminPermission } from '@/lib/permissions/utils'
78import { loadWorkflowFromNormalizedTables } from '@/lib/workflows/db-helpers'
@@ -28,14 +29,29 @@ export async function GET(request: NextRequest, { params }: { params: Promise<{
2829 const { id : workflowId } = await params
2930
3031 try {
31- // Get the session
32- const session = await getSession ( )
33- if ( ! session ?. user ?. id ) {
34- logger . warn ( `[${ requestId } ] Unauthorized access attempt for workflow ${ workflowId } ` )
35- return NextResponse . json ( { error : 'Unauthorized' } , { status : 401 } )
32+ // Check for internal JWT token for server-side calls
33+ const authHeader = request . headers . get ( 'authorization' )
34+ let isInternalCall = false
35+
36+ if ( authHeader ?. startsWith ( 'Bearer ' ) ) {
37+ const token = authHeader . split ( ' ' ) [ 1 ]
38+ isInternalCall = await verifyInternalToken ( token )
3639 }
3740
38- const userId = session . user . id
41+ let userId : string | null = null
42+
43+ if ( isInternalCall ) {
44+ // For internal calls, we'll skip user-specific access checks
45+ logger . info ( `[${ requestId } ] Internal API call for workflow ${ workflowId } ` )
46+ } else {
47+ // Get the session for regular user calls
48+ const session = await getSession ( )
49+ if ( ! session ?. user ?. id ) {
50+ logger . warn ( `[${ requestId } ] Unauthorized access attempt for workflow ${ workflowId } ` )
51+ return NextResponse . json ( { error : 'Unauthorized' } , { status : 401 } )
52+ }
53+ userId = session . user . id
54+ }
3955
4056 // Fetch the workflow
4157 const workflowData = await db
@@ -52,26 +68,31 @@ export async function GET(request: NextRequest, { params }: { params: Promise<{
5268 // Check if user has access to this workflow
5369 let hasAccess = false
5470
55- // Case 1: User owns the workflow
56- if ( workflowData . userId === userId ) {
71+ if ( isInternalCall ) {
72+ // Internal calls have full access
5773 hasAccess = true
58- }
59-
60- // Case 2: Workflow belongs to a workspace the user has permissions for
61- if ( ! hasAccess && workflowData . workspaceId ) {
62- const userPermission = await getUserEntityPermissions (
63- userId ,
64- 'workspace' ,
65- workflowData . workspaceId
66- )
67- if ( userPermission !== null ) {
74+ } else {
75+ // Case 1: User owns the workflow
76+ if ( workflowData . userId === userId ) {
6877 hasAccess = true
6978 }
70- }
7179
72- if ( ! hasAccess ) {
73- logger . warn ( `[${ requestId } ] User ${ userId } denied access to workflow ${ workflowId } ` )
74- return NextResponse . json ( { error : 'Access denied' } , { status : 403 } )
80+ // Case 2: Workflow belongs to a workspace the user has permissions for
81+ if ( ! hasAccess && workflowData . workspaceId && userId ) {
82+ const userPermission = await getUserEntityPermissions (
83+ userId ,
84+ 'workspace' ,
85+ workflowData . workspaceId
86+ )
87+ if ( userPermission !== null ) {
88+ hasAccess = true
89+ }
90+ }
91+
92+ if ( ! hasAccess ) {
93+ logger . warn ( `[${ requestId } ] User ${ userId } denied access to workflow ${ workflowId } ` )
94+ return NextResponse . json ( { error : 'Access denied' } , { status : 403 } )
95+ }
7596 }
7697
7798 // Try to load from normalized tables first
0 commit comments