otplib 12.0.1 → 13.0.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 @@
1
+ {"version":3,"sources":["../src/functional.ts","../src/defaults.ts"],"sourcesContent":["import { generateSecret as generateSecretCore, ConfigurationError } from \"@otplib/core\";\nimport {\n generate as generateHOTP,\n generateSync as generateHOTPSync,\n verify as verifyHOTP,\n verifySync as verifyHOTPSync,\n} from \"@otplib/hotp\";\nimport {\n generate as generateTOTP,\n generateSync as generateTOTPSync,\n verify as verifyTOTP,\n verifySync as verifyTOTPSync,\n} from \"@otplib/totp\";\nimport { generateTOTP as generateTOTPURI } from \"@otplib/uri\";\n\nimport {\n defaultCrypto,\n defaultBase32,\n normalizeGenerateOptions,\n normalizeVerifyOptions,\n} from \"./defaults\";\n\nimport type { OTPGenerateOptions, OTPVerifyOptions, OTPStrategy, StrategyHandlers } from \"./types\";\nimport type { CryptoPlugin, Base32Plugin, Digits, HashAlgorithm } from \"@otplib/core\";\nimport type { VerifyResult as HOTPVerifyResult } from \"@otplib/hotp\";\nimport type { VerifyResult as TOTPVerifyResult } from \"@otplib/totp\";\n\nexport type { OTPStrategy };\n\nexport type VerifyResult = TOTPVerifyResult | HOTPVerifyResult;\n\nfunction executeByStrategy<T>(\n strategy: OTPStrategy,\n counter: number | undefined,\n handlers: StrategyHandlers<T>,\n): T {\n if (strategy === \"totp\") {\n return handlers.totp();\n }\n if (strategy === \"hotp\") {\n if (counter === undefined) {\n throw new ConfigurationError(\n \"Counter is required for HOTP strategy. Example: { strategy: 'hotp', counter: 0 }\",\n );\n }\n return handlers.hotp(counter);\n }\n throw new ConfigurationError(\n `Unknown OTP strategy: ${strategy}. Valid strategies are 'totp' or 'hotp'.`,\n );\n}\n\n/**\n * Generate a random secret key for use with OTP\n *\n * The secret is encoded in Base32 format for compatibility with\n * Google Authenticator and other authenticator apps.\n *\n * @param options - Secret generation options\n * @returns Base32-encoded secret key\n *\n * @example\n * ```ts\n * import { generateSecret } from 'otplib';\n *\n * const secret = generateSecret();\n * // Returns: 'JBSWY3DPEHPK3PXP'\n * ```\n *\n * @example With custom plugins\n * ```ts\n * import { generateSecret, NodeCryptoPlugin } from 'otplib';\n *\n * const secret = generateSecret({\n * crypto: new NodeCryptoPlugin(),\n * });\n * ```\n */\nexport function generateSecret(options?: {\n /**\n * Number of random bytes to generate (default: 20)\n * 20 bytes = 160 bits, which provides a good security margin\n */\n length?: number;\n\n /**\n * Crypto plugin to use (default: NobleCryptoPlugin)\n */\n crypto?: CryptoPlugin;\n\n /**\n * Base32 plugin to use (default: ScureBase32Plugin)\n */\n base32?: Base32Plugin;\n}): string {\n const { crypto = defaultCrypto, base32 = defaultBase32, length = 20 } = options || {};\n\n return generateSecretCore({ crypto, base32, length });\n}\n\n/**\n * Generate an otpauth:// URI for QR code generation\n *\n * This URI can be used to generate a QR code that can be scanned\n * by Google Authenticator and other authenticator apps.\n *\n * @param options - URI generation options\n * @returns otpauth:// URI string\n *\n * @example\n * ```ts\n * import { generateURI } from 'otplib';\n *\n * const uri = generateURI({\n * issuer: 'ACME Co',\n * label: 'john@example.com',\n * secret: 'JBSWY3DPEHPK3PXP',\n * });\n * // Returns: 'otpauth://totp/ACME%20Co:john%40example.com?secret=...'\n * ```\n */\nexport function generateURI(options: {\n issuer: string;\n label: string;\n secret: string;\n algorithm?: HashAlgorithm;\n digits?: Digits;\n period?: number;\n}): string {\n const { issuer, label, secret, algorithm = \"sha1\", digits = 6, period = 30 } = options;\n return generateTOTPURI({ issuer, label, secret, algorithm, digits, period });\n}\n\n/**\n * Generate an OTP code\n *\n * Generates a one-time password based on the specified strategy.\n * - 'totp': Time-based OTP (default)\n * - 'hotp': HMAC-based OTP\n *\n * @param options - OTP generation options\n * @returns OTP code\n *\n * @example TOTP\n * ```ts\n * import { generate } from 'otplib';\n *\n * const token = await generate({\n * secret: 'JBSWY3DPEHPK3PXP',\n * });\n * // Returns: '123456'\n * ```\n *\n * @example HOTP\n * ```ts\n * import { generate } from 'otplib';\n *\n * const token = await generate({\n * secret: 'JBSWY3DPEHPK3PXP',\n * strategy: 'hotp',\n * counter: 0,\n * });\n * ```\n *\n * @example With custom plugins\n * ```ts\n * import { generate, NodeCryptoPlugin } from 'otplib';\n *\n * const token = await generate({\n * secret: 'JBSWY3DPEHPK3PXP',\n * crypto: new NodeCryptoPlugin(),\n * });\n * ```\n */\nexport async function generate(options: OTPGenerateOptions): Promise<string> {\n const opts = normalizeGenerateOptions(options);\n const { secret, crypto, base32, algorithm, digits } = opts;\n const commonOptions = { secret, crypto, base32, algorithm, digits };\n\n return executeByStrategy(opts.strategy, opts.counter, {\n totp: () =>\n generateTOTP({\n ...commonOptions,\n period: opts.period,\n epoch: opts.epoch,\n t0: opts.t0,\n }),\n hotp: (counter) =>\n generateHOTP({\n ...commonOptions,\n counter,\n }),\n });\n}\n\n/**\n * Generate an OTP code synchronously\n *\n * This is the synchronous version of {@link generate}. It requires a crypto\n * plugin that supports synchronous HMAC operations.\n *\n * @param options - OTP generation options\n * @returns OTP code\n * @throws {HMACError} If the crypto plugin doesn't support sync operations\n *\n * @example\n * ```ts\n * import { generateSync } from 'otplib';\n *\n * const token = generateSync({\n * secret: 'JBSWY3DPEHPK3PXP',\n * });\n * ```\n */\nexport function generateSync(options: OTPGenerateOptions): string {\n const opts = normalizeGenerateOptions(options);\n const { secret, crypto, base32, algorithm, digits } = opts;\n const commonOptions = { secret, crypto, base32, algorithm, digits };\n\n return executeByStrategy(opts.strategy, opts.counter, {\n totp: () =>\n generateTOTPSync({\n ...commonOptions,\n period: opts.period,\n epoch: opts.epoch,\n t0: opts.t0,\n }),\n hotp: (counter) =>\n generateHOTPSync({\n ...commonOptions,\n counter,\n }),\n });\n}\n\n/**\n * Verify an OTP code\n *\n * Verifies a provided OTP code against the expected value based on the strategy.\n * - 'totp': Time-based OTP (default, Google Authenticator compatible)\n * - 'hotp': HMAC-based OTP\n *\n * Uses constant-time comparison to prevent timing attacks.\n *\n * @param options - OTP verification options\n * @returns Verification result with validity and optional delta\n *\n * @example TOTP\n * ```ts\n * import { verify } from 'otplib';\n *\n * const result = await verify({\n * secret: 'JBSWY3DPEHPK3PXP',\n * token: '123456',\n * });\n * // Returns: { valid: true, delta: 0 }\n * ```\n *\n * @example HOTP\n * ```ts\n * import { verify } from 'otplib';\n *\n * const result = await verify({\n * secret: 'JBSWY3DPEHPK3PXP',\n * token: '123456',\n * strategy: 'hotp',\n * counter: 0,\n * });\n * ```\n *\n * @example With epochTolerance for TOTP\n * ```ts\n * import { verify, NodeCryptoPlugin } from 'otplib';\n *\n * const result = await verify({\n * secret: 'JBSWY3DPEHPK3PXP',\n * token: '123456',\n * epochTolerance: 30,\n * crypto: new NodeCryptoPlugin(),\n * });\n * ```\n */\nexport async function verify(options: OTPVerifyOptions): Promise<VerifyResult> {\n const opts = normalizeVerifyOptions(options);\n const { secret, token, crypto, base32, algorithm, digits } = opts;\n const commonOptions = { secret, token, crypto, base32, algorithm, digits };\n\n return executeByStrategy(opts.strategy, opts.counter, {\n totp: () =>\n verifyTOTP({\n ...commonOptions,\n period: opts.period,\n epoch: opts.epoch,\n t0: opts.t0,\n epochTolerance: opts.epochTolerance,\n }),\n hotp: (counter) =>\n verifyHOTP({\n ...commonOptions,\n counter,\n counterTolerance: opts.counterTolerance,\n }),\n });\n}\n\n/**\n * Verify an OTP code synchronously\n *\n * This is the synchronous version of {@link verify}. It requires a crypto\n * plugin that supports synchronous HMAC operations.\n *\n * @param options - OTP verification options\n * @returns Verification result with validity and optional delta\n * @throws {HMACError} If the crypto plugin doesn't support sync operations\n *\n * @example\n * ```ts\n * import { verifySync } from 'otplib';\n *\n * const result = verifySync({\n * secret: 'JBSWY3DPEHPK3PXP',\n * token: '123456',\n * });\n * ```\n */\nexport function verifySync(options: OTPVerifyOptions): VerifyResult {\n const opts = normalizeVerifyOptions(options);\n const { secret, token, crypto, base32, algorithm, digits } = opts;\n const commonOptions = { secret, token, crypto, base32, algorithm, digits };\n\n return executeByStrategy(opts.strategy, opts.counter, {\n totp: () =>\n verifyTOTPSync({\n ...commonOptions,\n period: opts.period,\n epoch: opts.epoch,\n t0: opts.t0,\n epochTolerance: opts.epochTolerance,\n }),\n hotp: (counter) =>\n verifyHOTPSync({\n ...commonOptions,\n counter,\n counterTolerance: opts.counterTolerance,\n }),\n });\n}\n","/**\n * Default plugin instances\n *\n * Shared across functional and class APIs to ensure singleton behavior\n * and reduce memory overhead.\n */\nimport { ScureBase32Plugin } from \"@otplib/plugin-base32-scure\";\nimport { NobleCryptoPlugin } from \"@otplib/plugin-crypto-noble\";\n\nimport type {\n OTPGenerateOptions,\n OTPVerifyOptions,\n OTPGenerateOptionsWithDefaults,\n OTPVerifyOptionsWithDefaults,\n} from \"./types\";\n\n/**\n * Default crypto plugin instance (Noble Hashes)\n *\n * This plugin provides cross-platform cryptographic operations\n * using the @noble/hashes library.\n */\nexport const defaultCrypto = Object.freeze(new NobleCryptoPlugin());\n\n/**\n * Default Base32 plugin instance (@scure/base)\n *\n * This plugin provides Base32 encoding/decoding operations\n * using the @scure/base library.\n */\nexport const defaultBase32 = Object.freeze(new ScureBase32Plugin());\n\nexport function normalizeGenerateOptions(\n options: OTPGenerateOptions,\n): OTPGenerateOptionsWithDefaults {\n return {\n secret: options.secret,\n strategy: options.strategy ?? \"totp\",\n crypto: options.crypto ?? defaultCrypto,\n base32: options.base32 ?? defaultBase32,\n algorithm: options.algorithm ?? \"sha1\",\n digits: options.digits ?? 6,\n period: options.period ?? 30,\n epoch: options.epoch ?? Math.floor(Date.now() / 1000),\n t0: options.t0 ?? 0,\n counter: options.counter,\n };\n}\n\nexport function normalizeVerifyOptions(options: OTPVerifyOptions): OTPVerifyOptionsWithDefaults {\n return {\n ...normalizeGenerateOptions(options),\n token: options.token,\n epochTolerance: options.epochTolerance ?? 0,\n counterTolerance: options.counterTolerance ?? 0,\n };\n}\n"],"mappings":"yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,cAA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L,EACH,QAAArB,CACF,CAAC,CACL,CAAC,CACH,CAiDA,eAAsB2B,EAAOxB,EAAkD,CAC7E,IAAMgB,EAAOS,EAAuBzB,CAAO,EACrC,CAAE,OAAAU,EAAQ,MAAAgB,EAAO,OAAAzB,EAAQ,OAAAE,EAAQ,UAAAQ,EAAW,OAAAC,CAAO,EAAII,EACvDE,EAAgB,CAAE,OAAAR,EAAQ,MAAAgB,EAAO,OAAAzB,EAAQ,OAAAE,EAAQ,UAAAQ,EAAW,OAAAC,CAAO,EAEzE,OAAOjB,EAAkBqB,EAAK,SAAUA,EAAK,QAAS,CACpD,KAAM,OACJ,EAAAW,QAAW,CACT,GAAGT,EACH,OAAQF,EAAK,OACb,MAAOA,EAAK,MACZ,GAAIA,EAAK,GACT,eAAgBA,EAAK,cACvB,CAAC,EACH,KAAOnB,MACL,EAAA+B,QAAW,CACT,GAAGV,EACH,QAAArB,EACA,iBAAkBmB,EAAK,gBACzB,CAAC,CACL,CAAC,CACH,CAsBO,SAASa,EAAW7B,EAAyC,CAClE,IAAMgB,EAAOS,EAAuBzB,CAAO,EACrC,CAAE,OAAAU,EAAQ,MAAAgB,EAAO,OAAAzB,EAAQ,OAAAE,EAAQ,UAAAQ,EAAW,OAAAC,CAAO,EAAII,EACvDE,EAAgB,CAAE,OAAAR,EAAQ,MAAAgB,EAAO,OAAAzB,EAAQ,OAAAE,EAAQ,UAAAQ,EAAW,OAAAC,CAAO,EAEzE,OAAOjB,EAAkBqB,EAAK,SAAUA,EAAK,QAAS,CACpD,KAAM,OACJ,EAAAc,YAAe,CACb,GAAGZ,EACH,OAAQF,EAAK,OACb,MAAOA,EAAK,MACZ,GAAIA,EAAK,GACT,eAAgBA,EAAK,cACvB,CAAC,EACH,KAAOnB,MACL,EAAAkC,YAAe,CACb,GAAGb,EACH,QAAArB,EACA,iBAAkBmB,EAAK,gBACzB,CAAC,CACL,CAAC,CACH","names":["functional_exports","__export","generate","generateSecret","generateSync","generateURI","verify","verifySync","__toCommonJS","import_core","import_hotp","import_totp","import_uri","import_plugin_base32_scure","import_plugin_crypto_noble","defaultCrypto","defaultBase32","normalizeGenerateOptions","options","normalizeVerifyOptions","executeByStrategy","strategy","counter","handlers","generateSecret","options","crypto","defaultCrypto","base32","defaultBase32","length","generateSecretCore","generateURI","issuer","label","secret","algorithm","digits","period","generateTOTPURI","generate","opts","normalizeGenerateOptions","commonOptions","generateTOTP","generateHOTP","generateSync","generateTOTPSync","generateHOTPSync","verify","normalizeVerifyOptions","token","verifyTOTP","verifyHOTP","verifySync","verifyTOTPSync","verifyHOTPSync"]}
@@ -0,0 +1,210 @@
1
+ import { a as OTPGenerateOptions, b as OTPVerifyOptions } from './types-D1FZb7MW.cjs';
2
+ export { c as OTPStrategy } from './types-D1FZb7MW.cjs';
3
+ import { CryptoPlugin, Base32Plugin, HashAlgorithm, Digits } from '@otplib/core';
4
+ import { VerifyResult as VerifyResult$2 } from '@otplib/hotp';
5
+ import { VerifyResult as VerifyResult$1 } from '@otplib/totp';
6
+
7
+ type VerifyResult = VerifyResult$1 | VerifyResult$2;
8
+ /**
9
+ * Generate a random secret key for use with OTP
10
+ *
11
+ * The secret is encoded in Base32 format for compatibility with
12
+ * Google Authenticator and other authenticator apps.
13
+ *
14
+ * @param options - Secret generation options
15
+ * @returns Base32-encoded secret key
16
+ *
17
+ * @example
18
+ * ```ts
19
+ * import { generateSecret } from 'otplib';
20
+ *
21
+ * const secret = generateSecret();
22
+ * // Returns: 'JBSWY3DPEHPK3PXP'
23
+ * ```
24
+ *
25
+ * @example With custom plugins
26
+ * ```ts
27
+ * import { generateSecret, NodeCryptoPlugin } from 'otplib';
28
+ *
29
+ * const secret = generateSecret({
30
+ * crypto: new NodeCryptoPlugin(),
31
+ * });
32
+ * ```
33
+ */
34
+ declare function generateSecret(options?: {
35
+ /**
36
+ * Number of random bytes to generate (default: 20)
37
+ * 20 bytes = 160 bits, which provides a good security margin
38
+ */
39
+ length?: number;
40
+ /**
41
+ * Crypto plugin to use (default: NobleCryptoPlugin)
42
+ */
43
+ crypto?: CryptoPlugin;
44
+ /**
45
+ * Base32 plugin to use (default: ScureBase32Plugin)
46
+ */
47
+ base32?: Base32Plugin;
48
+ }): string;
49
+ /**
50
+ * Generate an otpauth:// URI for QR code generation
51
+ *
52
+ * This URI can be used to generate a QR code that can be scanned
53
+ * by Google Authenticator and other authenticator apps.
54
+ *
55
+ * @param options - URI generation options
56
+ * @returns otpauth:// URI string
57
+ *
58
+ * @example
59
+ * ```ts
60
+ * import { generateURI } from 'otplib';
61
+ *
62
+ * const uri = generateURI({
63
+ * issuer: 'ACME Co',
64
+ * label: 'john@example.com',
65
+ * secret: 'JBSWY3DPEHPK3PXP',
66
+ * });
67
+ * // Returns: 'otpauth://totp/ACME%20Co:john%40example.com?secret=...'
68
+ * ```
69
+ */
70
+ declare function generateURI(options: {
71
+ issuer: string;
72
+ label: string;
73
+ secret: string;
74
+ algorithm?: HashAlgorithm;
75
+ digits?: Digits;
76
+ period?: number;
77
+ }): string;
78
+ /**
79
+ * Generate an OTP code
80
+ *
81
+ * Generates a one-time password based on the specified strategy.
82
+ * - 'totp': Time-based OTP (default)
83
+ * - 'hotp': HMAC-based OTP
84
+ *
85
+ * @param options - OTP generation options
86
+ * @returns OTP code
87
+ *
88
+ * @example TOTP
89
+ * ```ts
90
+ * import { generate } from 'otplib';
91
+ *
92
+ * const token = await generate({
93
+ * secret: 'JBSWY3DPEHPK3PXP',
94
+ * });
95
+ * // Returns: '123456'
96
+ * ```
97
+ *
98
+ * @example HOTP
99
+ * ```ts
100
+ * import { generate } from 'otplib';
101
+ *
102
+ * const token = await generate({
103
+ * secret: 'JBSWY3DPEHPK3PXP',
104
+ * strategy: 'hotp',
105
+ * counter: 0,
106
+ * });
107
+ * ```
108
+ *
109
+ * @example With custom plugins
110
+ * ```ts
111
+ * import { generate, NodeCryptoPlugin } from 'otplib';
112
+ *
113
+ * const token = await generate({
114
+ * secret: 'JBSWY3DPEHPK3PXP',
115
+ * crypto: new NodeCryptoPlugin(),
116
+ * });
117
+ * ```
118
+ */
119
+ declare function generate(options: OTPGenerateOptions): Promise<string>;
120
+ /**
121
+ * Generate an OTP code synchronously
122
+ *
123
+ * This is the synchronous version of {@link generate}. It requires a crypto
124
+ * plugin that supports synchronous HMAC operations.
125
+ *
126
+ * @param options - OTP generation options
127
+ * @returns OTP code
128
+ * @throws {HMACError} If the crypto plugin doesn't support sync operations
129
+ *
130
+ * @example
131
+ * ```ts
132
+ * import { generateSync } from 'otplib';
133
+ *
134
+ * const token = generateSync({
135
+ * secret: 'JBSWY3DPEHPK3PXP',
136
+ * });
137
+ * ```
138
+ */
139
+ declare function generateSync(options: OTPGenerateOptions): string;
140
+ /**
141
+ * Verify an OTP code
142
+ *
143
+ * Verifies a provided OTP code against the expected value based on the strategy.
144
+ * - 'totp': Time-based OTP (default, Google Authenticator compatible)
145
+ * - 'hotp': HMAC-based OTP
146
+ *
147
+ * Uses constant-time comparison to prevent timing attacks.
148
+ *
149
+ * @param options - OTP verification options
150
+ * @returns Verification result with validity and optional delta
151
+ *
152
+ * @example TOTP
153
+ * ```ts
154
+ * import { verify } from 'otplib';
155
+ *
156
+ * const result = await verify({
157
+ * secret: 'JBSWY3DPEHPK3PXP',
158
+ * token: '123456',
159
+ * });
160
+ * // Returns: { valid: true, delta: 0 }
161
+ * ```
162
+ *
163
+ * @example HOTP
164
+ * ```ts
165
+ * import { verify } from 'otplib';
166
+ *
167
+ * const result = await verify({
168
+ * secret: 'JBSWY3DPEHPK3PXP',
169
+ * token: '123456',
170
+ * strategy: 'hotp',
171
+ * counter: 0,
172
+ * });
173
+ * ```
174
+ *
175
+ * @example With epochTolerance for TOTP
176
+ * ```ts
177
+ * import { verify, NodeCryptoPlugin } from 'otplib';
178
+ *
179
+ * const result = await verify({
180
+ * secret: 'JBSWY3DPEHPK3PXP',
181
+ * token: '123456',
182
+ * epochTolerance: 30,
183
+ * crypto: new NodeCryptoPlugin(),
184
+ * });
185
+ * ```
186
+ */
187
+ declare function verify(options: OTPVerifyOptions): Promise<VerifyResult>;
188
+ /**
189
+ * Verify an OTP code synchronously
190
+ *
191
+ * This is the synchronous version of {@link verify}. It requires a crypto
192
+ * plugin that supports synchronous HMAC operations.
193
+ *
194
+ * @param options - OTP verification options
195
+ * @returns Verification result with validity and optional delta
196
+ * @throws {HMACError} If the crypto plugin doesn't support sync operations
197
+ *
198
+ * @example
199
+ * ```ts
200
+ * import { verifySync } from 'otplib';
201
+ *
202
+ * const result = verifySync({
203
+ * secret: 'JBSWY3DPEHPK3PXP',
204
+ * token: '123456',
205
+ * });
206
+ * ```
207
+ */
208
+ declare function verifySync(options: OTPVerifyOptions): VerifyResult;
209
+
210
+ export { type VerifyResult, generate, generateSecret, generateSync, generateURI, verify, verifySync };
@@ -0,0 +1,210 @@
1
+ import { a as OTPGenerateOptions, b as OTPVerifyOptions } from './types-D1FZb7MW.js';
2
+ export { c as OTPStrategy } from './types-D1FZb7MW.js';
3
+ import { CryptoPlugin, Base32Plugin, HashAlgorithm, Digits } from '@otplib/core';
4
+ import { VerifyResult as VerifyResult$2 } from '@otplib/hotp';
5
+ import { VerifyResult as VerifyResult$1 } from '@otplib/totp';
6
+
7
+ type VerifyResult = VerifyResult$1 | VerifyResult$2;
8
+ /**
9
+ * Generate a random secret key for use with OTP
10
+ *
11
+ * The secret is encoded in Base32 format for compatibility with
12
+ * Google Authenticator and other authenticator apps.
13
+ *
14
+ * @param options - Secret generation options
15
+ * @returns Base32-encoded secret key
16
+ *
17
+ * @example
18
+ * ```ts
19
+ * import { generateSecret } from 'otplib';
20
+ *
21
+ * const secret = generateSecret();
22
+ * // Returns: 'JBSWY3DPEHPK3PXP'
23
+ * ```
24
+ *
25
+ * @example With custom plugins
26
+ * ```ts
27
+ * import { generateSecret, NodeCryptoPlugin } from 'otplib';
28
+ *
29
+ * const secret = generateSecret({
30
+ * crypto: new NodeCryptoPlugin(),
31
+ * });
32
+ * ```
33
+ */
34
+ declare function generateSecret(options?: {
35
+ /**
36
+ * Number of random bytes to generate (default: 20)
37
+ * 20 bytes = 160 bits, which provides a good security margin
38
+ */
39
+ length?: number;
40
+ /**
41
+ * Crypto plugin to use (default: NobleCryptoPlugin)
42
+ */
43
+ crypto?: CryptoPlugin;
44
+ /**
45
+ * Base32 plugin to use (default: ScureBase32Plugin)
46
+ */
47
+ base32?: Base32Plugin;
48
+ }): string;
49
+ /**
50
+ * Generate an otpauth:// URI for QR code generation
51
+ *
52
+ * This URI can be used to generate a QR code that can be scanned
53
+ * by Google Authenticator and other authenticator apps.
54
+ *
55
+ * @param options - URI generation options
56
+ * @returns otpauth:// URI string
57
+ *
58
+ * @example
59
+ * ```ts
60
+ * import { generateURI } from 'otplib';
61
+ *
62
+ * const uri = generateURI({
63
+ * issuer: 'ACME Co',
64
+ * label: 'john@example.com',
65
+ * secret: 'JBSWY3DPEHPK3PXP',
66
+ * });
67
+ * // Returns: 'otpauth://totp/ACME%20Co:john%40example.com?secret=...'
68
+ * ```
69
+ */
70
+ declare function generateURI(options: {
71
+ issuer: string;
72
+ label: string;
73
+ secret: string;
74
+ algorithm?: HashAlgorithm;
75
+ digits?: Digits;
76
+ period?: number;
77
+ }): string;
78
+ /**
79
+ * Generate an OTP code
80
+ *
81
+ * Generates a one-time password based on the specified strategy.
82
+ * - 'totp': Time-based OTP (default)
83
+ * - 'hotp': HMAC-based OTP
84
+ *
85
+ * @param options - OTP generation options
86
+ * @returns OTP code
87
+ *
88
+ * @example TOTP
89
+ * ```ts
90
+ * import { generate } from 'otplib';
91
+ *
92
+ * const token = await generate({
93
+ * secret: 'JBSWY3DPEHPK3PXP',
94
+ * });
95
+ * // Returns: '123456'
96
+ * ```
97
+ *
98
+ * @example HOTP
99
+ * ```ts
100
+ * import { generate } from 'otplib';
101
+ *
102
+ * const token = await generate({
103
+ * secret: 'JBSWY3DPEHPK3PXP',
104
+ * strategy: 'hotp',
105
+ * counter: 0,
106
+ * });
107
+ * ```
108
+ *
109
+ * @example With custom plugins
110
+ * ```ts
111
+ * import { generate, NodeCryptoPlugin } from 'otplib';
112
+ *
113
+ * const token = await generate({
114
+ * secret: 'JBSWY3DPEHPK3PXP',
115
+ * crypto: new NodeCryptoPlugin(),
116
+ * });
117
+ * ```
118
+ */
119
+ declare function generate(options: OTPGenerateOptions): Promise<string>;
120
+ /**
121
+ * Generate an OTP code synchronously
122
+ *
123
+ * This is the synchronous version of {@link generate}. It requires a crypto
124
+ * plugin that supports synchronous HMAC operations.
125
+ *
126
+ * @param options - OTP generation options
127
+ * @returns OTP code
128
+ * @throws {HMACError} If the crypto plugin doesn't support sync operations
129
+ *
130
+ * @example
131
+ * ```ts
132
+ * import { generateSync } from 'otplib';
133
+ *
134
+ * const token = generateSync({
135
+ * secret: 'JBSWY3DPEHPK3PXP',
136
+ * });
137
+ * ```
138
+ */
139
+ declare function generateSync(options: OTPGenerateOptions): string;
140
+ /**
141
+ * Verify an OTP code
142
+ *
143
+ * Verifies a provided OTP code against the expected value based on the strategy.
144
+ * - 'totp': Time-based OTP (default, Google Authenticator compatible)
145
+ * - 'hotp': HMAC-based OTP
146
+ *
147
+ * Uses constant-time comparison to prevent timing attacks.
148
+ *
149
+ * @param options - OTP verification options
150
+ * @returns Verification result with validity and optional delta
151
+ *
152
+ * @example TOTP
153
+ * ```ts
154
+ * import { verify } from 'otplib';
155
+ *
156
+ * const result = await verify({
157
+ * secret: 'JBSWY3DPEHPK3PXP',
158
+ * token: '123456',
159
+ * });
160
+ * // Returns: { valid: true, delta: 0 }
161
+ * ```
162
+ *
163
+ * @example HOTP
164
+ * ```ts
165
+ * import { verify } from 'otplib';
166
+ *
167
+ * const result = await verify({
168
+ * secret: 'JBSWY3DPEHPK3PXP',
169
+ * token: '123456',
170
+ * strategy: 'hotp',
171
+ * counter: 0,
172
+ * });
173
+ * ```
174
+ *
175
+ * @example With epochTolerance for TOTP
176
+ * ```ts
177
+ * import { verify, NodeCryptoPlugin } from 'otplib';
178
+ *
179
+ * const result = await verify({
180
+ * secret: 'JBSWY3DPEHPK3PXP',
181
+ * token: '123456',
182
+ * epochTolerance: 30,
183
+ * crypto: new NodeCryptoPlugin(),
184
+ * });
185
+ * ```
186
+ */
187
+ declare function verify(options: OTPVerifyOptions): Promise<VerifyResult>;
188
+ /**
189
+ * Verify an OTP code synchronously
190
+ *
191
+ * This is the synchronous version of {@link verify}. It requires a crypto
192
+ * plugin that supports synchronous HMAC operations.
193
+ *
194
+ * @param options - OTP verification options
195
+ * @returns Verification result with validity and optional delta
196
+ * @throws {HMACError} If the crypto plugin doesn't support sync operations
197
+ *
198
+ * @example
199
+ * ```ts
200
+ * import { verifySync } from 'otplib';
201
+ *
202
+ * const result = verifySync({
203
+ * secret: 'JBSWY3DPEHPK3PXP',
204
+ * token: '123456',
205
+ * });
206
+ * ```
207
+ */
208
+ declare function verifySync(options: OTPVerifyOptions): VerifyResult;
209
+
210
+ export { type VerifyResult, generate, generateSecret, generateSync, generateURI, verify, verifySync };
@@ -0,0 +1,2 @@
1
+ import{generateSecret as h,ConfigurationError as O}from"@otplib/core";import{generate as P,generateSync as d,verify as S,verifySync as b}from"@otplib/hotp";import{generate as V,generateSync as x,verify as H,verifySync as R}from"@otplib/totp";import{generateTOTP as v}from"@otplib/uri";import{ScureBase32Plugin as T}from"@otplib/plugin-base32-scure";import{NobleCryptoPlugin as m}from"@otplib/plugin-crypto-noble";var g=Object.freeze(new m),u=Object.freeze(new T);function c(t){return{secret:t.secret,strategy:t.strategy??"totp",crypto:t.crypto??g,base32:t.base32??u,algorithm:t.algorithm??"sha1",digits:t.digits??6,period:t.period??30,epoch:t.epoch??Math.floor(Date.now()/1e3),t0:t.t0??0,counter:t.counter}}function l(t){return{...c(t),token:t.token,epochTolerance:t.epochTolerance??0,counterTolerance:t.counterTolerance??0}}function y(t,e,r){if(t==="totp")return r.totp();if(t==="hotp"){if(e===void 0)throw new O("Counter is required for HOTP strategy. Example: { strategy: 'hotp', counter: 0 }");return r.hotp(e)}throw new O(`Unknown OTP strategy: ${t}. Valid strategies are 'totp' or 'hotp'.`)}function U(t){let{crypto:e=g,base32:r=u,length:o=20}=t||{};return h({crypto:e,base32:r,length:o})}function j(t){let{issuer:e,label:r,secret:o,algorithm:n="sha1",digits:i=6,period:s=30}=t;return v({issuer:e,label:r,secret:o,algorithm:n,digits:i,period:s})}async function A(t){let e=c(t),{secret:r,crypto:o,base32:n,algorithm:i,digits:s}=e,a={secret:r,crypto:o,base32:n,algorithm:i,digits:s};return y(e.strategy,e.counter,{totp:()=>V({...a,period:e.period,epoch:e.epoch,t0:e.t0}),hotp:p=>P({...a,counter:p})})}function E(t){let e=c(t),{secret:r,crypto:o,base32:n,algorithm:i,digits:s}=e,a={secret:r,crypto:o,base32:n,algorithm:i,digits:s};return y(e.strategy,e.counter,{totp:()=>x({...a,period:e.period,epoch:e.epoch,t0:e.t0}),hotp:p=>d({...a,counter:p})})}async function I(t){let e=l(t),{secret:r,token:o,crypto:n,base32:i,algorithm:s,digits:a}=e,p={secret:r,token:o,crypto:n,base32:i,algorithm:s,digits:a};return y(e.strategy,e.counter,{totp:()=>H({...p,period:e.period,epoch:e.epoch,t0:e.t0,epochTolerance:e.epochTolerance}),hotp:f=>S({...p,counter:f,counterTolerance:e.counterTolerance})})}function q(t){let e=l(t),{secret:r,token:o,crypto:n,base32:i,algorithm:s,digits:a}=e,p={secret:r,token:o,crypto:n,base32:i,algorithm:s,digits:a};return y(e.strategy,e.counter,{totp:()=>R({...p,period:e.period,epoch:e.epoch,t0:e.t0,epochTolerance:e.epochTolerance}),hotp:f=>b({...p,counter:f,counterTolerance:e.counterTolerance})})}export{A as generate,U as generateSecret,E as generateSync,j as generateURI,I as verify,q as verifySync};
2
+ //# sourceMappingURL=functional.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/functional.ts","../src/defaults.ts"],"sourcesContent":["import { generateSecret as generateSecretCore, ConfigurationError } from \"@otplib/core\";\nimport {\n generate as generateHOTP,\n generateSync as generateHOTPSync,\n verify as verifyHOTP,\n verifySync as verifyHOTPSync,\n} from \"@otplib/hotp\";\nimport {\n generate as generateTOTP,\n generateSync as generateTOTPSync,\n verify as verifyTOTP,\n verifySync as verifyTOTPSync,\n} from \"@otplib/totp\";\nimport { generateTOTP as generateTOTPURI } from \"@otplib/uri\";\n\nimport {\n defaultCrypto,\n defaultBase32,\n normalizeGenerateOptions,\n normalizeVerifyOptions,\n} from \"./defaults\";\n\nimport type { OTPGenerateOptions, OTPVerifyOptions, OTPStrategy, StrategyHandlers } from \"./types\";\nimport type { CryptoPlugin, Base32Plugin, Digits, HashAlgorithm } from \"@otplib/core\";\nimport type { VerifyResult as HOTPVerifyResult } from \"@otplib/hotp\";\nimport type { VerifyResult as TOTPVerifyResult } from \"@otplib/totp\";\n\nexport type { OTPStrategy };\n\nexport type VerifyResult = TOTPVerifyResult | HOTPVerifyResult;\n\nfunction executeByStrategy<T>(\n strategy: OTPStrategy,\n counter: number | undefined,\n handlers: StrategyHandlers<T>,\n): T {\n if (strategy === \"totp\") {\n return handlers.totp();\n }\n if (strategy === \"hotp\") {\n if (counter === undefined) {\n throw new ConfigurationError(\n \"Counter is required for HOTP strategy. Example: { strategy: 'hotp', counter: 0 }\",\n );\n }\n return handlers.hotp(counter);\n }\n throw new ConfigurationError(\n `Unknown OTP strategy: ${strategy}. Valid strategies are 'totp' or 'hotp'.`,\n );\n}\n\n/**\n * Generate a random secret key for use with OTP\n *\n * The secret is encoded in Base32 format for compatibility with\n * Google Authenticator and other authenticator apps.\n *\n * @param options - Secret generation options\n * @returns Base32-encoded secret key\n *\n * @example\n * ```ts\n * import { generateSecret } from 'otplib';\n *\n * const secret = generateSecret();\n * // Returns: 'JBSWY3DPEHPK3PXP'\n * ```\n *\n * @example With custom plugins\n * ```ts\n * import { generateSecret, NodeCryptoPlugin } from 'otplib';\n *\n * const secret = generateSecret({\n * crypto: new NodeCryptoPlugin(),\n * });\n * ```\n */\nexport function generateSecret(options?: {\n /**\n * Number of random bytes to generate (default: 20)\n * 20 bytes = 160 bits, which provides a good security margin\n */\n length?: number;\n\n /**\n * Crypto plugin to use (default: NobleCryptoPlugin)\n */\n crypto?: CryptoPlugin;\n\n /**\n * Base32 plugin to use (default: ScureBase32Plugin)\n */\n base32?: Base32Plugin;\n}): string {\n const { crypto = defaultCrypto, base32 = defaultBase32, length = 20 } = options || {};\n\n return generateSecretCore({ crypto, base32, length });\n}\n\n/**\n * Generate an otpauth:// URI for QR code generation\n *\n * This URI can be used to generate a QR code that can be scanned\n * by Google Authenticator and other authenticator apps.\n *\n * @param options - URI generation options\n * @returns otpauth:// URI string\n *\n * @example\n * ```ts\n * import { generateURI } from 'otplib';\n *\n * const uri = generateURI({\n * issuer: 'ACME Co',\n * label: 'john@example.com',\n * secret: 'JBSWY3DPEHPK3PXP',\n * });\n * // Returns: 'otpauth://totp/ACME%20Co:john%40example.com?secret=...'\n * ```\n */\nexport function generateURI(options: {\n issuer: string;\n label: string;\n secret: string;\n algorithm?: HashAlgorithm;\n digits?: Digits;\n period?: number;\n}): string {\n const { issuer, label, secret, algorithm = \"sha1\", digits = 6, period = 30 } = options;\n return generateTOTPURI({ issuer, label, secret, algorithm, digits, period });\n}\n\n/**\n * Generate an OTP code\n *\n * Generates a one-time password based on the specified strategy.\n * - 'totp': Time-based OTP (default)\n * - 'hotp': HMAC-based OTP\n *\n * @param options - OTP generation options\n * @returns OTP code\n *\n * @example TOTP\n * ```ts\n * import { generate } from 'otplib';\n *\n * const token = await generate({\n * secret: 'JBSWY3DPEHPK3PXP',\n * });\n * // Returns: '123456'\n * ```\n *\n * @example HOTP\n * ```ts\n * import { generate } from 'otplib';\n *\n * const token = await generate({\n * secret: 'JBSWY3DPEHPK3PXP',\n * strategy: 'hotp',\n * counter: 0,\n * });\n * ```\n *\n * @example With custom plugins\n * ```ts\n * import { generate, NodeCryptoPlugin } from 'otplib';\n *\n * const token = await generate({\n * secret: 'JBSWY3DPEHPK3PXP',\n * crypto: new NodeCryptoPlugin(),\n * });\n * ```\n */\nexport async function generate(options: OTPGenerateOptions): Promise<string> {\n const opts = normalizeGenerateOptions(options);\n const { secret, crypto, base32, algorithm, digits } = opts;\n const commonOptions = { secret, crypto, base32, algorithm, digits };\n\n return executeByStrategy(opts.strategy, opts.counter, {\n totp: () =>\n generateTOTP({\n ...commonOptions,\n period: opts.period,\n epoch: opts.epoch,\n t0: opts.t0,\n }),\n hotp: (counter) =>\n generateHOTP({\n ...commonOptions,\n counter,\n }),\n });\n}\n\n/**\n * Generate an OTP code synchronously\n *\n * This is the synchronous version of {@link generate}. It requires a crypto\n * plugin that supports synchronous HMAC operations.\n *\n * @param options - OTP generation options\n * @returns OTP code\n * @throws {HMACError} If the crypto plugin doesn't support sync operations\n *\n * @example\n * ```ts\n * import { generateSync } from 'otplib';\n *\n * const token = generateSync({\n * secret: 'JBSWY3DPEHPK3PXP',\n * });\n * ```\n */\nexport function generateSync(options: OTPGenerateOptions): string {\n const opts = normalizeGenerateOptions(options);\n const { secret, crypto, base32, algorithm, digits } = opts;\n const commonOptions = { secret, crypto, base32, algorithm, digits };\n\n return executeByStrategy(opts.strategy, opts.counter, {\n totp: () =>\n generateTOTPSync({\n ...commonOptions,\n period: opts.period,\n epoch: opts.epoch,\n t0: opts.t0,\n }),\n hotp: (counter) =>\n generateHOTPSync({\n ...commonOptions,\n counter,\n }),\n });\n}\n\n/**\n * Verify an OTP code\n *\n * Verifies a provided OTP code against the expected value based on the strategy.\n * - 'totp': Time-based OTP (default, Google Authenticator compatible)\n * - 'hotp': HMAC-based OTP\n *\n * Uses constant-time comparison to prevent timing attacks.\n *\n * @param options - OTP verification options\n * @returns Verification result with validity and optional delta\n *\n * @example TOTP\n * ```ts\n * import { verify } from 'otplib';\n *\n * const result = await verify({\n * secret: 'JBSWY3DPEHPK3PXP',\n * token: '123456',\n * });\n * // Returns: { valid: true, delta: 0 }\n * ```\n *\n * @example HOTP\n * ```ts\n * import { verify } from 'otplib';\n *\n * const result = await verify({\n * secret: 'JBSWY3DPEHPK3PXP',\n * token: '123456',\n * strategy: 'hotp',\n * counter: 0,\n * });\n * ```\n *\n * @example With epochTolerance for TOTP\n * ```ts\n * import { verify, NodeCryptoPlugin } from 'otplib';\n *\n * const result = await verify({\n * secret: 'JBSWY3DPEHPK3PXP',\n * token: '123456',\n * epochTolerance: 30,\n * crypto: new NodeCryptoPlugin(),\n * });\n * ```\n */\nexport async function verify(options: OTPVerifyOptions): Promise<VerifyResult> {\n const opts = normalizeVerifyOptions(options);\n const { secret, token, crypto, base32, algorithm, digits } = opts;\n const commonOptions = { secret, token, crypto, base32, algorithm, digits };\n\n return executeByStrategy(opts.strategy, opts.counter, {\n totp: () =>\n verifyTOTP({\n ...commonOptions,\n period: opts.period,\n epoch: opts.epoch,\n t0: opts.t0,\n epochTolerance: opts.epochTolerance,\n }),\n hotp: (counter) =>\n verifyHOTP({\n ...commonOptions,\n counter,\n counterTolerance: opts.counterTolerance,\n }),\n });\n}\n\n/**\n * Verify an OTP code synchronously\n *\n * This is the synchronous version of {@link verify}. It requires a crypto\n * plugin that supports synchronous HMAC operations.\n *\n * @param options - OTP verification options\n * @returns Verification result with validity and optional delta\n * @throws {HMACError} If the crypto plugin doesn't support sync operations\n *\n * @example\n * ```ts\n * import { verifySync } from 'otplib';\n *\n * const result = verifySync({\n * secret: 'JBSWY3DPEHPK3PXP',\n * token: '123456',\n * });\n * ```\n */\nexport function verifySync(options: OTPVerifyOptions): VerifyResult {\n const opts = normalizeVerifyOptions(options);\n const { secret, token, crypto, base32, algorithm, digits } = opts;\n const commonOptions = { secret, token, crypto, base32, algorithm, digits };\n\n return executeByStrategy(opts.strategy, opts.counter, {\n totp: () =>\n verifyTOTPSync({\n ...commonOptions,\n period: opts.period,\n epoch: opts.epoch,\n t0: opts.t0,\n epochTolerance: opts.epochTolerance,\n }),\n hotp: (counter) =>\n verifyHOTPSync({\n ...commonOptions,\n counter,\n counterTolerance: opts.counterTolerance,\n }),\n });\n}\n","/**\n * Default plugin instances\n *\n * Shared across functional and class APIs to ensure singleton behavior\n * and reduce memory overhead.\n */\nimport { ScureBase32Plugin } from \"@otplib/plugin-base32-scure\";\nimport { NobleCryptoPlugin } from \"@otplib/plugin-crypto-noble\";\n\nimport type {\n OTPGenerateOptions,\n OTPVerifyOptions,\n OTPGenerateOptionsWithDefaults,\n OTPVerifyOptionsWithDefaults,\n} from \"./types\";\n\n/**\n * Default crypto plugin instance (Noble Hashes)\n *\n * This plugin provides cross-platform cryptographic operations\n * using the @noble/hashes library.\n */\nexport const defaultCrypto = Object.freeze(new NobleCryptoPlugin());\n\n/**\n * Default Base32 plugin instance (@scure/base)\n *\n * This plugin provides Base32 encoding/decoding operations\n * using the @scure/base library.\n */\nexport const defaultBase32 = Object.freeze(new ScureBase32Plugin());\n\nexport function normalizeGenerateOptions(\n options: OTPGenerateOptions,\n): OTPGenerateOptionsWithDefaults {\n return {\n secret: options.secret,\n strategy: options.strategy ?? \"totp\",\n crypto: options.crypto ?? defaultCrypto,\n base32: options.base32 ?? defaultBase32,\n algorithm: options.algorithm ?? \"sha1\",\n digits: options.digits ?? 6,\n period: options.period ?? 30,\n epoch: options.epoch ?? Math.floor(Date.now() / 1000),\n t0: options.t0 ?? 0,\n counter: options.counter,\n };\n}\n\nexport function normalizeVerifyOptions(options: OTPVerifyOptions): OTPVerifyOptionsWithDefaults {\n return {\n ...normalizeGenerateOptions(options),\n token: options.token,\n epochTolerance: options.epochTolerance ?? 0,\n counterTolerance: options.counterTolerance ?? 0,\n };\n}\n"],"mappings":"AAAA,OAAS,kBAAkBA,EAAoB,sBAAAC,MAA0B,eACzE,OACE,YAAYC,EACZ,gBAAgBC,EAChB,UAAUC,EACV,cAAcC,MACT,eACP,OACE,YAAYC,EACZ,gBAAgBC,EAChB,UAAUC,EACV,cAAcC,MACT,eACP,OAAS,gBAAgBC,MAAuB,cCPhD,OAAS,qBAAAC,MAAyB,8BAClC,OAAS,qBAAAC,MAAyB,8BAe3B,IAAMC,EAAgB,OAAO,OAAO,IAAID,CAAmB,EAQrDE,EAAgB,OAAO,OAAO,IAA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AAW,OAAAC,CAAO,EAEzE,OAAOlB,EAAkBsB,EAAK,SAAUA,EAAK,QAAS,CACpD,KAAM,IACJW,EAAW,CACT,GAAGT,EACH,OAAQF,EAAK,OACb,MAAOA,EAAK,MACZ,GAAIA,EAAK,GACT,eAAgBA,EAAK,cACvB,CAAC,EACH,KAAOpB,GACLgC,EAAW,CACT,GAAGV,EACH,QAAAtB,EACA,iBAAkBoB,EAAK,gBACzB,CAAC,CACL,CAAC,CACH,CAsBO,SAASa,EAAW7B,EAAyC,CAClE,IAAMgB,EAAOS,EAAuBzB,CAAO,EACrC,CAAE,OAAAU,EAAQ,MAAAgB,EAAO,OAAAzB,EAAQ,OAAAE,EAAQ,UAAAQ,EAAW,OAAAC,CAAO,EAAII,EACvDE,EAAgB,CAAE,OAAAR,EAAQ,MAAAgB,EAAO,OAAAzB,EAAQ,OAAAE,EAAQ,UAAAQ,EAAW,OAAAC,CAAO,EAEzE,OAAOlB,EAAkBsB,EAAK,SAAUA,EAAK,QAAS,CACpD,KAAM,IACJc,EAAe,CACb,GAAGZ,EACH,OAAQF,EAAK,OACb,MAAOA,EAAK,MACZ,GAAIA,EAAK,GACT,eAAgBA,EAAK,cACvB,CAAC,EACH,KAAOpB,GACLmC,EAAe,CACb,GAAGb,EACH,QAAAtB,EACA,iBAAkBoB,EAAK,gBACzB,CAAC,CACL,CAAC,CACH","names":["generateSecretCore","ConfigurationError","generateHOTP","generateHOTPSync","verifyHOTP","verifyHOTPSync","generateTOTP","generateTOTPSync","verifyTOTP","verifyTOTPSync","generateTOTPURI","ScureBase32Plugin","NobleCryptoPlugin","defaultCrypto","defaultBase32","normalizeGenerateOptions","options","normalizeVerifyOptions","executeByStrategy","strategy","counter","handlers","ConfigurationError","generateSecret","options","crypto","defaultCrypto","base32","defaultBase32","length","generateSecretCore","generateURI","issuer","label","secret","algorithm","digits","period","generateTOTPURI","generate","opts","normalizeGenerateOptions","commonOptions","generateTOTP","generateHOTP","generateSync","generateTOTPSync","generateHOTPSync","verify","normalizeVerifyOptions","token","verifyTOTP","verifyHOTP","verifySync","verifyTOTPSync","verifyHOTPSync"]}
package/dist/index.cjs ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";var V=Object.defineProperty;var k=Object.getOwnPropertyDescriptor;var z=Object.getOwnPropertyNames;var W=Object.prototype.hasOwnProperty;var E=(t,e)=>{for(var r in e)V(t,r,{get:e[r],enumerable:!0})},j=(t,e,r,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of z(e))!W.call(t,o)&&o!==r&&V(t,o,{get:()=>e[o],enumerable:!(n=k(e,o))||n.enumerable});return t};var F=t=>j(V({},"__esModule",{value:!0}),t);var N={};E(N,{HOTP:()=>D.HOTP,NobleCryptoPlugin:()=>A.NobleCryptoPlugin,OTP:()=>b,ScureBase32Plugin:()=>I.ScureBase32Plugin,TOTP:()=>U.TOTP,generate:()=>m,generateSecret:()=>G,generateSync:()=>T,generateURI:()=>C,verify:()=>h,verifySync:()=>P,wrapResult:()=>d.wrapResult,wrapResultAsync:()=>d.wrapResultAsync});module.exports=F(N);var l=require("@otplib/core"),y=require("@otplib/hotp"),c=require("@otplib/totp"),H=require("@otplib/uri");var x=require("@otplib/plugin-base32-scure"),v=require("@otplib/plugin-crypto-noble"),g=Object.freeze(new v.NobleCryptoPlugin),u=Object.freeze(new x.ScureBase32Plugin);function f(t){return{secret:t.secret,strategy:t.strategy??"totp",crypto:t.crypto??g,base32:t.base32??u,algorithm:t.algorithm??"sha1",digits:t.digits??6,period:t.period??30,epoch:t.epoch??Math.floor(Date.now()/1e3),t0:t.t0??0,counter:t.counter}}function R(t){return{...f(t),token:t.token,epochTolerance:t.epochTolerance??0,counterTolerance:t.counterTolerance??0}}function O(t,e,r){if(t==="totp")return r.totp();if(t==="hotp"){if(e===void 0)throw new l.ConfigurationError("Counter is required for HOTP strategy. Example: { strategy: 'hotp', counter: 0 }");return r.hotp(e)}throw new l.ConfigurationError(`Unknown OTP strategy: ${t}. Valid strategies are 'totp' or 'hotp'.`)}function G(t){let{crypto:e=g,base32:r=u,length:n=20}=t||{};return(0,l.generateSecret)({crypto:e,base32:r,length:n})}function C(t){let{issuer:e,label:r,secret:n,algorithm:o="sha1",digits:i=6,period:s=30}=t;return(0,H.generateTOTP)({issuer:e,label:r,secret:n,algorithm:o,digits:i,period:s})}async function m(t){let e=f(t),{secret:r,crypto:n,base32:o,algorithm:i,digits:s}=e,a={secret:r,crypto:n,base32:o,algorithm:i,digits:s};return O(e.strategy,e.counter,{totp:()=>(0,c.generate)({...a,period:e.period,epoch:e.epoch,t0:e.t0}),hotp:p=>(0,y.generate)({...a,counter:p})})}function T(t){let e=f(t),{secret:r,crypto:n,base32:o,algorithm:i,digits:s}=e,a={secret:r,crypto:n,base32:o,algorithm:i,digits:s};return O(e.strategy,e.counter,{totp:()=>(0,c.generateSync)({...a,period:e.period,epoch:e.epoch,t0:e.t0}),hotp:p=>(0,y.generateSync)({...a,counter:p})})}async function h(t){let e=R(t),{secret:r,token:n,crypto:o,base32:i,algorithm:s,digits:a}=e,p={secret:r,token:n,crypto:o,base32:i,algorithm:s,digits:a};return O(e.strategy,e.counter,{totp:()=>(0,c.verify)({...p,period:e.period,epoch:e.epoch,t0:e.t0,epochTolerance:e.epochTolerance}),hotp:S=>(0,y.verify)({...p,counter:S,counterTolerance:e.counterTolerance})})}function P(t){let e=R(t),{secret:r,token:n,crypto:o,base32:i,algorithm:s,digits:a}=e,p={secret:r,token:n,crypto:o,base32:i,algorithm:s,digits:a};return O(e.strategy,e.counter,{totp:()=>(0,c.verifySync)({...p,period:e.period,epoch:e.epoch,t0:e.t0,epochTolerance:e.epochTolerance}),hotp:S=>(0,y.verifySync)({...p,counter:S,counterTolerance:e.counterTolerance})})}var w=require("@otplib/core"),B=require("@otplib/uri");var b=class{strategy;crypto;base32;constructor(e={}){let{strategy:r="totp",crypto:n=g,base32:o=u}=e;this.strategy=r,this.crypto=n,this.base32=o}getStrategy(){return this.strategy}generateSecret(e=20){return(0,w.generateSecret)({crypto:this.crypto,base32:this.base32,length:e})}async generate(e){return m({...e,strategy:this.strategy,crypto:this.crypto,base32:this.base32})}generateSync(e){return T({...e,strategy:this.strategy,crypto:this.crypto,base32:this.base32})}async verify(e){return h({...e,strategy:this.strategy,crypto:this.crypto,base32:this.base32})}verifySync(e){return P({...e,strategy:this.strategy,crypto:this.crypto,base32:this.base32})}generateURI(e){if(this.strategy==="hotp")throw new Error("generateURI is not available for HOTP strategy");let{issuer:r,label:n,secret:o,algorithm:i="sha1",digits:s=6,period:a=30}=e;return(0,B.generateTOTP)({issuer:r,label:n,secret:o,algorithm:i,digits:s,period:a})}};var D=require("@otplib/hotp"),U=require("@otplib/totp"),d=require("@otplib/core"),A=require("@otplib/plugin-crypto-noble"),I=require("@otplib/plugin-base32-scure");0&&(module.exports={HOTP,NobleCryptoPlugin,OTP,ScureBase32Plugin,TOTP,generate,generateSecret,generateSync,generateURI,verify,verifySync,wrapResult,wrapResultAsync});
2
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts","../src/functional.ts","../src/defaults.ts","../src/class.ts"],"sourcesContent":["export type {\n OTPAuthOptions,\n TOTPOptions,\n OTPGenerateOptions as OTPFunctionalOptions,\n OTPVerifyOptions as OTPVerifyFunctionalOptions,\n} from \"./types\";\n\nexport {\n generateSecret,\n generateURI,\n generate,\n generateSync,\n verify,\n verifySync,\n type OTPStrategy,\n} from \"./functional\";\n\nexport {\n OTP,\n type OTPClassOptions,\n type OTPGenerateOptions,\n type OTPVerifyOptions,\n type OTPURIGenerateOptions,\n} from \"./class\";\n\nexport type { Base32Plugin, CryptoPlugin, HashAlgorithm, OTPResult } from \"@otplib/core\";\nexport type { VerifyResult } from \"@otplib/totp\";\n\nexport { HOTP } from \"@otplib/hotp\";\nexport { TOTP } from \"@otplib/totp\";\n\n// Result wrapping utilities\nexport { wrapResult, wrapResultAsync } from \"@otplib/core\";\n\n// Default Plugins\nexport { NobleCryptoPlugin } from \"@otplib/plugin-crypto-noble\";\nexport { ScureBase32Plugin } from \"@otplib/plugin-base32-scure\";\n","import { generateSecret as generateSecretCore, ConfigurationError } from \"@otplib/core\";\nimport {\n generate as generateHOTP,\n generateSync as generateHOTPSync,\n verify as verifyHOTP,\n verifySync as verifyHOTPSync,\n} from \"@otplib/hotp\";\nimport {\n generate as generateTOTP,\n generateSync as generateTOTPSync,\n verify as verifyTOTP,\n verifySync as verifyTOTPSync,\n} from \"@otplib/totp\";\nimport { generateTOTP as generateTOTPURI } from \"@otplib/uri\";\n\nimport {\n defaultCrypto,\n defaultBase32,\n normalizeGenerateOptions,\n normalizeVerifyOptions,\n} from \"./defaults\";\n\nimport type { OTPGenerateOptions, OTPVerifyOptions, OTPStrategy, StrategyHandlers } from \"./types\";\nimport type { CryptoPlugin, Base32Plugin, Digits, HashAlgorithm } from \"@otplib/core\";\nimport type { VerifyResult as HOTPVerifyResult } from \"@otplib/hotp\";\nimport type { VerifyResult as TOTPVerifyResult } from \"@otplib/totp\";\n\nexport type { OTPStrategy };\n\nexport type VerifyResult = TOTPVerifyResult | HOTPVerifyResult;\n\nfunction executeByStrategy<T>(\n strategy: OTPStrategy,\n counter: number | undefined,\n handlers: StrategyHandlers<T>,\n): T {\n if (strategy === \"totp\") {\n return handlers.totp();\n }\n if (strategy === \"hotp\") {\n if (counter === undefined) {\n throw new ConfigurationError(\n \"Counter is required for HOTP strategy. Example: { strategy: 'hotp', counter: 0 }\",\n );\n }\n return handlers.hotp(counter);\n }\n throw new ConfigurationError(\n `Unknown OTP strategy: ${strategy}. Valid strategies are 'totp' or 'hotp'.`,\n );\n}\n\n/**\n * Generate a random secret key for use with OTP\n *\n * The secret is encoded in Base32 format for compatibility with\n * Google Authenticator and other authenticator apps.\n *\n * @param options - Secret generation options\n * @returns Base32-encoded secret key\n *\n * @example\n * ```ts\n * import { generateSecret } from 'otplib';\n *\n * const secret = generateSecret();\n * // Returns: 'JBSWY3DPEHPK3PXP'\n * ```\n *\n * @example With custom plugins\n * ```ts\n * import { generateSecret, NodeCryptoPlugin } from 'otplib';\n *\n * const secret = generateSecret({\n * crypto: new NodeCryptoPlugin(),\n * });\n * ```\n */\nexport function generateSecret(options?: {\n /**\n * Number of random bytes to generate (default: 20)\n * 20 bytes = 160 bits, which provides a good security margin\n */\n length?: number;\n\n /**\n * Crypto plugin to use (default: NobleCryptoPlugin)\n */\n crypto?: CryptoPlugin;\n\n /**\n * Base32 plugin to use (default: ScureBase32Plugin)\n */\n base32?: Base32Plugin;\n}): string {\n const { crypto = defaultCrypto, base32 = defaultBase32, length = 20 } = options || {};\n\n return generateSecretCore({ crypto, base32, length });\n}\n\n/**\n * Generate an otpauth:// URI for QR code generation\n *\n * This URI can be used to generate a QR code that can be scanned\n * by Google Authenticator and other authenticator apps.\n *\n * @param options - URI generation options\n * @returns otpauth:// URI string\n *\n * @example\n * ```ts\n * import { generateURI } from 'otplib';\n *\n * const uri = generateURI({\n * issuer: 'ACME Co',\n * label: 'john@example.com',\n * secret: 'JBSWY3DPEHPK3PXP',\n * });\n * // Returns: 'otpauth://totp/ACME%20Co:john%40example.com?secret=...'\n * ```\n */\nexport function generateURI(options: {\n issuer: string;\n label: string;\n secret: string;\n algorithm?: HashAlgorithm;\n digits?: Digits;\n period?: number;\n}): string {\n const { issuer, label, secret, algorithm = \"sha1\", digits = 6, period = 30 } = options;\n return generateTOTPURI({ issuer, label, secret, algorithm, digits, period });\n}\n\n/**\n * Generate an OTP code\n *\n * Generates a one-time password based on the specified strategy.\n * - 'totp': Time-based OTP (default)\n * - 'hotp': HMAC-based OTP\n *\n * @param options - OTP generation options\n * @returns OTP code\n *\n * @example TOTP\n * ```ts\n * import { generate } from 'otplib';\n *\n * const token = await generate({\n * secret: 'JBSWY3DPEHPK3PXP',\n * });\n * // Returns: '123456'\n * ```\n *\n * @example HOTP\n * ```ts\n * import { generate } from 'otplib';\n *\n * const token = await generate({\n * secret: 'JBSWY3DPEHPK3PXP',\n * strategy: 'hotp',\n * counter: 0,\n * });\n * ```\n *\n * @example With custom plugins\n * ```ts\n * import { generate, NodeCryptoPlugin } from 'otplib';\n *\n * const token = await generate({\n * secret: 'JBSWY3DPEHPK3PXP',\n * crypto: new NodeCryptoPlugin(),\n * });\n * ```\n */\nexport async function generate(options: OTPGenerateOptions): Promise<string> {\n const opts = normalizeGenerateOptions(options);\n const { secret, crypto, base32, algorithm, digits } = opts;\n const commonOptions = { secret, crypto, base32, algorithm, digits };\n\n return executeByStrategy(opts.strategy, opts.counter, {\n totp: () =>\n generateTOTP({\n ...commonOptions,\n period: opts.period,\n epoch: opts.epoch,\n t0: opts.t0,\n }),\n hotp: (counter) =>\n generateHOTP({\n ...commonOptions,\n counter,\n }),\n });\n}\n\n/**\n * Generate an OTP code synchronously\n *\n * This is the synchronous version of {@link generate}. It requires a crypto\n * plugin that supports synchronous HMAC operations.\n *\n * @param options - OTP generation options\n * @returns OTP code\n * @throws {HMACError} If the crypto plugin doesn't support sync operations\n *\n * @example\n * ```ts\n * import { generateSync } from 'otplib';\n *\n * const token = generateSync({\n * secret: 'JBSWY3DPEHPK3PXP',\n * });\n * ```\n */\nexport function generateSync(options: OTPGenerateOptions): string {\n const opts = normalizeGenerateOptions(options);\n const { secret, crypto, base32, algorithm, digits } = opts;\n const commonOptions = { secret, crypto, base32, algorithm, digits };\n\n return executeByStrategy(opts.strategy, opts.counter, {\n totp: () =>\n generateTOTPSync({\n ...commonOptions,\n period: opts.period,\n epoch: opts.epoch,\n t0: opts.t0,\n }),\n hotp: (counter) =>\n generateHOTPSync({\n ...commonOptions,\n counter,\n }),\n });\n}\n\n/**\n * Verify an OTP code\n *\n * Verifies a provided OTP code against the expected value based on the strategy.\n * - 'totp': Time-based OTP (default, Google Authenticator compatible)\n * - 'hotp': HMAC-based OTP\n *\n * Uses constant-time comparison to prevent timing attacks.\n *\n * @param options - OTP verification options\n * @returns Verification result with validity and optional delta\n *\n * @example TOTP\n * ```ts\n * import { verify } from 'otplib';\n *\n * const result = await verify({\n * secret: 'JBSWY3DPEHPK3PXP',\n * token: '123456',\n * });\n * // Returns: { valid: true, delta: 0 }\n * ```\n *\n * @example HOTP\n * ```ts\n * import { verify } from 'otplib';\n *\n * const result = await verify({\n * secret: 'JBSWY3DPEHPK3PXP',\n * token: '123456',\n * strategy: 'hotp',\n * counter: 0,\n * });\n * ```\n *\n * @example With epochTolerance for TOTP\n * ```ts\n * import { verify, NodeCryptoPlugin } from 'otplib';\n *\n * const result = await verify({\n * secret: 'JBSWY3DPEHPK3PXP',\n * token: '123456',\n * epochTolerance: 30,\n * crypto: new NodeCryptoPlugin(),\n * });\n * ```\n */\nexport async function verify(options: OTPVerifyOptions): Promise<VerifyResult> {\n const opts = normalizeVerifyOptions(options);\n const { secret, token, crypto, base32, algorithm, digits } = opts;\n const commonOptions = { secret, token, crypto, base32, algorithm, digits };\n\n return executeByStrategy(opts.strategy, opts.counter, {\n totp: () =>\n verifyTOTP({\n ...commonOptions,\n period: opts.period,\n epoch: opts.epoch,\n t0: opts.t0,\n epochTolerance: opts.epochTolerance,\n }),\n hotp: (counter) =>\n verifyHOTP({\n ...commonOptions,\n counter,\n counterTolerance: opts.counterTolerance,\n }),\n });\n}\n\n/**\n * Verify an OTP code synchronously\n *\n * This is the synchronous version of {@link verify}. It requires a crypto\n * plugin that supports synchronous HMAC operations.\n *\n * @param options - OTP verification options\n * @returns Verification result with validity and optional delta\n * @throws {HMACError} If the crypto plugin doesn't support sync operations\n *\n * @example\n * ```ts\n * import { verifySync } from 'otplib';\n *\n * const result = verifySync({\n * secret: 'JBSWY3DPEHPK3PXP',\n * token: '123456',\n * });\n * ```\n */\nexport function verifySync(options: OTPVerifyOptions): VerifyResult {\n const opts = normalizeVerifyOptions(options);\n const { secret, token, crypto, base32, algorithm, digits } = opts;\n const commonOptions = { secret, token, crypto, base32, algorithm, digits };\n\n return executeByStrategy(opts.strategy, opts.counter, {\n totp: () =>\n verifyTOTPSync({\n ...commonOptions,\n period: opts.period,\n epoch: opts.epoch,\n t0: opts.t0,\n epochTolerance: opts.epochTolerance,\n }),\n hotp: (counter) =>\n verifyHOTPSync({\n ...commonOptions,\n counter,\n counterTolerance: opts.counterTolerance,\n }),\n });\n}\n","/**\n * Default plugin instances\n *\n * Shared across functional and class APIs to ensure singleton behavior\n * and reduce memory overhead.\n */\nimport { ScureBase32Plugin } from \"@otplib/plugin-base32-scure\";\nimport { NobleCryptoPlugin } from \"@otplib/plugin-crypto-noble\";\n\nimport type {\n OTPGenerateOptions,\n OTPVerifyOptions,\n OTPGenerateOptionsWithDefaults,\n OTPVerifyOptionsWithDefaults,\n} from \"./types\";\n\n/**\n * Default crypto plugin instance (Noble Hashes)\n *\n * This plugin provides cross-platform cryptographic operations\n * using the @noble/hashes library.\n */\nexport const defaultCrypto = Object.freeze(new NobleCryptoPlugin());\n\n/**\n * Default Base32 plugin instance (@scure/base)\n *\n * This plugin provides Base32 encoding/decoding operations\n * using the @scure/base library.\n */\nexport const defaultBase32 = Object.freeze(new ScureBase32Plugin());\n\nexport function normalizeGenerateOptions(\n options: OTPGenerateOptions,\n): OTPGenerateOptionsWithDefaults {\n return {\n secret: options.secret,\n strategy: options.strategy ?? \"totp\",\n crypto: options.crypto ?? defaultCrypto,\n base32: options.base32 ?? defaultBase32,\n algorithm: options.algorithm ?? \"sha1\",\n digits: options.digits ?? 6,\n period: options.period ?? 30,\n epoch: options.epoch ?? Math.floor(Date.now() / 1000),\n t0: options.t0 ?? 0,\n counter: options.counter,\n };\n}\n\nexport function normalizeVerifyOptions(options: OTPVerifyOptions): OTPVerifyOptionsWithDefaults {\n return {\n ...normalizeGenerateOptions(options),\n token: options.token,\n epochTolerance: options.epochTolerance ?? 0,\n counterTolerance: options.counterTolerance ?? 0,\n };\n}\n","/**\n * OTP Wrapper Class\n *\n * A unified class that dynamically handles TOTP and HOTP strategies.\n */\n\nimport { generateSecret as generateSecretCore } from \"@otplib/core\";\nimport { generateTOTP as generateTOTPURI } from \"@otplib/uri\";\n\nimport { defaultCrypto, defaultBase32 } from \"./defaults\";\nimport {\n generate as functionalGenerate,\n generateSync as functionalGenerateSync,\n verify as functionalVerify,\n verifySync as functionalVerifySync,\n} from \"./functional\";\n\nimport type { OTPStrategy } from \"./functional\";\nimport type { CryptoPlugin, Digits, HashAlgorithm, Base32Plugin } from \"@otplib/core\";\nimport type { VerifyResult as HOTPVerifyResult } from \"@otplib/hotp\";\nimport type { VerifyResult as TOTPVerifyResult } from \"@otplib/totp\";\n\n/**\n * Combined verify result that works for both TOTP and HOTP\n */\nexport type VerifyResult = TOTPVerifyResult | HOTPVerifyResult;\n\n/**\n * Options for the OTP class\n */\nexport type OTPClassOptions = {\n /**\n * OTP strategy to use\n * - 'totp': Time-based OTP (default)\n * - 'hotp': HMAC-based OTP\n */\n strategy?: OTPStrategy;\n\n /**\n * Crypto plugin to use (default: NobleCryptoPlugin)\n */\n crypto?: CryptoPlugin;\n\n /**\n * Base32 plugin to use (default: ScureBase32Plugin)\n */\n base32?: Base32Plugin;\n};\n\n/**\n * Options for generating a token with the OTP class\n */\nexport type OTPGenerateOptions = {\n /**\n * Base32-encoded secret key\n */\n secret: string;\n\n /**\n * Hash algorithm (default: 'sha1')\n */\n algorithm?: HashAlgorithm;\n\n /**\n * Number of digits (default: 6)\n */\n digits?: Digits;\n\n /**\n * Current Unix epoch timestamp in seconds (default: now)\n * Used by TOTP strategy\n */\n epoch?: number;\n\n /**\n * Initial Unix time to start counting time steps (default: 0)\n * Used by TOTP strategy\n */\n t0?: number;\n\n /**\n * Time step in seconds (default: 30)\n * Used by TOTP strategy\n */\n period?: number;\n\n /**\n * Counter value\n * Used by HOTP strategy (required)\n */\n counter?: number;\n};\n\n/**\n * Options for verifying a token with the OTP class\n */\nexport type OTPVerifyOptions = {\n /**\n * Base32-encoded secret key\n */\n secret: string;\n\n /**\n * OTP code to verify\n */\n token: string;\n\n /**\n * Hash algorithm (default: 'sha1')\n */\n algorithm?: HashAlgorithm;\n\n /**\n * Number of digits (default: 6)\n */\n digits?: Digits;\n\n /**\n * Current Unix epoch timestamp in seconds (default: now)\n * Used by TOTP strategy\n */\n epoch?: number;\n\n /**\n * Initial Unix time to start counting time steps (default: 0)\n * Used by TOTP strategy\n */\n t0?: number;\n\n /**\n * Time step in seconds (default: 30)\n * Used by TOTP strategy\n */\n period?: number;\n\n /**\n * Counter value\n * Used by HOTP strategy (required)\n */\n counter?: number;\n\n /**\n * Time tolerance in seconds for TOTP verification (default: 0)\n * - Number: symmetric tolerance (same for past and future)\n * - Tuple [past, future]: asymmetric tolerance\n * Use [5, 0] for RFC-compliant past-only verification.\n */\n epochTolerance?: number | [number, number];\n\n /**\n * Counter tolerance for HOTP verification (default: 0)\n * - Number: symmetric look-ahead window\n * - Array: asymmetric window\n */\n counterTolerance?: number | number[];\n};\n\n/**\n * Options for generating URI with the OTP class\n */\nexport type OTPURIGenerateOptions = {\n /**\n * Issuer name (e.g., 'ACME Co')\n */\n issuer: string;\n\n /**\n * Label/Account name (e.g., 'john@example.com')\n */\n label: string;\n\n /**\n * Base32-encoded secret key\n */\n secret: string;\n\n /**\n * Hash algorithm (default: 'sha1')\n */\n algorithm?: HashAlgorithm;\n\n /**\n * Number of digits (default: 6)\n */\n digits?: Digits;\n\n /**\n * Time step in seconds (default: 30)\n */\n period?: number;\n};\n\n/**\n * OTP Class\n *\n * A wrapper class that dynamically handles TOTP and HOTP strategies.\n *\n * @example\n * ```ts\n * import { OTP } from 'otplib';\n *\n * // Create OTP instance with TOTP strategy (default)\n * const otp = new OTP({ strategy: 'totp' });\n *\n * // Generate and verify\n * const secret = otp.generateSecret();\n * const token = await otp.generate({ secret });\n * const result = await otp.verify({ secret, token });\n * ```\n *\n * @example With HOTP strategy\n * ```ts\n * import { OTP } from 'otplib';\n *\n * const otp = new OTP({ strategy: 'hotp' });\n * const token = await otp.generate({ secret: 'ABC123', counter: 0 });\n * ```\n *\n * @example Generating otpauth:// URI for authenticator apps\n * ```ts\n * import { OTP } from 'otplib';\n *\n * const otp = new OTP({ strategy: 'totp' });\n * const uri = otp.generateURI({\n * issuer: 'MyApp',\n * label: 'user@example.com',\n * secret: 'ABC123',\n * });\n * ```\n */\nexport class OTP {\n private readonly strategy: OTPStrategy;\n private readonly crypto: CryptoPlugin;\n private readonly base32: Base32Plugin;\n\n constructor(options: OTPClassOptions = {}) {\n const { strategy = \"totp\", crypto = defaultCrypto, base32 = defaultBase32 } = options;\n\n this.strategy = strategy;\n this.crypto = crypto;\n this.base32 = base32;\n }\n\n /**\n * Get the current strategy\n */\n getStrategy(): OTPStrategy {\n return this.strategy;\n }\n\n /**\n * Generate a random secret key\n *\n * @param length - Number of random bytes (default: 20)\n * @returns Base32-encoded secret key\n */\n generateSecret(length: number = 20): string {\n return generateSecretCore({ crypto: this.crypto, base32: this.base32, length });\n }\n\n /**\n * Generate an OTP token based on the configured strategy\n *\n * @param options - Generation options\n * @returns OTP code\n */\n async generate(options: OTPGenerateOptions): Promise<string> {\n return functionalGenerate({\n ...options,\n strategy: this.strategy,\n crypto: this.crypto,\n base32: this.base32,\n });\n }\n\n /**\n * Generate an OTP token based on the configured strategy synchronously\n *\n * @param options - Generation options\n * @returns OTP code\n * @throws {HMACError} If the crypto plugin doesn't support sync operations\n */\n generateSync(options: OTPGenerateOptions): string {\n return functionalGenerateSync({\n ...options,\n strategy: this.strategy,\n crypto: this.crypto,\n base32: this.base32,\n });\n }\n\n /**\n * Verify an OTP token based on the configured strategy\n *\n * @param options - Verification options\n * @returns Verification result with validity and optional delta\n */\n async verify(options: OTPVerifyOptions): Promise<VerifyResult> {\n return functionalVerify({\n ...options,\n strategy: this.strategy,\n crypto: this.crypto,\n base32: this.base32,\n });\n }\n\n /**\n * Verify an OTP token based on the configured strategy synchronously\n *\n * @param options - Verification options\n * @returns Verification result with validity and optional delta\n * @throws {HMACError} If the crypto plugin doesn't support sync operations\n */\n verifySync(options: OTPVerifyOptions): VerifyResult {\n return functionalVerifySync({\n ...options,\n strategy: this.strategy,\n crypto: this.crypto,\n base32: this.base32,\n });\n }\n\n /**\n * Generate an otpauth:// URI for QR code generation\n *\n * Only available for TOTP strategy.\n *\n * @param options - URI generation options\n * @returns otpauth:// URI string\n */\n generateURI(options: OTPURIGenerateOptions): string {\n if (this.strategy === \"hotp\") {\n throw new Error(\"generateURI is not available for HOTP strategy\");\n }\n\n const { issuer, label, secret, algorithm = \"sha1\", digits = 6, period = 30 } = options;\n\n return generateTOTPURI({\n issuer,\n label,\n secret,\n algorithm,\n digits,\n period,\n });\n }\n}\n"],"mappings":"yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,mEAAAE,EAAA,uEAAAC,EAAA,mBAAAC,EAAA,iBAAAC,EAAA,gBAAAC,EAAA,WAAAC,EAAA,eAAAC,EAAA,mFAAAC,EAAAT,GCAA,IAAAU,EAAyE,wBACzEC,EAKO,wBACPC,EAKO,wBACPC,EAAgD,uBCPhD,IAAAC,EAAkC,uCAClCC,EAAkC,uCAerBC,EAAgB,OAAO,OAAO,IAAI,mBAAmB,EAQrDC,EAAgB,OAAO,OAAO,IAAI,mBAAmB,EAE3D,SAASC,EACdC,EACgC,CAChC,MAAO,CACL,OAAQA,EAAQ,OAChB,SAAUA,EAAQ,UAAY,OAC9B,OAAQA,EAAQ,QAAUH,EAC1B,OAAQG,EAAQ,QAAUF,EAC1B,UAAWE,EAAQ,WAAa,OAChC,OAAQA,EAAQ,QAAU,EAC1B,OAAQA,EAAQ,QAAU,GAC1B,MAAOA,EAAQ,OAAS,KAAK,MAAM,KAAK,IAAI,EAAI,GAAI,EACpD,GAAIA,EAAQ,IAAM,EAClB,QAASA,EAAQ,OACnB,CACF,CAEO,SAASC,EAAuBD,EAAyD,CAC9F,MAAO,CACL,GAAGD,EAAyBC,CAAO,EACnC,MAAOA,EAAQ,MACf,eAAgBA,EAAQ,gBAAkB,EAC1C,iBAAkBA,EAAQ,kBAAoB,CAChD,CACF,CDzBA,SAASE,EACPC,EACAC,EACAC,EACG,CACH,GAAIF,IAAa,OACf,OAAOE,EAAS,KAAK,EAEvB,GAAI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ACH,CAUA,YAAYA,EAAwC,CAClD,GAAI,KAAK,WAAa,OACpB,MAAM,IAAI,MAAM,gDAAgD,EAGlE,GAAM,CAAE,OAAAY,EAAQ,MAAAC,EAAO,OAAAC,EAAQ,UAAAC,EAAY,OAAQ,OAAAC,EAAS,EAAG,OAAAC,EAAS,EAAG,EAAIjB,EAE/E,SAAO,EAAAkB,cAAgB,CACrB,OAAAN,EACA,MAAAC,EACA,OAAAC,EACA,UAAAC,EACA,OAAAC,EACA,OAAAC,CACF,CAAC,CACH,CACF,EH9TA,IAAAE,EAAqB,wBACrBC,EAAqB,wBAGrBC,EAA4C,wBAG5CC,EAAkC,uCAClCC,EAAkC","names":["src_exports","__export","OTP","generate","generateSecret","generateSync","generateURI","verify","verifySync","__toCommonJS","import_core","import_hotp","import_totp","import_uri","import_plugin_base32_scure","import_plugin_crypto_noble","defaultCrypto","defaultBase32","normalizeGenerateOptions","options","normalizeVerifyOptions","executeByStrategy","strategy","counter","handlers","generateSecret","options","crypto","defaultCrypto","base32","defaultBase32","length","generateSecretCore","generateURI","issuer","label","secret","algorithm","digits","period","generateTOTPURI","generate","opts","normalizeGenerateOptions","commonOptions","generateTOTP","generateHOTP","generateSync","generateTOTPSync","generateHOTPSync","verify","normalizeVerifyOptions","token","verifyTOTP","verifyHOTP","verifySync","verifyTOTPSync","verifyHOTPSync","import_core","import_uri","OTP","options","strategy","crypto","defaultCrypto","base32","defaultBase32","length","generateSecretCore","generate","generateSync","verify","verifySync","issuer","label","secret","algorithm","digits","period","generateTOTPURI","import_hotp","import_totp","import_core","import_plugin_crypto_noble","import_plugin_base32_scure"]}
@@ -0,0 +1,8 @@
1
+ export { O as OTPAuthOptions, a as OTPFunctionalOptions, c as OTPStrategy, b as OTPVerifyFunctionalOptions } from './types-D1FZb7MW.cjs';
2
+ export { generate, generateSecret, generateSync, generateURI, verify, verifySync } from './functional.cjs';
3
+ export { OTP, OTPClassOptions, OTPGenerateOptions, OTPURIGenerateOptions, OTPVerifyOptions } from './class.cjs';
4
+ export { Base32Plugin, CryptoPlugin, HashAlgorithm, OTPResult, wrapResult, wrapResultAsync } from '@otplib/core';
5
+ export { TOTP, TOTPOptions, VerifyResult } from '@otplib/totp';
6
+ export { HOTP } from '@otplib/hotp';
7
+ export { NobleCryptoPlugin } from '@otplib/plugin-crypto-noble';
8
+ export { ScureBase32Plugin } from '@otplib/plugin-base32-scure';
@@ -0,0 +1,8 @@
1
+ export { O as OTPAuthOptions, a as OTPFunctionalOptions, c as OTPStrategy, b as OTPVerifyFunctionalOptions } from './types-D1FZb7MW.js';
2
+ export { generate, generateSecret, generateSync, generateURI, verify, verifySync } from './functional.js';
3
+ export { OTP, OTPClassOptions, OTPGenerateOptions, OTPURIGenerateOptions, OTPVerifyOptions } from './class.js';
4
+ export { Base32Plugin, CryptoPlugin, HashAlgorithm, OTPResult, wrapResult, wrapResultAsync } from '@otplib/core';
5
+ export { TOTP, TOTPOptions, VerifyResult } from '@otplib/totp';
6
+ export { HOTP } from '@otplib/hotp';
7
+ export { NobleCryptoPlugin } from '@otplib/plugin-crypto-noble';
8
+ export { ScureBase32Plugin } from '@otplib/plugin-base32-scure';
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ import{generateSecret as V,ConfigurationError as b}from"@otplib/core";import{generate as R,generateSync as x,verify as v,verifySync as H}from"@otplib/hotp";import{generate as G,generateSync as C,verify as w,verifySync as B}from"@otplib/totp";import{generateTOTP as D}from"@otplib/uri";import{ScureBase32Plugin as d}from"@otplib/plugin-base32-scure";import{NobleCryptoPlugin as S}from"@otplib/plugin-crypto-noble";var y=Object.freeze(new S),c=Object.freeze(new d);function g(t){return{secret:t.secret,strategy:t.strategy??"totp",crypto:t.crypto??y,base32:t.base32??c,algorithm:t.algorithm??"sha1",digits:t.digits??6,period:t.period??30,epoch:t.epoch??Math.floor(Date.now()/1e3),t0:t.t0??0,counter:t.counter}}function f(t){return{...g(t),token:t.token,epochTolerance:t.epochTolerance??0,counterTolerance:t.counterTolerance??0}}function u(t,e,r){if(t==="totp")return r.totp();if(t==="hotp"){if(e===void 0)throw new b("Counter is required for HOTP strategy. Example: { strategy: 'hotp', counter: 0 }");return r.hotp(e)}throw new b(`Unknown OTP strategy: ${t}. Valid strategies are 'totp' or 'hotp'.`)}function U(t){let{crypto:e=y,base32:r=c,length:o=20}=t||{};return V({crypto:e,base32:r,length:o})}function A(t){let{issuer:e,label:r,secret:o,algorithm:n="sha1",digits:i=6,period:s=30}=t;return D({issuer:e,label:r,secret:o,algorithm:n,digits:i,period:s})}async function O(t){let e=g(t),{secret:r,crypto:o,base32:n,algorithm:i,digits:s}=e,a={secret:r,crypto:o,base32:n,algorithm:i,digits:s};return u(e.strategy,e.counter,{totp:()=>G({...a,period:e.period,epoch:e.epoch,t0:e.t0}),hotp:p=>R({...a,counter:p})})}function m(t){let e=g(t),{secret:r,crypto:o,base32:n,algorithm:i,digits:s}=e,a={secret:r,crypto:o,base32:n,algorithm:i,digits:s};return u(e.strategy,e.counter,{totp:()=>C({...a,period:e.period,epoch:e.epoch,t0:e.t0}),hotp:p=>x({...a,counter:p})})}async function T(t){let e=f(t),{secret:r,token:o,crypto:n,base32:i,algorithm:s,digits:a}=e,p={secret:r,token:o,crypto:n,base32:i,algorithm:s,digits:a};return u(e.strategy,e.counter,{totp:()=>w({...p,period:e.period,epoch:e.epoch,t0:e.t0,epochTolerance:e.epochTolerance}),hotp:l=>v({...p,counter:l,counterTolerance:e.counterTolerance})})}function h(t){let e=f(t),{secret:r,token:o,crypto:n,base32:i,algorithm:s,digits:a}=e,p={secret:r,token:o,crypto:n,base32:i,algorithm:s,digits:a};return u(e.strategy,e.counter,{totp:()=>B({...p,period:e.period,epoch:e.epoch,t0:e.t0,epochTolerance:e.epochTolerance}),hotp:l=>H({...p,counter:l,counterTolerance:e.counterTolerance})})}import{generateSecret as I}from"@otplib/core";import{generateTOTP as k}from"@otplib/uri";var P=class{strategy;crypto;base32;constructor(e={}){let{strategy:r="totp",crypto:o=y,base32:n=c}=e;this.strategy=r,this.crypto=o,this.base32=n}getStrategy(){return this.strategy}generateSecret(e=20){return I({crypto:this.crypto,base32:this.base32,length:e})}async generate(e){return O({...e,strategy:this.strategy,crypto:this.crypto,base32:this.base32})}generateSync(e){return m({...e,strategy:this.strategy,crypto:this.crypto,base32:this.base32})}async verify(e){return T({...e,strategy:this.strategy,crypto:this.crypto,base32:this.base32})}verifySync(e){return h({...e,strategy:this.strategy,crypto:this.crypto,base32:this.base32})}generateURI(e){if(this.strategy==="hotp")throw new Error("generateURI is not available for HOTP strategy");let{issuer:r,label:o,secret:n,algorithm:i="sha1",digits:s=6,period:a=30}=e;return k({issuer:r,label:o,secret:n,algorithm:i,digits:s,period:a})}};import{HOTP as te}from"@otplib/hotp";import{TOTP as oe}from"@otplib/totp";import{wrapResult as ie,wrapResultAsync as se}from"@otplib/core";import{NobleCryptoPlugin as pe}from"@otplib/plugin-crypto-noble";import{ScureBase32Plugin as ce}from"@otplib/plugin-base32-scure";export{te as HOTP,pe as NobleCryptoPlugin,P as OTP,ce as ScureBase32Plugin,oe as TOTP,O as generate,U as generateSecret,m as generateSync,A as generateURI,T as verify,h as verifySync,ie as wrapResult,se as wrapResultAsync};
2
+ //# sourceMappingURL=index.js.map