-
Notifications
You must be signed in to change notification settings - Fork 309
Expand file tree
/
Copy pathapi-key.service.ts
More file actions
303 lines (275 loc) · 8.29 KB
/
api-key.service.ts
File metadata and controls
303 lines (275 loc) · 8.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import {
Injectable,
Logger,
NotFoundException,
BadRequestException,
} from '@nestjs/common';
import { db } from '@trycompai/db';
import { statement } from '@trycompai/auth';
import { createHash, randomBytes } from 'node:crypto';
/** Result from validating an API key */
export interface ApiKeyValidationResult {
organizationId: string;
scopes: string[];
}
@Injectable()
export class ApiKeyService {
private readonly logger = new Logger(ApiKeyService.name);
/**
* Hash an API key for comparison
* @param apiKey The API key to hash
* @param salt Optional salt to use for hashing
* @returns The hashed API key
*/
private hashApiKey(apiKey: string, salt?: string): string {
if (salt) {
// If salt is provided, use it for hashing
return createHash('sha256')
.update(apiKey + salt)
.digest('hex');
}
// For backward compatibility, hash without salt
return createHash('sha256').update(apiKey).digest('hex');
}
private generateApiKey(): string {
const apiKey = randomBytes(32).toString('hex');
return `comp_${apiKey}`;
}
/** Extract the first 8 chars after the `comp_` prefix for indexed lookup */
private extractPrefix(apiKey: string): string {
return apiKey.slice(5, 13);
}
private generateSalt(): string {
return randomBytes(16).toString('hex');
}
async create(
organizationId: string,
name: string,
expiresAt?: string,
scopes?: string[],
) {
// New keys must have explicit scopes — no more legacy empty-scope keys
if (!scopes || scopes.length === 0) {
throw new BadRequestException(
'API keys must have at least one scope. Use the "Full Access" preset to grant all permissions.',
);
}
// Validate all scopes against the allowlist
const availableScopes = this.getAvailableScopes();
const invalid = scopes.filter((s) => !availableScopes.includes(s));
if (invalid.length > 0) {
throw new BadRequestException(
`Invalid scopes: ${invalid.join(', ')}`,
);
}
const apiKey = this.generateApiKey();
const salt = this.generateSalt();
const hashedKey = this.hashApiKey(apiKey, salt);
let expirationDate: Date | null = null;
if (expiresAt && expiresAt !== 'never') {
const now = new Date();
switch (expiresAt) {
case '30days':
expirationDate = new Date(now.setDate(now.getDate() + 30));
break;
case '90days':
expirationDate = new Date(now.setDate(now.getDate() + 90));
break;
case '1year':
expirationDate = new Date(
now.setFullYear(now.getFullYear() + 1),
);
break;
default:
throw new BadRequestException(
`Invalid expiresAt value: ${expiresAt}. Must be "never", "30days", "90days", or "1year".`,
);
}
}
const keyPrefix = this.extractPrefix(apiKey);
const record = await db.apiKey.create({
data: {
name,
key: hashedKey,
keyPrefix,
salt,
expiresAt: expirationDate,
organizationId,
scopes,
},
select: {
id: true,
name: true,
createdAt: true,
expiresAt: true,
},
});
return {
...record,
key: apiKey,
createdAt: record.createdAt.toISOString(),
expiresAt: record.expiresAt ? record.expiresAt.toISOString() : null,
};
}
async revoke(apiKeyId: string, organizationId: string) {
const result = await db.apiKey.updateMany({
where: {
id: apiKeyId,
organizationId,
},
data: {
isActive: false,
},
});
if (result.count === 0) {
throw new NotFoundException(
'API key not found or not authorized to revoke',
);
}
return { success: true };
}
/**
* Extract API key from request headers
* @param apiKeyHeader X-API-Key header value
* @returns The API key if found, null otherwise
*/
extractApiKey(apiKeyHeader?: string): string | null {
// Check if it's an X-API-Key header
if (apiKeyHeader) {
return apiKeyHeader;
}
return null;
}
/**
* Validate an API key and return the organization ID + scopes
* @param apiKey The API key to validate
* @returns The validation result if valid, null otherwise
*/
async validateApiKey(apiKey: string): Promise<ApiKeyValidationResult | null> {
if (!apiKey) {
return null;
}
try {
// Check if the model exists in the Prisma client
if (typeof db.apiKey === 'undefined') {
this.logger.error(
'ApiKey model not found. Make sure to run migrations.',
);
return null;
}
// Use key prefix for indexed lookup when available (new keys),
// fall back to full scan for legacy keys without prefix
const keyPrefix = apiKey.startsWith('comp_') ? this.extractPrefix(apiKey) : null;
const apiKeyRecords = await db.apiKey.findMany({
where: {
isActive: true,
OR: [
{ expiresAt: null },
{ expiresAt: { gt: new Date() } },
],
...(keyPrefix ? { keyPrefix } : {}),
},
select: {
id: true,
key: true,
salt: true,
organizationId: true,
expiresAt: true,
scopes: true,
},
});
// Find the matching API key by hashing with each candidate's salt
const matchingRecord = apiKeyRecords.find((record) => {
const hashedKey = record.salt
? this.hashApiKey(apiKey, record.salt)
: this.hashApiKey(apiKey);
return hashedKey === record.key;
});
if (!matchingRecord) {
// If prefix lookup found nothing, try legacy keys (no prefix set)
if (keyPrefix) {
const legacyRecords = await db.apiKey.findMany({
where: {
isActive: true,
keyPrefix: null,
OR: [
{ expiresAt: null },
{ expiresAt: { gt: new Date() } },
],
},
select: {
id: true,
key: true,
salt: true,
organizationId: true,
expiresAt: true,
scopes: true,
},
});
const legacyMatch = legacyRecords.find((record) => {
const hashedKey = record.salt
? this.hashApiKey(apiKey, record.salt)
: this.hashApiKey(apiKey);
return hashedKey === record.key;
});
if (legacyMatch) {
// Backfill the prefix for future lookups
await db.apiKey.update({
where: { id: legacyMatch.id },
data: { keyPrefix, lastUsedAt: new Date() },
});
return {
organizationId: legacyMatch.organizationId,
scopes: legacyMatch.scopes,
};
}
}
this.logger.warn('Invalid or expired API key attempted');
return null;
}
// Update the lastUsedAt timestamp
await db.apiKey.update({
where: {
id: matchingRecord.id,
},
data: {
lastUsedAt: new Date(),
},
});
this.logger.log(
`Valid API key used for organization: ${matchingRecord.organizationId}`,
);
return {
organizationId: matchingRecord.organizationId,
scopes: matchingRecord.scopes,
};
} catch (error) {
this.logger.error('Error validating API key:', error);
return null;
}
}
/**
* Resources from better-auth that are not used by any API endpoint's @RequirePermission.
* These are handled internally by better-auth for session-based auth only.
*/
private static readonly INTERNAL_ONLY_RESOURCES = [
'invitation',
'team',
];
/**
* Returns all valid `resource:action` scope pairs derived from the permission statement.
* Excludes internal-only resources that no API endpoint uses via @RequirePermission.
*/
getAvailableScopes(): string[] {
const scopes: string[] = [];
for (const [resource, actions] of Object.entries(statement)) {
if (ApiKeyService.INTERNAL_ONLY_RESOURCES.includes(resource)) {
continue;
}
for (const action of actions) {
scopes.push(`${resource}:${action}`);
}
}
return scopes;
}
}