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.
- package/LICENSE +2 -2
- package/README.md +64 -502
- package/dist/class.cjs +2 -0
- package/dist/class.cjs.map +1 -0
- package/dist/class.d.cts +250 -0
- package/dist/class.d.ts +250 -0
- package/dist/class.js +2 -0
- package/dist/class.js.map +1 -0
- package/dist/functional.cjs +2 -0
- package/dist/functional.cjs.map +1 -0
- package/dist/functional.d.cts +210 -0
- package/dist/functional.d.ts +210 -0
- package/dist/functional.js +2 -0
- package/dist/functional.js.map +1 -0
- package/dist/index.cjs +2 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +8 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/dist/types-D1FZb7MW.d.cts +102 -0
- package/dist/types-D1FZb7MW.d.ts +102 -0
- package/package.json +69 -25
- package/core.d.ts +0 -1
- package/core.js +0 -23
- package/index.d.ts +0 -1
- package/index.js +0 -23
- package/v11.d.ts +0 -1
- package/v11.js +0 -23
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/class.ts","../src/defaults.ts","../src/functional.ts"],"sourcesContent":["/**\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","/**\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","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"],"mappings":"yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,SAAAE,IAAA,eAAAC,EAAAH,GAMA,IAAAI,EAAqD,wBACrDC,EAAgD,uBCDhD,IAAAC,EAAkC,uCAClCC,EAAkC,uCAerBC,EAAgB,OAAO,OAAO,IAAI,mBAAmB,EAQrDC,EAAgB,OAAO,OA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,CAClD,OAAOW,EAAqB,CAC1B,GAAGX,EACH,SAAU,KAAK,SACf,OAAQ,KAAK,OACb,OAAQ,KAAK,MACf,CAAC,CACH,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","names":["class_exports","__export","OTP","__toCommonJS","import_core","import_uri","import_plugin_base32_scure","import_plugin_crypto_noble","defaultCrypto","defaultBase32","normalizeGenerateOptions","options","normalizeVerifyOptions","import_core","import_hotp","import_totp","import_uri","executeByStrategy","strategy","counter","handlers","generate","options","opts","normalizeGenerateOptions","secret","crypto","base32","algorithm","digits","commonOptions","executeByStrategy","generateTOTP","counter","generateHOTP","generateSync","generateTOTPSync","generateHOTPSync","verify","normalizeVerifyOptions","token","verifyTOTP","verifyHOTP","verifySync","verifyTOTPSync","verifyHOTPSync","OTP","options","strategy","crypto","defaultCrypto","base32","defaultBase32","length","generateSecretCore","generate","generateSync","verify","verifySync","issuer","label","secret","algorithm","digits","period","generateTOTPURI"]}
|
package/dist/class.d.cts
ADDED
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
import { c as OTPStrategy } from './types-D1FZb7MW.cjs';
|
|
2
|
+
import { CryptoPlugin, Base32Plugin, HashAlgorithm, Digits } from '@otplib/core';
|
|
3
|
+
import { VerifyResult as VerifyResult$2 } from '@otplib/hotp';
|
|
4
|
+
import { VerifyResult as VerifyResult$1 } from '@otplib/totp';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* OTP Wrapper Class
|
|
8
|
+
*
|
|
9
|
+
* A unified class that dynamically handles TOTP and HOTP strategies.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Combined verify result that works for both TOTP and HOTP
|
|
14
|
+
*/
|
|
15
|
+
type VerifyResult = VerifyResult$1 | VerifyResult$2;
|
|
16
|
+
/**
|
|
17
|
+
* Options for the OTP class
|
|
18
|
+
*/
|
|
19
|
+
type OTPClassOptions = {
|
|
20
|
+
/**
|
|
21
|
+
* OTP strategy to use
|
|
22
|
+
* - 'totp': Time-based OTP (default)
|
|
23
|
+
* - 'hotp': HMAC-based OTP
|
|
24
|
+
*/
|
|
25
|
+
strategy?: OTPStrategy;
|
|
26
|
+
/**
|
|
27
|
+
* Crypto plugin to use (default: NobleCryptoPlugin)
|
|
28
|
+
*/
|
|
29
|
+
crypto?: CryptoPlugin;
|
|
30
|
+
/**
|
|
31
|
+
* Base32 plugin to use (default: ScureBase32Plugin)
|
|
32
|
+
*/
|
|
33
|
+
base32?: Base32Plugin;
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* Options for generating a token with the OTP class
|
|
37
|
+
*/
|
|
38
|
+
type OTPGenerateOptions = {
|
|
39
|
+
/**
|
|
40
|
+
* Base32-encoded secret key
|
|
41
|
+
*/
|
|
42
|
+
secret: string;
|
|
43
|
+
/**
|
|
44
|
+
* Hash algorithm (default: 'sha1')
|
|
45
|
+
*/
|
|
46
|
+
algorithm?: HashAlgorithm;
|
|
47
|
+
/**
|
|
48
|
+
* Number of digits (default: 6)
|
|
49
|
+
*/
|
|
50
|
+
digits?: Digits;
|
|
51
|
+
/**
|
|
52
|
+
* Current Unix epoch timestamp in seconds (default: now)
|
|
53
|
+
* Used by TOTP strategy
|
|
54
|
+
*/
|
|
55
|
+
epoch?: number;
|
|
56
|
+
/**
|
|
57
|
+
* Initial Unix time to start counting time steps (default: 0)
|
|
58
|
+
* Used by TOTP strategy
|
|
59
|
+
*/
|
|
60
|
+
t0?: number;
|
|
61
|
+
/**
|
|
62
|
+
* Time step in seconds (default: 30)
|
|
63
|
+
* Used by TOTP strategy
|
|
64
|
+
*/
|
|
65
|
+
period?: number;
|
|
66
|
+
/**
|
|
67
|
+
* Counter value
|
|
68
|
+
* Used by HOTP strategy (required)
|
|
69
|
+
*/
|
|
70
|
+
counter?: number;
|
|
71
|
+
};
|
|
72
|
+
/**
|
|
73
|
+
* Options for verifying a token with the OTP class
|
|
74
|
+
*/
|
|
75
|
+
type OTPVerifyOptions = {
|
|
76
|
+
/**
|
|
77
|
+
* Base32-encoded secret key
|
|
78
|
+
*/
|
|
79
|
+
secret: string;
|
|
80
|
+
/**
|
|
81
|
+
* OTP code to verify
|
|
82
|
+
*/
|
|
83
|
+
token: string;
|
|
84
|
+
/**
|
|
85
|
+
* Hash algorithm (default: 'sha1')
|
|
86
|
+
*/
|
|
87
|
+
algorithm?: HashAlgorithm;
|
|
88
|
+
/**
|
|
89
|
+
* Number of digits (default: 6)
|
|
90
|
+
*/
|
|
91
|
+
digits?: Digits;
|
|
92
|
+
/**
|
|
93
|
+
* Current Unix epoch timestamp in seconds (default: now)
|
|
94
|
+
* Used by TOTP strategy
|
|
95
|
+
*/
|
|
96
|
+
epoch?: number;
|
|
97
|
+
/**
|
|
98
|
+
* Initial Unix time to start counting time steps (default: 0)
|
|
99
|
+
* Used by TOTP strategy
|
|
100
|
+
*/
|
|
101
|
+
t0?: number;
|
|
102
|
+
/**
|
|
103
|
+
* Time step in seconds (default: 30)
|
|
104
|
+
* Used by TOTP strategy
|
|
105
|
+
*/
|
|
106
|
+
period?: number;
|
|
107
|
+
/**
|
|
108
|
+
* Counter value
|
|
109
|
+
* Used by HOTP strategy (required)
|
|
110
|
+
*/
|
|
111
|
+
counter?: number;
|
|
112
|
+
/**
|
|
113
|
+
* Time tolerance in seconds for TOTP verification (default: 0)
|
|
114
|
+
* - Number: symmetric tolerance (same for past and future)
|
|
115
|
+
* - Tuple [past, future]: asymmetric tolerance
|
|
116
|
+
* Use [5, 0] for RFC-compliant past-only verification.
|
|
117
|
+
*/
|
|
118
|
+
epochTolerance?: number | [number, number];
|
|
119
|
+
/**
|
|
120
|
+
* Counter tolerance for HOTP verification (default: 0)
|
|
121
|
+
* - Number: symmetric look-ahead window
|
|
122
|
+
* - Array: asymmetric window
|
|
123
|
+
*/
|
|
124
|
+
counterTolerance?: number | number[];
|
|
125
|
+
};
|
|
126
|
+
/**
|
|
127
|
+
* Options for generating URI with the OTP class
|
|
128
|
+
*/
|
|
129
|
+
type OTPURIGenerateOptions = {
|
|
130
|
+
/**
|
|
131
|
+
* Issuer name (e.g., 'ACME Co')
|
|
132
|
+
*/
|
|
133
|
+
issuer: string;
|
|
134
|
+
/**
|
|
135
|
+
* Label/Account name (e.g., 'john@example.com')
|
|
136
|
+
*/
|
|
137
|
+
label: string;
|
|
138
|
+
/**
|
|
139
|
+
* Base32-encoded secret key
|
|
140
|
+
*/
|
|
141
|
+
secret: string;
|
|
142
|
+
/**
|
|
143
|
+
* Hash algorithm (default: 'sha1')
|
|
144
|
+
*/
|
|
145
|
+
algorithm?: HashAlgorithm;
|
|
146
|
+
/**
|
|
147
|
+
* Number of digits (default: 6)
|
|
148
|
+
*/
|
|
149
|
+
digits?: Digits;
|
|
150
|
+
/**
|
|
151
|
+
* Time step in seconds (default: 30)
|
|
152
|
+
*/
|
|
153
|
+
period?: number;
|
|
154
|
+
};
|
|
155
|
+
/**
|
|
156
|
+
* OTP Class
|
|
157
|
+
*
|
|
158
|
+
* A wrapper class that dynamically handles TOTP and HOTP strategies.
|
|
159
|
+
*
|
|
160
|
+
* @example
|
|
161
|
+
* ```ts
|
|
162
|
+
* import { OTP } from 'otplib';
|
|
163
|
+
*
|
|
164
|
+
* // Create OTP instance with TOTP strategy (default)
|
|
165
|
+
* const otp = new OTP({ strategy: 'totp' });
|
|
166
|
+
*
|
|
167
|
+
* // Generate and verify
|
|
168
|
+
* const secret = otp.generateSecret();
|
|
169
|
+
* const token = await otp.generate({ secret });
|
|
170
|
+
* const result = await otp.verify({ secret, token });
|
|
171
|
+
* ```
|
|
172
|
+
*
|
|
173
|
+
* @example With HOTP strategy
|
|
174
|
+
* ```ts
|
|
175
|
+
* import { OTP } from 'otplib';
|
|
176
|
+
*
|
|
177
|
+
* const otp = new OTP({ strategy: 'hotp' });
|
|
178
|
+
* const token = await otp.generate({ secret: 'ABC123', counter: 0 });
|
|
179
|
+
* ```
|
|
180
|
+
*
|
|
181
|
+
* @example Generating otpauth:// URI for authenticator apps
|
|
182
|
+
* ```ts
|
|
183
|
+
* import { OTP } from 'otplib';
|
|
184
|
+
*
|
|
185
|
+
* const otp = new OTP({ strategy: 'totp' });
|
|
186
|
+
* const uri = otp.generateURI({
|
|
187
|
+
* issuer: 'MyApp',
|
|
188
|
+
* label: 'user@example.com',
|
|
189
|
+
* secret: 'ABC123',
|
|
190
|
+
* });
|
|
191
|
+
* ```
|
|
192
|
+
*/
|
|
193
|
+
declare class OTP {
|
|
194
|
+
private readonly strategy;
|
|
195
|
+
private readonly crypto;
|
|
196
|
+
private readonly base32;
|
|
197
|
+
constructor(options?: OTPClassOptions);
|
|
198
|
+
/**
|
|
199
|
+
* Get the current strategy
|
|
200
|
+
*/
|
|
201
|
+
getStrategy(): OTPStrategy;
|
|
202
|
+
/**
|
|
203
|
+
* Generate a random secret key
|
|
204
|
+
*
|
|
205
|
+
* @param length - Number of random bytes (default: 20)
|
|
206
|
+
* @returns Base32-encoded secret key
|
|
207
|
+
*/
|
|
208
|
+
generateSecret(length?: number): string;
|
|
209
|
+
/**
|
|
210
|
+
* Generate an OTP token based on the configured strategy
|
|
211
|
+
*
|
|
212
|
+
* @param options - Generation options
|
|
213
|
+
* @returns OTP code
|
|
214
|
+
*/
|
|
215
|
+
generate(options: OTPGenerateOptions): Promise<string>;
|
|
216
|
+
/**
|
|
217
|
+
* Generate an OTP token based on the configured strategy synchronously
|
|
218
|
+
*
|
|
219
|
+
* @param options - Generation options
|
|
220
|
+
* @returns OTP code
|
|
221
|
+
* @throws {HMACError} If the crypto plugin doesn't support sync operations
|
|
222
|
+
*/
|
|
223
|
+
generateSync(options: OTPGenerateOptions): string;
|
|
224
|
+
/**
|
|
225
|
+
* Verify an OTP token based on the configured strategy
|
|
226
|
+
*
|
|
227
|
+
* @param options - Verification options
|
|
228
|
+
* @returns Verification result with validity and optional delta
|
|
229
|
+
*/
|
|
230
|
+
verify(options: OTPVerifyOptions): Promise<VerifyResult>;
|
|
231
|
+
/**
|
|
232
|
+
* Verify an OTP token based on the configured strategy synchronously
|
|
233
|
+
*
|
|
234
|
+
* @param options - Verification options
|
|
235
|
+
* @returns Verification result with validity and optional delta
|
|
236
|
+
* @throws {HMACError} If the crypto plugin doesn't support sync operations
|
|
237
|
+
*/
|
|
238
|
+
verifySync(options: OTPVerifyOptions): VerifyResult;
|
|
239
|
+
/**
|
|
240
|
+
* Generate an otpauth:// URI for QR code generation
|
|
241
|
+
*
|
|
242
|
+
* Only available for TOTP strategy.
|
|
243
|
+
*
|
|
244
|
+
* @param options - URI generation options
|
|
245
|
+
* @returns otpauth:// URI string
|
|
246
|
+
*/
|
|
247
|
+
generateURI(options: OTPURIGenerateOptions): string;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
export { OTP, type OTPClassOptions, type OTPGenerateOptions, type OTPURIGenerateOptions, type OTPVerifyOptions, type VerifyResult };
|
package/dist/class.d.ts
ADDED
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
import { c as OTPStrategy } from './types-D1FZb7MW.js';
|
|
2
|
+
import { CryptoPlugin, Base32Plugin, HashAlgorithm, Digits } from '@otplib/core';
|
|
3
|
+
import { VerifyResult as VerifyResult$2 } from '@otplib/hotp';
|
|
4
|
+
import { VerifyResult as VerifyResult$1 } from '@otplib/totp';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* OTP Wrapper Class
|
|
8
|
+
*
|
|
9
|
+
* A unified class that dynamically handles TOTP and HOTP strategies.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Combined verify result that works for both TOTP and HOTP
|
|
14
|
+
*/
|
|
15
|
+
type VerifyResult = VerifyResult$1 | VerifyResult$2;
|
|
16
|
+
/**
|
|
17
|
+
* Options for the OTP class
|
|
18
|
+
*/
|
|
19
|
+
type OTPClassOptions = {
|
|
20
|
+
/**
|
|
21
|
+
* OTP strategy to use
|
|
22
|
+
* - 'totp': Time-based OTP (default)
|
|
23
|
+
* - 'hotp': HMAC-based OTP
|
|
24
|
+
*/
|
|
25
|
+
strategy?: OTPStrategy;
|
|
26
|
+
/**
|
|
27
|
+
* Crypto plugin to use (default: NobleCryptoPlugin)
|
|
28
|
+
*/
|
|
29
|
+
crypto?: CryptoPlugin;
|
|
30
|
+
/**
|
|
31
|
+
* Base32 plugin to use (default: ScureBase32Plugin)
|
|
32
|
+
*/
|
|
33
|
+
base32?: Base32Plugin;
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* Options for generating a token with the OTP class
|
|
37
|
+
*/
|
|
38
|
+
type OTPGenerateOptions = {
|
|
39
|
+
/**
|
|
40
|
+
* Base32-encoded secret key
|
|
41
|
+
*/
|
|
42
|
+
secret: string;
|
|
43
|
+
/**
|
|
44
|
+
* Hash algorithm (default: 'sha1')
|
|
45
|
+
*/
|
|
46
|
+
algorithm?: HashAlgorithm;
|
|
47
|
+
/**
|
|
48
|
+
* Number of digits (default: 6)
|
|
49
|
+
*/
|
|
50
|
+
digits?: Digits;
|
|
51
|
+
/**
|
|
52
|
+
* Current Unix epoch timestamp in seconds (default: now)
|
|
53
|
+
* Used by TOTP strategy
|
|
54
|
+
*/
|
|
55
|
+
epoch?: number;
|
|
56
|
+
/**
|
|
57
|
+
* Initial Unix time to start counting time steps (default: 0)
|
|
58
|
+
* Used by TOTP strategy
|
|
59
|
+
*/
|
|
60
|
+
t0?: number;
|
|
61
|
+
/**
|
|
62
|
+
* Time step in seconds (default: 30)
|
|
63
|
+
* Used by TOTP strategy
|
|
64
|
+
*/
|
|
65
|
+
period?: number;
|
|
66
|
+
/**
|
|
67
|
+
* Counter value
|
|
68
|
+
* Used by HOTP strategy (required)
|
|
69
|
+
*/
|
|
70
|
+
counter?: number;
|
|
71
|
+
};
|
|
72
|
+
/**
|
|
73
|
+
* Options for verifying a token with the OTP class
|
|
74
|
+
*/
|
|
75
|
+
type OTPVerifyOptions = {
|
|
76
|
+
/**
|
|
77
|
+
* Base32-encoded secret key
|
|
78
|
+
*/
|
|
79
|
+
secret: string;
|
|
80
|
+
/**
|
|
81
|
+
* OTP code to verify
|
|
82
|
+
*/
|
|
83
|
+
token: string;
|
|
84
|
+
/**
|
|
85
|
+
* Hash algorithm (default: 'sha1')
|
|
86
|
+
*/
|
|
87
|
+
algorithm?: HashAlgorithm;
|
|
88
|
+
/**
|
|
89
|
+
* Number of digits (default: 6)
|
|
90
|
+
*/
|
|
91
|
+
digits?: Digits;
|
|
92
|
+
/**
|
|
93
|
+
* Current Unix epoch timestamp in seconds (default: now)
|
|
94
|
+
* Used by TOTP strategy
|
|
95
|
+
*/
|
|
96
|
+
epoch?: number;
|
|
97
|
+
/**
|
|
98
|
+
* Initial Unix time to start counting time steps (default: 0)
|
|
99
|
+
* Used by TOTP strategy
|
|
100
|
+
*/
|
|
101
|
+
t0?: number;
|
|
102
|
+
/**
|
|
103
|
+
* Time step in seconds (default: 30)
|
|
104
|
+
* Used by TOTP strategy
|
|
105
|
+
*/
|
|
106
|
+
period?: number;
|
|
107
|
+
/**
|
|
108
|
+
* Counter value
|
|
109
|
+
* Used by HOTP strategy (required)
|
|
110
|
+
*/
|
|
111
|
+
counter?: number;
|
|
112
|
+
/**
|
|
113
|
+
* Time tolerance in seconds for TOTP verification (default: 0)
|
|
114
|
+
* - Number: symmetric tolerance (same for past and future)
|
|
115
|
+
* - Tuple [past, future]: asymmetric tolerance
|
|
116
|
+
* Use [5, 0] for RFC-compliant past-only verification.
|
|
117
|
+
*/
|
|
118
|
+
epochTolerance?: number | [number, number];
|
|
119
|
+
/**
|
|
120
|
+
* Counter tolerance for HOTP verification (default: 0)
|
|
121
|
+
* - Number: symmetric look-ahead window
|
|
122
|
+
* - Array: asymmetric window
|
|
123
|
+
*/
|
|
124
|
+
counterTolerance?: number | number[];
|
|
125
|
+
};
|
|
126
|
+
/**
|
|
127
|
+
* Options for generating URI with the OTP class
|
|
128
|
+
*/
|
|
129
|
+
type OTPURIGenerateOptions = {
|
|
130
|
+
/**
|
|
131
|
+
* Issuer name (e.g., 'ACME Co')
|
|
132
|
+
*/
|
|
133
|
+
issuer: string;
|
|
134
|
+
/**
|
|
135
|
+
* Label/Account name (e.g., 'john@example.com')
|
|
136
|
+
*/
|
|
137
|
+
label: string;
|
|
138
|
+
/**
|
|
139
|
+
* Base32-encoded secret key
|
|
140
|
+
*/
|
|
141
|
+
secret: string;
|
|
142
|
+
/**
|
|
143
|
+
* Hash algorithm (default: 'sha1')
|
|
144
|
+
*/
|
|
145
|
+
algorithm?: HashAlgorithm;
|
|
146
|
+
/**
|
|
147
|
+
* Number of digits (default: 6)
|
|
148
|
+
*/
|
|
149
|
+
digits?: Digits;
|
|
150
|
+
/**
|
|
151
|
+
* Time step in seconds (default: 30)
|
|
152
|
+
*/
|
|
153
|
+
period?: number;
|
|
154
|
+
};
|
|
155
|
+
/**
|
|
156
|
+
* OTP Class
|
|
157
|
+
*
|
|
158
|
+
* A wrapper class that dynamically handles TOTP and HOTP strategies.
|
|
159
|
+
*
|
|
160
|
+
* @example
|
|
161
|
+
* ```ts
|
|
162
|
+
* import { OTP } from 'otplib';
|
|
163
|
+
*
|
|
164
|
+
* // Create OTP instance with TOTP strategy (default)
|
|
165
|
+
* const otp = new OTP({ strategy: 'totp' });
|
|
166
|
+
*
|
|
167
|
+
* // Generate and verify
|
|
168
|
+
* const secret = otp.generateSecret();
|
|
169
|
+
* const token = await otp.generate({ secret });
|
|
170
|
+
* const result = await otp.verify({ secret, token });
|
|
171
|
+
* ```
|
|
172
|
+
*
|
|
173
|
+
* @example With HOTP strategy
|
|
174
|
+
* ```ts
|
|
175
|
+
* import { OTP } from 'otplib';
|
|
176
|
+
*
|
|
177
|
+
* const otp = new OTP({ strategy: 'hotp' });
|
|
178
|
+
* const token = await otp.generate({ secret: 'ABC123', counter: 0 });
|
|
179
|
+
* ```
|
|
180
|
+
*
|
|
181
|
+
* @example Generating otpauth:// URI for authenticator apps
|
|
182
|
+
* ```ts
|
|
183
|
+
* import { OTP } from 'otplib';
|
|
184
|
+
*
|
|
185
|
+
* const otp = new OTP({ strategy: 'totp' });
|
|
186
|
+
* const uri = otp.generateURI({
|
|
187
|
+
* issuer: 'MyApp',
|
|
188
|
+
* label: 'user@example.com',
|
|
189
|
+
* secret: 'ABC123',
|
|
190
|
+
* });
|
|
191
|
+
* ```
|
|
192
|
+
*/
|
|
193
|
+
declare class OTP {
|
|
194
|
+
private readonly strategy;
|
|
195
|
+
private readonly crypto;
|
|
196
|
+
private readonly base32;
|
|
197
|
+
constructor(options?: OTPClassOptions);
|
|
198
|
+
/**
|
|
199
|
+
* Get the current strategy
|
|
200
|
+
*/
|
|
201
|
+
getStrategy(): OTPStrategy;
|
|
202
|
+
/**
|
|
203
|
+
* Generate a random secret key
|
|
204
|
+
*
|
|
205
|
+
* @param length - Number of random bytes (default: 20)
|
|
206
|
+
* @returns Base32-encoded secret key
|
|
207
|
+
*/
|
|
208
|
+
generateSecret(length?: number): string;
|
|
209
|
+
/**
|
|
210
|
+
* Generate an OTP token based on the configured strategy
|
|
211
|
+
*
|
|
212
|
+
* @param options - Generation options
|
|
213
|
+
* @returns OTP code
|
|
214
|
+
*/
|
|
215
|
+
generate(options: OTPGenerateOptions): Promise<string>;
|
|
216
|
+
/**
|
|
217
|
+
* Generate an OTP token based on the configured strategy synchronously
|
|
218
|
+
*
|
|
219
|
+
* @param options - Generation options
|
|
220
|
+
* @returns OTP code
|
|
221
|
+
* @throws {HMACError} If the crypto plugin doesn't support sync operations
|
|
222
|
+
*/
|
|
223
|
+
generateSync(options: OTPGenerateOptions): string;
|
|
224
|
+
/**
|
|
225
|
+
* Verify an OTP token based on the configured strategy
|
|
226
|
+
*
|
|
227
|
+
* @param options - Verification options
|
|
228
|
+
* @returns Verification result with validity and optional delta
|
|
229
|
+
*/
|
|
230
|
+
verify(options: OTPVerifyOptions): Promise<VerifyResult>;
|
|
231
|
+
/**
|
|
232
|
+
* Verify an OTP token based on the configured strategy synchronously
|
|
233
|
+
*
|
|
234
|
+
* @param options - Verification options
|
|
235
|
+
* @returns Verification result with validity and optional delta
|
|
236
|
+
* @throws {HMACError} If the crypto plugin doesn't support sync operations
|
|
237
|
+
*/
|
|
238
|
+
verifySync(options: OTPVerifyOptions): VerifyResult;
|
|
239
|
+
/**
|
|
240
|
+
* Generate an otpauth:// URI for QR code generation
|
|
241
|
+
*
|
|
242
|
+
* Only available for TOTP strategy.
|
|
243
|
+
*
|
|
244
|
+
* @param options - URI generation options
|
|
245
|
+
* @returns otpauth:// URI string
|
|
246
|
+
*/
|
|
247
|
+
generateURI(options: OTPURIGenerateOptions): string;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
export { OTP, type OTPClassOptions, type OTPGenerateOptions, type OTPURIGenerateOptions, type OTPVerifyOptions, type VerifyResult };
|
package/dist/class.js
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{generateSecret as w}from"@otplib/core";import{generateTOTP as B}from"@otplib/uri";import{ScureBase32Plugin as d}from"@otplib/plugin-base32-scure";import{NobleCryptoPlugin as S}from"@otplib/plugin-crypto-noble";var c=Object.freeze(new S),y=Object.freeze(new d);function g(t){return{secret:t.secret,strategy:t.strategy??"totp",crypto:t.crypto??c,base32:t.base32??y,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}}import{generateSecret as A,ConfigurationError as m}from"@otplib/core";import{generate as V,generateSync as R,verify as x,verifySync as v}from"@otplib/hotp";import{generate as H,generateSync as G,verify as C,verifySync as D}from"@otplib/totp";import{generateTOTP as q}from"@otplib/uri";function u(t,e,r){if(t==="totp")return r.totp();if(t==="hotp"){if(e===void 0)throw new m("Counter is required for HOTP strategy. Example: { strategy: 'hotp', counter: 0 }");return r.hotp(e)}throw new m(`Unknown OTP strategy: ${t}. Valid strategies are 'totp' or 'hotp'.`)}async function O(t){let e=g(t),{secret:r,crypto:o,base32:n,algorithm:s,digits:a}=e,i={secret:r,crypto:o,base32:n,algorithm:s,digits:a};return u(e.strategy,e.counter,{totp:()=>H({...i,period:e.period,epoch:e.epoch,t0:e.t0}),hotp:p=>V({...i,counter:p})})}function h(t){let e=g(t),{secret:r,crypto:o,base32:n,algorithm:s,digits:a}=e,i={secret:r,crypto:o,base32:n,algorithm:s,digits:a};return u(e.strategy,e.counter,{totp:()=>G({...i,period:e.period,epoch:e.epoch,t0:e.t0}),hotp:p=>R({...i,counter:p})})}async function T(t){let e=f(t),{secret:r,token:o,crypto:n,base32:s,algorithm:a,digits:i}=e,p={secret:r,token:o,crypto:n,base32:s,algorithm:a,digits:i};return u(e.strategy,e.counter,{totp:()=>C({...p,period:e.period,epoch:e.epoch,t0:e.t0,epochTolerance:e.epochTolerance}),hotp:l=>x({...p,counter:l,counterTolerance:e.counterTolerance})})}function P(t){let e=f(t),{secret:r,token:o,crypto:n,base32:s,algorithm:a,digits:i}=e,p={secret:r,token:o,crypto:n,base32:s,algorithm:a,digits:i};return u(e.strategy,e.counter,{totp:()=>D({...p,period:e.period,epoch:e.epoch,t0:e.t0,epochTolerance:e.epochTolerance}),hotp:l=>v({...p,counter:l,counterTolerance:e.counterTolerance})})}var b=class{strategy;crypto;base32;constructor(e={}){let{strategy:r="totp",crypto:o=c,base32:n=y}=e;this.strategy=r,this.crypto=o,this.base32=n}getStrategy(){return this.strategy}generateSecret(e=20){return w({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 h({...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 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:o,secret:n,algorithm:s="sha1",digits:a=6,period:i=30}=e;return B({issuer:r,label:o,secret:n,algorithm:s,digits:a,period:i})}};export{b as OTP};
|
|
2
|
+
//# sourceMappingURL=class.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/class.ts","../src/defaults.ts","../src/functional.ts"],"sourcesContent":["/**\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","/**\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","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"],"mappings":"AAMA,OAAS,kBAAkBA,MAA0B,eACrD,OAAS,gBAAgBC,MAAuB,cCDhD,OAAS,qBAAAC,MAAyB,8BAClC,OAAS,qBAAAC,MAAyB,8BAe3B,IAAMC,EAAgB,OAAO,OAAO,IAAID,CAAmB,EAQrDE,EAAgB,OAAO,OAAO,IAAIH,CAAmB,EAE3D,SAASI,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,EAAy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AE/E,OAAOkB,EAAgB,CACrB,OAAAN,EACA,MAAAC,EACA,OAAAC,EACA,UAAAC,EACA,OAAAC,EACA,OAAAC,CACF,CAAC,CACH,CACF","names":["generateSecretCore","generateTOTPURI","ScureBase32Plugin","NobleCryptoPlugin","defaultCrypto","defaultBase32","normalizeGenerateOptions","options","normalizeVerifyOptions","generateSecretCore","ConfigurationError","generateHOTP","generateHOTPSync","verifyHOTP","verifyHOTPSync","generateTOTP","generateTOTPSync","verifyTOTP","verifyTOTPSync","generateTOTPURI","executeByStrategy","strategy","counter","handlers","ConfigurationError","generate","options","opts","normalizeGenerateOptions","secret","crypto","base32","algorithm","digits","commonOptions","executeByStrategy","generateTOTP","counter","generateHOTP","generateSync","generateTOTPSync","generateHOTPSync","verify","normalizeVerifyOptions","token","verifyTOTP","verifyHOTP","verifySync","verifyTOTPSync","verifyHOTPSync","OTP","options","strategy","crypto","defaultCrypto","base32","defaultBase32","length","generateSecretCore","generate","generateSync","verify","verifySync","issuer","label","secret","algorithm","digits","period","generateTOTPURI"]}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"use strict";var O=Object.defineProperty;var b=Object.getOwnPropertyDescriptor;var V=Object.getOwnPropertyNames;var x=Object.prototype.hasOwnProperty;var H=(t,e)=>{for(var r in e)O(t,r,{get:e[r],enumerable:!0})},R=(t,e,r,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of V(e))!x.call(t,o)&&o!==r&&O(t,o,{get:()=>e[o],enumerable:!(n=b(e,o))||n.enumerable});return t};var v=t=>R(O({},"__esModule",{value:!0}),t);var k={};H(k,{generate:()=>C,generateSecret:()=>G,generateSync:()=>D,generateURI:()=>w,verify:()=>z,verifySync:()=>B});module.exports=v(k);var f=require("@otplib/core"),c=require("@otplib/hotp"),y=require("@otplib/totp"),S=require("@otplib/uri");var P=require("@otplib/plugin-base32-scure"),d=require("@otplib/plugin-crypto-noble"),T=Object.freeze(new d.NobleCryptoPlugin),m=Object.freeze(new P.ScureBase32Plugin);function g(t){return{secret:t.secret,strategy:t.strategy??"totp",crypto:t.crypto??T,base32:t.base32??m,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 h(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 f.ConfigurationError("Counter is required for HOTP strategy. Example: { strategy: 'hotp', counter: 0 }");return r.hotp(e)}throw new f.ConfigurationError(`Unknown OTP strategy: ${t}. Valid strategies are 'totp' or 'hotp'.`)}function G(t){let{crypto:e=T,base32:r=m,length:n=20}=t||{};return(0,f.generateSecret)({crypto:e,base32:r,length:n})}function w(t){let{issuer:e,label:r,secret:n,algorithm:o="sha1",digits:i=6,period:s=30}=t;return(0,S.generateTOTP)({issuer:e,label:r,secret:n,algorithm:o,digits:i,period:s})}async function C(t){let e=g(t),{secret:r,crypto:n,base32:o,algorithm:i,digits:s}=e,a={secret:r,crypto:n,base32:o,algorithm:i,digits:s};return u(e.strategy,e.counter,{totp:()=>(0,y.generate)({...a,period:e.period,epoch:e.epoch,t0:e.t0}),hotp:p=>(0,c.generate)({...a,counter:p})})}function D(t){let e=g(t),{secret:r,crypto:n,base32:o,algorithm:i,digits:s}=e,a={secret:r,crypto:n,base32:o,algorithm:i,digits:s};return u(e.strategy,e.counter,{totp:()=>(0,y.generateSync)({...a,period:e.period,epoch:e.epoch,t0:e.t0}),hotp:p=>(0,c.generateSync)({...a,counter:p})})}async function z(t){let e=h(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 u(e.strategy,e.counter,{totp:()=>(0,y.verify)({...p,period:e.period,epoch:e.epoch,t0:e.t0,epochTolerance:e.epochTolerance}),hotp:l=>(0,c.verify)({...p,counter:l,counterTolerance:e.counterTolerance})})}function B(t){let e=h(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 u(e.strategy,e.counter,{totp:()=>(0,y.verifySync)({...p,period:e.period,epoch:e.epoch,t0:e.t0,epochTolerance:e.epochTolerance}),hotp:l=>(0,c.verifySync)({...p,counter:l,counterTolerance:e.counterTolerance})})}0&&(module.exports={generate,generateSecret,generateSync,generateURI,verify,verifySync});
|
|
2
|
+
//# sourceMappingURL=functional.cjs.map
|