Skip to content

Commit

Permalink
feat(api, schema): Add preview field in API Key (#680)
Browse files Browse the repository at this point in the history
  • Loading branch information
rajdip-b authored Jan 30, 2025
1 parent b2be010 commit 06d8c44
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 12 deletions.
6 changes: 6 additions & 0 deletions apps/api/src/api-key/api-key.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,13 @@ describe('Api Key Role Controller Tests', () => {
expect(response.json().name).toBe('Test')
expect(response.json().slug).toBeDefined()
expect(response.json().value).toMatch(/^ks_*/)
expect(response.json().preview).toMatch(/^ks_*/)
expect(response.json().authorities).toEqual(['READ_API_KEY'])

const last4CharsOfValue = response.json().value.slice(-4)
const last4CharsOfPreview = response.json().preview.slice(-4)
expect(last4CharsOfValue).toBe(last4CharsOfPreview)

const apiKey = await prisma.apiKey.findUnique({
where: {
id: response.json().id
Expand Down Expand Up @@ -254,6 +259,7 @@ describe('Api Key Role Controller Tests', () => {
id: apiKey.id,
name: 'Test Key',
slug: apiKey.slug,
preview: apiKey.preview,
authorities: ['READ_API_KEY', 'CREATE_ENVIRONMENT'],
expiresAt: expect.any(String),
createdAt: expect.any(String),
Expand Down
9 changes: 9 additions & 0 deletions apps/api/src/api-key/service/api-key.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export class ApiKeyService {
private apiKeySelect = {
id: true,
expiresAt: true,
preview: true,
name: true,
slug: true,
authorities: true,
Expand All @@ -41,12 +42,20 @@ export class ApiKeyService {
await this.isApiKeyUnique(user, dto.name)

const plainTextApiKey = generateApiKey()

// Generate the preview key in format ks_****<last 4 chars>
const previewKey = `ks_****${plainTextApiKey.slice(-4)}`
this.logger.log(
`User ${user.id} created API key ${previewKey} with name ${dto.name}`
)

const hashedApiKey = toSHA256(plainTextApiKey)
const apiKey = await this.prisma.apiKey.create({
data: {
name: dto.name,
slug: await generateEntitySlug(dto.name, 'API_KEY', this.prisma),
value: hashedApiKey,
preview: previewKey,
authorities: dto.authorities
? {
set: dto.authorities
Expand Down
10 changes: 4 additions & 6 deletions apps/api/src/environment/service/environment.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,12 +325,10 @@ export class EnvironmentService {

// Parse the secret and variable counts for each environment
for (const environment of items) {
const [secrets, variables] = await Promise.all([
this.getSecretCount(environment.id),
this.getVariableCount(environment.id)
])
environment['secrets'] = secrets
environment['variables'] = variables
const secretCount = await this.getSecretCount(environment.id)
const variableCount = await this.getVariableCount(environment.id)
environment['secrets'] = secretCount
environment['variables'] = variableCount
}

// Calculate metadata for pagination
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "ApiKey" ADD COLUMN "preview" TEXT NOT NULL DEFAULT 'ks_******';
1 change: 1 addition & 0 deletions apps/api/src/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,7 @@ model Variable {
model ApiKey {
id String @id @default(cuid())
name String
preview String @default("ks_******")
slug String @unique
value String @unique
expiresAt DateTime?
Expand Down
1 change: 1 addition & 0 deletions packages/schema/src/api-key/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const ApiKeySchema = z.object({
name: z.string(),
slug: z.string(),
value: z.string(),
preview: z.string(),
expiresAt: z.string().datetime().nullable(),
createdAt: z.string().datetime(),
updatedAt: z.string().datetime(),
Expand Down
15 changes: 10 additions & 5 deletions packages/schema/tests/api-key.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ describe('API Key Schemas Tests', () => {
name: 'API Key Name',
slug: 'api-key-slug',
value: 'api-key-value',
preview: 'api-key-preview',
expiresAt: '2024-10-10T10:00:00Z',
createdAt: '2024-10-09T10:00:00Z',
updatedAt: '2024-10-09T10:00:00Z',
Expand All @@ -44,7 +45,7 @@ describe('API Key Schemas Tests', () => {
// no userId
})
expect(result.success).toBe(false)
expect(result.error?.issues).toHaveLength(3)
expect(result.error?.issues).toHaveLength(4)
})
})

Expand Down Expand Up @@ -95,6 +96,7 @@ describe('API Key Schemas Tests', () => {
name: 'API Key Name',
slug: 'api-key-slug',
value: 'api-key-value',
preview: 'api-key-preview',
expiresAt: '2024-10-10T10:00:00Z',
createdAt: '2024-10-09T10:00:00Z',
updatedAt: '2024-10-09T10:00:00Z',
Expand All @@ -118,7 +120,7 @@ describe('API Key Schemas Tests', () => {
// no userId
})
expect(result.success).toBe(false)
expect(result.error?.issues).toHaveLength(3)
expect(result.error?.issues).toHaveLength(4)
})
})

Expand Down Expand Up @@ -151,6 +153,7 @@ describe('API Key Schemas Tests', () => {
id: 'apikey123',
name: 'API Key Name',
slug: 'api-key-slug',
preview: 'api-key-preview',
expiresAt: '2024-10-10T10:00:00Z',
createdAt: '2024-10-09T10:00:00Z',
updatedAt: '2024-10-09T10:00:00Z',
Expand All @@ -170,7 +173,7 @@ describe('API Key Schemas Tests', () => {
authorities: ['INVALID_AUTHORITY'] // Invalid authority
})
expect(result.success).toBe(false)
expect(result.error?.issues).toHaveLength(2)
expect(result.error?.issues).toHaveLength(3)
})
})

Expand Down Expand Up @@ -232,6 +235,7 @@ describe('API Key Schemas Tests', () => {
id: 'apikey123',
name: 'API Key Name',
slug: 'api-key-slug',
preview: 'api-key-preview',
expiresAt: '2024-10-10T10:00:00Z',
createdAt: '2024-10-09T10:00:00Z',
updatedAt: '2024-10-09T10:00:00Z',
Expand Down Expand Up @@ -283,7 +287,7 @@ describe('API Key Schemas Tests', () => {
}
})
expect(result.success).toBe(false)
expect(result.error?.issues).toHaveLength(2)
expect(result.error?.issues).toHaveLength(3)
})
})

Expand All @@ -310,6 +314,7 @@ describe('API Key Schemas Tests', () => {
id: 'apikey123',
name: 'API Key Name',
slug: 'api-key-slug',
preview: 'api-key-preview',
expiresAt: '2024-10-10T10:00:00Z',
createdAt: '2024-10-09T10:00:00Z',
updatedAt: '2024-10-09T10:00:00Z',
Expand All @@ -329,7 +334,7 @@ describe('API Key Schemas Tests', () => {
authorities: ['INVALID_AUTHORITY'] // Invalid authority
})
expect(result.success).toBe(false)
expect(result.error?.issues).toHaveLength(2)
expect(result.error?.issues).toHaveLength(3)
})
})

Expand Down
1 change: 0 additions & 1 deletion packages/schema/tests/variable.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ describe('Variable Schema Tests', () => {
}
]
})
console.log(result.error)
expect(result.success).toBe(true)
})

Expand Down

0 comments on commit 06d8c44

Please sign in to comment.