1- import { db } from '@sim/db'
2- import { account } from '@sim/db/schema'
3- import { eq } from 'drizzle-orm'
41import { type NextRequest , NextResponse } from 'next/server'
5- import { getSession } from '@/lib/auth'
2+ import { authorizeCredentialUse } from '@/lib/auth/credential-access '
63import { createLogger } from '@/lib/logs/console/logger'
74import { generateRequestId } from '@/lib/utils'
8- import { refreshAccessTokenIfNeeded } from '@/app/api/auth/oauth/utils'
5+ import { getCredential , refreshAccessTokenIfNeeded } from '@/app/api/auth/oauth/utils'
96
107export const dynamic = 'force-dynamic'
118
@@ -18,46 +15,39 @@ export async function GET(request: NextRequest) {
1815 const requestId = generateRequestId ( )
1916
2017 try {
21- // Get the session
22- const session = await getSession ( )
23-
24- // Check if the user is authenticated
25- if ( ! session ?. user ?. id ) {
26- logger . warn ( `[${ requestId } ] Unauthenticated request rejected` )
27- return NextResponse . json ( { error : 'User not authenticated' } , { status : 401 } )
28- }
29-
3018 // Get the credential ID from the query params
3119 const { searchParams } = new URL ( request . url )
3220 const credentialId = searchParams . get ( 'credentialId' )
3321 const query = searchParams . get ( 'query' ) || ''
22+ const workflowId = searchParams . get ( 'workflowId' ) || undefined
3423
3524 if ( ! credentialId ) {
3625 logger . warn ( `[${ requestId } ] Missing credential ID` )
3726 return NextResponse . json ( { error : 'Credential ID is required' } , { status : 400 } )
3827 }
3928
40- // Get the credential from the database
41- const credentials = await db . select ( ) . from ( account ) . where ( eq ( account . id , credentialId ) ) . limit ( 1 )
29+ const authz = await authorizeCredentialUse ( request , {
30+ credentialId,
31+ workflowId,
32+ requireWorkflowIdForInternal : false ,
33+ } )
4234
43- if ( ! credentials . length ) {
44- logger . warn ( `[ ${ requestId } ] Credential not found` , { credentialId } )
45- return NextResponse . json ( { error : 'Credential not found' } , { status : 404 } )
35+ if ( ! authz . ok || ! authz . credentialOwnerUserId ) {
36+ const status = authz . error === ' Credential not found' ? 404 : 403
37+ return NextResponse . json ( { error : authz . error || 'Unauthorized' } , { status } )
4638 }
4739
48- const credential = credentials [ 0 ]
49-
50- // Check if the credential belongs to the user
51- if ( credential . userId !== session . user . id ) {
52- logger . warn ( `[${ requestId } ] Unauthorized credential access attempt` , {
53- credentialUserId : credential . userId ,
54- requestUserId : session . user . id ,
55- } )
56- return NextResponse . json ( { error : 'Unauthorized' } , { status : 403 } )
40+ const credential = await getCredential ( requestId , credentialId , authz . credentialOwnerUserId )
41+ if ( ! credential ) {
42+ return NextResponse . json ( { error : 'Credential not found' } , { status : 404 } )
5743 }
5844
5945 // Refresh access token if needed using the utility function
60- const accessToken = await refreshAccessTokenIfNeeded ( credentialId , session . user . id , requestId )
46+ const accessToken = await refreshAccessTokenIfNeeded (
47+ credentialId ,
48+ authz . credentialOwnerUserId ,
49+ requestId
50+ )
6151
6252 if ( ! accessToken ) {
6353 return NextResponse . json ( { error : 'Failed to obtain valid access token' } , { status : 401 } )
0 commit comments