@dev-blinq/bvt-playwright-js 1.0.0-dev.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,685 @@
1
+ import * as crypto$1 from "node:crypto";
2
+
3
+ //#region ../../node_modules/otpauth/dist/otpauth.node.mjs
4
+ //! otpauth 9.5.0 | (c) Héctor Molinero Fernández | MIT | https://github.com/hectorm/otpauth
5
+ /**
6
+ * Converts an integer to an Uint8Array.
7
+ * @param {number} num Integer.
8
+ * @returns {Uint8Array} Uint8Array.
9
+ */ const uintDecode = (num) => {
10
+ const buf = /* @__PURE__ */ new ArrayBuffer(8);
11
+ const arr = new Uint8Array(buf);
12
+ let acc = num;
13
+ for (let i = 7; i >= 0; i--) {
14
+ if (acc === 0) break;
15
+ arr[i] = acc & 255;
16
+ acc -= arr[i];
17
+ acc /= 256;
18
+ }
19
+ return arr;
20
+ };
21
+ /**
22
+ * "globalThis" ponyfill.
23
+ * @see [A horrifying globalThis polyfill in universal JavaScript](https://mathiasbynens.be/notes/globalthis)
24
+ * @type {Object.<string, *>}
25
+ */ const globalScope = (() => {
26
+ if (typeof globalThis === "object") return globalThis;
27
+ else {
28
+ Object.defineProperty(Object.prototype, "__GLOBALTHIS__", {
29
+ get() {
30
+ return this;
31
+ },
32
+ configurable: true
33
+ });
34
+ try {
35
+ if (typeof __GLOBALTHIS__ !== "undefined") return __GLOBALTHIS__;
36
+ } finally {
37
+ delete Object.prototype.__GLOBALTHIS__;
38
+ }
39
+ }
40
+ if (typeof self !== "undefined") return self;
41
+ else if (typeof window !== "undefined") return window;
42
+ else if (typeof global !== "undefined") return global;
43
+ })();
44
+ /**
45
+ * Canonicalizes a hash algorithm name.
46
+ * @param {string} algorithm Hash algorithm name.
47
+ * @returns {"SHA1"|"SHA224"|"SHA256"|"SHA384"|"SHA512"|"SHA3-224"|"SHA3-256"|"SHA3-384"|"SHA3-512"} Canonicalized hash algorithm name.
48
+ */ const canonicalizeAlgorithm = (algorithm) => {
49
+ switch (true) {
50
+ case /^(?:SHA-?1|SSL3-SHA1)$/i.test(algorithm): return "SHA1";
51
+ case /^SHA(?:2?-)?224$/i.test(algorithm): return "SHA224";
52
+ case /^SHA(?:2?-)?256$/i.test(algorithm): return "SHA256";
53
+ case /^SHA(?:2?-)?384$/i.test(algorithm): return "SHA384";
54
+ case /^SHA(?:2?-)?512$/i.test(algorithm): return "SHA512";
55
+ case /^SHA3-224$/i.test(algorithm): return "SHA3-224";
56
+ case /^SHA3-256$/i.test(algorithm): return "SHA3-256";
57
+ case /^SHA3-384$/i.test(algorithm): return "SHA3-384";
58
+ case /^SHA3-512$/i.test(algorithm): return "SHA3-512";
59
+ default: throw new TypeError(`Unknown hash algorithm: ${algorithm}`);
60
+ }
61
+ };
62
+ /**
63
+ * Calculates an HMAC digest.
64
+ * @param {string} algorithm Algorithm.
65
+ * @param {Uint8Array} key Key.
66
+ * @param {Uint8Array} message Message.
67
+ * @returns {Uint8Array} Digest.
68
+ */ const hmacDigest = (algorithm, key, message) => {
69
+ if (crypto$1?.createHmac) {
70
+ const hmac = crypto$1.createHmac(algorithm, globalScope.Buffer.from(key));
71
+ hmac.update(globalScope.Buffer.from(message));
72
+ return hmac.digest();
73
+ } else throw new Error("Missing HMAC function");
74
+ };
75
+ /**
76
+ * RFC 4648 base32 alphabet without pad.
77
+ * @type {string}
78
+ */ const ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
79
+ /**
80
+ * Converts a base32 string to an Uint8Array (RFC 4648).
81
+ * @see [LinusU/base32-decode](https://github.com/LinusU/base32-decode)
82
+ * @param {string} str Base32 string.
83
+ * @returns {Uint8Array} Uint8Array.
84
+ */ const base32Decode = (str) => {
85
+ str = str.replace(/ /g, "");
86
+ let end = str.length;
87
+ while (str[end - 1] === "=") --end;
88
+ str = (end < str.length ? str.substring(0, end) : str).toUpperCase();
89
+ const buf = /* @__PURE__ */ new ArrayBuffer(str.length * 5 / 8 | 0);
90
+ const arr = new Uint8Array(buf);
91
+ let bits = 0;
92
+ let value = 0;
93
+ let index = 0;
94
+ for (let i = 0; i < str.length; i++) {
95
+ const idx = ALPHABET.indexOf(str[i]);
96
+ if (idx === -1) throw new TypeError(`Invalid character found: ${str[i]}`);
97
+ value = value << 5 | idx;
98
+ bits += 5;
99
+ if (bits >= 8) {
100
+ bits -= 8;
101
+ arr[index++] = value >>> bits;
102
+ }
103
+ }
104
+ return arr;
105
+ };
106
+ /**
107
+ * Converts an Uint8Array to a base32 string (RFC 4648).
108
+ * @see [LinusU/base32-encode](https://github.com/LinusU/base32-encode)
109
+ * @param {Uint8Array} arr Uint8Array.
110
+ * @returns {string} Base32 string.
111
+ */ const base32Encode = (arr) => {
112
+ let bits = 0;
113
+ let value = 0;
114
+ let str = "";
115
+ for (let i = 0; i < arr.length; i++) {
116
+ value = value << 8 | arr[i];
117
+ bits += 8;
118
+ while (bits >= 5) {
119
+ str += ALPHABET[value >>> bits - 5 & 31];
120
+ bits -= 5;
121
+ }
122
+ }
123
+ if (bits > 0) str += ALPHABET[value << 5 - bits & 31];
124
+ return str;
125
+ };
126
+ /**
127
+ * Converts a hexadecimal string to an Uint8Array.
128
+ * @param {string} str Hexadecimal string.
129
+ * @returns {Uint8Array} Uint8Array.
130
+ */ const hexDecode = (str) => {
131
+ str = str.replace(/ /g, "");
132
+ const buf = /* @__PURE__ */ new ArrayBuffer(str.length / 2);
133
+ const arr = new Uint8Array(buf);
134
+ for (let i = 0; i < str.length; i += 2) arr[i / 2] = parseInt(str.substring(i, i + 2), 16);
135
+ return arr;
136
+ };
137
+ /**
138
+ * Converts an Uint8Array to a hexadecimal string.
139
+ * @param {Uint8Array} arr Uint8Array.
140
+ * @returns {string} Hexadecimal string.
141
+ */ const hexEncode = (arr) => {
142
+ let str = "";
143
+ for (let i = 0; i < arr.length; i++) {
144
+ const hex = arr[i].toString(16);
145
+ if (hex.length === 1) str += "0";
146
+ str += hex;
147
+ }
148
+ return str.toUpperCase();
149
+ };
150
+ /**
151
+ * Converts a Latin-1 string to an Uint8Array.
152
+ * @param {string} str Latin-1 string.
153
+ * @returns {Uint8Array} Uint8Array.
154
+ */ const latin1Decode = (str) => {
155
+ const buf = new ArrayBuffer(str.length);
156
+ const arr = new Uint8Array(buf);
157
+ for (let i = 0; i < str.length; i++) arr[i] = str.charCodeAt(i) & 255;
158
+ return arr;
159
+ };
160
+ /**
161
+ * Converts an Uint8Array to a Latin-1 string.
162
+ * @param {Uint8Array} arr Uint8Array.
163
+ * @returns {string} Latin-1 string.
164
+ */ const latin1Encode = (arr) => {
165
+ let str = "";
166
+ for (let i = 0; i < arr.length; i++) str += String.fromCharCode(arr[i]);
167
+ return str;
168
+ };
169
+ /**
170
+ * TextEncoder instance.
171
+ * @type {TextEncoder|null}
172
+ */ const ENCODER = globalScope.TextEncoder ? new globalScope.TextEncoder() : null;
173
+ /**
174
+ * TextDecoder instance.
175
+ * @type {TextDecoder|null}
176
+ */ const DECODER = globalScope.TextDecoder ? new globalScope.TextDecoder() : null;
177
+ /**
178
+ * Converts an UTF-8 string to an Uint8Array.
179
+ * @param {string} str String.
180
+ * @returns {Uint8Array} Uint8Array.
181
+ */ const utf8Decode = (str) => {
182
+ if (!ENCODER) throw new Error("Encoding API not available");
183
+ return ENCODER.encode(str);
184
+ };
185
+ /**
186
+ * Converts an Uint8Array to an UTF-8 string.
187
+ * @param {Uint8Array} arr Uint8Array.
188
+ * @returns {string} String.
189
+ */ const utf8Encode = (arr) => {
190
+ if (!DECODER) throw new Error("Encoding API not available");
191
+ return DECODER.decode(arr);
192
+ };
193
+ /**
194
+ * Returns random bytes.
195
+ * @param {number} size Size.
196
+ * @returns {Uint8Array} Random bytes.
197
+ */ const randomBytes = (size) => {
198
+ if (crypto$1?.randomBytes) return crypto$1.randomBytes(size);
199
+ else if (globalScope.crypto?.getRandomValues) return globalScope.crypto.getRandomValues(new Uint8Array(size));
200
+ else throw new Error("Cryptography API not available");
201
+ };
202
+ /**
203
+ * OTP secret key.
204
+ */ var Secret = class Secret {
205
+ /**
206
+ * Converts a Latin-1 string to a Secret object.
207
+ * @param {string} str Latin-1 string.
208
+ * @returns {Secret} Secret object.
209
+ */ static fromLatin1(str) {
210
+ return new Secret({ buffer: latin1Decode(str).buffer });
211
+ }
212
+ /**
213
+ * Converts an UTF-8 string to a Secret object.
214
+ * @param {string} str UTF-8 string.
215
+ * @returns {Secret} Secret object.
216
+ */ static fromUTF8(str) {
217
+ return new Secret({ buffer: utf8Decode(str).buffer });
218
+ }
219
+ /**
220
+ * Converts a base32 string to a Secret object.
221
+ * @param {string} str Base32 string.
222
+ * @returns {Secret} Secret object.
223
+ */ static fromBase32(str) {
224
+ return new Secret({ buffer: base32Decode(str).buffer });
225
+ }
226
+ /**
227
+ * Converts a hexadecimal string to a Secret object.
228
+ * @param {string} str Hexadecimal string.
229
+ * @returns {Secret} Secret object.
230
+ */ static fromHex(str) {
231
+ return new Secret({ buffer: hexDecode(str).buffer });
232
+ }
233
+ /**
234
+ * Secret key buffer.
235
+ * @deprecated For backward compatibility, the "bytes" property should be used instead.
236
+ * @type {ArrayBufferLike}
237
+ */ get buffer() {
238
+ return this.bytes.buffer;
239
+ }
240
+ /**
241
+ * Latin-1 string representation of secret key.
242
+ * @type {string}
243
+ */ get latin1() {
244
+ Object.defineProperty(this, "latin1", {
245
+ enumerable: true,
246
+ writable: false,
247
+ configurable: false,
248
+ value: latin1Encode(this.bytes)
249
+ });
250
+ return this.latin1;
251
+ }
252
+ /**
253
+ * UTF-8 string representation of secret key.
254
+ * @type {string}
255
+ */ get utf8() {
256
+ Object.defineProperty(this, "utf8", {
257
+ enumerable: true,
258
+ writable: false,
259
+ configurable: false,
260
+ value: utf8Encode(this.bytes)
261
+ });
262
+ return this.utf8;
263
+ }
264
+ /**
265
+ * Base32 string representation of secret key.
266
+ * @type {string}
267
+ */ get base32() {
268
+ Object.defineProperty(this, "base32", {
269
+ enumerable: true,
270
+ writable: false,
271
+ configurable: false,
272
+ value: base32Encode(this.bytes)
273
+ });
274
+ return this.base32;
275
+ }
276
+ /**
277
+ * Hexadecimal string representation of secret key.
278
+ * @type {string}
279
+ */ get hex() {
280
+ Object.defineProperty(this, "hex", {
281
+ enumerable: true,
282
+ writable: false,
283
+ configurable: false,
284
+ value: hexEncode(this.bytes)
285
+ });
286
+ return this.hex;
287
+ }
288
+ /**
289
+ * Creates a secret key object.
290
+ * @param {Object} [config] Configuration options.
291
+ * @param {ArrayBufferLike} [config.buffer] Secret key buffer.
292
+ * @param {number} [config.size=20] Number of random bytes to generate, ignored if 'buffer' is provided.
293
+ */ constructor({ buffer, size = 20 } = {}) {
294
+ /**
295
+ * Secret key.
296
+ * @type {Uint8Array}
297
+ * @readonly
298
+ */ this.bytes = typeof buffer === "undefined" ? randomBytes(size) : new Uint8Array(buffer);
299
+ Object.defineProperty(this, "bytes", {
300
+ enumerable: true,
301
+ writable: false,
302
+ configurable: false,
303
+ value: this.bytes
304
+ });
305
+ }
306
+ };
307
+ /**
308
+ * Returns true if a is equal to b, without leaking timing information that would allow an attacker to guess one of the values.
309
+ * @param {string} a String a.
310
+ * @param {string} b String b.
311
+ * @returns {boolean} Equality result.
312
+ */ const timingSafeEqual = (a, b) => {
313
+ if (crypto$1?.timingSafeEqual) return crypto$1.timingSafeEqual(globalScope.Buffer.from(a), globalScope.Buffer.from(b));
314
+ else {
315
+ if (a.length !== b.length) throw new TypeError("Input strings must have the same length");
316
+ let i = -1;
317
+ let out = 0;
318
+ while (++i < a.length) out |= a.charCodeAt(i) ^ b.charCodeAt(i);
319
+ return out === 0;
320
+ }
321
+ };
322
+ /**
323
+ * HOTP: An HMAC-based One-time Password Algorithm.
324
+ * @see [RFC 4226](https://datatracker.ietf.org/doc/html/rfc4226)
325
+ */ var HOTP = class HOTP {
326
+ /**
327
+ * Default configuration.
328
+ * @type {{
329
+ * issuer: string,
330
+ * label: string,
331
+ * issuerInLabel: boolean,
332
+ * algorithm: string,
333
+ * digits: number,
334
+ * counter: number
335
+ * window: number
336
+ * }}
337
+ */ static get defaults() {
338
+ return {
339
+ issuer: "",
340
+ label: "OTPAuth",
341
+ issuerInLabel: true,
342
+ algorithm: "SHA1",
343
+ digits: 6,
344
+ counter: 0,
345
+ window: 1
346
+ };
347
+ }
348
+ /**
349
+ * Generates an HOTP token.
350
+ * @param {Object} config Configuration options.
351
+ * @param {Secret} config.secret Secret key.
352
+ * @param {string} [config.algorithm='SHA1'] HMAC hashing algorithm.
353
+ * @param {number} [config.digits=6] Token length.
354
+ * @param {number} [config.counter=0] Counter value.
355
+ * @param {(algorithm: string, key: Uint8Array, message: Uint8Array) => Uint8Array} [config.hmac] Custom HMAC function.
356
+ * @returns {string} Token.
357
+ */ static generate({ secret, algorithm = HOTP.defaults.algorithm, digits = HOTP.defaults.digits, counter = HOTP.defaults.counter, hmac = hmacDigest }) {
358
+ const message = uintDecode(counter);
359
+ const digest = hmac(algorithm, secret.bytes, message);
360
+ if (!digest?.byteLength || digest.byteLength < 19) throw new TypeError("Return value must be at least 19 bytes");
361
+ const offset = digest[digest.byteLength - 1] & 15;
362
+ return (((digest[offset] & 127) << 24 | (digest[offset + 1] & 255) << 16 | (digest[offset + 2] & 255) << 8 | digest[offset + 3] & 255) % 10 ** digits).toString().padStart(digits, "0");
363
+ }
364
+ /**
365
+ * Generates an HOTP token.
366
+ * @param {Object} [config] Configuration options.
367
+ * @param {number} [config.counter=this.counter++] Counter value.
368
+ * @returns {string} Token.
369
+ */ generate({ counter = this.counter++ } = {}) {
370
+ return HOTP.generate({
371
+ secret: this.secret,
372
+ algorithm: this.algorithm,
373
+ digits: this.digits,
374
+ counter,
375
+ hmac: this.hmac
376
+ });
377
+ }
378
+ /**
379
+ * Validates an HOTP token.
380
+ * @param {Object} config Configuration options.
381
+ * @param {string} config.token Token value.
382
+ * @param {Secret} config.secret Secret key.
383
+ * @param {string} [config.algorithm='SHA1'] HMAC hashing algorithm.
384
+ * @param {number} [config.digits=6] Token length.
385
+ * @param {number} [config.counter=0] Counter value.
386
+ * @param {number} [config.window=1] Window of counter values to test.
387
+ * @param {(algorithm: string, key: Uint8Array, message: Uint8Array) => Uint8Array} [config.hmac] Custom HMAC function.
388
+ * @returns {number|null} Token delta or null if it is not found in the search window, in which case it should be considered invalid.
389
+ */ static validate({ token, secret, algorithm, digits = HOTP.defaults.digits, counter = HOTP.defaults.counter, window = HOTP.defaults.window, hmac = hmacDigest }) {
390
+ if (token.length !== digits) return null;
391
+ let delta = null;
392
+ const check = (i) => {
393
+ if (timingSafeEqual(token, HOTP.generate({
394
+ secret,
395
+ algorithm,
396
+ digits,
397
+ counter: i,
398
+ hmac
399
+ }))) delta = i - counter;
400
+ };
401
+ check(counter);
402
+ for (let i = 1; i <= window && delta === null; ++i) {
403
+ check(counter - i);
404
+ if (delta !== null) break;
405
+ check(counter + i);
406
+ if (delta !== null) break;
407
+ }
408
+ return delta;
409
+ }
410
+ /**
411
+ * Validates an HOTP token.
412
+ * @param {Object} config Configuration options.
413
+ * @param {string} config.token Token value.
414
+ * @param {number} [config.counter=this.counter] Counter value.
415
+ * @param {number} [config.window=1] Window of counter values to test.
416
+ * @returns {number|null} Token delta or null if it is not found in the search window, in which case it should be considered invalid.
417
+ */ validate({ token, counter = this.counter, window }) {
418
+ return HOTP.validate({
419
+ token,
420
+ secret: this.secret,
421
+ algorithm: this.algorithm,
422
+ digits: this.digits,
423
+ counter,
424
+ window,
425
+ hmac: this.hmac
426
+ });
427
+ }
428
+ /**
429
+ * Returns a Google Authenticator key URI.
430
+ * @returns {string} URI.
431
+ */ toString() {
432
+ const e = encodeURIComponent;
433
+ 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)}`;
434
+ }
435
+ /**
436
+ * Creates an HOTP object.
437
+ * @param {Object} [config] Configuration options.
438
+ * @param {string} [config.issuer=''] Account provider.
439
+ * @param {string} [config.label='OTPAuth'] Account label.
440
+ * @param {boolean} [config.issuerInLabel=true] Include issuer prefix in label.
441
+ * @param {Secret|string} [config.secret=Secret] Secret key.
442
+ * @param {string} [config.algorithm='SHA1'] HMAC hashing algorithm.
443
+ * @param {number} [config.digits=6] Token length.
444
+ * @param {number} [config.counter=0] Initial counter value.
445
+ * @param {(algorithm: string, key: Uint8Array, message: Uint8Array) => Uint8Array} [config.hmac] Custom HMAC function.
446
+ */ 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, hmac } = {}) {
447
+ /**
448
+ * Account provider.
449
+ * @type {string}
450
+ */ this.issuer = issuer;
451
+ /**
452
+ * Account label.
453
+ * @type {string}
454
+ */ this.label = label;
455
+ /**
456
+ * Include issuer prefix in label.
457
+ * @type {boolean}
458
+ */ this.issuerInLabel = issuerInLabel;
459
+ /**
460
+ * Secret key.
461
+ * @type {Secret}
462
+ */ this.secret = typeof secret === "string" ? Secret.fromBase32(secret) : secret;
463
+ /**
464
+ * HMAC hashing algorithm.
465
+ * @type {string}
466
+ */ this.algorithm = hmac ? algorithm : canonicalizeAlgorithm(algorithm);
467
+ /**
468
+ * Token length.
469
+ * @type {number}
470
+ */ this.digits = digits;
471
+ /**
472
+ * Initial counter value.
473
+ * @type {number}
474
+ */ this.counter = counter;
475
+ /**
476
+ * Custom HMAC function.
477
+ * @type {((algorithm: string, key: Uint8Array, message: Uint8Array) => Uint8Array)|undefined}
478
+ */ this.hmac = hmac;
479
+ }
480
+ };
481
+ /**
482
+ * TOTP: Time-Based One-Time Password Algorithm.
483
+ * @see [RFC 6238](https://datatracker.ietf.org/doc/html/rfc6238)
484
+ */ var TOTP = class TOTP {
485
+ /**
486
+ * Default configuration.
487
+ * @type {{
488
+ * issuer: string,
489
+ * label: string,
490
+ * issuerInLabel: boolean,
491
+ * algorithm: string,
492
+ * digits: number,
493
+ * period: number
494
+ * window: number
495
+ * }}
496
+ */ static get defaults() {
497
+ return {
498
+ issuer: "",
499
+ label: "OTPAuth",
500
+ issuerInLabel: true,
501
+ algorithm: "SHA1",
502
+ digits: 6,
503
+ period: 30,
504
+ window: 1
505
+ };
506
+ }
507
+ /**
508
+ * Calculates the counter. i.e. the number of periods since timestamp 0.
509
+ * @param {Object} [config] Configuration options.
510
+ * @param {number} [config.period=30] Token time-step duration.
511
+ * @param {number} [config.timestamp=Date.now] Timestamp value in milliseconds.
512
+ * @returns {number} Counter.
513
+ */ static counter({ period = TOTP.defaults.period, timestamp = Date.now() } = {}) {
514
+ return Math.floor(timestamp / 1e3 / period);
515
+ }
516
+ /**
517
+ * Calculates the counter. i.e. the number of periods since timestamp 0.
518
+ * @param {Object} [config] Configuration options.
519
+ * @param {number} [config.timestamp=Date.now] Timestamp value in milliseconds.
520
+ * @returns {number} Counter.
521
+ */ counter({ timestamp = Date.now() } = {}) {
522
+ return TOTP.counter({
523
+ period: this.period,
524
+ timestamp
525
+ });
526
+ }
527
+ /**
528
+ * Calculates the remaining time in milliseconds until the next token is generated.
529
+ * @param {Object} [config] Configuration options.
530
+ * @param {number} [config.period=30] Token time-step duration.
531
+ * @param {number} [config.timestamp=Date.now] Timestamp value in milliseconds.
532
+ * @returns {number} counter.
533
+ */ static remaining({ period = TOTP.defaults.period, timestamp = Date.now() } = {}) {
534
+ return period * 1e3 - timestamp % (period * 1e3);
535
+ }
536
+ /**
537
+ * Calculates the remaining time in milliseconds until the next token is generated.
538
+ * @param {Object} [config] Configuration options.
539
+ * @param {number} [config.timestamp=Date.now] Timestamp value in milliseconds.
540
+ * @returns {number} counter.
541
+ */ remaining({ timestamp = Date.now() } = {}) {
542
+ return TOTP.remaining({
543
+ period: this.period,
544
+ timestamp
545
+ });
546
+ }
547
+ /**
548
+ * Generates a TOTP token.
549
+ * @param {Object} config Configuration options.
550
+ * @param {Secret} config.secret Secret key.
551
+ * @param {string} [config.algorithm='SHA1'] HMAC hashing algorithm.
552
+ * @param {number} [config.digits=6] Token length.
553
+ * @param {number} [config.period=30] Token time-step duration.
554
+ * @param {number} [config.timestamp=Date.now] Timestamp value in milliseconds.
555
+ * @param {(algorithm: string, key: Uint8Array, message: Uint8Array) => Uint8Array} [config.hmac] Custom HMAC function.
556
+ * @returns {string} Token.
557
+ */ static generate({ secret, algorithm, digits, period = TOTP.defaults.period, timestamp = Date.now(), hmac }) {
558
+ return HOTP.generate({
559
+ secret,
560
+ algorithm,
561
+ digits,
562
+ counter: TOTP.counter({
563
+ period,
564
+ timestamp
565
+ }),
566
+ hmac
567
+ });
568
+ }
569
+ /**
570
+ * Generates a TOTP token.
571
+ * @param {Object} [config] Configuration options.
572
+ * @param {number} [config.timestamp=Date.now] Timestamp value in milliseconds.
573
+ * @returns {string} Token.
574
+ */ generate({ timestamp = Date.now() } = {}) {
575
+ return TOTP.generate({
576
+ secret: this.secret,
577
+ algorithm: this.algorithm,
578
+ digits: this.digits,
579
+ period: this.period,
580
+ timestamp,
581
+ hmac: this.hmac
582
+ });
583
+ }
584
+ /**
585
+ * Validates a TOTP token.
586
+ * @param {Object} config Configuration options.
587
+ * @param {string} config.token Token value.
588
+ * @param {Secret} config.secret Secret key.
589
+ * @param {string} [config.algorithm='SHA1'] HMAC hashing algorithm.
590
+ * @param {number} [config.digits=6] Token length.
591
+ * @param {number} [config.period=30] Token time-step duration.
592
+ * @param {number} [config.timestamp=Date.now] Timestamp value in milliseconds.
593
+ * @param {number} [config.window=1] Window of counter values to test.
594
+ * @param {(algorithm: string, key: Uint8Array, message: Uint8Array) => Uint8Array} [config.hmac] Custom HMAC function.
595
+ * @returns {number|null} Token delta or null if it is not found in the search window, in which case it should be considered invalid.
596
+ */ static validate({ token, secret, algorithm, digits, period = TOTP.defaults.period, timestamp = Date.now(), window, hmac }) {
597
+ return HOTP.validate({
598
+ token,
599
+ secret,
600
+ algorithm,
601
+ digits,
602
+ counter: TOTP.counter({
603
+ period,
604
+ timestamp
605
+ }),
606
+ window,
607
+ hmac
608
+ });
609
+ }
610
+ /**
611
+ * Validates a TOTP token.
612
+ * @param {Object} config Configuration options.
613
+ * @param {string} config.token Token value.
614
+ * @param {number} [config.timestamp=Date.now] Timestamp value in milliseconds.
615
+ * @param {number} [config.window=1] Window of counter values to test.
616
+ * @returns {number|null} Token delta or null if it is not found in the search window, in which case it should be considered invalid.
617
+ */ validate({ token, timestamp, window }) {
618
+ return TOTP.validate({
619
+ token,
620
+ secret: this.secret,
621
+ algorithm: this.algorithm,
622
+ digits: this.digits,
623
+ period: this.period,
624
+ timestamp,
625
+ window,
626
+ hmac: this.hmac
627
+ });
628
+ }
629
+ /**
630
+ * Returns a Google Authenticator key URI.
631
+ * @returns {string} URI.
632
+ */ toString() {
633
+ const e = encodeURIComponent;
634
+ return `otpauth://totp/${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)}&period=${e(this.period)}`;
635
+ }
636
+ /**
637
+ * Creates a TOTP object.
638
+ * @param {Object} [config] Configuration options.
639
+ * @param {string} [config.issuer=''] Account provider.
640
+ * @param {string} [config.label='OTPAuth'] Account label.
641
+ * @param {boolean} [config.issuerInLabel=true] Include issuer prefix in label.
642
+ * @param {Secret|string} [config.secret=Secret] Secret key.
643
+ * @param {string} [config.algorithm='SHA1'] HMAC hashing algorithm.
644
+ * @param {number} [config.digits=6] Token length.
645
+ * @param {number} [config.period=30] Token time-step duration.
646
+ * @param {(algorithm: string, key: Uint8Array, message: Uint8Array) => Uint8Array} [config.hmac] Custom HMAC function.
647
+ */ constructor({ issuer = TOTP.defaults.issuer, label = TOTP.defaults.label, issuerInLabel = TOTP.defaults.issuerInLabel, secret = new Secret(), algorithm = TOTP.defaults.algorithm, digits = TOTP.defaults.digits, period = TOTP.defaults.period, hmac } = {}) {
648
+ /**
649
+ * Account provider.
650
+ * @type {string}
651
+ */ this.issuer = issuer;
652
+ /**
653
+ * Account label.
654
+ * @type {string}
655
+ */ this.label = label;
656
+ /**
657
+ * Include issuer prefix in label.
658
+ * @type {boolean}
659
+ */ this.issuerInLabel = issuerInLabel;
660
+ /**
661
+ * Secret key.
662
+ * @type {Secret}
663
+ */ this.secret = typeof secret === "string" ? Secret.fromBase32(secret) : secret;
664
+ /**
665
+ * HMAC hashing algorithm.
666
+ * @type {string}
667
+ */ this.algorithm = hmac ? algorithm : canonicalizeAlgorithm(algorithm);
668
+ /**
669
+ * Token length.
670
+ * @type {number}
671
+ */ this.digits = digits;
672
+ /**
673
+ * Token time-step duration.
674
+ * @type {number}
675
+ */ this.period = period;
676
+ /**
677
+ * Custom HMAC function.
678
+ * @type {((algorithm: string, key: Uint8Array, message: Uint8Array) => Uint8Array)|undefined}
679
+ */ this.hmac = hmac;
680
+ }
681
+ };
682
+
683
+ //#endregion
684
+ export { TOTP };
685
+ //# sourceMappingURL=otpauth.node-k0uQ9qOV.mjs.map