🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more

otp-without-db

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install
o

otp-without-db

Database less OTP verification with cryptography

1.0.6
latest
69

Supply Chain Security

100

Vulnerability

98

Quality

76

Maintenance

100

License

Unpopular package

Quality

This package is not very popular.

Found 1 instance in 1 package

Version published
Weekly downloads
258
-54.01%
Maintainers
1
Weekly downloads
 
Created
Issues
3

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:

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

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

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

  • 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")
ArgumentRequireddefaultDescription
phoneOrEmailtrueN/APhone or email
otptrueN/AOTP
keyfalse""unique and secret key for HMAC see: createHmac
expiresAfterfalse5Expirty in minutes
algorithmfalsesha256Algorithm 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")
ArgumentRequireddefaultDescription
phoneOrEmailtrueN/APhone or email
otptrueN/AOTP
hashtrueN/AThe hash that was returned from the user
keyfalse""unique and secret key for HMAC see: createHmac
algorithmfalsesha256Algorithm used for hashing the data. Any supported algorithm from OpenSSL

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

FAQs

Package last updated on 20 Nov 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