npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

otp-gen-val

v1.0.7

Published

An OTP generator and validator compatible with google, microsoft, saleforce authenticator apps

Downloads

178

Readme

One-Time Password library compatible with Authenticator apps: Google, Microsoft , Salesforce etc

A simple and secure one time password (otp) generator and validator based on crypto. It follows specifications defined in RFC4226 and RFC6238.

Installation

Requires Node.js v10+ to run.

Install the dependencies and devDependencies and start the server.

npm install otp-gen-val --save

Kindly carefully complete the procedures below to integrate OTP into your login flow.

Steps

  1. Generate and save key
  2. Generate QR data
  3. Validate OTP entered

1. Generate and save key

You will start by generating a key that will be in base 32 (according to specifications). Make sure you store this against the user in a permanent storage i.e db.

Usage


//CommonJS

const otpManager = require('otp-gen-val');
const key = otpManager.generateKey(); //store

//ES6

import { generateKey } from 'otp-gen-val';
const key = generateKey();//store

2. Generate QR data

Next we will generate the QR data that will be scanned or alternatively if they cannot scan they will enter data that is generated on the authenticator app.


//CommonJS

const otpManager = require('otp-gen-val');
const qrData = otpManager.generateQRData("your_generated_key","your_issuer_i.eg Google","your_account_name i.e [email protected]");

//ES6

import { generateQRData } from 'otp-gen-val';
const qrObject = generateQRData("your_generated_key","your_issuer_i.eg Google","your_account_name i.e [email protected]");

A user can now choose either to scan or enter details manually. If you want a user to scan you can extract qrData from the returned object and render it as follows:

<img src="${qrObject.qrData}" alt="${qrObject.label}" />

Alternatively you can tell them to enter the details manually as follows: Under account name and key that are required by autheticator app they can enter the account name and key fields that are in the return object.

3. Validate generated OTP

Finally we can proceed to validate generated OTP and if it passed we can proceed to allow the user to login/authenticate:

// CommonJS
const otp = require('otp-gen-val'); // Import the OTP library

// Validate the OTP
const userOTP = "entered_otp";
const secretKey = "generated_key";
const isValid = otp.validateOTP(userOTP, secretKey);

console.log(isValid ? "Valid OTP" : "Invalid OTP");

// ES6
import { validateOTP } from 'otp-gen-val'; // Import the OTP function

// Validate the OTP
const userOTP = "entered_otp";
const secretKey = "generated_key";
const isValid = validateOTP(userOTP, secretKey);

console.log(isValid ? "Valid OTP" : "Invalid OTP");

Contributors

We would like to thank the following contributors for their valuable contributions to this project:

If you would like to contribute to this project, please fork it on GitHub and submit a pull request.

License

This OTP library module is licensed under the MIT License.