|
1
|
import { generateTotpCode, generateTotpUri, verifyTotpCode } from "../totp" |
|
2
|
|
|
3
|
describe("TOTP utilities", () => { |
|
4
|
const secret = "GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ" |
|
5
|
|
|
6
|
it("generates RFC 6238 compatible SHA1 codes", () => { |
|
7
|
expect( |
|
8
|
generateTotpCode({ |
|
9
|
secret, |
|
10
|
digits: 8, |
|
11
|
timestamp: 59_000, |
|
12
|
}) |
|
13
|
).toBe("94287082") |
|
14
|
|
|
15
|
expect( |
|
16
|
generateTotpCode({ |
|
17
|
secret, |
|
18
|
digits: 8, |
|
19
|
timestamp: 1_111_111_109_000, |
|
20
|
}) |
|
21
|
).toBe("07081804") |
|
22
|
}) |
|
23
|
|
|
24
|
it("verifies codes inside the configured window", () => { |
|
25
|
const code = generateTotpCode({ |
|
26
|
secret, |
|
27
|
digits: 8, |
|
28
|
timestamp: 59_000, |
|
29
|
}) |
|
30
|
|
|
31
|
expect( |
|
32
|
verifyTotpCode({ |
|
33
|
secret, |
|
34
|
code, |
|
35
|
digits: 8, |
|
36
|
timestamp: 89_000, |
|
37
|
window: 1, |
|
38
|
}) |
|
39
|
).toBe(true) |
|
40
|
|
|
41
|
expect( |
|
42
|
verifyTotpCode({ |
|
43
|
secret, |
|
44
|
code, |
|
45
|
digits: 8, |
|
46
|
timestamp: 119_000, |
|
47
|
window: 1, |
|
48
|
}) |
|
49
|
).toBe(false) |
|
50
|
}) |
|
51
|
|
|
52
|
it("percent-encodes the otpauth issuer and label", () => { |
|
53
|
const uri = generateTotpUri({ |
|
54
|
issuer: "Medusa Cloud", |
|
55
|
accountName: "test@example.com", |
|
56
|
secret, |
|
57
|
}) |
|
58
|
|
|
59
|
expect(uri).toContain("otpauth://totp/Medusa%20Cloud:test%40example.com") |
|
60
|
expect(uri).toContain("issuer=Medusa%20Cloud") |
|
61
|
expect(uri).not.toContain("Medusa+Cloud") |
|
62
|
}) |
|
63
|
}) |