-
-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathhotp.js
223 lines (210 loc) · 6.16 KB
/
hotp.js
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import { uintDecode } from "./internal/encoding/uint.js";
import { canonicalizeAlgorithm, hmacDigest } from "./internal/crypto/hmac-digest.js";
import { Secret } from "./secret.js";
import { timingSafeEqual } from "./internal/crypto/timing-safe-equal.js";
/**
* HOTP: An HMAC-based One-time Password Algorithm.
* @see [RFC 4226](https://datatracker.ietf.org/doc/html/rfc4226)
*/
class HOTP {
/**
* Default configuration.
* @type {{
* issuer: string,
* label: string,
* issuerInLabel: boolean,
* algorithm: string,
* digits: number,
* counter: number
* window: number
* }}
*/
static get defaults() {
return {
issuer: "",
label: "OTPAuth",
issuerInLabel: true,
algorithm: "SHA1",
digits: 6,
counter: 0,
window: 1,
};
}
/**
* Creates an HOTP object.
* @param {Object} [config] Configuration options.
* @param {string} [config.issuer=''] Account provider.
* @param {string} [config.label='OTPAuth'] Account label.
* @param {boolean} [config.issuerInLabel=true] Include issuer prefix in label.
* @param {Secret|string} [config.secret=Secret] Secret key.
* @param {string} [config.algorithm='SHA1'] HMAC hashing algorithm.
* @param {number} [config.digits=6] Token length.
* @param {number} [config.counter=0] Initial counter value.
*/
constructor({
issuer = HOTP.defaults.issuer,
label = HOTP.defaults.label,
issuerInLabel = HOTP.defaults.issuerInLabel,
secret = new Secret(),
algorithm = HOTP.defaults.algorithm,
digits = HOTP.defaults.digits,
counter = HOTP.defaults.counter,
} = {}) {
/**
* Account provider.
* @type {string}
*/
this.issuer = issuer;
/**
* Account label.
* @type {string}
*/
this.label = label;
/**
* Include issuer prefix in label.
* @type {boolean}
*/
this.issuerInLabel = issuerInLabel;
/**
* Secret key.
* @type {Secret}
*/
this.secret = typeof secret === "string" ? Secret.fromBase32(secret) : secret;
/**
* HMAC hashing algorithm.
* @type {string}
*/
this.algorithm = canonicalizeAlgorithm(algorithm);
/**
* Token length.
* @type {number}
*/
this.digits = digits;
/**
* Initial counter value.
* @type {number}
*/
this.counter = counter;
}
/**
* Generates an HOTP token.
* @param {Object} config Configuration options.
* @param {Secret} config.secret Secret key.
* @param {string} [config.algorithm='SHA1'] HMAC hashing algorithm.
* @param {number} [config.digits=6] Token length.
* @param {number} [config.counter=0] Counter value.
* @returns {string} Token.
*/
static generate({
secret,
algorithm = HOTP.defaults.algorithm,
digits = HOTP.defaults.digits,
counter = HOTP.defaults.counter,
}) {
const digest = hmacDigest(algorithm, secret.bytes, uintDecode(counter));
const offset = digest[digest.byteLength - 1] & 15;
const otp =
(((digest[offset] & 127) << 24) |
((digest[offset + 1] & 255) << 16) |
((digest[offset + 2] & 255) << 8) |
(digest[offset + 3] & 255)) %
10 ** digits;
return otp.toString().padStart(digits, "0");
}
/**
* Generates an HOTP token.
* @param {Object} [config] Configuration options.
* @param {number} [config.counter=this.counter++] Counter value.
* @returns {string} Token.
*/
generate({ counter = this.counter++ } = {}) {
return HOTP.generate({
secret: this.secret,
algorithm: this.algorithm,
digits: this.digits,
counter,
});
}
/**
* Validates an HOTP token.
* @param {Object} config Configuration options.
* @param {string} config.token Token value.
* @param {Secret} config.secret Secret key.
* @param {string} [config.algorithm='SHA1'] HMAC hashing algorithm.
* @param {number} [config.digits=6] Token length.
* @param {number} [config.counter=0] Counter value.
* @param {number} [config.window=1] Window of counter values to test.
* @returns {number|null} Token delta or null if it is not found in the search window, in which case it should be considered invalid.
*/
static validate({
token,
secret,
algorithm,
digits = HOTP.defaults.digits,
counter = HOTP.defaults.counter,
window = HOTP.defaults.window,
}) {
// Return early if the token length does not match the digit number.
if (token.length !== digits) return null;
let delta = null;
const check = (/** @type {number} */ i) => {
const generatedToken = HOTP.generate({
secret,
algorithm,
digits,
counter: i,
});
if (timingSafeEqual(token, generatedToken)) {
delta = i - counter;
}
};
check(counter);
for (let i = 1; i <= window && delta === null; ++i) {
check(counter - i);
if (delta !== null) break;
check(counter + i);
if (delta !== null) break;
}
return delta;
}
/**
* Validates an HOTP token.
* @param {Object} config Configuration options.
* @param {string} config.token Token value.
* @param {number} [config.counter=this.counter] Counter value.
* @param {number} [config.window=1] Window of counter values to test.
* @returns {number|null} Token delta or null if it is not found in the search window, in which case it should be considered invalid.
*/
validate({ token, counter = this.counter, window }) {
return HOTP.validate({
token,
secret: this.secret,
algorithm: this.algorithm,
digits: this.digits,
counter,
window,
});
}
/**
* Returns a Google Authenticator key URI.
* @returns {string} URI.
*/
toString() {
const e = encodeURIComponent;
return (
"otpauth://hotp/" +
`${
this.issuer.length > 0
? this.issuerInLabel
? `${e(this.issuer)}:${e(this.label)}?issuer=${e(this.issuer)}&`
: `${e(this.label)}?issuer=${e(this.issuer)}&`
: `${e(this.label)}?`
}` +
`secret=${e(this.secret.base32)}&` +
`algorithm=${e(this.algorithm)}&` +
`digits=${e(this.digits)}&` +
`counter=${e(this.counter)}`
);
}
}
export { HOTP };