Repository navigation
Expand file tree
/
Copy path_helpers.ts
More file actions
93 lines (78 loc) · 2.83 KB
/
Copy path_helpers.ts
File metadata and controls
93 lines (78 loc) · 2.83 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
import { createHash } from 'node:crypto'
import { genAuthCode } from '@codebuff/common/util/credentials'
const OPAQUE_CLI_AUTH_CODE_TOKEN_RE = /^[A-Za-z0-9_-]{43}$/
export function buildCliAuthCode(
fingerprintId: string,
expiresAt: string,
fingerprintHash: string,
): string {
return `${fingerprintId}.${expiresAt}.${fingerprintHash}`
}
export function isOpaqueCliAuthCodeToken(authCode: string): boolean {
return OPAQUE_CLI_AUTH_CODE_TOKEN_RE.test(authCode.trim())
}
export function getCliAuthCodeHashPrefix(authCode: string): string {
return createHash('sha256').update(authCode.trim()).digest('hex').slice(0, 12)
}
export async function resolveCliAuthCode(
authCode: string,
consumeCliAuthCodeToken: (authCodeToken: string) => Promise<string | null>,
): Promise<{ authCode: string; resolvedOpaqueToken: boolean }> {
const normalizedAuthCode = authCode.trim()
if (!isOpaqueCliAuthCodeToken(normalizedAuthCode)) {
return { authCode: normalizedAuthCode, resolvedOpaqueToken: false }
}
const signedAuthCode = await consumeCliAuthCodeToken(normalizedAuthCode)
if (!signedAuthCode) {
return { authCode: normalizedAuthCode, resolvedOpaqueToken: false }
}
return {
authCode: signedAuthCode,
resolvedOpaqueToken: true,
}
}
export function parseAuthCode(authCode: string): {
fingerprintId: string
expiresAt: string
receivedHash: string
} {
const normalizedAuthCode = authCode.trim()
const hashSeparatorIndex = normalizedAuthCode.lastIndexOf('.')
const expiresSeparatorIndex = normalizedAuthCode.lastIndexOf(
'.',
hashSeparatorIndex - 1,
)
if (hashSeparatorIndex === -1 || expiresSeparatorIndex === -1) {
const legacyMatch = normalizedAuthCode.match(
/^(?<fingerprintId>.+)-(?<expiresAt>\d+)-(?<receivedHash>[a-f0-9]{64})$/i,
)
if (legacyMatch?.groups) {
return {
fingerprintId: legacyMatch.groups.fingerprintId,
expiresAt: legacyMatch.groups.expiresAt,
receivedHash: legacyMatch.groups.receivedHash,
}
}
return { fingerprintId: '', expiresAt: '', receivedHash: '' }
}
const fingerprintId = normalizedAuthCode.slice(0, expiresSeparatorIndex)
const expiresAt = normalizedAuthCode.slice(
expiresSeparatorIndex + 1,
hashSeparatorIndex,
)
const receivedHash = normalizedAuthCode.slice(hashSeparatorIndex + 1)
return { fingerprintId, expiresAt, receivedHash }
}
export function validateAuthCode(
receivedHash: string,
fingerprintId: string,
expiresAt: string,
secret: string,
): { valid: boolean; expectedHash: string } {
const expectedHash = genAuthCode(fingerprintId, expiresAt, secret)
return { valid: receivedHash === expectedHash, expectedHash }
}
export function isAuthCodeExpired(expiresAt: string): boolean {
const expiresAtMs = Number(expiresAt)
return !Number.isFinite(expiresAtMs) || expiresAtMs < Date.now()
}