{"library":"otp","title":"OTP Utility for Google Authenticator","description":"This package provides comprehensive utilities for generating and verifying One-Time Passwords (OTP), adhering to both HOTP (HMAC-Based One-Time Password Algorithm) as defined in RFC 4226 and TOTP (Time-Based One-Time Password Algorithm) as defined in RFC 6238. It is designed to be compatible with popular OTP mechanisms like Google Authenticator. The current stable version is 2.0.1, indicating active development and maintenance. The library differentiates itself by offering direct support for generating Google Authenticator-compatible URLs, parsing existing OTP URLs or base32-encoded secrets, and including a JSON reviver for seamless serialization and deserialization of OTP objects. While a specific release cadence is not formally stated, significant refactors, such as the TypeScript conversion in v1.0.0, highlight ongoing efforts to modernize and improve the codebase.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install otp"],"cli":null},"imports":["import OTP from 'otp';","import OTP from 'otp';\nconst options = OTP.parse('otpauth://...');","import OTP from 'otp';\nconst obj = JSON.parse(jsonString, OTP.reviveJSON);"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import OTP from 'otp';\n\nasync function demonstrateOtp() {\n  // Use a securely generated, base32-encoded secret.\n  // For demonstration, a common test secret \"JBSWY3DPEHPK3PXP\" (Hello!) is used.\n  const secret = 'JBSWY3DPEHPK3PXP'; // Replace with a strong, random secret in production\n\n  console.log('Using Secret (Base32):', secret);\n\n  // Initialize OTP with a secret and desired name\n  const otpInstance = new OTP({\n    name: 'MyOTPApp',\n    secret: secret,\n    codeLength: 6,\n    timeSlice: 30, // Default is 30 seconds\n  });\n\n  // --- TOTP Demonstration ---\n  console.log('\\n--- TOTP ---');\n  const currentTotpCode = await otpInstance.totp();\n  console.log(`Current TOTP code (valid for ~30s): ${currentTotpCode}`);\n  console.log('Google Authenticator URL (TOTP):', otpInstance.totpURL);\n\n  // --- HOTP Demonstration ---\n  console.log('\\n--- HOTP ---');\n  // For HOTP, the counter must be managed and incremented server-side\n  const counter = 123;\n  const hotpCode = await otpInstance.hotp(counter);\n  console.log(`HOTP code for counter ${counter}: ${hotpCode}`);\n  console.log('Google Authenticator URL (HOTP):', otpInstance.hotpURL);\n\n  // --- Parsing an OTP URL ---\n  console.log('\\n--- URL Parsing ---');\n  const exampleTotpUrl = otpInstance.totpURL; // Or any `otpauth://` URL\n  const parsedOptions = OTP.parse(exampleTotpUrl);\n  console.log('Parsed OTP options from URL:', parsedOptions);\n  // Example: parsedOptions might contain { type: 'totp', label: 'MyOTPApp', secret: 'JBSWY3DPEHPK3PXP', ... }\n\n  // --- Reviving an OTP object from JSON ---\n  console.log('\\n--- JSON Reviver ---');\n  const stringifiedOtp = JSON.stringify(otpInstance);\n  console.log('Stringified OTP object (truncated):', stringifiedOtp.substring(0, 100) + '...');\n  const revivedOtp = JSON.parse(stringifiedOtp, OTP.reviveJSON);\n  console.log('Revived OTP object secret matches original:', revivedOtp.secret === otpInstance.secret);\n}\n\ndemonstrateOtp().catch(console.error);","lang":"typescript","description":"This quickstart demonstrates how to initialize the OTP class with a secret, generate both Time-Based One-Time Passwords (TOTP) and HMAC-Based One-Time Passwords (HOTP), generate Google Authenticator-compatible URLs, parse existing OTP URLs, and utilize the provided JSON reviver for object serialization.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}