@otplib/v12-adapter 13.2.0 → 13.3.0
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.
- package/README.md +1 -1
- package/dist/index.cjs +1 -551
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +1 -518
- package/dist/index.js.map +1 -1
- package/dist/metafile-cjs.json +1 -1
- package/dist/metafile-esm.json +1 -1
- package/package.json +9 -9
package/README.md
CHANGED
package/dist/index.cjs
CHANGED
|
@@ -1,552 +1,2 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// src/index.ts
|
|
21
|
-
var index_exports = {};
|
|
22
|
-
__export(index_exports, {
|
|
23
|
-
Authenticator: () => Authenticator,
|
|
24
|
-
HOTP: () => HOTP,
|
|
25
|
-
HashAlgorithms: () => HashAlgorithms,
|
|
26
|
-
KeyEncodings: () => KeyEncodings,
|
|
27
|
-
NobleCryptoPlugin: () => import_plugin_crypto_noble4.NobleCryptoPlugin,
|
|
28
|
-
ScureBase32Plugin: () => import_plugin_base32_scure4.ScureBase32Plugin,
|
|
29
|
-
TOTP: () => TOTP,
|
|
30
|
-
authenticator: () => authenticator,
|
|
31
|
-
hotp: () => hotp,
|
|
32
|
-
hotpDigestToToken: () => hotpDigestToToken,
|
|
33
|
-
totp: () => totp
|
|
34
|
-
});
|
|
35
|
-
module.exports = __toCommonJS(index_exports);
|
|
36
|
-
|
|
37
|
-
// src/hotp.ts
|
|
38
|
-
var import_core = require("@otplib/core");
|
|
39
|
-
var import_hotp = require("@otplib/hotp");
|
|
40
|
-
var import_plugin_base32_scure = require("@otplib/plugin-base32-scure");
|
|
41
|
-
var import_plugin_crypto_noble = require("@otplib/plugin-crypto-noble");
|
|
42
|
-
var import_uri = require("@otplib/uri");
|
|
43
|
-
var import_base = require("@scure/base");
|
|
44
|
-
|
|
45
|
-
// src/types.ts
|
|
46
|
-
var HashAlgorithms = {
|
|
47
|
-
SHA1: "sha1",
|
|
48
|
-
SHA256: "sha256",
|
|
49
|
-
SHA512: "sha512"
|
|
50
|
-
};
|
|
51
|
-
var KeyEncodings = {
|
|
52
|
-
ASCII: "ascii",
|
|
53
|
-
HEX: "hex",
|
|
54
|
-
BASE32: "base32",
|
|
55
|
-
BASE64: "base64",
|
|
56
|
-
LATIN1: "latin1",
|
|
57
|
-
UTF8: "utf8"
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
// src/hotp.ts
|
|
61
|
-
var defaultCrypto = new import_plugin_crypto_noble.NobleCryptoPlugin();
|
|
62
|
-
var defaultBase32 = new import_plugin_base32_scure.ScureBase32Plugin();
|
|
63
|
-
function secretToBytes(secret, encoding) {
|
|
64
|
-
if (encoding === KeyEncodings.BASE32 || encoding === "base32") {
|
|
65
|
-
return defaultBase32.decode(secret);
|
|
66
|
-
}
|
|
67
|
-
if (encoding === KeyEncodings.HEX || encoding === "hex") {
|
|
68
|
-
return import_base.hex.decode(secret.replace(/\s/g, ""));
|
|
69
|
-
}
|
|
70
|
-
return (0, import_core.stringToBytes)(secret);
|
|
71
|
-
}
|
|
72
|
-
function hotpDigestToToken(hexDigest, digits) {
|
|
73
|
-
const digestBytes = import_base.hex.decode(hexDigest);
|
|
74
|
-
const truncated = (0, import_core.dynamicTruncate)(digestBytes);
|
|
75
|
-
return (0, import_core.truncateDigits)(truncated, digits);
|
|
76
|
-
}
|
|
77
|
-
var HOTP = class _HOTP {
|
|
78
|
-
/**
|
|
79
|
-
* Stored options that can be modified
|
|
80
|
-
*/
|
|
81
|
-
_options = {};
|
|
82
|
-
/**
|
|
83
|
-
* Default options applied to all operations
|
|
84
|
-
*/
|
|
85
|
-
_defaultOptions = {};
|
|
86
|
-
constructor(defaultOptions = {}) {
|
|
87
|
-
this._defaultOptions = {
|
|
88
|
-
...defaultOptions,
|
|
89
|
-
guardrails: (0, import_core.createGuardrails)(defaultOptions.guardrails)
|
|
90
|
-
};
|
|
91
|
-
this._options = {};
|
|
92
|
-
}
|
|
93
|
-
/**
|
|
94
|
-
* Get current options (merged with defaults)
|
|
95
|
-
*/
|
|
96
|
-
get options() {
|
|
97
|
-
return { ...this._defaultOptions, ...this._options };
|
|
98
|
-
}
|
|
99
|
-
/**
|
|
100
|
-
* Set options (replaces current options)
|
|
101
|
-
*/
|
|
102
|
-
set options(value) {
|
|
103
|
-
this._options = { ...value };
|
|
104
|
-
}
|
|
105
|
-
/**
|
|
106
|
-
* Creates a new instance with the specified default options
|
|
107
|
-
*/
|
|
108
|
-
create(defaultOptions = {}) {
|
|
109
|
-
return new _HOTP(defaultOptions);
|
|
110
|
-
}
|
|
111
|
-
/**
|
|
112
|
-
* Returns class options polyfilled with default values
|
|
113
|
-
*/
|
|
114
|
-
allOptions() {
|
|
115
|
-
const merged = {
|
|
116
|
-
algorithm: HashAlgorithms.SHA1,
|
|
117
|
-
digits: 6,
|
|
118
|
-
encoding: KeyEncodings.ASCII,
|
|
119
|
-
crypto: defaultCrypto,
|
|
120
|
-
base32: defaultBase32,
|
|
121
|
-
...this._defaultOptions,
|
|
122
|
-
...this._options
|
|
123
|
-
};
|
|
124
|
-
return Object.freeze(merged);
|
|
125
|
-
}
|
|
126
|
-
/**
|
|
127
|
-
* Reset options to defaults
|
|
128
|
-
*/
|
|
129
|
-
resetOptions() {
|
|
130
|
-
this._options = {};
|
|
131
|
-
return this;
|
|
132
|
-
}
|
|
133
|
-
/**
|
|
134
|
-
* Generate an HOTP token
|
|
135
|
-
*/
|
|
136
|
-
generate(secret, counter) {
|
|
137
|
-
const opts = this.allOptions();
|
|
138
|
-
const secretBytes = secretToBytes(secret, opts.encoding);
|
|
139
|
-
return (0, import_hotp.generateSync)({
|
|
140
|
-
secret: secretBytes,
|
|
141
|
-
counter,
|
|
142
|
-
algorithm: opts.algorithm,
|
|
143
|
-
digits: opts.digits,
|
|
144
|
-
crypto: opts.crypto,
|
|
145
|
-
guardrails: opts.guardrails
|
|
146
|
-
});
|
|
147
|
-
}
|
|
148
|
-
/**
|
|
149
|
-
* Check if a token is valid for the given secret and counter
|
|
150
|
-
*/
|
|
151
|
-
check(token, secret, counter) {
|
|
152
|
-
const opts = this.allOptions();
|
|
153
|
-
const secretBytes = secretToBytes(secret, opts.encoding);
|
|
154
|
-
try {
|
|
155
|
-
const result = (0, import_hotp.verifySync)({
|
|
156
|
-
secret: secretBytes,
|
|
157
|
-
token,
|
|
158
|
-
counter,
|
|
159
|
-
algorithm: opts.algorithm,
|
|
160
|
-
digits: opts.digits,
|
|
161
|
-
counterTolerance: 0,
|
|
162
|
-
crypto: opts.crypto,
|
|
163
|
-
guardrails: opts.guardrails
|
|
164
|
-
});
|
|
165
|
-
return result.valid;
|
|
166
|
-
} catch {
|
|
167
|
-
return false;
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
/**
|
|
171
|
-
* Verify a token (object-based API)
|
|
172
|
-
*/
|
|
173
|
-
verify(opts) {
|
|
174
|
-
if (typeof opts !== "object") {
|
|
175
|
-
throw new Error("Expecting argument 0 of verify to be an object");
|
|
176
|
-
}
|
|
177
|
-
return this.check(opts.token, opts.secret, opts.counter);
|
|
178
|
-
}
|
|
179
|
-
/**
|
|
180
|
-
* Generate an otpauth:// URI for HOTP
|
|
181
|
-
*/
|
|
182
|
-
keyuri(accountName, issuer, secret, counter) {
|
|
183
|
-
const opts = this.allOptions();
|
|
184
|
-
return (0, import_uri.generateHOTP)({
|
|
185
|
-
label: accountName,
|
|
186
|
-
issuer,
|
|
187
|
-
secret,
|
|
188
|
-
algorithm: opts.algorithm,
|
|
189
|
-
digits: opts.digits,
|
|
190
|
-
counter
|
|
191
|
-
});
|
|
192
|
-
}
|
|
193
|
-
};
|
|
194
|
-
|
|
195
|
-
// src/totp.ts
|
|
196
|
-
var import_plugin_base32_scure2 = require("@otplib/plugin-base32-scure");
|
|
197
|
-
var import_plugin_crypto_noble2 = require("@otplib/plugin-crypto-noble");
|
|
198
|
-
var import_totp = require("@otplib/totp");
|
|
199
|
-
var import_uri2 = require("@otplib/uri");
|
|
200
|
-
var defaultCrypto2 = new import_plugin_crypto_noble2.NobleCryptoPlugin();
|
|
201
|
-
var defaultBase322 = new import_plugin_base32_scure2.ScureBase32Plugin();
|
|
202
|
-
function parseWindow(window, step) {
|
|
203
|
-
if (window === void 0 || window === 0) {
|
|
204
|
-
return 0;
|
|
205
|
-
}
|
|
206
|
-
if (typeof window === "number") {
|
|
207
|
-
return window * step;
|
|
208
|
-
}
|
|
209
|
-
return [window[0] * step, window[1] * step];
|
|
210
|
-
}
|
|
211
|
-
var TOTP = class _TOTP extends HOTP {
|
|
212
|
-
constructor(defaultOptions = {}) {
|
|
213
|
-
super(defaultOptions);
|
|
214
|
-
}
|
|
215
|
-
/**
|
|
216
|
-
* Creates a new TOTP instance with the specified default options
|
|
217
|
-
*/
|
|
218
|
-
create(defaultOptions = {}) {
|
|
219
|
-
return new _TOTP(defaultOptions);
|
|
220
|
-
}
|
|
221
|
-
/**
|
|
222
|
-
* Returns class options polyfilled with TOTP default values
|
|
223
|
-
*/
|
|
224
|
-
allOptions() {
|
|
225
|
-
const merged = {
|
|
226
|
-
algorithm: HashAlgorithms.SHA1,
|
|
227
|
-
digits: 6,
|
|
228
|
-
encoding: KeyEncodings.ASCII,
|
|
229
|
-
epoch: Date.now(),
|
|
230
|
-
step: 30,
|
|
231
|
-
window: 0,
|
|
232
|
-
crypto: defaultCrypto2,
|
|
233
|
-
base32: defaultBase322,
|
|
234
|
-
...this._defaultOptions,
|
|
235
|
-
...this._options
|
|
236
|
-
};
|
|
237
|
-
return Object.freeze(merged);
|
|
238
|
-
}
|
|
239
|
-
/**
|
|
240
|
-
* Generate a TOTP token
|
|
241
|
-
*
|
|
242
|
-
* @param secret - The secret key
|
|
243
|
-
* @returns The OTP token
|
|
244
|
-
*/
|
|
245
|
-
generate(secret) {
|
|
246
|
-
const opts = this.allOptions();
|
|
247
|
-
const secretBytes = secretToBytes(secret, opts.encoding);
|
|
248
|
-
const epochSeconds = Math.floor(opts.epoch / 1e3);
|
|
249
|
-
return (0, import_totp.generateSync)({
|
|
250
|
-
secret: secretBytes,
|
|
251
|
-
algorithm: opts.algorithm,
|
|
252
|
-
digits: opts.digits,
|
|
253
|
-
period: opts.step,
|
|
254
|
-
epoch: epochSeconds,
|
|
255
|
-
t0: 0,
|
|
256
|
-
crypto: opts.crypto,
|
|
257
|
-
guardrails: opts.guardrails
|
|
258
|
-
});
|
|
259
|
-
}
|
|
260
|
-
/**
|
|
261
|
-
* Check if a token is valid for the given secret
|
|
262
|
-
*
|
|
263
|
-
* @param token - The token to verify
|
|
264
|
-
* @param secret - The secret key
|
|
265
|
-
* @returns true if valid
|
|
266
|
-
*/
|
|
267
|
-
check(token, secret) {
|
|
268
|
-
const delta = this.checkDelta(token, secret);
|
|
269
|
-
return typeof delta === "number";
|
|
270
|
-
}
|
|
271
|
-
/**
|
|
272
|
-
* Check token and return the time window delta
|
|
273
|
-
*
|
|
274
|
-
* @param token - The token to verify
|
|
275
|
-
* @param secret - The secret key
|
|
276
|
-
* @returns Window delta (0 = current, positive = future, negative = past), null if invalid
|
|
277
|
-
*/
|
|
278
|
-
checkDelta(token, secret) {
|
|
279
|
-
const opts = this.allOptions();
|
|
280
|
-
const secretBytes = secretToBytes(secret, opts.encoding);
|
|
281
|
-
const epochSeconds = Math.floor(opts.epoch / 1e3);
|
|
282
|
-
const step = opts.step;
|
|
283
|
-
const window = opts.window;
|
|
284
|
-
const epochTolerance = parseWindow(window, step);
|
|
285
|
-
try {
|
|
286
|
-
const result = (0, import_totp.verifySync)({
|
|
287
|
-
secret: secretBytes,
|
|
288
|
-
token,
|
|
289
|
-
algorithm: opts.algorithm,
|
|
290
|
-
digits: opts.digits,
|
|
291
|
-
period: step,
|
|
292
|
-
epoch: epochSeconds,
|
|
293
|
-
t0: 0,
|
|
294
|
-
epochTolerance,
|
|
295
|
-
crypto: opts.crypto,
|
|
296
|
-
guardrails: opts.guardrails
|
|
297
|
-
});
|
|
298
|
-
if (!result.valid) {
|
|
299
|
-
return null;
|
|
300
|
-
}
|
|
301
|
-
return result.delta;
|
|
302
|
-
} catch {
|
|
303
|
-
return null;
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
/**
|
|
307
|
-
* Verify a token (object-based API)
|
|
308
|
-
*
|
|
309
|
-
* @param opts - Verification options
|
|
310
|
-
* @returns true if valid
|
|
311
|
-
*/
|
|
312
|
-
verify(opts) {
|
|
313
|
-
if (typeof opts !== "object") {
|
|
314
|
-
throw new Error("Expecting argument 0 of verify to be an object");
|
|
315
|
-
}
|
|
316
|
-
return this.check(opts.token, opts.secret);
|
|
317
|
-
}
|
|
318
|
-
/**
|
|
319
|
-
* Generate an otpauth:// URI for TOTP
|
|
320
|
-
*
|
|
321
|
-
* @param accountName - Account name for the URI
|
|
322
|
-
* @param issuer - Issuer name
|
|
323
|
-
* @param secret - The secret key (should be Base32 for QR codes)
|
|
324
|
-
* @returns The otpauth:// URI
|
|
325
|
-
*/
|
|
326
|
-
keyuri(accountName, issuer, secret) {
|
|
327
|
-
const opts = this.allOptions();
|
|
328
|
-
return (0, import_uri2.generateTOTP)({
|
|
329
|
-
label: accountName,
|
|
330
|
-
issuer,
|
|
331
|
-
secret,
|
|
332
|
-
algorithm: opts.algorithm,
|
|
333
|
-
digits: opts.digits,
|
|
334
|
-
period: opts.step
|
|
335
|
-
});
|
|
336
|
-
}
|
|
337
|
-
/**
|
|
338
|
-
* Get time used in current step (seconds elapsed in current window)
|
|
339
|
-
*
|
|
340
|
-
* @returns Seconds used in current step
|
|
341
|
-
*/
|
|
342
|
-
timeUsed() {
|
|
343
|
-
const opts = this.allOptions();
|
|
344
|
-
const epochSeconds = Math.floor(opts.epoch / 1e3);
|
|
345
|
-
return epochSeconds % opts.step;
|
|
346
|
-
}
|
|
347
|
-
/**
|
|
348
|
-
* Get time remaining until next token
|
|
349
|
-
*
|
|
350
|
-
* @returns Seconds remaining in current step
|
|
351
|
-
*/
|
|
352
|
-
timeRemaining() {
|
|
353
|
-
const opts = this.allOptions();
|
|
354
|
-
const epochSeconds = Math.floor(opts.epoch / 1e3);
|
|
355
|
-
return (0, import_totp.getRemainingTime)(epochSeconds, opts.step, 0);
|
|
356
|
-
}
|
|
357
|
-
};
|
|
358
|
-
|
|
359
|
-
// src/authenticator.ts
|
|
360
|
-
var import_core2 = require("@otplib/core");
|
|
361
|
-
var import_plugin_base32_scure3 = require("@otplib/plugin-base32-scure");
|
|
362
|
-
var import_plugin_crypto_noble3 = require("@otplib/plugin-crypto-noble");
|
|
363
|
-
var import_totp2 = require("@otplib/totp");
|
|
364
|
-
var defaultCrypto3 = new import_plugin_crypto_noble3.NobleCryptoPlugin();
|
|
365
|
-
var defaultBase323 = new import_plugin_base32_scure3.ScureBase32Plugin();
|
|
366
|
-
function defaultKeyEncoder(secret, _encoding) {
|
|
367
|
-
const bytes = new TextEncoder().encode(secret);
|
|
368
|
-
return defaultBase323.encode(bytes);
|
|
369
|
-
}
|
|
370
|
-
function defaultKeyDecoder(encodedSecret, _encoding) {
|
|
371
|
-
const bytes = defaultBase323.decode(encodedSecret);
|
|
372
|
-
return new TextDecoder().decode(bytes);
|
|
373
|
-
}
|
|
374
|
-
var Authenticator = class _Authenticator extends TOTP {
|
|
375
|
-
constructor(defaultOptions = {}) {
|
|
376
|
-
super(defaultOptions);
|
|
377
|
-
}
|
|
378
|
-
/**
|
|
379
|
-
* Creates a new Authenticator instance with the specified default options
|
|
380
|
-
*/
|
|
381
|
-
create(defaultOptions = {}) {
|
|
382
|
-
return new _Authenticator(defaultOptions);
|
|
383
|
-
}
|
|
384
|
-
/**
|
|
385
|
-
* Returns class options polyfilled with Authenticator default values
|
|
386
|
-
*/
|
|
387
|
-
allOptions() {
|
|
388
|
-
const merged = {
|
|
389
|
-
algorithm: HashAlgorithms.SHA1,
|
|
390
|
-
digits: 6,
|
|
391
|
-
encoding: KeyEncodings.HEX,
|
|
392
|
-
epoch: Date.now(),
|
|
393
|
-
step: 30,
|
|
394
|
-
window: 0,
|
|
395
|
-
keyEncoder: defaultKeyEncoder,
|
|
396
|
-
keyDecoder: defaultKeyDecoder,
|
|
397
|
-
crypto: defaultCrypto3,
|
|
398
|
-
base32: defaultBase323,
|
|
399
|
-
...this._defaultOptions,
|
|
400
|
-
...this._options
|
|
401
|
-
};
|
|
402
|
-
return Object.freeze(merged);
|
|
403
|
-
}
|
|
404
|
-
/**
|
|
405
|
-
* Generate an OTP token from a Base32 secret
|
|
406
|
-
*
|
|
407
|
-
* @param secret - Base32-encoded secret
|
|
408
|
-
* @returns The OTP token
|
|
409
|
-
*/
|
|
410
|
-
generate(secret) {
|
|
411
|
-
const opts = this.allOptions();
|
|
412
|
-
const secretBytes = defaultBase323.decode(secret);
|
|
413
|
-
const epoch = opts.epoch;
|
|
414
|
-
const epochSeconds = epoch >= 1e12 ? Math.floor(epoch / 1e3) : epoch;
|
|
415
|
-
return (0, import_totp2.generateSync)({
|
|
416
|
-
secret: secretBytes,
|
|
417
|
-
algorithm: opts.algorithm,
|
|
418
|
-
digits: opts.digits,
|
|
419
|
-
period: opts.step,
|
|
420
|
-
epoch: epochSeconds,
|
|
421
|
-
t0: 0,
|
|
422
|
-
crypto: opts.crypto,
|
|
423
|
-
guardrails: opts.guardrails
|
|
424
|
-
});
|
|
425
|
-
}
|
|
426
|
-
/**
|
|
427
|
-
* Check if a token is valid for the given Base32 secret
|
|
428
|
-
*
|
|
429
|
-
* @param token - The token to verify
|
|
430
|
-
* @param secret - Base32-encoded secret
|
|
431
|
-
* @returns true if valid
|
|
432
|
-
*/
|
|
433
|
-
check(token, secret) {
|
|
434
|
-
const delta = this.checkDelta(token, secret);
|
|
435
|
-
return typeof delta === "number";
|
|
436
|
-
}
|
|
437
|
-
/**
|
|
438
|
-
* Check token and return the time window delta
|
|
439
|
-
*
|
|
440
|
-
* @param token - The token to verify
|
|
441
|
-
* @param secret - Base32-encoded secret
|
|
442
|
-
* @returns Window delta (0 = current, positive = future, negative = past), null if invalid
|
|
443
|
-
*/
|
|
444
|
-
checkDelta(token, secret) {
|
|
445
|
-
const opts = this.allOptions();
|
|
446
|
-
const secretBytes = defaultBase323.decode(secret);
|
|
447
|
-
const epoch = opts.epoch;
|
|
448
|
-
const epochSeconds = epoch >= 1e12 ? Math.floor(epoch / 1e3) : epoch;
|
|
449
|
-
const step = opts.step;
|
|
450
|
-
const window = opts.window;
|
|
451
|
-
let epochTolerance = 0;
|
|
452
|
-
if (typeof window === "number") {
|
|
453
|
-
epochTolerance = window * step;
|
|
454
|
-
} else if (Array.isArray(window)) {
|
|
455
|
-
epochTolerance = [window[0] * step, window[1] * step];
|
|
456
|
-
}
|
|
457
|
-
try {
|
|
458
|
-
const result = (0, import_totp2.verifySync)({
|
|
459
|
-
secret: secretBytes,
|
|
460
|
-
token,
|
|
461
|
-
algorithm: opts.algorithm,
|
|
462
|
-
digits: opts.digits,
|
|
463
|
-
period: step,
|
|
464
|
-
epoch: epochSeconds,
|
|
465
|
-
t0: 0,
|
|
466
|
-
epochTolerance,
|
|
467
|
-
crypto: opts.crypto,
|
|
468
|
-
guardrails: opts.guardrails
|
|
469
|
-
});
|
|
470
|
-
if (!result.valid) {
|
|
471
|
-
return null;
|
|
472
|
-
}
|
|
473
|
-
return result.delta;
|
|
474
|
-
} catch {
|
|
475
|
-
return null;
|
|
476
|
-
}
|
|
477
|
-
}
|
|
478
|
-
/**
|
|
479
|
-
* Verify a token (object-based API)
|
|
480
|
-
*
|
|
481
|
-
* @param opts - Verification options
|
|
482
|
-
* @returns true if valid
|
|
483
|
-
*/
|
|
484
|
-
verify(opts) {
|
|
485
|
-
if (typeof opts !== "object") {
|
|
486
|
-
throw new Error("Expecting argument 0 of verify to be an object");
|
|
487
|
-
}
|
|
488
|
-
return this.check(opts.token, opts.secret);
|
|
489
|
-
}
|
|
490
|
-
/**
|
|
491
|
-
* Encode a raw secret to Base32
|
|
492
|
-
*
|
|
493
|
-
* @param secret - Raw secret string
|
|
494
|
-
* @returns Base32-encoded secret
|
|
495
|
-
*/
|
|
496
|
-
encode(secret) {
|
|
497
|
-
const opts = this.allOptions();
|
|
498
|
-
if (opts.keyEncoder) {
|
|
499
|
-
return opts.keyEncoder(secret, opts.encoding);
|
|
500
|
-
}
|
|
501
|
-
return defaultKeyEncoder(secret, opts.encoding);
|
|
502
|
-
}
|
|
503
|
-
/**
|
|
504
|
-
* Decode a Base32 secret to raw string
|
|
505
|
-
*
|
|
506
|
-
* @param secret - Base32-encoded secret
|
|
507
|
-
* @returns Raw secret string
|
|
508
|
-
*/
|
|
509
|
-
decode(secret) {
|
|
510
|
-
const opts = this.allOptions();
|
|
511
|
-
if (opts.keyDecoder) {
|
|
512
|
-
return opts.keyDecoder(secret, opts.encoding);
|
|
513
|
-
}
|
|
514
|
-
return defaultKeyDecoder(secret, opts.encoding);
|
|
515
|
-
}
|
|
516
|
-
/**
|
|
517
|
-
* Generate a random Base32-encoded secret
|
|
518
|
-
*
|
|
519
|
-
* @param numberOfBytes - Number of bytes for the secret (default: 20)
|
|
520
|
-
* @returns Base32-encoded secret
|
|
521
|
-
*/
|
|
522
|
-
generateSecret(numberOfBytes = 20) {
|
|
523
|
-
const opts = this.allOptions();
|
|
524
|
-
return (0, import_core2.generateSecret)({
|
|
525
|
-
crypto: opts.crypto,
|
|
526
|
-
base32: opts.base32,
|
|
527
|
-
length: numberOfBytes
|
|
528
|
-
});
|
|
529
|
-
}
|
|
530
|
-
};
|
|
531
|
-
|
|
532
|
-
// src/index.ts
|
|
533
|
-
var import_plugin_crypto_noble4 = require("@otplib/plugin-crypto-noble");
|
|
534
|
-
var import_plugin_base32_scure4 = require("@otplib/plugin-base32-scure");
|
|
535
|
-
var hotp = new HOTP();
|
|
536
|
-
var totp = new TOTP();
|
|
537
|
-
var authenticator = new Authenticator();
|
|
538
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
539
|
-
0 && (module.exports = {
|
|
540
|
-
Authenticator,
|
|
541
|
-
HOTP,
|
|
542
|
-
HashAlgorithms,
|
|
543
|
-
KeyEncodings,
|
|
544
|
-
NobleCryptoPlugin,
|
|
545
|
-
ScureBase32Plugin,
|
|
546
|
-
TOTP,
|
|
547
|
-
authenticator,
|
|
548
|
-
hotp,
|
|
549
|
-
hotpDigestToToken,
|
|
550
|
-
totp
|
|
551
|
-
});
|
|
1
|
+
"use strict";var S=Object.defineProperty;var I=Object.getOwnPropertyDescriptor;var G=Object.getOwnPropertyNames;var M=Object.prototype.hasOwnProperty;var U=(o,e)=>{for(var t in e)S(o,t,{get:e[t],enumerable:!0})},z=(o,e,t,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of G(e))!M.call(o,n)&&n!==t&&S(o,n,{get:()=>e[n],enumerable:!(r=I(e,n))||r.enumerable});return o};var V=o=>z(S({},"__esModule",{value:!0}),o);var W={};U(W,{Authenticator:()=>h,HOTP:()=>a,HashAlgorithms:()=>c,KeyEncodings:()=>i,NobleCryptoPlugin:()=>j.NobleCryptoPlugin,ScureBase32Plugin:()=>_.ScureBase32Plugin,TOTP:()=>d,authenticator:()=>L,hotp:()=>N,hotpDigestToToken:()=>A,totp:()=>F});module.exports=V(W);var p=require("@otplib/core"),O=require("@otplib/hotp"),K=require("@otplib/plugin-base32-scure"),E=require("@otplib/plugin-crypto-noble"),H=require("@otplib/uri"),P=require("@scure/base");var c={SHA1:"sha1",SHA256:"sha256",SHA512:"sha512"},i={ASCII:"ascii",HEX:"hex",BASE32:"base32",BASE64:"base64",LATIN1:"latin1",UTF8:"utf8"};function f(o,e){return e===i.BASE32||e==="base32"?K.base32.decode(o):e===i.HEX||e==="hex"?P.hex.decode(o.replace(/\s/g,"")):(0,p.stringToBytes)(o)}function A(o,e){let t=P.hex.decode(o),r=(0,p.dynamicTruncate)(t);return(0,p.truncateDigits)(r,e)}var a=class o{_options={};_defaultOptions={};constructor(e={}){this._defaultOptions={...e,guardrails:(0,p.createGuardrails)(e.guardrails)},this._options={}}get options(){return{...this._defaultOptions,...this._options}}set options(e){this._options={...e}}create(e={}){return new o(e)}allOptions(){let e={algorithm:c.SHA1,digits:6,encoding:i.ASCII,crypto:E.crypto,base32:K.base32,...this._defaultOptions,...this._options};return Object.freeze(e)}resetOptions(){return this._options={},this}generate(e,t){let r=this.allOptions(),n=f(e,r.encoding);return(0,O.generateSync)({secret:n,counter:t,algorithm:r.algorithm,digits:r.digits,crypto:r.crypto,guardrails:r.guardrails})}check(e,t,r){let n=this.allOptions(),s=f(t,n.encoding);try{return(0,O.verifySync)({secret:s,token:e,counter:r,algorithm:n.algorithm,digits:n.digits,counterTolerance:0,crypto:n.crypto,guardrails:n.guardrails}).valid}catch{return!1}}verify(e){if(typeof e!="object")throw new Error("Expecting argument 0 of verify to be an object");return this.check(e.token,e.secret,e.counter)}keyuri(e,t,r,n){let s=this.allOptions();return(0,H.generateHOTP)({label:e,issuer:t,secret:r,algorithm:s.algorithm,digits:s.digits,counter:n})}};var B=require("@otplib/plugin-base32-scure"),D=require("@otplib/plugin-crypto-noble"),u=require("@otplib/totp"),v=require("@otplib/uri");function X(o,e){return o===void 0||o===0?0:typeof o=="number"?o*e:[o[0]*e,o[1]*e]}var d=class o extends a{constructor(e={}){super(e)}create(e={}){return new o(e)}allOptions(){let e={algorithm:c.SHA1,digits:6,encoding:i.ASCII,epoch:Date.now(),step:30,window:0,crypto:D.crypto,base32:B.base32,...this._defaultOptions,...this._options};return Object.freeze(e)}generate(e){let t=this.allOptions(),r=f(e,t.encoding),n=Math.floor(t.epoch/1e3);return(0,u.generateSync)({secret:r,algorithm:t.algorithm,digits:t.digits,period:t.step,epoch:n,t0:0,crypto:t.crypto,guardrails:t.guardrails})}check(e,t){return typeof this.checkDelta(e,t)=="number"}checkDelta(e,t){let r=this.allOptions(),n=f(t,r.encoding),s=Math.floor(r.epoch/1e3),T=r.step,l=r.window,y=X(l,T);try{let g=(0,u.verifySync)({secret:n,token:e,algorithm:r.algorithm,digits:r.digits,period:T,epoch:s,t0:0,epochTolerance:y,crypto:r.crypto,guardrails:r.guardrails});return g.valid?g.delta:null}catch{return null}}verify(e){if(typeof e!="object")throw new Error("Expecting argument 0 of verify to be an object");return this.check(e.token,e.secret)}keyuri(e,t,r){let n=this.allOptions();return(0,v.generateTOTP)({label:e,issuer:t,secret:r,algorithm:n.algorithm,digits:n.digits,period:n.step})}timeUsed(){let e=this.allOptions();return Math.floor(e.epoch/1e3)%e.step}timeRemaining(){let e=this.allOptions(),t=Math.floor(e.epoch/1e3);return(0,u.getRemainingTime)(t,e.step,0)}};var w=require("@otplib/core"),m=require("@otplib/plugin-base32-scure"),C=require("@otplib/plugin-crypto-noble"),b=require("@otplib/totp");function k(o,e){let t=new TextEncoder().encode(o);return m.base32.encode(t)}function R(o,e){let t=m.base32.decode(o);return new TextDecoder().decode(t)}var h=class o extends d{constructor(e={}){super(e)}create(e={}){return new o(e)}allOptions(){let e={algorithm:c.SHA1,digits:6,encoding:i.HEX,epoch:Date.now(),step:30,window:0,keyEncoder:k,keyDecoder:R,crypto:C.crypto,base32:m.base32,...this._defaultOptions,...this._options};return Object.freeze(e)}generate(e){let t=this.allOptions(),r=m.base32.decode(e),n=t.epoch,s=n>=1e12?Math.floor(n/1e3):n;return(0,b.generateSync)({secret:r,algorithm:t.algorithm,digits:t.digits,period:t.step,epoch:s,t0:0,crypto:t.crypto,guardrails:t.guardrails})}check(e,t){return typeof this.checkDelta(e,t)=="number"}checkDelta(e,t){let r=this.allOptions(),n=m.base32.decode(t),s=r.epoch,T=s>=1e12?Math.floor(s/1e3):s,l=r.step,y=r.window,g=0;typeof y=="number"?g=y*l:Array.isArray(y)&&(g=[y[0]*l,y[1]*l]);try{let x=(0,b.verifySync)({secret:n,token:e,algorithm:r.algorithm,digits:r.digits,period:l,epoch:T,t0:0,epochTolerance:g,crypto:r.crypto,guardrails:r.guardrails});return x.valid?x.delta:null}catch{return null}}verify(e){if(typeof e!="object")throw new Error("Expecting argument 0 of verify to be an object");return this.check(e.token,e.secret)}encode(e){let t=this.allOptions();return t.keyEncoder?t.keyEncoder(e,t.encoding):k(e,t.encoding)}decode(e){let t=this.allOptions();return t.keyDecoder?t.keyDecoder(e,t.encoding):R(e,t.encoding)}generateSecret(e=20){let t=this.allOptions();return(0,w.generateSecret)({crypto:t.crypto,base32:t.base32,length:e})}};var j=require("@otplib/plugin-crypto-noble"),_=require("@otplib/plugin-base32-scure"),N=new a,F=new d,L=new h;0&&(module.exports={Authenticator,HOTP,HashAlgorithms,KeyEncodings,NobleCryptoPlugin,ScureBase32Plugin,TOTP,authenticator,hotp,hotpDigestToToken,totp});
|
|
552
2
|
//# sourceMappingURL=index.cjs.map
|