DEV Community

Rohan Shukla
Rohan Shukla

Posted on

Enhance Your App's Security with OTP-Agent

📖 Introduction

In the rapidly evolving digital world, securing user data is crucial. otp-agent is a powerful JavaScript package designed to generate one-time passwords (OTPs) to strengthen your application's security. It supports various types of OTPs, including Time-based One-Time Passwords (TOTP), HMAC-based One-Time Passwords (HOTP), custom OTPs, and even Recovery Codes.


Why Choose otp-agent?

otp-agent streamlines the process of OTP generation and management, making it an indispensable tool for any secure application. Here are some key benefits:

  • 🛡️ Enhanced Security: Adds an additional layer of protection against unauthorized access.
  • 🔀 Versatility: Supports multiple OTP types, including Time-based One-Time Passwords (TOTP), HMAC-based One-Time Passwords (HOTP), customizable OTPs, and Recovery Codes.
  • 💡 Customizability: Allows you to create OTPs with specific characters and lengths, tailoring them to your needs.
  • 🌐 Easy Integration: Compatible with CommonJS and ES6 modules, making it simple to integrate into websites, mobile apps, or desktop applications.
  • 🔧 Wide Use Cases: Suitable for user authentication, transaction verification, and access control.

Installation

First, ensure you have Node.js installed. Then, install otp-agent using npm or Yarn:

With npm:

npm install otp-agent
Enter fullscreen mode Exit fullscreen mode

With Yarn:

yarn add otp-agent
Enter fullscreen mode Exit fullscreen mode

Key Features

🔑 OTP (One-Time Password)

Generate customizable OTPs up to 100 characters long:

import { generateOTP } from "otp-agent";

let otp = generateOTP();
console.log(otp); // Example output: 526775

otp = generateOTP({ length: 4, numbers: true, alphabets: true });
console.log(otp); // Example output: i5v3

otp = generateOTP({
    length: 8,
    numbers: true,
    alphabets: true,
    upperCaseAlphabets: true,
    specialChars: true,
});
console.log(otp); // Example output: NZ9O#akS
Enter fullscreen mode Exit fullscreen mode

✨ Custom OTP

Create OTPs with specified characters and lengths:

import { generateCustomOTP } from "otp-agent";

const customOTP = generateCustomOTP("Abc@123", { length: 5 });
console.log(customOTP); // Example output: 1@c3c
Enter fullscreen mode Exit fullscreen mode

⏳ TOTP (Time-based One-Time Password)

Generate time-based OTPs that change periodically:

import { generateTOTP } from "otp-agent";

// Define your secret key
const totp = generateTOTP({ secret: "JBSWY3DPEHPK3PXP" });
console.log(totp); // 123456
Enter fullscreen mode Exit fullscreen mode

🔐 HOTP (HMAC-based One-Time Password)

Create counter-based OTPs for persistent use until authenticated:

import { generateHOTP } from "otp-agent";

// Define your secret key and counter
const hotp = generateHOTP({ secret: "JBSWY3DPEHPK3PXP", counter: 1 });
console.log(hotp); // 654321
Enter fullscreen mode Exit fullscreen mode

🔐 Recovery Code

Recovery codes are used as backup authentication methods when the primary OTP (OTP, TOTP, HOTP) method is unavailable:

import { generateRecoveryCodes } from "otp-agent";

const recoveryCodes = generateRecoveryCodes({
    numberOfCodes: 4,
    codeLength: 8,
});
console.log(recoveryCodes); // Example output: ['44ba0b8c', '3a550413', 'f7cb9a40', '046ee4a0']
Enter fullscreen mode Exit fullscreen mode

Conclusion

Incorporating otp-agent into your web applications can significantly enhance security by generating and managing one-time passwords with ease. Its versatility, customizability, and easy integration make it an ideal choice for developers looking to protect user data and ensure secure access. Start using otp-agent today and safeguard your applications effortlessly.


Happy coding! 🚀

Top comments (0)