🚀 Socket Launch Week Day 5:Introducing Repository Access Permissions and Custom Roles.Learn more
Sign In

totp-library

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

totp-library

authenticator integration

latest
npmnpm
Version
1.0.1
Version published
Maintainers
1
Created
Source

Two-Factor Authentication (TOTP) Library

This library provides functions for generating and validating Time-Based One-Time Passwords (TOTP) for implementing two-factor authentication in Node.js applications. It uses QR codes for easy setup and follows the TOTP standard.

Installation

npm install totp-library

Usage

1. Import the Library

   import { generateSecretKey, createQrCode, validateCode } from "totp-library";

2. Generate a Secret Key

   const userEmail = "user@example.com";
   const secretKey = generateSecretKey();
   const companyName = "Your_Company_Name";

3. Create and Display QR Code

   createQrCode(userEmail, secretKey, companyName)
   .then((qrCode) => {
   if (qrCode) {
   console.log("QR Code generated successfully.");
   console.log(qrCode);
   } else {
   console.log("Error generating QR Code.");
   }
   })
   .catch((error) => console.error("QR Code generation error:", error));

4. Validate User-Provided Code

   const isCodeValid = validateCode("Enter_User_Code", secretKey);

if (isCodeValid) {
console.log("Verification successful. Access granted!");
} else {
console.log("Verification failed. Access denied!");
}

Functions

generateSecretKey(): string
  • Generates a random secret key for a user.
createQrCode(email: string, secretKey: string, companyName: string): Promise<string>
  • Generates a QR code containing the TOTP URI for the provided user details.
validateCode(userProvidedCode: string, secretKey: string): boolean
  • Validates the user-provided TOTP against the generated TOTP.

Example

For a complete example, see the provided code at the end of this README.

Example Usage

// User details
const userEmail = "user@example.com";

// Generate a new secret key
const secretKey = generateSecretKey();

// Create and display QR code
createQrCode(userEmail, secretKey, "Your Company Name")
.then((qrCode) => {
if (qrCode) {
console.log("QR Code generated successfully.");
console.log(qrCode);
} else {
console.log("Error generating QR Code.");
}
})
.catch((error) => console.error("QR Code generation error:", error));

// Validate user-provided code
const isCodeValid = validateCode("Enter_User_Code", secretKey);

if (isCodeValid) {
console.log("Verification successful. Access granted!");
} else {
console.log("Verification failed. Access denied!");
}

Keywords

authenticator

FAQs

Package last updated on 07 Dec 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts