-
-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathcodinitAuth.ts
More file actions
43 lines (38 loc) · 1.53 KB
/
Copy pathcodinitAuth.ts
File metadata and controls
43 lines (38 loc) · 1.53 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
import { generateKeyPair, exportPKCS8, exportJWK } from 'jose';
import type { CodinitProject } from './types.js';
import { queryEnvVariableWithRetries, setEnvVariablesWithRetries } from './codinitEnvVariables.js';
import { logger } from './utils/logger.js';
export async function initializeCodinitAuth(project: CodinitProject) {
const SITE_URL = await queryEnvVariableWithRetries(project, 'SITE_URL');
const JWKS = await queryEnvVariableWithRetries(project, 'JWKS');
const JWT_PRIVATE_KEY = await queryEnvVariableWithRetries(project, 'JWT_PRIVATE_KEY');
const newEnv: Record<string, string> = {};
if (SITE_URL && SITE_URL !== 'http://127.0.0.1:5173') {
console.warn('SITE_URL is not http://127.0.0.1:5173');
}
if (!SITE_URL) {
newEnv.SITE_URL = 'http://127.0.0.1:5173';
}
if (!JWKS || !JWT_PRIVATE_KEY) {
const keys = await generateKeys();
newEnv.JWKS = JSON.stringify(keys.JWKS);
newEnv.JWT_PRIVATE_KEY = keys.JWT_PRIVATE_KEY;
}
if (!SITE_URL) {
newEnv.SITE_URL = 'http://127.0.0.1:5173';
}
if (Object.entries(newEnv).length > 0) {
await setEnvVariablesWithRetries(project, newEnv);
}
logger.info('✅ CodinIT Auth setup!');
}
async function generateKeys() {
const keys = await generateKeyPair('RS256', { extractable: true });
const privateKey = await exportPKCS8(keys.privateKey);
const publicKey = await exportJWK(keys.publicKey);
const jwks = { keys: [{ use: 'sig', ...publicKey }] };
return {
JWT_PRIVATE_KEY: `${privateKey.trimEnd().replace(/\n/g, ' ')}`,
JWKS: jwks,
};
}