|
| 1 | +import { db } from '@sim/db' |
| 2 | +import { |
| 3 | + account, |
| 4 | + credential, |
| 5 | + credentialMember, |
| 6 | + permissions, |
| 7 | + user, |
| 8 | + workflow, |
| 9 | + workspace, |
| 10 | +} from '@sim/db/schema' |
| 11 | +import { deleteWorkspaceFixture, insertWorkspaceFixture } from '@sim/db/testing/workspace-fixtures' |
| 12 | +import { createSessionPrincipal } from '@sim/testing/factories/principal.factory' |
| 13 | +import { createRouteContext } from '@sim/testing/helpers/http' |
| 14 | +import { createMockRequest } from '@sim/testing/mocks/request.mock' |
| 15 | +import { generateId } from '@sim/utils/id' |
| 16 | +import { eq, inArray } from 'drizzle-orm' |
| 17 | +import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest' |
| 18 | + |
| 19 | +const { sessionUser } = vi.hoisted(() => ({ sessionUser: { id: '' } })) |
| 20 | +vi.mock('@/lib/auth/hybrid', async (importOriginal) => ({ |
| 21 | + ...(await importOriginal<typeof import('@/lib/auth/hybrid')>()), |
| 22 | + checkSessionOrInternalAuth: async () => ({ |
| 23 | + success: true, |
| 24 | + userId: sessionUser.id, |
| 25 | + authType: 'session', |
| 26 | + }), |
| 27 | +})) |
| 28 | + |
| 29 | +import { listInternalCredentials } from '@/lib/credentials/application/crud' |
| 30 | +import { GET } from '@/app/api/auth/oauth/credentials/route' |
| 31 | + |
| 32 | +/** |
| 33 | + * Single-credential lookups must apply the same credential-membership rule as the |
| 34 | + * list endpoints: a workspace teammate who is not a member of a connection never |
| 35 | + * learns its account identity, provider, or scopes. |
| 36 | + */ |
| 37 | +const ownerId = generateId() |
| 38 | +const memberId = generateId() |
| 39 | +const outsiderId = generateId() |
| 40 | +const adminId = generateId() |
| 41 | +const userIds = [ownerId, memberId, outsiderId, adminId] |
| 42 | +const workspaceId = generateId() |
| 43 | +const workflowId = generateId() |
| 44 | +const accountId = generateId() |
| 45 | +const oauthCredentialId = generateId() |
| 46 | +const serviceAccountCredentialId = generateId() |
| 47 | +const personalEnvCredentialId = generateId() |
| 48 | + |
| 49 | +async function lookupOAuth(userId: string, query: Record<string, string>) { |
| 50 | + sessionUser.id = userId |
| 51 | + const params = new URLSearchParams(query).toString() |
| 52 | + const response = await GET( |
| 53 | + createMockRequest({ method: 'GET', url: `/api/auth/oauth/credentials?${params}` }), |
| 54 | + createRouteContext({}) |
| 55 | + ) |
| 56 | + const body = (await response.json()) as { credentials?: Array<{ name: string }> } |
| 57 | + return { status: response.status, names: body.credentials?.map((row) => row.name) } |
| 58 | +} |
| 59 | + |
| 60 | +async function lookupInternal(userId: string, credentialId: string) { |
| 61 | + const result = await listInternalCredentials.execute({ |
| 62 | + principal: createSessionPrincipal({ userId }), |
| 63 | + input: { workspaceId, credentialId }, |
| 64 | + }) |
| 65 | + if (result.mode !== 'lookup') throw new Error('Expected a lookup result') |
| 66 | + return result.credential?.displayName ?? null |
| 67 | +} |
| 68 | + |
| 69 | +beforeAll(async () => { |
| 70 | + const now = new Date() |
| 71 | + await db.insert(user).values( |
| 72 | + userIds.map((id) => ({ |
| 73 | + id, |
| 74 | + name: id, |
| 75 | + email: `${id}@credential-lookup.invalid`, |
| 76 | + emailVerified: true, |
| 77 | + createdAt: now, |
| 78 | + updatedAt: now, |
| 79 | + })) |
| 80 | + ) |
| 81 | + await insertWorkspaceFixture(db, { |
| 82 | + id: workspaceId, |
| 83 | + name: 'Credential lookup', |
| 84 | + ownerId, |
| 85 | + billedAccountUserId: ownerId, |
| 86 | + }) |
| 87 | + await db.insert(permissions).values( |
| 88 | + userIds.map((userId) => ({ |
| 89 | + id: generateId(), |
| 90 | + userId, |
| 91 | + entityType: 'workspace' as const, |
| 92 | + entityId: workspaceId, |
| 93 | + permissionType: userId === adminId ? ('admin' as const) : ('read' as const), |
| 94 | + })) |
| 95 | + ) |
| 96 | + await db.insert(workflow).values({ |
| 97 | + id: workflowId, |
| 98 | + userId: ownerId, |
| 99 | + workspaceId, |
| 100 | + name: 'Credential lookup', |
| 101 | + lastSynced: now, |
| 102 | + createdAt: now, |
| 103 | + updatedAt: now, |
| 104 | + }) |
| 105 | + await db.insert(account).values({ |
| 106 | + id: accountId, |
| 107 | + accountId: 'owner@example.com', |
| 108 | + providerId: 'google-email', |
| 109 | + userId: ownerId, |
| 110 | + scope: 'https://www.googleapis.com/auth/gmail.send', |
| 111 | + createdAt: now, |
| 112 | + updatedAt: now, |
| 113 | + }) |
| 114 | + await db.insert(credential).values([ |
| 115 | + { |
| 116 | + id: oauthCredentialId, |
| 117 | + workspaceId, |
| 118 | + type: 'oauth', |
| 119 | + displayName: 'owner@example.com', |
| 120 | + providerId: 'google-email', |
| 121 | + accountId, |
| 122 | + createdBy: ownerId, |
| 123 | + }, |
| 124 | + { |
| 125 | + id: serviceAccountCredentialId, |
| 126 | + workspaceId, |
| 127 | + type: 'service_account', |
| 128 | + displayName: 'robot@example.iam.gserviceaccount.com', |
| 129 | + providerId: 'google-service-account', |
| 130 | + createdBy: ownerId, |
| 131 | + }, |
| 132 | + { |
| 133 | + id: personalEnvCredentialId, |
| 134 | + workspaceId, |
| 135 | + type: 'env_personal', |
| 136 | + displayName: 'OWNER_PRIVATE_KEY', |
| 137 | + envKey: 'OWNER_PRIVATE_KEY', |
| 138 | + envOwnerUserId: ownerId, |
| 139 | + createdBy: ownerId, |
| 140 | + }, |
| 141 | + ]) |
| 142 | + await db.insert(credentialMember).values([ |
| 143 | + ...[oauthCredentialId, serviceAccountCredentialId, personalEnvCredentialId].map( |
| 144 | + (credentialId) => ({ |
| 145 | + id: generateId(), |
| 146 | + credentialId, |
| 147 | + userId: ownerId, |
| 148 | + role: 'admin' as const, |
| 149 | + status: 'active' as const, |
| 150 | + }) |
| 151 | + ), |
| 152 | + ...[oauthCredentialId, serviceAccountCredentialId].map((credentialId) => ({ |
| 153 | + id: generateId(), |
| 154 | + credentialId, |
| 155 | + userId: memberId, |
| 156 | + role: 'member' as const, |
| 157 | + status: 'active' as const, |
| 158 | + })), |
| 159 | + ]) |
| 160 | +}) |
| 161 | + |
| 162 | +afterAll(async () => { |
| 163 | + await deleteWorkspaceFixture(db, eq(workspace.id, workspaceId)) |
| 164 | + await db.delete(user).where(inArray(user.id, userIds)) |
| 165 | +}) |
| 166 | + |
| 167 | +describe('GET /api/auth/oauth/credentials by credentialId', () => { |
| 168 | + it.each([ |
| 169 | + ['an OAuth', oauthCredentialId], |
| 170 | + ['a service-account', serviceAccountCredentialId], |
| 171 | + ])('refuses a non-member who pins %s credential to its workflow', async (_, credentialId) => { |
| 172 | + expect(await lookupOAuth(outsiderId, { credentialId, workflowId })).toEqual({ |
| 173 | + status: 403, |
| 174 | + names: undefined, |
| 175 | + }) |
| 176 | + }) |
| 177 | + |
| 178 | + it('answers a non-member about an unsupported credential exactly as about an unknown id', async () => { |
| 179 | + const unknown = await lookupOAuth(outsiderId, { credentialId: generateId(), workflowId }) |
| 180 | + expect(unknown).toEqual({ status: 200, names: [] }) |
| 181 | + expect( |
| 182 | + await lookupOAuth(outsiderId, { credentialId: personalEnvCredentialId, workflowId }) |
| 183 | + ).toEqual(unknown) |
| 184 | + }) |
| 185 | + |
| 186 | + it.each([ |
| 187 | + ['a credential member', memberId], |
| 188 | + ['a workspace admin', adminId], |
| 189 | + ])('returns the credential to %s pinning its workflow', async (_, userId) => { |
| 190 | + expect(await lookupOAuth(userId, { credentialId: oauthCredentialId, workflowId })).toEqual({ |
| 191 | + status: 200, |
| 192 | + names: ['owner@example.com'], |
| 193 | + }) |
| 194 | + }) |
| 195 | +}) |
| 196 | + |
| 197 | +describe('listInternalCredentials lookup mode', () => { |
| 198 | + it('reads a credential the caller is not a member of as absent', async () => { |
| 199 | + expect(await lookupInternal(outsiderId, oauthCredentialId)).toBeNull() |
| 200 | + }) |
| 201 | + |
| 202 | + it('reads a legacy account id the caller is not a member of as absent', async () => { |
| 203 | + expect(await lookupInternal(outsiderId, accountId)).toBeNull() |
| 204 | + }) |
| 205 | + |
| 206 | + it("hides another user's personal environment variable, even from a workspace admin", async () => { |
| 207 | + expect(await lookupInternal(adminId, personalEnvCredentialId)).toBeNull() |
| 208 | + expect(await lookupInternal(ownerId, personalEnvCredentialId)).toBe('OWNER_PRIVATE_KEY') |
| 209 | + }) |
| 210 | + |
| 211 | + it.each([ |
| 212 | + ['a credential member', memberId], |
| 213 | + ['a workspace admin', adminId], |
| 214 | + ])('returns the credential to %s', async (_, userId) => { |
| 215 | + expect(await lookupInternal(userId, oauthCredentialId)).toBe('owner@example.com') |
| 216 | + }) |
| 217 | +}) |
0 commit comments