@@ -35,6 +35,7 @@ import {
3535} from '@/lib/oauth/utils'
3636import { captureServerEvent } from '@/lib/posthog/server'
3737import { getToolMetadata } from '@/tools/metadata'
38+ import type { OAuthConfig } from '@/tools/types'
3839import { extractZohoDeskBaseFromScope } from '@/tools/zoho_desk/host-allowlist'
3940
4041const logger = createLogger ( 'OAuthTokenResolution' )
@@ -233,21 +234,29 @@ export async function completeOAuthCredentialToken(params: {
233234 }
234235}
235236
236- function validateToolCredentialBinding (
237- toolId : string | undefined ,
238- providerId : string | null | undefined ,
239- credentialKind : 'oauth' | 'service-account'
237+ function validateToolCredentialKind (
238+ requiredKind : OAuthConfig [ 'credentialKind' ] ,
239+ actualKind : NonNullable < OAuthConfig [ 'credentialKind' ] >
240240) : Extract < ResolveCredentialTokenResult , { ok : false } > | undefined {
241- if ( ! toolId ) return
242- const policy = getToolMetadata ( toolId ) ?. oauth
243- if ( policy ?. credentialKind && policy . credentialKind !== credentialKind ) {
241+ if ( requiredKind && requiredKind !== actualKind ) {
244242 return {
245243 ok : false ,
246244 status : 400 ,
247245 code : 'CREDENTIAL_TYPE_MISMATCH' ,
248246 error : 'The selected credential type is not supported by this tool' ,
249247 }
250248 }
249+ }
250+
251+ function validateToolCredentialBinding (
252+ toolId : string | undefined ,
253+ providerId : string | null | undefined ,
254+ credentialKind : NonNullable < OAuthConfig [ 'credentialKind' ] >
255+ ) : Extract < ResolveCredentialTokenResult , { ok : false } > | undefined {
256+ if ( ! toolId ) return
257+ const policy = getToolMetadata ( toolId ) ?. oauth
258+ const kindError = validateToolCredentialKind ( policy ?. credentialKind , credentialKind )
259+ if ( kindError ) return kindError
251260 const service = policy ?. required
252261 ? ( getServiceConfigByServiceId ( policy . provider ) ??
253262 getServiceConfigByProviderId ( policy . provider ) )
@@ -509,6 +518,9 @@ export async function resolveCredentialAccessToken(
509518 error : 'This tool is not configured to use managed credentials' ,
510519 }
511520 }
521+ const kindError = validateToolCredentialKind ( toolMetadata . oauth . credentialKind , 'oauth' )
522+ if ( kindError ) return kindError
523+
512524 const requiredScopes =
513525 toolMetadata . oauth . requiredScopes ?? getCanonicalScopesForProvider ( toolMetadata . oauth . provider )
514526 if ( requiredScopes . length === 0 && toolMetadata . oauth . requiredScopes === undefined ) {
0 commit comments