@@ -127,13 +127,24 @@ export const POST = withRouteHandler(async (request: NextRequest) => {
127127 }
128128 }
129129
130- let managedOAuthPrincipal : WorkflowExecutionDelegatedPrincipal | null = null
131- const managedOAuthDelegation = parsed . data . headers ?. [ MANAGED_OAUTH_DELEGATION_HEADER ]
132- if ( managedOAuthDelegation && credentialId ) {
130+ const resolved = credentialId ? await resolveOAuthAccountId ( credentialId ) : null
131+ if ( resolved ?. credentialType === 'managed_oauth' && resolved . credentialId ) {
132+ const managedOAuthDelegation = parsed . data . headers ?. [ MANAGED_OAUTH_DELEGATION_HEADER ]
133+ if ( ! managedOAuthDelegation ) {
134+ return NextResponse . json (
135+ {
136+ code : 'MANAGED_CREDENTIAL_DELEGATION_REQUIRED' ,
137+ error : 'Managed credentials can only be used by an authenticated workflow execution' ,
138+ } ,
139+ { status : 403 }
140+ )
141+ }
142+
143+ let managedOAuthPrincipal : WorkflowExecutionDelegatedPrincipal
133144 try {
134145 managedOAuthPrincipal = await authenticateManagedOAuthDelegation (
135146 managedOAuthDelegation ,
136- credentialId
147+ resolved . credentialId
137148 )
138149 } catch ( error ) {
139150 if ( ! ( error instanceof InvalidManagedOAuthDelegationError ) ) throw error
@@ -145,10 +156,6 @@ export const POST = withRouteHandler(async (request: NextRequest) => {
145156 { status : 401 }
146157 )
147158 }
148- }
149-
150- const resolved = credentialId ? await resolveOAuthAccountId ( credentialId ) : null
151- if ( resolved ?. credentialType === 'managed_oauth' && resolved . credentialId ) {
152159 if ( ! toolId ) {
153160 return NextResponse . json (
154161 {
@@ -187,16 +194,6 @@ export const POST = withRouteHandler(async (request: NextRequest) => {
187194 )
188195 }
189196
190- if ( ! managedOAuthPrincipal ) {
191- return NextResponse . json (
192- {
193- code : 'MANAGED_CREDENTIAL_DELEGATION_REQUIRED' ,
194- error : 'Managed credentials can only be used by an authenticated workflow execution' ,
195- } ,
196- { status : 403 }
197- )
198- }
199-
200197 try {
201198 const result = await resolveManagedOAuthCredentialToken . execute ( {
202199 principal : managedOAuthPrincipal ,
0 commit comments