@otplib/v12-adapter 13.0.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.
@@ -0,0 +1,427 @@
1
+ import { HashAlgorithm, CryptoPlugin, Base32Plugin } from '@otplib/core';
2
+ export { NobleCryptoPlugin } from '@otplib/plugin-crypto-noble';
3
+ export { ScureBase32Plugin } from '@otplib/plugin-base32-scure';
4
+
5
+ /**
6
+ * @otplib/v12-adapter
7
+ *
8
+ * v12-compatible type definitions for migration adapter.
9
+ * These types mirror the v12 API to provide drop-in compatibility.
10
+ */
11
+
12
+ /**
13
+ * v12-style hash algorithms constant
14
+ */
15
+ declare const HashAlgorithms: {
16
+ readonly SHA1: "sha1";
17
+ readonly SHA256: "sha256";
18
+ readonly SHA512: "sha512";
19
+ };
20
+ type HashAlgorithms = (typeof HashAlgorithms)[keyof typeof HashAlgorithms];
21
+ /**
22
+ * v12-style key encodings constant
23
+ */
24
+ declare const KeyEncodings: {
25
+ readonly ASCII: "ascii";
26
+ readonly HEX: "hex";
27
+ readonly BASE32: "base32";
28
+ readonly BASE64: "base64";
29
+ readonly LATIN1: "latin1";
30
+ readonly UTF8: "utf8";
31
+ };
32
+ type KeyEncodings = (typeof KeyEncodings)[keyof typeof KeyEncodings];
33
+ /**
34
+ * v12-style secret key type (string-based)
35
+ */
36
+ type SecretKey = string;
37
+ /**
38
+ * Base32 encoded secret key
39
+ */
40
+ type Base32SecretKey = string;
41
+ /**
42
+ * v12-style createDigest function signature
43
+ */
44
+ type CreateDigest<T = string> = (algorithm: HashAlgorithm, hmacKey: T, counter: T) => T;
45
+ /**
46
+ * v12-style createHmacKey function signature
47
+ */
48
+ type CreateHmacKey<T = string> = (algorithm: HashAlgorithm, secret: SecretKey, encoding: KeyEncodings) => T;
49
+ /**
50
+ * v12-style createRandomBytes function signature
51
+ */
52
+ type CreateRandomBytes<T = string> = (size: number, encoding: KeyEncodings) => T;
53
+ /**
54
+ * v12-style keyEncoder function signature
55
+ */
56
+ type KeyEncoder<T = Base32SecretKey> = (secret: SecretKey, encoding: KeyEncodings) => T;
57
+ /**
58
+ * v12-style keyDecoder function signature
59
+ */
60
+ type KeyDecoder<T = SecretKey> = (encodedSecret: Base32SecretKey, encoding: KeyEncodings) => T;
61
+ /**
62
+ * v12-compatible HOTP options
63
+ */
64
+ type HOTPOptions<T = string> = {
65
+ /** Algorithm for HMAC (default: sha1) */
66
+ algorithm?: HashAlgorithm;
67
+ /** Creates the digest for token generation */
68
+ createDigest?: CreateDigest<T>;
69
+ /** Formats the secret into HMAC key */
70
+ createHmacKey?: CreateHmacKey<T>;
71
+ /** Number of digits in token (default: 6) */
72
+ digits?: number;
73
+ /** Secret encoding (default: ascii) */
74
+ encoding?: KeyEncodings;
75
+ /** Pre-computed digest (use with caution) */
76
+ digest?: string;
77
+ /** v13 crypto plugin (internal use) */
78
+ crypto?: CryptoPlugin;
79
+ /** v13 base32 plugin (internal use) */
80
+ base32?: Base32Plugin;
81
+ };
82
+ /**
83
+ * v12-compatible TOTP options
84
+ */
85
+ type TOTPOptions<T = string> = HOTPOptions<T> & {
86
+ /** Starting epoch in milliseconds (default: Date.now()) */
87
+ epoch?: number;
88
+ /** Time step in seconds (default: 30) */
89
+ step?: number;
90
+ /** Verification window - number of steps or [past, future] */
91
+ window?: number | [number, number];
92
+ };
93
+ /**
94
+ * v12-compatible Authenticator options
95
+ */
96
+ type AuthenticatorOptions<T = string> = TOTPOptions<T> & {
97
+ /** Encodes secret to Base32 */
98
+ keyEncoder?: KeyEncoder<T>;
99
+ /** Decodes Base32 secret */
100
+ keyDecoder?: KeyDecoder<T>;
101
+ /** Creates random bytes for secret generation */
102
+ createRandomBytes?: CreateRandomBytes<T>;
103
+ };
104
+ /**
105
+ * Resolved HOTP options with guaranteed defaults
106
+ */
107
+ type ResolvedHOTPOptions = {
108
+ algorithm: HashAlgorithm;
109
+ digits: number;
110
+ encoding: KeyEncodings;
111
+ crypto: CryptoPlugin;
112
+ base32: Base32Plugin;
113
+ };
114
+ /**
115
+ * Resolved TOTP options with guaranteed defaults
116
+ */
117
+ type ResolvedTOTPOptions = ResolvedHOTPOptions & {
118
+ epoch: number;
119
+ step: number;
120
+ window: number | [number, number];
121
+ };
122
+ /**
123
+ * Resolved Authenticator options with guaranteed defaults
124
+ */
125
+ type ResolvedAuthenticatorOptions = ResolvedTOTPOptions & {
126
+ keyEncoder?: KeyEncoder;
127
+ keyDecoder?: KeyDecoder;
128
+ };
129
+
130
+ /**
131
+ * @otplib/v12-adapter
132
+ *
133
+ * v12-compatible HOTP class implementation.
134
+ * Provides synchronous API wrapper around v13's HOTP implementation.
135
+ */
136
+
137
+ /**
138
+ * Converts a digest to a token of a specified length.
139
+ * Uses dynamicTruncate and truncateDigits from core.
140
+ */
141
+ declare function hotpDigestToToken(hexDigest: string, digits: number): string;
142
+ /**
143
+ * v12-compatible HOTP class
144
+ *
145
+ * Provides the same API as otplib v12 HOTP class while using v13's
146
+ * implementation internally.
147
+ *
148
+ * @example
149
+ * ```typescript
150
+ * import { HOTP } from '@otplib/v12-adapter';
151
+ *
152
+ * const hotp = new HOTP();
153
+ * const secret = 'JBSWY3DPEHPK3PXP';
154
+ * const token = hotp.generate(secret, 0);
155
+ * const isValid = hotp.check(token, secret, 0);
156
+ * ```
157
+ */
158
+ declare class HOTP<T extends HOTPOptions = HOTPOptions> {
159
+ /**
160
+ * Stored options that can be modified
161
+ */
162
+ protected _options: Partial<T>;
163
+ /**
164
+ * Default options applied to all operations
165
+ */
166
+ protected _defaultOptions: Partial<T>;
167
+ constructor(defaultOptions?: Partial<T>);
168
+ /**
169
+ * Get current options (merged with defaults)
170
+ */
171
+ get options(): Partial<T>;
172
+ /**
173
+ * Set options (replaces current options)
174
+ */
175
+ set options(value: Partial<T>);
176
+ /**
177
+ * Creates a new instance with the specified default options
178
+ */
179
+ create(defaultOptions?: Partial<T>): HOTP<T>;
180
+ /**
181
+ * Returns class options polyfilled with default values
182
+ */
183
+ allOptions(): Readonly<ResolvedHOTPOptions>;
184
+ /**
185
+ * Reset options to defaults
186
+ */
187
+ resetOptions(): this;
188
+ /**
189
+ * Generate an HOTP token
190
+ */
191
+ generate(secret: SecretKey, counter: number): string;
192
+ /**
193
+ * Check if a token is valid for the given secret and counter
194
+ */
195
+ check(token: string, secret: SecretKey, counter: number): boolean;
196
+ /**
197
+ * Verify a token (object-based API)
198
+ */
199
+ verify(opts: {
200
+ token: string;
201
+ secret: SecretKey;
202
+ counter: number;
203
+ }): boolean;
204
+ /**
205
+ * Generate an otpauth:// URI for HOTP
206
+ */
207
+ keyuri(accountName: string, issuer: string, secret: SecretKey, counter: number): string;
208
+ }
209
+
210
+ /**
211
+ * @otplib/v12-adapter
212
+ *
213
+ * v12-compatible TOTP class implementation.
214
+ * Provides synchronous API wrapper around v13's TOTP implementation.
215
+ */
216
+
217
+ /**
218
+ * v12-compatible TOTP class
219
+ *
220
+ * Provides the same API as otplib v12 TOTP class while using v13's
221
+ * implementation internally.
222
+ *
223
+ * @example
224
+ * ```typescript
225
+ * import { TOTP } from '@otplib/v12-adapter';
226
+ *
227
+ * const totp = new TOTP();
228
+ * const secret = 'JBSWY3DPEHPK3PXP';
229
+ * const token = totp.generate(secret);
230
+ * const isValid = totp.check(token, secret);
231
+ * ```
232
+ */
233
+ declare class TOTP<T extends TOTPOptions = TOTPOptions> extends HOTP<T> {
234
+ constructor(defaultOptions?: Partial<T>);
235
+ /**
236
+ * Creates a new TOTP instance with the specified default options
237
+ */
238
+ create(defaultOptions?: Partial<T>): TOTP<T>;
239
+ /**
240
+ * Returns class options polyfilled with TOTP default values
241
+ */
242
+ allOptions(): Readonly<ResolvedTOTPOptions>;
243
+ /**
244
+ * Generate a TOTP token
245
+ *
246
+ * @param secret - The secret key
247
+ * @returns The OTP token
248
+ */
249
+ generate(secret: SecretKey): string;
250
+ /**
251
+ * Check if a token is valid for the given secret
252
+ *
253
+ * @param token - The token to verify
254
+ * @param secret - The secret key
255
+ * @returns true if valid
256
+ */
257
+ check(token: string, secret: SecretKey): boolean;
258
+ /**
259
+ * Check token and return the time window delta
260
+ *
261
+ * @param token - The token to verify
262
+ * @param secret - The secret key
263
+ * @returns Window delta (0 = current, positive = future, negative = past), null if invalid
264
+ */
265
+ checkDelta(token: string, secret: SecretKey): number | null;
266
+ /**
267
+ * Verify a token (object-based API)
268
+ *
269
+ * @param opts - Verification options
270
+ * @returns true if valid
271
+ */
272
+ verify(opts: {
273
+ token: string;
274
+ secret: SecretKey;
275
+ }): boolean;
276
+ /**
277
+ * Generate an otpauth:// URI for TOTP
278
+ *
279
+ * @param accountName - Account name for the URI
280
+ * @param issuer - Issuer name
281
+ * @param secret - The secret key (should be Base32 for QR codes)
282
+ * @returns The otpauth:// URI
283
+ */
284
+ keyuri(accountName: string, issuer: string, secret: SecretKey): string;
285
+ /**
286
+ * Get time used in current step (seconds elapsed in current window)
287
+ *
288
+ * @returns Seconds used in current step
289
+ */
290
+ timeUsed(): number;
291
+ /**
292
+ * Get time remaining until next token
293
+ *
294
+ * @returns Seconds remaining in current step
295
+ */
296
+ timeRemaining(): number;
297
+ }
298
+
299
+ /**
300
+ * @otplib/v12-adapter
301
+ *
302
+ * v12-compatible Authenticator class implementation.
303
+ * Provides same API as v12 with Base32 encoding support.
304
+ */
305
+
306
+ /**
307
+ * v12-compatible Authenticator class
308
+ *
309
+ * The Authenticator class is a TOTP variant that uses Base32-encoded secrets
310
+ * by default, making it compatible with Google Authenticator and similar apps.
311
+ *
312
+ * @example
313
+ * ```typescript
314
+ * import { Authenticator } from '@otplib/v12-adapter';
315
+ *
316
+ * const authenticator = new Authenticator();
317
+ * const secret = authenticator.generateSecret();
318
+ * const token = authenticator.generate(secret);
319
+ * const isValid = authenticator.check(token, secret);
320
+ * const uri = authenticator.keyuri('user@example.com', 'MyApp', secret);
321
+ * ```
322
+ */
323
+ declare class Authenticator<T extends AuthenticatorOptions = AuthenticatorOptions> extends TOTP<T> {
324
+ constructor(defaultOptions?: Partial<T>);
325
+ /**
326
+ * Creates a new Authenticator instance with the specified default options
327
+ */
328
+ create(defaultOptions?: Partial<T>): Authenticator<T>;
329
+ /**
330
+ * Returns class options polyfilled with Authenticator default values
331
+ */
332
+ allOptions(): Readonly<ResolvedAuthenticatorOptions>;
333
+ /**
334
+ * Generate an OTP token from a Base32 secret
335
+ *
336
+ * @param secret - Base32-encoded secret
337
+ * @returns The OTP token
338
+ */
339
+ generate(secret: Base32SecretKey): string;
340
+ /**
341
+ * Check if a token is valid for the given Base32 secret
342
+ *
343
+ * @param token - The token to verify
344
+ * @param secret - Base32-encoded secret
345
+ * @returns true if valid
346
+ */
347
+ check(token: string, secret: Base32SecretKey): boolean;
348
+ /**
349
+ * Check token and return the time window delta
350
+ *
351
+ * @param token - The token to verify
352
+ * @param secret - Base32-encoded secret
353
+ * @returns Window delta (0 = current, positive = future, negative = past), null if invalid
354
+ */
355
+ checkDelta(token: string, secret: Base32SecretKey): number | null;
356
+ /**
357
+ * Verify a token (object-based API)
358
+ *
359
+ * @param opts - Verification options
360
+ * @returns true if valid
361
+ */
362
+ verify(opts: {
363
+ token: string;
364
+ secret: Base32SecretKey;
365
+ }): boolean;
366
+ /**
367
+ * Encode a raw secret to Base32
368
+ *
369
+ * @param secret - Raw secret string
370
+ * @returns Base32-encoded secret
371
+ */
372
+ encode(secret: SecretKey): Base32SecretKey;
373
+ /**
374
+ * Decode a Base32 secret to raw string
375
+ *
376
+ * @param secret - Base32-encoded secret
377
+ * @returns Raw secret string
378
+ */
379
+ decode(secret: Base32SecretKey): SecretKey;
380
+ /**
381
+ * Generate a random Base32-encoded secret
382
+ *
383
+ * @param numberOfBytes - Number of bytes for the secret (default: 20)
384
+ * @returns Base32-encoded secret
385
+ */
386
+ generateSecret(numberOfBytes?: number): Base32SecretKey;
387
+ }
388
+
389
+ /**
390
+ * Pre-configured HOTP instance
391
+ *
392
+ * @example
393
+ * ```typescript
394
+ * import { hotp } from '@otplib/v12-adapter';
395
+ *
396
+ * const token = hotp.generate('JBSWY3DPEHPK3PXP', 0);
397
+ * const isValid = hotp.check(token, 'JBSWY3DPEHPK3PXP', 0);
398
+ * ```
399
+ */
400
+ declare const hotp: HOTP<HOTPOptions>;
401
+ /**
402
+ * Pre-configured TOTP instance
403
+ *
404
+ * @example
405
+ * ```typescript
406
+ * import { totp } from '@otplib/v12-adapter';
407
+ *
408
+ * const token = totp.generate('JBSWY3DPEHPK3PXP');
409
+ * const isValid = totp.check(token, 'JBSWY3DPEHPK3PXP');
410
+ * ```
411
+ */
412
+ declare const totp: TOTP<TOTPOptions>;
413
+ /**
414
+ * Pre-configured Authenticator instance
415
+ *
416
+ * @example
417
+ * ```typescript
418
+ * import { authenticator } from '@otplib/v12-adapter';
419
+ *
420
+ * const secret = authenticator.generateSecret();
421
+ * const token = authenticator.generate(secret);
422
+ * const isValid = authenticator.check(token, secret);
423
+ * ```
424
+ */
425
+ declare const authenticator: Authenticator<AuthenticatorOptions>;
426
+
427
+ export { Authenticator, type AuthenticatorOptions, type Base32SecretKey, type CreateDigest, type CreateHmacKey, type CreateRandomBytes, HOTP, type HOTPOptions, HashAlgorithms, type KeyDecoder, type KeyEncoder, KeyEncodings, type SecretKey, TOTP, type TOTPOptions, authenticator, hotp, hotpDigestToToken, totp };