~bigbes/lethe

ref: a1e67ca703ed1c77d8629f2bdbb57668f85c98c2 lethe/web/src/lib/auth.test.ts -rw-r--r-- 6.8 KiB
a1e67ca7 — Eugene Blikh collector: add Claude Code parser 24 days ago
                                                                                
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import { describe, it, expect, afterEach, vi } from 'vitest'
import { createHash } from 'node:crypto'
import {
  generatePKCEPair,
  generateState,
  parseCallbackParams,
  tokenStore,
  countCallbackFailures,
} from './auth'

// ── Helpers ──────────────────────────────────────────────────────────────────

function b64urlNode(buf: Buffer): string {
  return buf.toString('base64').replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '')
}

// ── generatePKCEPair ─────────────────────────────────────────────────────────

describe('generatePKCEPair', () => {
  it('verifier length is 43–128 characters (RFC 7636 §4.1)', async () => {
    const { verifier } = await generatePKCEPair()
    expect(verifier.length).toBeGreaterThanOrEqual(43)
    expect(verifier.length).toBeLessThanOrEqual(128)
  })

  it('verifier contains only base64url-no-pad chars', async () => {
    const { verifier } = await generatePKCEPair()
    expect(verifier).toMatch(/^[A-Za-z0-9\-_]+$/)
  })

  it('challenge has no padding characters', async () => {
    const { challenge } = await generatePKCEPair()
    expect(challenge).not.toContain('=')
    expect(challenge).not.toContain('+')
    expect(challenge).not.toContain('/')
  })

  it('challenge is SHA-256(verifier) in base64url-no-pad', async () => {
    const { verifier, challenge } = await generatePKCEPair()
    const expected = b64urlNode(createHash('sha256').update(verifier).digest())
    expect(challenge).toBe(expected)
  })

  it('two consecutive calls produce different verifiers', async () => {
    const a = await generatePKCEPair()
    const b = await generatePKCEPair()
    expect(a.verifier).not.toBe(b.verifier)
  })
})

// ── generateState ────────────────────────────────────────────────────────────

describe('generateState', () => {
  it('returns a non-empty string', () => {
    expect(generateState().length).toBeGreaterThan(0)
  })

  it('two consecutive calls differ', () => {
    expect(generateState()).not.toBe(generateState())
  })

  it('always the same fixed length (22 chars for 16 bytes base64url-no-pad)', () => {
    const len = generateState().length
    for (let i = 0; i < 5; i++) {
      expect(generateState().length).toBe(len)
    }
  })

  it('contains only base64url-no-pad chars', () => {
    expect(generateState()).toMatch(/^[A-Za-z0-9\-_]+$/)
  })
})

// ── parseCallbackParams ──────────────────────────────────────────────────────

describe('parseCallbackParams', () => {
  it('?code=abc&state=xyz → {code, state}', () => {
    const result = parseCallbackParams('?code=abc&state=xyz')
    expect(result).toEqual({ code: 'abc', state: 'xyz' })
  })

  it('?error=access_denied&error_description=user-cancelled → {error, errorDescription}', () => {
    const result = parseCallbackParams('?error=access_denied&error_description=user-cancelled')
    expect(result).toEqual({ error: 'access_denied', errorDescription: 'user-cancelled' })
  })

  it('empty string → {error: missing_params}', () => {
    const result = parseCallbackParams('')
    expect(result).toEqual({ error: 'missing_params' })
  })

  it('?error=access_denied without description → {error} only', () => {
    const result = parseCallbackParams('?error=access_denied')
    expect(result).toEqual({ error: 'access_denied' })
  })

  it('?foo=bar with neither code nor error → {error: missing_params}', () => {
    const result = parseCallbackParams('?foo=bar')
    expect(result).toEqual({ error: 'missing_params' })
  })
})

// ── tokenStore ───────────────────────────────────────────────────────────────

describe('tokenStore', () => {
  afterEach(() => {
    tokenStore.set(null)
    vi.restoreAllMocks()
    vi.unstubAllGlobals()
  })

  it('get() is initially null', () => {
    expect(tokenStore.get()).toBeNull()
  })

  it('set("x") → get() === "x"', () => {
    tokenStore.set('x')
    expect(tokenStore.get()).toBe('x')
  })

  it('set(null) → get() === null', () => {
    tokenStore.set('x')
    tokenStore.set(null)
    expect(tokenStore.get()).toBeNull()
  })

  it('subscribe receives values in order', () => {
    const received: (string | null)[] = []
    tokenStore.subscribe((v) => received.push(v))
    tokenStore.set('x')
    tokenStore.set(null)
    expect(received).toEqual(['x', null])
  })

  it('unsubscribe stops further notifications', () => {
    const received: (string | null)[] = []
    const unsub = tokenStore.subscribe((v) => received.push(v))
    tokenStore.set('a')
    unsub()
    tokenStore.set('b')
    expect(received).toEqual(['a'])
  })

  it('does NOT touch window.localStorage (IV4)', () => {
    const setItemSpy = vi.fn()
    vi.stubGlobal('localStorage', {
      getItem: vi.fn(),
      setItem: setItemSpy,
      removeItem: vi.fn(),
      clear: vi.fn(),
      key: vi.fn(),
      length: 0,
    })

    tokenStore.set('secret')
    tokenStore.set(null)

    expect(setItemSpy).not.toHaveBeenCalled()
  })
})

// ── countCallbackFailures ────────────────────────────────────────────────────

describe('countCallbackFailures', () => {
  const FIVE_MIN = 300_000
  const now = 1_000_000

  it('empty log → {count: 0, blocked: false}', () => {
    expect(countCallbackFailures(now, [])).toEqual({ count: 0, blocked: false })
  })

  it('3 failures within 5min → {count: 3, blocked: false} (boundary: >3 blocks, not >=3)', () => {
    const log = [now - 1000, now - 2000, now - 3000]
    expect(countCallbackFailures(now, log)).toEqual({ count: 3, blocked: false })
  })

  it('4 failures within 5min → {count: 4, blocked: true}', () => {
    const log = [now - 1000, now - 2000, now - 3000, now - 4000]
    expect(countCallbackFailures(now, log)).toEqual({ count: 4, blocked: true })
  })

  it('4 failures but oldest is > 5min ago → only recent ones count', () => {
    // 3 recent + 1 old (> 5min ago)
    const log = [now - 1000, now - 2000, now - 3000, now - FIVE_MIN - 1]
    expect(countCallbackFailures(now, log)).toEqual({ count: 3, blocked: false })
  })

  it('is pure — does not mutate log array', () => {
    const log = [now - 1000, now - 2000]
    const original = [...log]
    countCallbackFailures(now, log)
    expect(log).toEqual(original)
  })
})