Skip to content

Commit 9f02f88

Browse files
fix(oauth): webhook + oauthblocks in workflow (#979)
* fix(oauth): webhook + oauthblocks in workflow * propagate workflow id * requireWorkflowId for internal can be false
1 parent 7a17112 commit 9f02f88

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

apps/sim/app/api/auth/oauth/token/route.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@ export async function POST(request: NextRequest) {
2828
return NextResponse.json({ error: 'Credential ID is required' }, { status: 400 })
2929
}
3030

31-
const authz = await authorizeCredentialUse(request, { credentialId, workflowId })
31+
// We already have workflowId from the parsed body; avoid forcing hybrid auth to re-read it
32+
const authz = await authorizeCredentialUse(request, {
33+
credentialId,
34+
workflowId,
35+
requireWorkflowIdForInternal: false,
36+
})
3237
if (!authz.ok || !authz.credentialOwnerUserId) {
3338
return NextResponse.json({ error: authz.error || 'Unauthorized' }, { status: 403 })
3439
}

apps/sim/public/.well-known/microsoft-identity-association.json renamed to apps/sim/public/.well-known/microsoft-identity-association

File renamed without changes.

apps/sim/tools/index.ts

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { generateInternalToken } from '@/lib/auth/internal'
12
import { createLogger } from '@/lib/logs/console/logger'
23
import { getBaseUrl } from '@/lib/urls/utils'
34
import type { ExecutionContext } from '@/executor/types'
@@ -116,18 +117,37 @@ export async function executeTool(
116117
credentialId: contextParams.credential,
117118
}
118119

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
121125
if (workflowId) {
122126
tokenPayload.workflowId = workflowId
123127
}
124128

125129
logger.info(`[${requestId}] Fetching access token from ${baseUrl}/api/auth/oauth/token`)
126130

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(), {
129149
method: 'POST',
130-
headers: { 'Content-Type': 'application/json' },
150+
headers: tokenHeaders,
131151
body: JSON.stringify(tokenPayload),
132152
})
133153

0 commit comments

Comments
 (0)