|
| 1 | +import { generateInternalToken } from '@/lib/auth/internal' |
1 | 2 | import { createLogger } from '@/lib/logs/console/logger' |
2 | 3 | import { getBaseUrl } from '@/lib/urls/utils' |
3 | 4 | import type { ExecutionContext } from '@/executor/types' |
@@ -116,18 +117,37 @@ export async function executeTool( |
116 | 117 | credentialId: contextParams.credential, |
117 | 118 | } |
118 | 119 |
|
119 | | - // Add workflowId if it exists in params or context |
120 | | - const workflowId = contextParams.workflowId || contextParams._context?.workflowId |
| 120 | + // Add workflowId if it exists in params, context, or executionContext |
| 121 | + const workflowId = |
| 122 | + contextParams.workflowId || |
| 123 | + contextParams._context?.workflowId || |
| 124 | + executionContext?.workflowId |
121 | 125 | if (workflowId) { |
122 | 126 | tokenPayload.workflowId = workflowId |
123 | 127 | } |
124 | 128 |
|
125 | 129 | logger.info(`[${requestId}] Fetching access token from ${baseUrl}/api/auth/oauth/token`) |
126 | 130 |
|
127 | | - const tokenUrl = new URL('/api/auth/oauth/token', baseUrl).toString() |
128 | | - const response = await fetch(tokenUrl, { |
| 131 | + // Build token URL and also include workflowId in query so server auth can read it |
| 132 | + const tokenUrlObj = new URL('/api/auth/oauth/token', baseUrl) |
| 133 | + if (workflowId) { |
| 134 | + tokenUrlObj.searchParams.set('workflowId', workflowId) |
| 135 | + } |
| 136 | + |
| 137 | + // Always send Content-Type; add internal auth on server-side runs |
| 138 | + const tokenHeaders: Record<string, string> = { 'Content-Type': 'application/json' } |
| 139 | + if (typeof window === 'undefined') { |
| 140 | + try { |
| 141 | + const internalToken = await generateInternalToken() |
| 142 | + tokenHeaders.Authorization = `Bearer ${internalToken}` |
| 143 | + } catch (_e) { |
| 144 | + // Swallow token generation errors; the request will fail and be reported upstream |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + const response = await fetch(tokenUrlObj.toString(), { |
129 | 149 | method: 'POST', |
130 | | - headers: { 'Content-Type': 'application/json' }, |
| 150 | + headers: tokenHeaders, |
131 | 151 | body: JSON.stringify(tokenPayload), |
132 | 152 | }) |
133 | 153 |
|
|
0 commit comments