@otplib/v12-adapter 13.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +53 -0
- package/dist/index.cjs +542 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +427 -0
- package/dist/index.d.ts +427 -0
- package/dist/index.js +509 -0
- package/dist/index.js.map +1 -0
- package/package.json +75 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/hotp.ts","../src/types.ts","../src/totp.ts","../src/authenticator.ts"],"sourcesContent":["/**\n * @otplib/v12-adapter\n *\n * Drop-in replacement adapter for migrating from otplib v12 to v13.\n *\n * This package provides the same API as otplib v12, making it easy\n * to migrate existing codebases to v13 without breaking changes.\n *\n * @example Using pre-configured instances (v12 style)\n * ```typescript\n * import { authenticator } from '@otplib/v12-adapter';\n *\n * const secret = authenticator.generateSecret();\n * const token = authenticator.generate(secret);\n * const isValid = authenticator.check(token, secret);\n * const uri = authenticator.keyuri('user@example.com', 'MyApp', secret);\n * ```\n *\n * @example Using class instances\n * ```typescript\n * import { Authenticator } from '@otplib/v12-adapter';\n *\n * const authenticator = new Authenticator({ step: 60 });\n * const secret = authenticator.generateSecret();\n * const token = authenticator.generate(secret);\n * ```\n */\n\n// Classes\nexport { HOTP } from \"./hotp\";\nexport { TOTP } from \"./totp\";\nexport { Authenticator } from \"./authenticator\";\n\n// Functional exports (v12-style)\nexport { hotpDigestToToken } from \"./hotp\";\n\n// Constants (v12-style)\nexport { HashAlgorithms, KeyEncodings } from \"./types\";\n\n// Types\nexport type {\n HOTPOptions,\n TOTPOptions,\n AuthenticatorOptions,\n SecretKey,\n Base32SecretKey,\n CreateDigest,\n CreateHmacKey,\n CreateRandomBytes,\n KeyEncoder,\n KeyDecoder,\n} from \"./types\";\n\n// Pre-configured instances (v12 style)\nimport { Authenticator } from \"./authenticator\";\nimport { HOTP } from \"./hotp\";\nimport { TOTP } from \"./totp\";\n\n/**\n * Pre-configured HOTP instance\n *\n * @example\n * ```typescript\n * import { hotp } from '@otplib/v12-adapter';\n *\n * const token = hotp.generate('JBSWY3DPEHPK3PXP', 0);\n * const isValid = hotp.check(token, 'JBSWY3DPEHPK3PXP', 0);\n * ```\n */\nexport const hotp = new HOTP();\n\n/**\n * Pre-configured TOTP instance\n *\n * @example\n * ```typescript\n * import { totp } from '@otplib/v12-adapter';\n *\n * const token = totp.generate('JBSWY3DPEHPK3PXP');\n * const isValid = totp.check(token, 'JBSWY3DPEHPK3PXP');\n * ```\n */\nexport const totp = new TOTP();\n\n/**\n * Pre-configured Authenticator instance\n *\n * @example\n * ```typescript\n * import { authenticator } from '@otplib/v12-adapter';\n *\n * const secret = authenticator.generateSecret();\n * const token = authenticator.generate(secret);\n * const isValid = authenticator.check(token, secret);\n * ```\n */\nexport const authenticator = new Authenticator();\n\n// Re-export v13 plugins for advanced usage\nexport { NobleCryptoPlugin } from \"@otplib/plugin-crypto-noble\";\nexport { ScureBase32Plugin } from \"@otplib/plugin-base32-scure\";\n","/**\n * @otplib/v12-adapter\n *\n * v12-compatible HOTP class implementation.\n * Provides synchronous API wrapper around v13's HOTP implementation.\n */\n\nimport { stringToBytes, hexToBytes, dynamicTruncate, truncateDigits } from \"@otplib/core\";\nimport { generateSync as hotpGenerateSync, verifySync as hotpVerifySync } from \"@otplib/hotp\";\nimport { ScureBase32Plugin } from \"@otplib/plugin-base32-scure\";\nimport { NobleCryptoPlugin } from \"@otplib/plugin-crypto-noble\";\nimport { generateHOTP as generateHOTPURI } from \"@otplib/uri\";\n\nimport { HashAlgorithms, KeyEncodings as KeyEncodingsConst } from \"./types\";\n\nimport type { HOTPOptions, SecretKey, ResolvedHOTPOptions } from \"./types\";\nimport type { Digits } from \"@otplib/core\";\n\n/**\n * Default crypto plugin instance\n */\nconst defaultCrypto = new NobleCryptoPlugin();\n\n/**\n * Default base32 plugin instance\n */\nconst defaultBase32 = new ScureBase32Plugin();\n\n/**\n * Convert a string secret to bytes based on encoding\n * @internal\n */\nexport function secretToBytes(secret: SecretKey, encoding?: string): Uint8Array {\n if (encoding === KeyEncodingsConst.BASE32 || encoding === \"base32\") {\n return defaultBase32.decode(secret);\n }\n if (encoding === KeyEncodingsConst.HEX || encoding === \"hex\") {\n return hexToBytes(secret.replace(/\\s/g, \"\"));\n }\n // Default: treat as ASCII/UTF-8\n return stringToBytes(secret);\n}\n\n/**\n * Converts a digest to a token of a specified length.\n * Uses dynamicTruncate and truncateDigits from core.\n */\nexport function hotpDigestToToken(hexDigest: string, digits: number): string {\n const digestBytes = hexToBytes(hexDigest);\n const truncated = dynamicTruncate(digestBytes);\n return truncateDigits(truncated, digits);\n}\n\n/**\n * v12-compatible HOTP class\n *\n * Provides the same API as otplib v12 HOTP class while using v13's\n * implementation internally.\n *\n * @example\n * ```typescript\n * import { HOTP } from '@otplib/v12-adapter';\n *\n * const hotp = new HOTP();\n * const secret = 'JBSWY3DPEHPK3PXP';\n * const token = hotp.generate(secret, 0);\n * const isValid = hotp.check(token, secret, 0);\n * ```\n */\nexport class HOTP<T extends HOTPOptions = HOTPOptions> {\n /**\n * Stored options that can be modified\n */\n protected _options: Partial<T> = {};\n\n /**\n * Default options applied to all operations\n */\n protected _defaultOptions: Partial<T> = {};\n\n constructor(defaultOptions: Partial<T> = {}) {\n this._defaultOptions = { ...defaultOptions };\n this._options = {};\n }\n\n /**\n * Get current options (merged with defaults)\n */\n get options(): Partial<T> {\n return { ...this._defaultOptions, ...this._options };\n }\n\n /**\n * Set options (replaces current options)\n */\n set options(value: Partial<T>) {\n this._options = { ...value };\n }\n\n /**\n * Creates a new instance with the specified default options\n */\n create(defaultOptions: Partial<T> = {}): HOTP<T> {\n return new HOTP<T>(defaultOptions);\n }\n\n /**\n * Returns class options polyfilled with default values\n */\n allOptions(): Readonly<ResolvedHOTPOptions> {\n const merged = {\n algorithm: HashAlgorithms.SHA1,\n digits: 6,\n encoding: KeyEncodingsConst.ASCII,\n crypto: defaultCrypto,\n base32: defaultBase32,\n ...this._defaultOptions,\n ...this._options,\n };\n return Object.freeze(merged) as Readonly<ResolvedHOTPOptions>;\n }\n\n /**\n * Reset options to defaults\n */\n resetOptions(): this {\n this._options = {};\n return this;\n }\n\n /**\n * Generate an HOTP token\n */\n generate(secret: SecretKey, counter: number): string {\n const opts = this.allOptions();\n const secretBytes = secretToBytes(secret, opts.encoding);\n\n return hotpGenerateSync({\n secret: secretBytes,\n counter,\n algorithm: opts.algorithm,\n digits: opts.digits as Digits,\n crypto: opts.crypto,\n });\n }\n\n /**\n * Check if a token is valid for the given secret and counter\n */\n check(token: string, secret: SecretKey, counter: number): boolean {\n const opts = this.allOptions();\n const secretBytes = secretToBytes(secret, opts.encoding);\n\n try {\n const result = hotpVerifySync({\n secret: secretBytes,\n token,\n counter,\n algorithm: opts.algorithm,\n digits: opts.digits as Digits,\n counterTolerance: 0,\n crypto: opts.crypto,\n });\n\n return result.valid;\n } catch {\n return false;\n }\n }\n\n /**\n * Verify a token (object-based API)\n */\n verify(opts: { token: string; secret: SecretKey; counter: number }): boolean {\n if (typeof opts !== \"object\") {\n throw new Error(\"Expecting argument 0 of verify to be an object\");\n }\n return this.check(opts.token, opts.secret, opts.counter);\n }\n\n /**\n * Generate an otpauth:// URI for HOTP\n */\n keyuri(accountName: string, issuer: string, secret: SecretKey, counter: number): string {\n const opts = this.allOptions();\n\n return generateHOTPURI({\n label: accountName,\n issuer,\n secret,\n algorithm: opts.algorithm,\n digits: opts.digits as Digits,\n counter,\n });\n }\n}\n","/**\n * @otplib/v12-adapter\n *\n * v12-compatible type definitions for migration adapter.\n * These types mirror the v12 API to provide drop-in compatibility.\n */\n\nimport type { CryptoPlugin, Base32Plugin, HashAlgorithm } from \"@otplib/core\";\n\n/**\n * v12-style hash algorithms constant\n */\nexport const HashAlgorithms = {\n SHA1: \"sha1\",\n SHA256: \"sha256\",\n SHA512: \"sha512\",\n} as const;\n\nexport type HashAlgorithms = (typeof HashAlgorithms)[keyof typeof HashAlgorithms];\n\n/**\n * v12-style key encodings constant\n */\nexport const KeyEncodings = {\n ASCII: \"ascii\",\n HEX: \"hex\",\n BASE32: \"base32\",\n BASE64: \"base64\",\n LATIN1: \"latin1\",\n UTF8: \"utf8\",\n} as const;\n\nexport type KeyEncodings = (typeof KeyEncodings)[keyof typeof KeyEncodings];\n\n/**\n * v12-style secret key type (string-based)\n */\nexport type SecretKey = string;\n\n/**\n * Base32 encoded secret key\n */\nexport type Base32SecretKey = string;\n\n/**\n * v12-style createDigest function signature\n */\nexport type CreateDigest<T = string> = (algorithm: HashAlgorithm, hmacKey: T, counter: T) => T;\n\n/**\n * v12-style createHmacKey function signature\n */\nexport type CreateHmacKey<T = string> = (\n algorithm: HashAlgorithm,\n secret: SecretKey,\n encoding: KeyEncodings,\n) => T;\n\n/**\n * v12-style createRandomBytes function signature\n */\nexport type CreateRandomBytes<T = string> = (size: number, encoding: KeyEncodings) => T;\n\n/**\n * v12-style keyEncoder function signature\n */\nexport type KeyEncoder<T = Base32SecretKey> = (secret: SecretKey, encoding: KeyEncodings) => T;\n\n/**\n * v12-style keyDecoder function signature\n */\nexport type KeyDecoder<T = SecretKey> = (\n encodedSecret: Base32SecretKey,\n encoding: KeyEncodings,\n) => T;\n\n/**\n * v12-compatible HOTP options\n */\nexport type HOTPOptions<T = string> = {\n /** Algorithm for HMAC (default: sha1) */\n algorithm?: HashAlgorithm;\n /** Creates the digest for token generation */\n createDigest?: CreateDigest<T>;\n /** Formats the secret into HMAC key */\n createHmacKey?: CreateHmacKey<T>;\n /** Number of digits in token (default: 6) */\n digits?: number;\n /** Secret encoding (default: ascii) */\n encoding?: KeyEncodings;\n /** Pre-computed digest (use with caution) */\n digest?: string;\n /** v13 crypto plugin (internal use) */\n crypto?: CryptoPlugin;\n /** v13 base32 plugin (internal use) */\n base32?: Base32Plugin;\n};\n\n/**\n * v12-compatible TOTP options\n */\nexport type TOTPOptions<T = string> = HOTPOptions<T> & {\n /** Starting epoch in milliseconds (default: Date.now()) */\n epoch?: number;\n /** Time step in seconds (default: 30) */\n step?: number;\n /** Verification window - number of steps or [past, future] */\n window?: number | [number, number];\n};\n\n/**\n * v12-compatible Authenticator options\n */\nexport type AuthenticatorOptions<T = string> = TOTPOptions<T> & {\n /** Encodes secret to Base32 */\n keyEncoder?: KeyEncoder<T>;\n /** Decodes Base32 secret */\n keyDecoder?: KeyDecoder<T>;\n /** Creates random bytes for secret generation */\n createRandomBytes?: CreateRandomBytes<T>;\n};\n\n/**\n * Resolved HOTP options with guaranteed defaults\n */\nexport type ResolvedHOTPOptions = {\n algorithm: HashAlgorithm;\n digits: number;\n encoding: KeyEncodings;\n crypto: CryptoPlugin;\n base32: Base32Plugin;\n};\n\n/**\n * Resolved TOTP options with guaranteed defaults\n */\nexport type ResolvedTOTPOptions = ResolvedHOTPOptions & {\n epoch: number;\n step: number;\n window: number | [number, number];\n};\n\n/**\n * Resolved Authenticator options with guaranteed defaults\n */\nexport type ResolvedAuthenticatorOptions = ResolvedTOTPOptions & {\n keyEncoder?: KeyEncoder;\n keyDecoder?: KeyDecoder;\n};\n","/**\n * @otplib/v12-adapter\n *\n * v12-compatible TOTP class implementation.\n * Provides synchronous API wrapper around v13's TOTP implementation.\n */\n\nimport { ScureBase32Plugin } from \"@otplib/plugin-base32-scure\";\nimport { NobleCryptoPlugin } from \"@otplib/plugin-crypto-noble\";\nimport {\n generateSync as totpGenerateSync,\n verifySync as totpVerifySync,\n getRemainingTime,\n} from \"@otplib/totp\";\nimport { generateTOTP as generateTOTPURI } from \"@otplib/uri\";\n\nimport { HOTP, secretToBytes } from \"./hotp\";\nimport { HashAlgorithms, KeyEncodings as KeyEncodingsConst } from \"./types\";\n\nimport type { TOTPOptions, SecretKey, ResolvedTOTPOptions } from \"./types\";\nimport type { Digits } from \"@otplib/core\";\n\n/**\n * Default crypto plugin instance\n */\nconst defaultCrypto = new NobleCryptoPlugin();\n\n/**\n * Default base32 plugin instance\n */\nconst defaultBase32 = new ScureBase32Plugin();\n\n/**\n * Parse window option into epochTolerance\n * v12 uses \"window\" as number of steps, v13 uses epochTolerance in seconds\n */\nfunction parseWindow(\n window: number | [number, number] | undefined,\n step: number,\n): number | [number, number] {\n if (window === undefined || window === 0) {\n return 0;\n }\n if (typeof window === \"number\") {\n return window * step;\n }\n // [past, future] steps → [past, future] seconds\n return [window[0] * step, window[1] * step];\n}\n\n/**\n * v12-compatible TOTP class\n *\n * Provides the same API as otplib v12 TOTP class while using v13's\n * implementation internally.\n *\n * @example\n * ```typescript\n * import { TOTP } from '@otplib/v12-adapter';\n *\n * const totp = new TOTP();\n * const secret = 'JBSWY3DPEHPK3PXP';\n * const token = totp.generate(secret);\n * const isValid = totp.check(token, secret);\n * ```\n */\nexport class TOTP<T extends TOTPOptions = TOTPOptions> extends HOTP<T> {\n constructor(defaultOptions: Partial<T> = {}) {\n super(defaultOptions);\n }\n\n /**\n * Creates a new TOTP instance with the specified default options\n */\n override create(defaultOptions: Partial<T> = {}): TOTP<T> {\n return new TOTP<T>(defaultOptions);\n }\n\n /**\n * Returns class options polyfilled with TOTP default values\n */\n override allOptions(): Readonly<ResolvedTOTPOptions> {\n const merged = {\n algorithm: HashAlgorithms.SHA1,\n digits: 6,\n encoding: KeyEncodingsConst.ASCII,\n epoch: Date.now(),\n step: 30,\n window: 0 as number | [number, number],\n crypto: defaultCrypto,\n base32: defaultBase32,\n ...this._defaultOptions,\n ...this._options,\n };\n return Object.freeze(merged) as Readonly<ResolvedTOTPOptions>;\n }\n\n /**\n * Generate a TOTP token\n *\n * @param secret - The secret key\n * @returns The OTP token\n */\n generate(secret: SecretKey): string {\n const opts = this.allOptions();\n const secretBytes = secretToBytes(secret, opts.encoding);\n // v12 uses epoch in milliseconds, always convert to seconds\n const epochSeconds = Math.floor(opts.epoch / 1000);\n\n return totpGenerateSync({\n secret: secretBytes,\n algorithm: opts.algorithm,\n digits: opts.digits as Digits,\n period: opts.step,\n epoch: epochSeconds,\n t0: 0,\n crypto: opts.crypto,\n });\n }\n\n /**\n * Check if a token is valid for the given secret\n *\n * @param token - The token to verify\n * @param secret - The secret key\n * @returns true if valid\n */\n check(token: string, secret: SecretKey): boolean {\n const delta = this.checkDelta(token, secret);\n return typeof delta === \"number\";\n }\n\n /**\n * Check token and return the time window delta\n *\n * @param token - The token to verify\n * @param secret - The secret key\n * @returns Window delta (0 = current, positive = future, negative = past), null if invalid\n */\n checkDelta(token: string, secret: SecretKey): number | null {\n const opts = this.allOptions();\n const secretBytes = secretToBytes(secret, opts.encoding);\n // v12 uses epoch in milliseconds, always convert to seconds\n const epochSeconds = Math.floor(opts.epoch / 1000);\n const step = opts.step;\n const window = opts.window;\n\n const epochTolerance = parseWindow(window, step);\n\n try {\n const result = totpVerifySync({\n secret: secretBytes,\n token,\n algorithm: opts.algorithm,\n digits: opts.digits as Digits,\n period: step,\n epoch: epochSeconds,\n t0: 0,\n epochTolerance,\n crypto: opts.crypto,\n });\n\n if (!result.valid) {\n return null;\n }\n\n // v13 returns delta directly as time step offset\n return result.delta;\n } catch {\n return null;\n }\n }\n\n /**\n * Verify a token (object-based API)\n *\n * @param opts - Verification options\n * @returns true if valid\n */\n verify(opts: { token: string; secret: SecretKey }): boolean {\n if (typeof opts !== \"object\") {\n throw new Error(\"Expecting argument 0 of verify to be an object\");\n }\n return this.check(opts.token, opts.secret);\n }\n\n /**\n * Generate an otpauth:// URI for TOTP\n *\n * @param accountName - Account name for the URI\n * @param issuer - Issuer name\n * @param secret - The secret key (should be Base32 for QR codes)\n * @returns The otpauth:// URI\n */\n keyuri(accountName: string, issuer: string, secret: SecretKey): string {\n const opts = this.allOptions();\n\n return generateTOTPURI({\n label: accountName,\n issuer,\n secret,\n algorithm: opts.algorithm,\n digits: opts.digits as Digits,\n period: opts.step,\n });\n }\n\n /**\n * Get time used in current step (seconds elapsed in current window)\n *\n * @returns Seconds used in current step\n */\n timeUsed(): number {\n const opts = this.allOptions();\n // v12 uses epoch in milliseconds, convert to seconds\n const epochSeconds = Math.floor(opts.epoch / 1000);\n return epochSeconds % opts.step;\n }\n\n /**\n * Get time remaining until next token\n *\n * @returns Seconds remaining in current step\n */\n timeRemaining(): number {\n const opts = this.allOptions();\n // v12 uses epoch in milliseconds, convert to seconds\n const epochSeconds = Math.floor(opts.epoch / 1000);\n return getRemainingTime(epochSeconds, opts.step, 0);\n }\n}\n","/**\n * @otplib/v12-adapter\n *\n * v12-compatible Authenticator class implementation.\n * Provides same API as v12 with Base32 encoding support.\n */\n\nimport { generateSecret as generateSecretCore } from \"@otplib/core\";\nimport { ScureBase32Plugin } from \"@otplib/plugin-base32-scure\";\nimport { NobleCryptoPlugin } from \"@otplib/plugin-crypto-noble\";\nimport { generateSync as totpGenerateSync, verifySync as totpVerifySync } from \"@otplib/totp\";\n\nimport { TOTP } from \"./totp\";\nimport { HashAlgorithms, KeyEncodings as KeyEncodingsConst } from \"./types\";\n\nimport type {\n AuthenticatorOptions,\n Base32SecretKey,\n SecretKey,\n KeyEncodings,\n ResolvedAuthenticatorOptions,\n} from \"./types\";\nimport type { Digits } from \"@otplib/core\";\n\n/**\n * Default crypto plugin instance\n */\nconst defaultCrypto = new NobleCryptoPlugin();\n\n/**\n * Default base32 plugin instance\n */\nconst defaultBase32 = new ScureBase32Plugin();\n\n/**\n * Default key encoder - encodes raw bytes to Base32\n */\nfunction defaultKeyEncoder(secret: SecretKey, _encoding: KeyEncodings): Base32SecretKey {\n const bytes = new TextEncoder().encode(secret);\n return defaultBase32.encode(bytes);\n}\n\n/**\n * Default key decoder - decodes Base32 to string\n */\nfunction defaultKeyDecoder(encodedSecret: Base32SecretKey, _encoding: KeyEncodings): SecretKey {\n const bytes = defaultBase32.decode(encodedSecret);\n return new TextDecoder().decode(bytes);\n}\n\n/**\n * v12-compatible Authenticator class\n *\n * The Authenticator class is a TOTP variant that uses Base32-encoded secrets\n * by default, making it compatible with Google Authenticator and similar apps.\n *\n * @example\n * ```typescript\n * import { Authenticator } from '@otplib/v12-adapter';\n *\n * const authenticator = new Authenticator();\n * const secret = authenticator.generateSecret();\n * const token = authenticator.generate(secret);\n * const isValid = authenticator.check(token, secret);\n * const uri = authenticator.keyuri('user@example.com', 'MyApp', secret);\n * ```\n */\nexport class Authenticator<T extends AuthenticatorOptions = AuthenticatorOptions> extends TOTP<T> {\n constructor(defaultOptions: Partial<T> = {}) {\n super(defaultOptions);\n }\n\n /**\n * Creates a new Authenticator instance with the specified default options\n */\n override create(defaultOptions: Partial<T> = {}): Authenticator<T> {\n return new Authenticator<T>(defaultOptions);\n }\n\n /**\n * Returns class options polyfilled with Authenticator default values\n */\n override allOptions(): Readonly<ResolvedAuthenticatorOptions> {\n const merged = {\n algorithm: HashAlgorithms.SHA1,\n digits: 6,\n encoding: KeyEncodingsConst.HEX,\n epoch: Date.now(),\n step: 30,\n window: 0 as number | [number, number],\n keyEncoder: defaultKeyEncoder,\n keyDecoder: defaultKeyDecoder,\n crypto: defaultCrypto,\n base32: defaultBase32,\n ...this._defaultOptions,\n ...this._options,\n };\n return Object.freeze(merged) as Readonly<ResolvedAuthenticatorOptions>;\n }\n\n /**\n * Generate an OTP token from a Base32 secret\n *\n * @param secret - Base32-encoded secret\n * @returns The OTP token\n */\n override generate(secret: Base32SecretKey): string {\n const opts = this.allOptions();\n\n // Generate using decoded secret (as raw bytes)\n const secretBytes = defaultBase32.decode(secret);\n const epoch = opts.epoch;\n const epochSeconds = epoch >= 1e12 ? Math.floor(epoch / 1000) : epoch;\n\n return totpGenerateSync({\n secret: secretBytes,\n algorithm: opts.algorithm,\n digits: opts.digits as Digits,\n period: opts.step,\n epoch: epochSeconds,\n t0: 0,\n crypto: opts.crypto,\n });\n }\n\n /**\n * Check if a token is valid for the given Base32 secret\n *\n * @param token - The token to verify\n * @param secret - Base32-encoded secret\n * @returns true if valid\n */\n override check(token: string, secret: Base32SecretKey): boolean {\n const delta = this.checkDelta(token, secret);\n return typeof delta === \"number\";\n }\n\n /**\n * Check token and return the time window delta\n *\n * @param token - The token to verify\n * @param secret - Base32-encoded secret\n * @returns Window delta (0 = current, positive = future, negative = past), null if invalid\n */\n override checkDelta(token: string, secret: Base32SecretKey): number | null {\n const opts = this.allOptions();\n const secretBytes = defaultBase32.decode(secret);\n const epoch = opts.epoch;\n const epochSeconds = epoch >= 1e12 ? Math.floor(epoch / 1000) : epoch;\n const step = opts.step;\n const window = opts.window;\n\n // Convert window (steps) to epochTolerance (seconds)\n let epochTolerance: number | [number, number] = 0;\n if (typeof window === \"number\") {\n epochTolerance = window * step;\n } else if (Array.isArray(window)) {\n epochTolerance = [window[0] * step, window[1] * step];\n }\n\n try {\n const result = totpVerifySync({\n secret: secretBytes,\n token,\n algorithm: opts.algorithm,\n digits: opts.digits as Digits,\n period: step,\n epoch: epochSeconds,\n t0: 0,\n epochTolerance,\n crypto: opts.crypto,\n });\n\n if (!result.valid) {\n return null;\n }\n\n return result.delta;\n } catch {\n return null;\n }\n }\n\n /**\n * Verify a token (object-based API)\n *\n * @param opts - Verification options\n * @returns true if valid\n */\n override verify(opts: { token: string; secret: Base32SecretKey }): boolean {\n if (typeof opts !== \"object\") {\n throw new Error(\"Expecting argument 0 of verify to be an object\");\n }\n return this.check(opts.token, opts.secret);\n }\n\n /**\n * Encode a raw secret to Base32\n *\n * @param secret - Raw secret string\n * @returns Base32-encoded secret\n */\n encode(secret: SecretKey): Base32SecretKey {\n const opts = this.allOptions();\n if (opts.keyEncoder) {\n return opts.keyEncoder(secret, opts.encoding);\n }\n return defaultKeyEncoder(secret, opts.encoding);\n }\n\n /**\n * Decode a Base32 secret to raw string\n *\n * @param secret - Base32-encoded secret\n * @returns Raw secret string\n */\n decode(secret: Base32SecretKey): SecretKey {\n const opts = this.allOptions();\n if (opts.keyDecoder) {\n return opts.keyDecoder(secret, opts.encoding);\n }\n return defaultKeyDecoder(secret, opts.encoding);\n }\n\n /**\n * Generate a random Base32-encoded secret\n *\n * @param numberOfBytes - Number of bytes for the secret (default: 20)\n * @returns Base32-encoded secret\n */\n generateSecret(numberOfBytes: number = 20): Base32Sec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7B,QAAI,KAAK,YAAY;AACnB,aAAO,KAAK,WAAW,QAAQ,KAAK,QAAQ;AAAA,IAC9C;AACA,WAAO,kBAAkB,QAAQ,KAAK,QAAQ;AAAA,EAChD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,OAAO,QAAoC;AACzC,UAAM,OAAO,KAAK,WAAW;AAC7B,QAAI,KAAK,YAAY;AACnB,aAAO,KAAK,WAAW,QAAQ,KAAK,QAAQ;AAAA,IAC9C;AACA,WAAO,kBAAkB,QAAQ,KAAK,QAAQ;AAAA,EAChD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,eAAe,gBAAwB,IAAqB;AAC1D,UAAM,OAAO,KAAK,WAAW;AAC7B,eAAO,aAAAC,gBAAmB;AAAA,MACxB,QAAQ,KAAK;AAAA,MACb,QAAQ,KAAK;AAAA,MACb,QAAQ;AAAA,IACV,CAAC;AAAA,EACH;AACF;;;AJ3IA,IAAAC,8BAAkC;AAClC,IAAAC,8BAAkC;AA/B3B,IAAM,OAAO,IAAI,KAAK;AAatB,IAAM,OAAO,IAAI,KAAK;AActB,IAAM,gBAAgB,IAAI,cAAc;","names":["hotpGenerateSync","hotpVerifySync","generateHOTPURI","import_plugin_base32_scure","import_plugin_crypto_noble","import_uri","defaultCrypto","defaultBase32","totpGenerateSync","totpVerifySync","generateTOTPURI","import_core","import_plugin_base32_scure","import_plugin_crypto_noble","import_totp","defaultCrypto","defaultBase32","totpGenerateSync","totpVerifySync","generateSecretCore","import_plugin_crypto_noble","import_plugin_base32_scure"]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,427 @@
|
|
|
1
|
+
import { HashAlgorithm, CryptoPlugin, Base32Plugin } from '@otplib/core';
|
|
2
|
+
export { NobleCryptoPlugin } from '@otplib/plugin-crypto-noble';
|
|
3
|
+
export { ScureBase32Plugin } from '@otplib/plugin-base32-scure';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @otplib/v12-adapter
|
|
7
|
+
*
|
|
8
|
+
* v12-compatible type definitions for migration adapter.
|
|
9
|
+
* These types mirror the v12 API to provide drop-in compatibility.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* v12-style hash algorithms constant
|
|
14
|
+
*/
|
|
15
|
+
declare const HashAlgorithms: {
|
|
16
|
+
readonly SHA1: "sha1";
|
|
17
|
+
readonly SHA256: "sha256";
|
|
18
|
+
readonly SHA512: "sha512";
|
|
19
|
+
};
|
|
20
|
+
type HashAlgorithms = (typeof HashAlgorithms)[keyof typeof HashAlgorithms];
|
|
21
|
+
/**
|
|
22
|
+
* v12-style key encodings constant
|
|
23
|
+
*/
|
|
24
|
+
declare const KeyEncodings: {
|
|
25
|
+
readonly ASCII: "ascii";
|
|
26
|
+
readonly HEX: "hex";
|
|
27
|
+
readonly BASE32: "base32";
|
|
28
|
+
readonly BASE64: "base64";
|
|
29
|
+
readonly LATIN1: "latin1";
|
|
30
|
+
readonly UTF8: "utf8";
|
|
31
|
+
};
|
|
32
|
+
type KeyEncodings = (typeof KeyEncodings)[keyof typeof KeyEncodings];
|
|
33
|
+
/**
|
|
34
|
+
* v12-style secret key type (string-based)
|
|
35
|
+
*/
|
|
36
|
+
type SecretKey = string;
|
|
37
|
+
/**
|
|
38
|
+
* Base32 encoded secret key
|
|
39
|
+
*/
|
|
40
|
+
type Base32SecretKey = string;
|
|
41
|
+
/**
|
|
42
|
+
* v12-style createDigest function signature
|
|
43
|
+
*/
|
|
44
|
+
type CreateDigest<T = string> = (algorithm: HashAlgorithm, hmacKey: T, counter: T) => T;
|
|
45
|
+
/**
|
|
46
|
+
* v12-style createHmacKey function signature
|
|
47
|
+
*/
|
|
48
|
+
type CreateHmacKey<T = string> = (algorithm: HashAlgorithm, secret: SecretKey, encoding: KeyEncodings) => T;
|
|
49
|
+
/**
|
|
50
|
+
* v12-style createRandomBytes function signature
|
|
51
|
+
*/
|
|
52
|
+
type CreateRandomBytes<T = string> = (size: number, encoding: KeyEncodings) => T;
|
|
53
|
+
/**
|
|
54
|
+
* v12-style keyEncoder function signature
|
|
55
|
+
*/
|
|
56
|
+
type KeyEncoder<T = Base32SecretKey> = (secret: SecretKey, encoding: KeyEncodings) => T;
|
|
57
|
+
/**
|
|
58
|
+
* v12-style keyDecoder function signature
|
|
59
|
+
*/
|
|
60
|
+
type KeyDecoder<T = SecretKey> = (encodedSecret: Base32SecretKey, encoding: KeyEncodings) => T;
|
|
61
|
+
/**
|
|
62
|
+
* v12-compatible HOTP options
|
|
63
|
+
*/
|
|
64
|
+
type HOTPOptions<T = string> = {
|
|
65
|
+
/** Algorithm for HMAC (default: sha1) */
|
|
66
|
+
algorithm?: HashAlgorithm;
|
|
67
|
+
/** Creates the digest for token generation */
|
|
68
|
+
createDigest?: CreateDigest<T>;
|
|
69
|
+
/** Formats the secret into HMAC key */
|
|
70
|
+
createHmacKey?: CreateHmacKey<T>;
|
|
71
|
+
/** Number of digits in token (default: 6) */
|
|
72
|
+
digits?: number;
|
|
73
|
+
/** Secret encoding (default: ascii) */
|
|
74
|
+
encoding?: KeyEncodings;
|
|
75
|
+
/** Pre-computed digest (use with caution) */
|
|
76
|
+
digest?: string;
|
|
77
|
+
/** v13 crypto plugin (internal use) */
|
|
78
|
+
crypto?: CryptoPlugin;
|
|
79
|
+
/** v13 base32 plugin (internal use) */
|
|
80
|
+
base32?: Base32Plugin;
|
|
81
|
+
};
|
|
82
|
+
/**
|
|
83
|
+
* v12-compatible TOTP options
|
|
84
|
+
*/
|
|
85
|
+
type TOTPOptions<T = string> = HOTPOptions<T> & {
|
|
86
|
+
/** Starting epoch in milliseconds (default: Date.now()) */
|
|
87
|
+
epoch?: number;
|
|
88
|
+
/** Time step in seconds (default: 30) */
|
|
89
|
+
step?: number;
|
|
90
|
+
/** Verification window - number of steps or [past, future] */
|
|
91
|
+
window?: number | [number, number];
|
|
92
|
+
};
|
|
93
|
+
/**
|
|
94
|
+
* v12-compatible Authenticator options
|
|
95
|
+
*/
|
|
96
|
+
type AuthenticatorOptions<T = string> = TOTPOptions<T> & {
|
|
97
|
+
/** Encodes secret to Base32 */
|
|
98
|
+
keyEncoder?: KeyEncoder<T>;
|
|
99
|
+
/** Decodes Base32 secret */
|
|
100
|
+
keyDecoder?: KeyDecoder<T>;
|
|
101
|
+
/** Creates random bytes for secret generation */
|
|
102
|
+
createRandomBytes?: CreateRandomBytes<T>;
|
|
103
|
+
};
|
|
104
|
+
/**
|
|
105
|
+
* Resolved HOTP options with guaranteed defaults
|
|
106
|
+
*/
|
|
107
|
+
type ResolvedHOTPOptions = {
|
|
108
|
+
algorithm: HashAlgorithm;
|
|
109
|
+
digits: number;
|
|
110
|
+
encoding: KeyEncodings;
|
|
111
|
+
crypto: CryptoPlugin;
|
|
112
|
+
base32: Base32Plugin;
|
|
113
|
+
};
|
|
114
|
+
/**
|
|
115
|
+
* Resolved TOTP options with guaranteed defaults
|
|
116
|
+
*/
|
|
117
|
+
type ResolvedTOTPOptions = ResolvedHOTPOptions & {
|
|
118
|
+
epoch: number;
|
|
119
|
+
step: number;
|
|
120
|
+
window: number | [number, number];
|
|
121
|
+
};
|
|
122
|
+
/**
|
|
123
|
+
* Resolved Authenticator options with guaranteed defaults
|
|
124
|
+
*/
|
|
125
|
+
type ResolvedAuthenticatorOptions = ResolvedTOTPOptions & {
|
|
126
|
+
keyEncoder?: KeyEncoder;
|
|
127
|
+
keyDecoder?: KeyDecoder;
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* @otplib/v12-adapter
|
|
132
|
+
*
|
|
133
|
+
* v12-compatible HOTP class implementation.
|
|
134
|
+
* Provides synchronous API wrapper around v13's HOTP implementation.
|
|
135
|
+
*/
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Converts a digest to a token of a specified length.
|
|
139
|
+
* Uses dynamicTruncate and truncateDigits from core.
|
|
140
|
+
*/
|
|
141
|
+
declare function hotpDigestToToken(hexDigest: string, digits: number): string;
|
|
142
|
+
/**
|
|
143
|
+
* v12-compatible HOTP class
|
|
144
|
+
*
|
|
145
|
+
* Provides the same API as otplib v12 HOTP class while using v13's
|
|
146
|
+
* implementation internally.
|
|
147
|
+
*
|
|
148
|
+
* @example
|
|
149
|
+
* ```typescript
|
|
150
|
+
* import { HOTP } from '@otplib/v12-adapter';
|
|
151
|
+
*
|
|
152
|
+
* const hotp = new HOTP();
|
|
153
|
+
* const secret = 'JBSWY3DPEHPK3PXP';
|
|
154
|
+
* const token = hotp.generate(secret, 0);
|
|
155
|
+
* const isValid = hotp.check(token, secret, 0);
|
|
156
|
+
* ```
|
|
157
|
+
*/
|
|
158
|
+
declare class HOTP<T extends HOTPOptions = HOTPOptions> {
|
|
159
|
+
/**
|
|
160
|
+
* Stored options that can be modified
|
|
161
|
+
*/
|
|
162
|
+
protected _options: Partial<T>;
|
|
163
|
+
/**
|
|
164
|
+
* Default options applied to all operations
|
|
165
|
+
*/
|
|
166
|
+
protected _defaultOptions: Partial<T>;
|
|
167
|
+
constructor(defaultOptions?: Partial<T>);
|
|
168
|
+
/**
|
|
169
|
+
* Get current options (merged with defaults)
|
|
170
|
+
*/
|
|
171
|
+
get options(): Partial<T>;
|
|
172
|
+
/**
|
|
173
|
+
* Set options (replaces current options)
|
|
174
|
+
*/
|
|
175
|
+
set options(value: Partial<T>);
|
|
176
|
+
/**
|
|
177
|
+
* Creates a new instance with the specified default options
|
|
178
|
+
*/
|
|
179
|
+
create(defaultOptions?: Partial<T>): HOTP<T>;
|
|
180
|
+
/**
|
|
181
|
+
* Returns class options polyfilled with default values
|
|
182
|
+
*/
|
|
183
|
+
allOptions(): Readonly<ResolvedHOTPOptions>;
|
|
184
|
+
/**
|
|
185
|
+
* Reset options to defaults
|
|
186
|
+
*/
|
|
187
|
+
resetOptions(): this;
|
|
188
|
+
/**
|
|
189
|
+
* Generate an HOTP token
|
|
190
|
+
*/
|
|
191
|
+
generate(secret: SecretKey, counter: number): string;
|
|
192
|
+
/**
|
|
193
|
+
* Check if a token is valid for the given secret and counter
|
|
194
|
+
*/
|
|
195
|
+
check(token: string, secret: SecretKey, counter: number): boolean;
|
|
196
|
+
/**
|
|
197
|
+
* Verify a token (object-based API)
|
|
198
|
+
*/
|
|
199
|
+
verify(opts: {
|
|
200
|
+
token: string;
|
|
201
|
+
secret: SecretKey;
|
|
202
|
+
counter: number;
|
|
203
|
+
}): boolean;
|
|
204
|
+
/**
|
|
205
|
+
* Generate an otpauth:// URI for HOTP
|
|
206
|
+
*/
|
|
207
|
+
keyuri(accountName: string, issuer: string, secret: SecretKey, counter: number): string;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* @otplib/v12-adapter
|
|
212
|
+
*
|
|
213
|
+
* v12-compatible TOTP class implementation.
|
|
214
|
+
* Provides synchronous API wrapper around v13's TOTP implementation.
|
|
215
|
+
*/
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* v12-compatible TOTP class
|
|
219
|
+
*
|
|
220
|
+
* Provides the same API as otplib v12 TOTP class while using v13's
|
|
221
|
+
* implementation internally.
|
|
222
|
+
*
|
|
223
|
+
* @example
|
|
224
|
+
* ```typescript
|
|
225
|
+
* import { TOTP } from '@otplib/v12-adapter';
|
|
226
|
+
*
|
|
227
|
+
* const totp = new TOTP();
|
|
228
|
+
* const secret = 'JBSWY3DPEHPK3PXP';
|
|
229
|
+
* const token = totp.generate(secret);
|
|
230
|
+
* const isValid = totp.check(token, secret);
|
|
231
|
+
* ```
|
|
232
|
+
*/
|
|
233
|
+
declare class TOTP<T extends TOTPOptions = TOTPOptions> extends HOTP<T> {
|
|
234
|
+
constructor(defaultOptions?: Partial<T>);
|
|
235
|
+
/**
|
|
236
|
+
* Creates a new TOTP instance with the specified default options
|
|
237
|
+
*/
|
|
238
|
+
create(defaultOptions?: Partial<T>): TOTP<T>;
|
|
239
|
+
/**
|
|
240
|
+
* Returns class options polyfilled with TOTP default values
|
|
241
|
+
*/
|
|
242
|
+
allOptions(): Readonly<ResolvedTOTPOptions>;
|
|
243
|
+
/**
|
|
244
|
+
* Generate a TOTP token
|
|
245
|
+
*
|
|
246
|
+
* @param secret - The secret key
|
|
247
|
+
* @returns The OTP token
|
|
248
|
+
*/
|
|
249
|
+
generate(secret: SecretKey): string;
|
|
250
|
+
/**
|
|
251
|
+
* Check if a token is valid for the given secret
|
|
252
|
+
*
|
|
253
|
+
* @param token - The token to verify
|
|
254
|
+
* @param secret - The secret key
|
|
255
|
+
* @returns true if valid
|
|
256
|
+
*/
|
|
257
|
+
check(token: string, secret: SecretKey): boolean;
|
|
258
|
+
/**
|
|
259
|
+
* Check token and return the time window delta
|
|
260
|
+
*
|
|
261
|
+
* @param token - The token to verify
|
|
262
|
+
* @param secret - The secret key
|
|
263
|
+
* @returns Window delta (0 = current, positive = future, negative = past), null if invalid
|
|
264
|
+
*/
|
|
265
|
+
checkDelta(token: string, secret: SecretKey): number | null;
|
|
266
|
+
/**
|
|
267
|
+
* Verify a token (object-based API)
|
|
268
|
+
*
|
|
269
|
+
* @param opts - Verification options
|
|
270
|
+
* @returns true if valid
|
|
271
|
+
*/
|
|
272
|
+
verify(opts: {
|
|
273
|
+
token: string;
|
|
274
|
+
secret: SecretKey;
|
|
275
|
+
}): boolean;
|
|
276
|
+
/**
|
|
277
|
+
* Generate an otpauth:// URI for TOTP
|
|
278
|
+
*
|
|
279
|
+
* @param accountName - Account name for the URI
|
|
280
|
+
* @param issuer - Issuer name
|
|
281
|
+
* @param secret - The secret key (should be Base32 for QR codes)
|
|
282
|
+
* @returns The otpauth:// URI
|
|
283
|
+
*/
|
|
284
|
+
keyuri(accountName: string, issuer: string, secret: SecretKey): string;
|
|
285
|
+
/**
|
|
286
|
+
* Get time used in current step (seconds elapsed in current window)
|
|
287
|
+
*
|
|
288
|
+
* @returns Seconds used in current step
|
|
289
|
+
*/
|
|
290
|
+
timeUsed(): number;
|
|
291
|
+
/**
|
|
292
|
+
* Get time remaining until next token
|
|
293
|
+
*
|
|
294
|
+
* @returns Seconds remaining in current step
|
|
295
|
+
*/
|
|
296
|
+
timeRemaining(): number;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* @otplib/v12-adapter
|
|
301
|
+
*
|
|
302
|
+
* v12-compatible Authenticator class implementation.
|
|
303
|
+
* Provides same API as v12 with Base32 encoding support.
|
|
304
|
+
*/
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* v12-compatible Authenticator class
|
|
308
|
+
*
|
|
309
|
+
* The Authenticator class is a TOTP variant that uses Base32-encoded secrets
|
|
310
|
+
* by default, making it compatible with Google Authenticator and similar apps.
|
|
311
|
+
*
|
|
312
|
+
* @example
|
|
313
|
+
* ```typescript
|
|
314
|
+
* import { Authenticator } from '@otplib/v12-adapter';
|
|
315
|
+
*
|
|
316
|
+
* const authenticator = new Authenticator();
|
|
317
|
+
* const secret = authenticator.generateSecret();
|
|
318
|
+
* const token = authenticator.generate(secret);
|
|
319
|
+
* const isValid = authenticator.check(token, secret);
|
|
320
|
+
* const uri = authenticator.keyuri('user@example.com', 'MyApp', secret);
|
|
321
|
+
* ```
|
|
322
|
+
*/
|
|
323
|
+
declare class Authenticator<T extends AuthenticatorOptions = AuthenticatorOptions> extends TOTP<T> {
|
|
324
|
+
constructor(defaultOptions?: Partial<T>);
|
|
325
|
+
/**
|
|
326
|
+
* Creates a new Authenticator instance with the specified default options
|
|
327
|
+
*/
|
|
328
|
+
create(defaultOptions?: Partial<T>): Authenticator<T>;
|
|
329
|
+
/**
|
|
330
|
+
* Returns class options polyfilled with Authenticator default values
|
|
331
|
+
*/
|
|
332
|
+
allOptions(): Readonly<ResolvedAuthenticatorOptions>;
|
|
333
|
+
/**
|
|
334
|
+
* Generate an OTP token from a Base32 secret
|
|
335
|
+
*
|
|
336
|
+
* @param secret - Base32-encoded secret
|
|
337
|
+
* @returns The OTP token
|
|
338
|
+
*/
|
|
339
|
+
generate(secret: Base32SecretKey): string;
|
|
340
|
+
/**
|
|
341
|
+
* Check if a token is valid for the given Base32 secret
|
|
342
|
+
*
|
|
343
|
+
* @param token - The token to verify
|
|
344
|
+
* @param secret - Base32-encoded secret
|
|
345
|
+
* @returns true if valid
|
|
346
|
+
*/
|
|
347
|
+
check(token: string, secret: Base32SecretKey): boolean;
|
|
348
|
+
/**
|
|
349
|
+
* Check token and return the time window delta
|
|
350
|
+
*
|
|
351
|
+
* @param token - The token to verify
|
|
352
|
+
* @param secret - Base32-encoded secret
|
|
353
|
+
* @returns Window delta (0 = current, positive = future, negative = past), null if invalid
|
|
354
|
+
*/
|
|
355
|
+
checkDelta(token: string, secret: Base32SecretKey): number | null;
|
|
356
|
+
/**
|
|
357
|
+
* Verify a token (object-based API)
|
|
358
|
+
*
|
|
359
|
+
* @param opts - Verification options
|
|
360
|
+
* @returns true if valid
|
|
361
|
+
*/
|
|
362
|
+
verify(opts: {
|
|
363
|
+
token: string;
|
|
364
|
+
secret: Base32SecretKey;
|
|
365
|
+
}): boolean;
|
|
366
|
+
/**
|
|
367
|
+
* Encode a raw secret to Base32
|
|
368
|
+
*
|
|
369
|
+
* @param secret - Raw secret string
|
|
370
|
+
* @returns Base32-encoded secret
|
|
371
|
+
*/
|
|
372
|
+
encode(secret: SecretKey): Base32SecretKey;
|
|
373
|
+
/**
|
|
374
|
+
* Decode a Base32 secret to raw string
|
|
375
|
+
*
|
|
376
|
+
* @param secret - Base32-encoded secret
|
|
377
|
+
* @returns Raw secret string
|
|
378
|
+
*/
|
|
379
|
+
decode(secret: Base32SecretKey): SecretKey;
|
|
380
|
+
/**
|
|
381
|
+
* Generate a random Base32-encoded secret
|
|
382
|
+
*
|
|
383
|
+
* @param numberOfBytes - Number of bytes for the secret (default: 20)
|
|
384
|
+
* @returns Base32-encoded secret
|
|
385
|
+
*/
|
|
386
|
+
generateSecret(numberOfBytes?: number): Base32SecretKey;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
/**
|
|
390
|
+
* Pre-configured HOTP instance
|
|
391
|
+
*
|
|
392
|
+
* @example
|
|
393
|
+
* ```typescript
|
|
394
|
+
* import { hotp } from '@otplib/v12-adapter';
|
|
395
|
+
*
|
|
396
|
+
* const token = hotp.generate('JBSWY3DPEHPK3PXP', 0);
|
|
397
|
+
* const isValid = hotp.check(token, 'JBSWY3DPEHPK3PXP', 0);
|
|
398
|
+
* ```
|
|
399
|
+
*/
|
|
400
|
+
declare const hotp: HOTP<HOTPOptions>;
|
|
401
|
+
/**
|
|
402
|
+
* Pre-configured TOTP instance
|
|
403
|
+
*
|
|
404
|
+
* @example
|
|
405
|
+
* ```typescript
|
|
406
|
+
* import { totp } from '@otplib/v12-adapter';
|
|
407
|
+
*
|
|
408
|
+
* const token = totp.generate('JBSWY3DPEHPK3PXP');
|
|
409
|
+
* const isValid = totp.check(token, 'JBSWY3DPEHPK3PXP');
|
|
410
|
+
* ```
|
|
411
|
+
*/
|
|
412
|
+
declare const totp: TOTP<TOTPOptions>;
|
|
413
|
+
/**
|
|
414
|
+
* Pre-configured Authenticator instance
|
|
415
|
+
*
|
|
416
|
+
* @example
|
|
417
|
+
* ```typescript
|
|
418
|
+
* import { authenticator } from '@otplib/v12-adapter';
|
|
419
|
+
*
|
|
420
|
+
* const secret = authenticator.generateSecret();
|
|
421
|
+
* const token = authenticator.generate(secret);
|
|
422
|
+
* const isValid = authenticator.check(token, secret);
|
|
423
|
+
* ```
|
|
424
|
+
*/
|
|
425
|
+
declare const authenticator: Authenticator<AuthenticatorOptions>;
|
|
426
|
+
|
|
427
|
+
export { Authenticator, type AuthenticatorOptions, type Base32SecretKey, type CreateDigest, type CreateHmacKey, type CreateRandomBytes, HOTP, type HOTPOptions, HashAlgorithms, type KeyDecoder, type KeyEncoder, KeyEncodings, type SecretKey, TOTP, type TOTPOptions, authenticator, hotp, hotpDigestToToken, totp };
|