forked from jordanlambrecht/tracker-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtotp.test.ts
More file actions
180 lines (149 loc) · 5.61 KB
/
Copy pathtotp.test.ts
File metadata and controls
180 lines (149 loc) · 5.61 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
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
// src/lib/__tests__/totp.test.ts
import { Secret, TOTP } from "otpauth"
import { describe, expect, it } from "vitest"
import {
generateBackupCodes,
generateTotpSecret,
hashBackupCode,
verifyAndConsumeBackupCode,
verifyTotpCode,
} from "@/lib/totp"
// ---------------------------------------------------------------------------
// generateTotpSecret
// ---------------------------------------------------------------------------
describe("generateTotpSecret", () => {
it("returns a base32 secret and otpauth URI", () => {
const { secret, uri } = generateTotpSecret("testuser")
expect(secret).toMatch(/^[A-Z2-7]+=*$/)
expect(uri).toContain("otpauth://totp/")
expect(uri).toContain("Tracker%20Tracker")
expect(uri).toContain(`secret=${secret}`)
})
it("generates unique secrets on each call", () => {
const a = generateTotpSecret("user")
const b = generateTotpSecret("user")
expect(a.secret).not.toBe(b.secret)
})
})
// ---------------------------------------------------------------------------
// verifyTotpCode
// ---------------------------------------------------------------------------
describe("verifyTotpCode", () => {
it("accepts a valid current code", () => {
const { secret } = generateTotpSecret("test")
const totp = new TOTP({
secret: Secret.fromBase32(secret),
algorithm: "SHA1",
digits: 6,
period: 30,
})
const code = totp.generate()
expect(verifyTotpCode(secret, code)).toBe(true)
})
it("rejects a completely wrong code", () => {
const { secret } = generateTotpSecret("test")
expect(verifyTotpCode(secret, "000000")).toBe(false)
})
it("rejects non-numeric input", () => {
const { secret } = generateTotpSecret("test")
expect(verifyTotpCode(secret, "abcdef")).toBe(false)
})
it("rejects empty string", () => {
const { secret } = generateTotpSecret("test")
expect(verifyTotpCode(secret, "")).toBe(false)
})
})
// ---------------------------------------------------------------------------
// generateBackupCodes
// ---------------------------------------------------------------------------
describe("generateBackupCodes", () => {
it("generates 8 codes", () => {
const codes = generateBackupCodes()
expect(codes).toHaveLength(8)
})
it("codes match XXXX-XXXX format", () => {
const codes = generateBackupCodes()
for (const code of codes) {
expect(code).toMatch(/^[0-9A-F]{4}-[0-9A-F]{4}$/)
}
})
it("generates unique codes", () => {
const codes = generateBackupCodes()
const unique = new Set(codes)
expect(unique.size).toBe(codes.length)
})
})
// ---------------------------------------------------------------------------
// hashBackupCode + verifyAndConsumeBackupCode
// ---------------------------------------------------------------------------
describe("hashBackupCode", () => {
it("returns a hash, salt, and used=false", () => {
const entry = hashBackupCode("ABCD-1234")
expect(entry.hash).toMatch(/^[0-9a-f]{64}$/)
expect(entry.salt).toMatch(/^[0-9a-f]{32}$/)
expect(entry.used).toBe(false)
})
it("produces different hashes for the same code (random salt)", () => {
const a = hashBackupCode("ABCD-1234")
const b = hashBackupCode("ABCD-1234")
expect(a.hash).not.toBe(b.hash)
expect(a.salt).not.toBe(b.salt)
})
})
describe("verifyAndConsumeBackupCode", () => {
it("verifies a valid backup code and marks it used", () => {
const code = "ABCD-EF01"
const entry = hashBackupCode(code)
const entries = [entry]
const { valid, updatedEntries } = verifyAndConsumeBackupCode(code, entries)
expect(valid).toBe(true)
expect(updatedEntries[0].used).toBe(true)
})
it("accepts code without dashes", () => {
const code = "ABCD-EF01"
const entry = hashBackupCode(code)
const { valid } = verifyAndConsumeBackupCode("ABCDEF01", [entry])
expect(valid).toBe(true)
})
it("accepts lowercase input", () => {
const code = "ABCD-EF01"
const entry = hashBackupCode(code)
const { valid } = verifyAndConsumeBackupCode("abcd-ef01", [entry])
expect(valid).toBe(true)
})
it("rejects an invalid code", () => {
const entry = hashBackupCode("ABCD-EF01")
const { valid, updatedEntries } = verifyAndConsumeBackupCode("WRONG-CODE", [entry])
expect(valid).toBe(false)
expect(updatedEntries[0].used).toBe(false)
})
it("rejects an already-used code", () => {
const code = "ABCD-EF01"
const entry = { ...hashBackupCode(code), used: true }
const { valid } = verifyAndConsumeBackupCode(code, [entry])
expect(valid).toBe(false)
})
it("only marks the matched code as used, not others", () => {
const codes = ["AAAA-1111", "BBBB-2222", "CCCC-3333"]
const entries = codes.map((c) => hashBackupCode(c))
const { valid, updatedEntries } = verifyAndConsumeBackupCode("BBBB-2222", entries)
expect(valid).toBe(true)
expect(updatedEntries[0].used).toBe(false)
expect(updatedEntries[1].used).toBe(true)
expect(updatedEntries[2].used).toBe(false)
})
it("works with a full set of generated codes", () => {
const codes = generateBackupCodes()
const entries = codes.map((c) => hashBackupCode(c))
// Verify the 5th code
const { valid, updatedEntries } = verifyAndConsumeBackupCode(codes[4], entries)
expect(valid).toBe(true)
expect(updatedEntries[4].used).toBe(true)
// Same code should now fail
const second = verifyAndConsumeBackupCode(codes[4], updatedEntries)
expect(second.valid).toBe(false)
// Different unused code should still work
const third = verifyAndConsumeBackupCode(codes[0], updatedEntries)
expect(third.valid).toBe(true)
})
})