Skip to content

Commit c6ab796

Browse files
author
Theodore Li
committed
Address bugbot
1 parent 8b14ba1 commit c6ab796

2 files changed

Lines changed: 30 additions & 23 deletions

File tree

apps/sim/app/api/auth/oauth/utils.ts

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -227,19 +227,13 @@ export async function safeAccountInsert(
227227
}
228228

229229
/**
230-
* Get a credential by ID and verify it belongs to the user
230+
* Get a credential by resolved account ID and verify it belongs to the user.
231231
*/
232-
export async function getCredential(requestId: string, credentialId: string, userId: string) {
233-
const resolved = await resolveOAuthAccountId(credentialId)
234-
if (!resolved) {
235-
logger.warn(`[${requestId}] Credential is not an OAuth credential`)
236-
return undefined
237-
}
238-
232+
async function getCredentialByAccountId(requestId: string, accountId: string, userId: string) {
239233
const credentials = await db
240234
.select()
241235
.from(account)
242-
.where(and(eq(account.id, resolved.accountId), eq(account.userId, userId)))
236+
.where(and(eq(account.id, accountId), eq(account.userId, userId)))
243237
.limit(1)
244238

245239
if (!credentials.length) {
@@ -249,8 +243,20 @@ export async function getCredential(requestId: string, credentialId: string, use
249243

250244
return {
251245
...credentials[0],
252-
resolvedCredentialId: resolved.accountId,
246+
resolvedCredentialId: accountId,
247+
}
248+
}
249+
250+
/**
251+
* Get a credential by ID and verify it belongs to the user.
252+
*/
253+
export async function getCredential(requestId: string, credentialId: string, userId: string) {
254+
const resolved = await resolveOAuthAccountId(credentialId)
255+
if (!resolved) {
256+
logger.warn(`[${requestId}] Credential is not an OAuth credential`)
257+
return undefined
253258
}
259+
return getCredentialByAccountId(requestId, resolved.accountId, userId)
254260
}
255261

256262
export async function getOAuthToken(userId: string, providerId: string): Promise<string | null> {
@@ -370,8 +376,8 @@ export async function refreshAccessTokenIfNeeded(
370376
return getServiceAccountToken(resolved.credentialId, scopes, impersonateEmail)
371377
}
372378

373-
// Get the credential directly using the getCredential helper
374-
const credential = await getCredential(requestId, credentialId, userId)
379+
// Use the already-resolved account ID to avoid a redundant resolveOAuthAccountId query
380+
const credential = await getCredentialByAccountId(requestId, resolved.accountId, userId)
375381

376382
if (!credential) {
377383
return null

apps/sim/app/api/credentials/[id]/route.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -125,21 +125,22 @@ export async function PUT(request: NextRequest, { params }: { params: Promise<{
125125
parseResult.data.serviceAccountJson !== undefined &&
126126
access.credential.type === 'service_account'
127127
) {
128+
let parsed: Record<string, unknown>
128129
try {
129-
const parsed = JSON.parse(parseResult.data.serviceAccountJson)
130-
if (
131-
parsed.type !== 'service_account' ||
132-
typeof parsed.client_email !== 'string' ||
133-
typeof parsed.private_key !== 'string' ||
134-
typeof parsed.project_id !== 'string'
135-
) {
136-
return NextResponse.json({ error: 'Invalid service account JSON key' }, { status: 400 })
137-
}
138-
const { encrypted } = await encryptSecret(parseResult.data.serviceAccountJson)
139-
updates.encryptedServiceAccountKey = encrypted
130+
parsed = JSON.parse(parseResult.data.serviceAccountJson)
140131
} catch {
141132
return NextResponse.json({ error: 'Invalid JSON format' }, { status: 400 })
142133
}
134+
if (
135+
parsed.type !== 'service_account' ||
136+
typeof parsed.client_email !== 'string' ||
137+
typeof parsed.private_key !== 'string' ||
138+
typeof parsed.project_id !== 'string'
139+
) {
140+
return NextResponse.json({ error: 'Invalid service account JSON key' }, { status: 400 })
141+
}
142+
const { encrypted } = await encryptSecret(parseResult.data.serviceAccountJson)
143+
updates.encryptedServiceAccountKey = encrypted
143144
}
144145

145146
if (Object.keys(updates).length === 0) {

0 commit comments

Comments
 (0)