mirror of
https://github.com/simstudioai/sim.git
synced 2026-04-06 03:00:16 -04:00
* feat(auth): allow google service account * Add gmail support for google services * Refresh creds on typing in impersonated email * Switch to adding subblock impersonateUserEmail conditionally * Directly pass subblock for impersonateUserEmail * Fix lint * Update documentation for google service accounts * Fix lint * Address comments * Remove hardcoded scopes, remove orphaned migration script * Simplify subblocks for google service account * Fix lint * Fix build error * Fix documentation scopes listed for google service accounts * Fix issue with credential selector, remove bigquery and ad support * create credentialCondition * Shift conditional render out of subblock * Simplify sublock values * Fix security message * Handle tool service accounts * Address bugbot * Fix lint * Fix manual credential input not showing impersonate * Fix tests * Allow watching param id and subblock ids * Fix bad test --------- Co-authored-by: Theodore Li <theo@sim.ai>
107 lines
3.5 KiB
TypeScript
107 lines
3.5 KiB
TypeScript
import { createLogger } from '@sim/logger'
|
|
import { NextResponse } from 'next/server'
|
|
import { authorizeCredentialUse } from '@/lib/auth/credential-access'
|
|
import { generateRequestId } from '@/lib/core/utils/request'
|
|
import { getScopesForService } from '@/lib/oauth/utils'
|
|
import { refreshAccessTokenIfNeeded, ServiceAccountTokenError } from '@/app/api/auth/oauth/utils'
|
|
|
|
const logger = createLogger('GoogleBigQueryDatasetsAPI')
|
|
|
|
export const dynamic = 'force-dynamic'
|
|
|
|
/**
|
|
* POST /api/tools/google_bigquery/datasets
|
|
*
|
|
* Fetches the list of BigQuery datasets for a given project using the caller's OAuth credential.
|
|
*
|
|
* @param request - Incoming request containing `credential`, `workflowId`, and `projectId` in the JSON body
|
|
* @returns JSON response with a `datasets` array, each entry containing `datasetReference` and optional `friendlyName`
|
|
*/
|
|
export async function POST(request: Request) {
|
|
const requestId = generateRequestId()
|
|
try {
|
|
const body = await request.json()
|
|
const { credential, workflowId, projectId, impersonateEmail } = body
|
|
|
|
if (!credential) {
|
|
logger.error('Missing credential in request')
|
|
return NextResponse.json({ error: 'Credential is required' }, { status: 400 })
|
|
}
|
|
|
|
if (!projectId) {
|
|
logger.error('Missing project ID in request')
|
|
return NextResponse.json({ error: 'Project ID is required' }, { status: 400 })
|
|
}
|
|
|
|
const authz = await authorizeCredentialUse(request as any, {
|
|
credentialId: credential,
|
|
workflowId,
|
|
})
|
|
if (!authz.ok || !authz.credentialOwnerUserId) {
|
|
return NextResponse.json({ error: authz.error || 'Unauthorized' }, { status: 403 })
|
|
}
|
|
|
|
const accessToken = await refreshAccessTokenIfNeeded(
|
|
credential,
|
|
authz.credentialOwnerUserId,
|
|
requestId,
|
|
getScopesForService('google-bigquery'),
|
|
impersonateEmail
|
|
)
|
|
if (!accessToken) {
|
|
logger.error('Failed to get access token', {
|
|
credentialId: credential,
|
|
userId: authz.credentialOwnerUserId,
|
|
})
|
|
return NextResponse.json(
|
|
{ error: 'Could not retrieve access token', authRequired: true },
|
|
{ status: 401 }
|
|
)
|
|
}
|
|
|
|
const response = await fetch(
|
|
`https://bigquery.googleapis.com/bigquery/v2/projects/${encodeURIComponent(projectId)}/datasets?maxResults=200`,
|
|
{
|
|
headers: {
|
|
Authorization: `Bearer ${accessToken}`,
|
|
'Content-Type': 'application/json',
|
|
},
|
|
}
|
|
)
|
|
|
|
if (!response.ok) {
|
|
const errorData = await response.json().catch(() => ({}))
|
|
logger.error('Failed to fetch BigQuery datasets', {
|
|
status: response.status,
|
|
error: errorData,
|
|
})
|
|
return NextResponse.json(
|
|
{ error: 'Failed to fetch BigQuery datasets', details: errorData },
|
|
{ status: response.status }
|
|
)
|
|
}
|
|
|
|
const data = await response.json()
|
|
const datasets = (data.datasets || []).map(
|
|
(ds: {
|
|
datasetReference: { datasetId: string; projectId: string }
|
|
friendlyName?: string
|
|
}) => ({
|
|
datasetReference: ds.datasetReference,
|
|
friendlyName: ds.friendlyName,
|
|
})
|
|
)
|
|
|
|
return NextResponse.json({ datasets })
|
|
} catch (error) {
|
|
if (error instanceof ServiceAccountTokenError) {
|
|
return NextResponse.json({ error: error.message }, { status: 400 })
|
|
}
|
|
logger.error('Error processing BigQuery datasets request:', error)
|
|
return NextResponse.json(
|
|
{ error: 'Failed to retrieve BigQuery datasets', details: (error as Error).message },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|