|
1 | 1 | import { genAuthCode } from '@codebuff/common/util/credentials' |
2 | 2 | import { afterEach, beforeEach, describe, expect, test } from 'bun:test' |
3 | 3 |
|
4 | | -import { parseAuthCode, validateAuthCode, isAuthCodeExpired } from '../_helpers' |
| 4 | +import { |
| 5 | + buildCliAuthCode, |
| 6 | + isAuthCodeExpired, |
| 7 | + isOpaqueCliAuthCodeToken, |
| 8 | + parseAuthCode, |
| 9 | + resolveCliAuthCode, |
| 10 | + validateAuthCode, |
| 11 | +} from '../_helpers' |
5 | 12 |
|
6 | 13 | describe('freebuff onboard/_helpers', () => { |
7 | 14 | describe('parseAuthCode', () => { |
@@ -78,6 +85,117 @@ describe('freebuff onboard/_helpers', () => { |
78 | 85 | }) |
79 | 86 | }) |
80 | 87 |
|
| 88 | + describe('opaque CLI auth code tokens', () => { |
| 89 | + const testSecret = 'test-secret-key' |
| 90 | + const testFingerprintId = 'fp-abc123' |
| 91 | + |
| 92 | + test('builds the signed auth code payload', () => { |
| 93 | + expect(buildCliAuthCode('fingerprint-id', '1704067200000', 'hash')).toBe( |
| 94 | + 'fingerprint-id.1704067200000.hash', |
| 95 | + ) |
| 96 | + }) |
| 97 | + |
| 98 | + test('identifies 43 character base64url browser tokens only', () => { |
| 99 | + const opaqueToken = 'A'.repeat(41) + '-_' |
| 100 | + const signedAuthCode = buildCliAuthCode( |
| 101 | + testFingerprintId, |
| 102 | + '1704067200000', |
| 103 | + 'a'.repeat(64), |
| 104 | + ) |
| 105 | + |
| 106 | + expect(isOpaqueCliAuthCodeToken(opaqueToken)).toBe(true) |
| 107 | + expect(isOpaqueCliAuthCodeToken(` ${opaqueToken}\n`)).toBe(true) |
| 108 | + expect(isOpaqueCliAuthCodeToken(signedAuthCode)).toBe(false) |
| 109 | + expect(isOpaqueCliAuthCodeToken('A'.repeat(42))).toBe(false) |
| 110 | + expect(isOpaqueCliAuthCodeToken(`${'A'.repeat(42)}.`)).toBe(false) |
| 111 | + }) |
| 112 | + |
| 113 | + test('resolves an opaque browser token before validation', async () => { |
| 114 | + const expiresAt = '4102444800000' |
| 115 | + const fingerprintHash = genAuthCode( |
| 116 | + testFingerprintId, |
| 117 | + expiresAt, |
| 118 | + testSecret, |
| 119 | + ) |
| 120 | + const signedAuthCode = buildCliAuthCode( |
| 121 | + testFingerprintId, |
| 122 | + expiresAt, |
| 123 | + fingerprintHash, |
| 124 | + ) |
| 125 | + const opaqueToken = 'a'.repeat(43) |
| 126 | + |
| 127 | + const result = await resolveCliAuthCode(opaqueToken, async (token) => { |
| 128 | + expect(token).toBe(opaqueToken) |
| 129 | + return signedAuthCode |
| 130 | + }) |
| 131 | + |
| 132 | + expect(result).toEqual({ |
| 133 | + authCode: signedAuthCode, |
| 134 | + resolvedOpaqueToken: true, |
| 135 | + }) |
| 136 | + |
| 137 | + const parsed = parseAuthCode(result.authCode) |
| 138 | + expect( |
| 139 | + validateAuthCode( |
| 140 | + parsed.receivedHash, |
| 141 | + parsed.fingerprintId, |
| 142 | + parsed.expiresAt, |
| 143 | + testSecret, |
| 144 | + ).valid, |
| 145 | + ).toBe(true) |
| 146 | + }) |
| 147 | + |
| 148 | + test('does not look up already signed auth codes', async () => { |
| 149 | + const signedAuthCode = buildCliAuthCode( |
| 150 | + testFingerprintId, |
| 151 | + '4102444800000', |
| 152 | + 'a'.repeat(64), |
| 153 | + ) |
| 154 | + let lookedUp = false |
| 155 | + |
| 156 | + const result = await resolveCliAuthCode(signedAuthCode, async () => { |
| 157 | + lookedUp = true |
| 158 | + return null |
| 159 | + }) |
| 160 | + |
| 161 | + expect(lookedUp).toBe(false) |
| 162 | + expect(result).toEqual({ |
| 163 | + authCode: signedAuthCode, |
| 164 | + resolvedOpaqueToken: false, |
| 165 | + }) |
| 166 | + }) |
| 167 | + |
| 168 | + test('resolves expired stored payloads so callers can show expired', async () => { |
| 169 | + const expiresAt = '0' |
| 170 | + const fingerprintHash = genAuthCode( |
| 171 | + testFingerprintId, |
| 172 | + expiresAt, |
| 173 | + testSecret, |
| 174 | + ) |
| 175 | + const signedAuthCode = buildCliAuthCode( |
| 176 | + testFingerprintId, |
| 177 | + expiresAt, |
| 178 | + fingerprintHash, |
| 179 | + ) |
| 180 | + |
| 181 | + const result = await resolveCliAuthCode( |
| 182 | + 'b'.repeat(43), |
| 183 | + async () => signedAuthCode, |
| 184 | + ) |
| 185 | + const parsed = parseAuthCode(result.authCode) |
| 186 | + |
| 187 | + expect(isAuthCodeExpired(parsed.expiresAt)).toBe(true) |
| 188 | + expect( |
| 189 | + validateAuthCode( |
| 190 | + parsed.receivedHash, |
| 191 | + parsed.fingerprintId, |
| 192 | + parsed.expiresAt, |
| 193 | + testSecret, |
| 194 | + ).valid, |
| 195 | + ).toBe(true) |
| 196 | + }) |
| 197 | + }) |
| 198 | + |
81 | 199 | describe('isAuthCodeExpired', () => { |
82 | 200 | let originalDateNow: typeof Date.now |
83 | 201 |
|
|
0 commit comments