|
1
|
import { createBase32Plugin, generateSecret } from "@otplib/core" |
|
2
|
import { crypto } from "@otplib/plugin-crypto-node" |
|
3
|
import { generateSync, verifySync } from "@otplib/totp" |
|
4
|
import { generateTOTP } from "@otplib/uri" |
|
5
|
|
|
6
|
const BASE32_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567" |
|
7
|
const base32 = createBase32Plugin({ |
|
8
|
name: "medusa-base32", |
|
9
|
encode: encodeBase32, |
|
10
|
decode: decodeBase32, |
|
11
|
}) |
|
12
|
|
|
13
|
export type TotpOptions = { |
|
14
|
secret: string |
|
15
|
code?: string |
|
16
|
digits?: number |
|
17
|
period?: number |
|
18
|
window?: number |
|
19
|
timestamp?: number |
|
20
|
} |
|
21
|
|
|
22
|
/** |
|
23
|
* Creates a Base32-encoded shared secret suitable for authenticator apps. |
|
24
|
*/ |
|
25
|
export function generateTotpSecret(size = 20): string { |
|
26
|
return generateSecret({ |
|
27
|
crypto, |
|
28
|
base32, |
|
29
|
length: size, |
|
30
|
}) |
|
31
|
} |
|
32
|
|
|
33
|
/** |
|
34
|
* Generates an RFC 6238 TOTP code for the current time step. |
|
35
|
*/ |
|
36
|
export function generateTotpCode({ |
|
37
|
secret, |
|
38
|
digits = 6, |
|
39
|
period = 30, |
|
40
|
timestamp = Date.now(), |
|
41
|
}: TotpOptions): string { |
|
42
|
return generateSync({ |
|
43
|
secret, |
|
44
|
crypto, |
|
45
|
base32, |
|
46
|
digits, |
|
47
|
period, |
|
48
|
epoch: Math.floor(timestamp / 1000), |
|
49
|
}) |
|
50
|
} |
|
51
|
|
|
52
|
/** |
|
53
|
* Verifies a TOTP code across the configured time-step window. |
|
54
|
*/ |
|
55
|
export function verifyTotpCode({ |
|
56
|
secret, |
|
57
|
code, |
|
58
|
digits = 6, |
|
59
|
period = 30, |
|
60
|
window = 1, |
|
61
|
timestamp = Date.now(), |
|
62
|
}: TotpOptions): boolean { |
|
63
|
if (!code || !/^\d+$/.test(code) || code.length !== digits) { |
|
64
|
return false |
|
65
|
} |
|
66
|
|
|
67
|
return verifySync({ |
|
68
|
secret, |
|
69
|
crypto, |
|
70
|
base32, |
|
71
|
token: code, |
|
72
|
digits, |
|
73
|
period, |
|
74
|
epoch: Math.floor(timestamp / 1000), |
|
75
|
epochTolerance: window * period, |
|
76
|
}).valid |
|
77
|
} |
|
78
|
|
|
79
|
/** |
|
80
|
* Builds the otpauth URI consumed by authenticator apps and QR codes. |
|
81
|
*/ |
|
82
|
export function generateTotpUri({ |
|
83
|
issuer, |
|
84
|
accountName, |
|
85
|
secret, |
|
86
|
digits = 6, |
|
87
|
period = 30, |
|
88
|
}: { |
|
89
|
issuer: string |
|
90
|
accountName: string |
|
91
|
secret: string |
|
92
|
digits?: number |
|
93
|
period?: number |
|
94
|
}): string { |
|
95
|
return generateTOTP({ |
|
96
|
issuer, |
|
97
|
label: accountName, |
|
98
|
secret, |
|
99
|
algorithm: "sha1", |
|
100
|
digits, |
|
101
|
period, |
|
102
|
}) |
|
103
|
} |
|
104
|
|
|
105
|
function encodeBase32(buffer: Uint8Array): string { |
|
106
|
let bits = 0 |
|
107
|
let value = 0 |
|
108
|
let output = "" |
|
109
|
|
|
110
|
for (const byte of buffer) { |
|
111
|
value = (value << 8) | byte |
|
112
|
bits += 8 |
|
113
|
|
|
114
|
while (bits >= 5) { |
|
115
|
const index = (value >> (bits - 5)) & 31 |
|
116
|
output += BASE32_ALPHABET[index] |
|
117
|
bits -= 5 |
|
118
|
value &= (1 << bits) - 1 |
|
119
|
} |
|
120
|
} |
|
121
|
|
|
122
|
if (bits > 0) { |
|
123
|
output += BASE32_ALPHABET[(value << (5 - bits)) & 31] |
|
124
|
} |
|
125
|
|
|
126
|
return output |
|
127
|
} |
|
128
|
|
|
129
|
function decodeBase32(secret: string): Uint8Array { |
|
130
|
const normalized = secret |
|
131
|
.replace(/=+$/g, "") |
|
132
|
.replace(/\s+/g, "") |
|
133
|
.toUpperCase() |
|
134
|
let bits = 0 |
|
135
|
let value = 0 |
|
136
|
const bytes: number[] = [] |
|
137
|
|
|
138
|
for (const char of normalized) { |
|
139
|
const index = BASE32_ALPHABET.indexOf(char) |
|
140
|
|
|
141
|
if (index === -1) { |
|
142
|
throw new Error("Invalid TOTP secret") |
|
143
|
} |
|
144
|
|
|
145
|
value = (value << 5) | index |
|
146
|
bits += 5 |
|
147
|
|
|
148
|
if (bits >= 8) { |
|
149
|
bytes.push((value >> (bits - 8)) & 255) |
|
150
|
bits -= 8 |
|
151
|
value &= (1 << bits) - 1 |
|
152
|
} |
|
153
|
} |
|
154
|
|
|
155
|
return Uint8Array.from(bytes) |
|
156
|
} |