OTP Utility for Google Authenticator

2.0.1 · active · verified Sun Apr 19

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.

Common errors

Warnings

Install

Imports

Quickstart

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.

import OTP from 'otp';

async function demonstrateOtp() {
  // Use a securely generated, base32-encoded secret.
  // For demonstration, a common test secret "JBSWY3DPEHPK3PXP" (Hello!) is used.
  const secret = 'JBSWY3DPEHPK3PXP'; // Replace with a strong, random secret in production

  console.log('Using Secret (Base32):', secret);

  // Initialize OTP with a secret and desired name
  const otpInstance = new OTP({
    name: 'MyOTPApp',
    secret: secret,
    codeLength: 6,
    timeSlice: 30, // Default is 30 seconds
  });

  // --- TOTP Demonstration ---
  console.log('\n--- TOTP ---');
  const currentTotpCode = await otpInstance.totp();
  console.log(`Current TOTP code (valid for ~30s): ${currentTotpCode}`);
  console.log('Google Authenticator URL (TOTP):', otpInstance.totpURL);

  // --- HOTP Demonstration ---
  console.log('\n--- HOTP ---');
  // For HOTP, the counter must be managed and incremented server-side
  const counter = 123;
  const hotpCode = await otpInstance.hotp(counter);
  console.log(`HOTP code for counter ${counter}: ${hotpCode}`);
  console.log('Google Authenticator URL (HOTP):', otpInstance.hotpURL);

  // --- Parsing an OTP URL ---
  console.log('\n--- URL Parsing ---');
  const exampleTotpUrl = otpInstance.totpURL; // Or any `otpauth://` URL
  const parsedOptions = OTP.parse(exampleTotpUrl);
  console.log('Parsed OTP options from URL:', parsedOptions);
  // Example: parsedOptions might contain { type: 'totp', label: 'MyOTPApp', secret: 'JBSWY3DPEHPK3PXP', ... }

  // --- Reviving an OTP object from JSON ---
  console.log('\n--- JSON Reviver ---');
  const stringifiedOtp = JSON.stringify(otpInstance);
  console.log('Stringified OTP object (truncated):', stringifiedOtp.substring(0, 100) + '...');
  const revivedOtp = JSON.parse(stringifiedOtp, OTP.reviveJSON);
  console.log('Revived OTP object secret matches original:', revivedOtp.secret === otpInstance.secret);
}

demonstrateOtp().catch(console.error);

view raw JSON →