@@ -2,8 +2,14 @@ import { createHash } from 'node:crypto'
22
33import { genAuthCode } from '@codebuff/common/util/credentials'
44
5- const OPAQUE_CLI_AUTH_CODE_TOKEN_RE = / ^ [ A - Z a - z 0 - 9 _ - ] { 43 } $ /
6- const CLI_AUTH_CODE_HASH_RE = / ^ [ a - f 0 - 9 ] { 64 } $ / i
5+ import {
6+ isCliAuthCodeCandidate ,
7+ isOpaqueCliAuthCodeToken ,
8+ parseCliAuthCodeShape ,
9+ } from '@/lib/cli-auth-code-shape'
10+
11+ export { isCliAuthCodeCandidate , isOpaqueCliAuthCodeToken }
12+
713const CLI_AUTH_CODE_TOKEN_IDENTIFIER_PREFIX = 'cli-login:'
814const CONSUMED_CLI_AUTH_CODE_TOKEN_IDENTIFIER_PREFIX = 'cli-login-consumed:'
915const CONSUMED_CLI_AUTH_CODE_TOKEN_VALUE = 'consumed'
@@ -20,23 +26,6 @@ export function buildCliAuthCode(
2026 return `${ fingerprintId } .${ expiresAt } .${ fingerprintHash } `
2127}
2228
23- export function isOpaqueCliAuthCodeToken ( authCode : string ) : boolean {
24- return OPAQUE_CLI_AUTH_CODE_TOKEN_RE . test ( authCode . trim ( ) )
25- }
26-
27- export function isCliAuthCodeCandidate ( authCode : string ) : boolean {
28- if ( isOpaqueCliAuthCodeToken ( authCode ) ) {
29- return true
30- }
31-
32- const { fingerprintId, expiresAt, receivedHash } = parseAuthCode ( authCode )
33- return (
34- fingerprintId . length > 0 &&
35- / ^ \d + $ / . test ( expiresAt ) &&
36- CLI_AUTH_CODE_HASH_RE . test ( receivedHash )
37- )
38- }
39-
4029export function getCliAuthCodeHashPrefix ( authCode : string ) : string {
4130 return getCliAuthCodeHash ( authCode ) . slice ( 0 , 12 )
4231}
@@ -123,36 +112,7 @@ export function parseAuthCode(authCode: string): {
123112 expiresAt : string
124113 receivedHash : string
125114} {
126- const normalizedAuthCode = authCode . trim ( )
127- const hashSeparatorIndex = normalizedAuthCode . lastIndexOf ( '.' )
128- const expiresSeparatorIndex = normalizedAuthCode . lastIndexOf (
129- '.' ,
130- hashSeparatorIndex - 1 ,
131- )
132-
133- if ( hashSeparatorIndex === - 1 || expiresSeparatorIndex === - 1 ) {
134- const legacyMatch = normalizedAuthCode . match (
135- / ^ (?< fingerprintId > .+ ) - (?< expiresAt > \d + ) - (?< receivedHash > [ a - f 0 - 9 ] { 64 } ) $ / i,
136- )
137- if ( legacyMatch ?. groups ) {
138- return {
139- fingerprintId : legacyMatch . groups . fingerprintId ,
140- expiresAt : legacyMatch . groups . expiresAt ,
141- receivedHash : legacyMatch . groups . receivedHash ,
142- }
143- }
144-
145- return { fingerprintId : '' , expiresAt : '' , receivedHash : '' }
146- }
147-
148- const fingerprintId = normalizedAuthCode . slice ( 0 , expiresSeparatorIndex )
149- const expiresAt = normalizedAuthCode . slice (
150- expiresSeparatorIndex + 1 ,
151- hashSeparatorIndex ,
152- )
153- const receivedHash = normalizedAuthCode . slice ( hashSeparatorIndex + 1 )
154-
155- return { fingerprintId, expiresAt, receivedHash }
115+ return parseCliAuthCodeShape ( authCode )
156116}
157117
158118export function validateAuthCode (
0 commit comments