-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsign-handler.ts
More file actions
62 lines (53 loc) · 1.45 KB
/
sign-handler.ts
File metadata and controls
62 lines (53 loc) · 1.45 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
import crypto from "crypto";
/**
* Shared signing logic for credential authentication
* Used by: Vercel function, Vite middleware, and preview server
*/
export interface SignRequest {
apiKey: string;
bypassRateLimit?: boolean;
}
export interface SignResponse {
signedConfig: string;
signature: string;
}
/**
* Sign credentials using HMAC-SHA256
* @param request - Request containing apiKey and optional flags
* @param secret - Signing secret from environment
* @returns Signed config and signature
*/
export function signCredentials(
request: SignRequest,
secret: string,
): SignResponse {
const { apiKey, bypassRateLimit } = request;
// Build config object (matches terminal server expectations)
const config = {
apiKey,
timestamp: Date.now(),
bypassRateLimit: bypassRateLimit || false,
};
// Serialize to JSON - this exact string is what gets signed
const configString = JSON.stringify(config);
// Generate HMAC-SHA256 signature
const hmac = crypto.createHmac("sha256", secret);
hmac.update(configString);
const signature = hmac.digest("hex");
return {
signedConfig: configString,
signature,
};
}
/**
* Get signing secret from environment variables
* Checks multiple variable names for compatibility
*/
export function getSigningSecret(): string | null {
return (
process.env.TERMINAL_SERVER_SIGNING_SECRET ||
process.env.SIGNING_SECRET ||
process.env.CI_BYPASS_SECRET ||
null
);
}