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-without-db

v1.0.6

Published

Database less OTP verification with cryptography

Downloads

1,253

Readme

OTP verification using Cryptography and without any Database

Background

This module is derived from my blog post on the technique. You can read the blog post here to understand the technique and motivation.

Dependencies:

This module depends on the Crypto built in module on nodeJS. This is the only dependency and should work in any system that has crypto support (Which is technocally majority of the systems at this moment)

This module also uses some modern JavaScript features like Template literals, Default arguments and modern object literal. This might be a problem if you are planning to use it with older versions of nodeJS

Installation

You can use npm to install the package with the following code

npm install --save otp-without-db

Usage

You need additional tool to create OTP, and send SMS. This module only takes care of the verification part.

You can take a look at the otp-generator module to create OTP for your users.

Verification process

OTP verification is done in the following steps:

  1. A hash is created with the phone number/email address and then sent to the user.

  2. The user also receives the OTP via SMS, email or any other method.

  3. The user sends back the hash, OTP and phone/email used in the first request.

  4. The server verifies the information and returns true if they match.

Here's a diagram that shows the whole process:

OTP Verification process

Generating OTP Hash

We will use the otp-generator tool mentioned previously to create OTP. You can use any other tool or technique.

const otpGen  = require("otp-generator");
const otpTool = require("otp-without-db"); 
const key     = "secretKey"; // Use unique key and keep it secret

let phone = "88017009090";  
let otp   = otpGen.generate(6, { upperCase: false, specialChars: false, alphabets: false });  

let hash = otpTool.createNewOTP(phone,otp,key);

You can then send this hash to the user as response. The generate method takes these following arguments (In particular order),

createNewOTP(phoneOrEmail,otp,key="",expiresAfter=5,algorithm="sha256")

| Argument | Required | default | Description | | ------------- | -------- | ------- | -------------- | | phoneOrEmail | true | N/A | Phone or email | | otp | true | N/A | OTP | | key | false | "" | unique and secret key for HMAC see: createHmac| | expiresAfter | false | 5 | Expirty in minutes| | algorithm | false | sha256| Algorithm used for hashing the data. Any supported algorithm from OpenSSL|

Verifying OTP hash

The user should get the hash from the HTTP request and should get the real OTP via SMS or email.

Then when the user sends back the information, they can be verified with the following code:

otpTool.verifyOTP(phone,otp,hash,key);

This method returns a Boolean. If the verification is successful, it will return true.

This method also takes the following arguments in particular order:

verifyOTP(phone,otp,hash,key="",algorithm="sha256")

| Argument | Required | default | Description | | ------------- | -------- | ------- | -------------- | | phoneOrEmail | true | N/A | Phone or email | | otp | true | N/A | OTP | | hash | true | N/A | The hash that was returned from the user | | key | false | "" | unique and secret key for HMAC see: createHmac| | algorithm | false | sha256| Algorithm used for hashing the data. Any supported algorithm from OpenSSL|


This product is created with 🖤 by Anam Ahmed, Any improvements and PR is welcome.