Logo

dev-resources.site

for different kinds of informations.

Database less OTP- A concept

Published at
1/2/2025
Categories
webdev
systemdesign
programming
discuss
Author
sbrakeshrath
Author
12 person written this
sbrakeshrath
open
Database less OTP- A concept

Helllo, firstly I am not an expert, so please teach me if anything goes wrong in this post and please don't judge my english.

Let's talk about the topic...

How a otp system works (as per my knowlwdge)

First way

First a user come to the authentication page, then he sees a form where he has to enter his email or phone number and request a otp to verify that email/phone and he recives a otp. Then he copies that otp and paste it in the box and now that email/phone is verified.

Second way

In this way he recives a link in his mail, and when he clicks on it the email gets verified.

What happens in backend

When a user requests for otp by sending his email to backend, The backend creates a random string and store it in redis cache whith a expiry life time along with its email. And when the user sends that otp, in backend that otp gets mathed with the stored one.

The concept

Thought

Everytime I have created this system for authentication I had a thought do I really need a database and a redish instance costs 1500 INR for a month and I think it's a pricy for a toy project as we don't really want OTP for any-other purpose. So I thought using a hashing kind of thing to eleminate the database completely.

Approach

Lets divide the OTP into 2 parts

  1. The raw OTP string
  2. Hashed OTP string The raw OTP string will be sent to the email/phone The hashed OTP will be shared with the requested user (Browser)

And in the next request user will provide the OTP from email and stored hash OTP. And, in backend we can compare them and if it matches.

But a user can change the hash and send it. So here comes the JSON web token we can add the hash and email to a JWT so that user can't change the hash and we can also add a JWT expiry time for the OTP.

Normally a OTP is 6 digit number. So a person can create a rainbow table of all 6 digits hash and cand match it to know the OTP. To over come this we can use hmac who takes a starting key so brute force is not possibele.

export function hashedOTP(otp: string) {
  return crypto.createHmac('sha256', process.env.CRYPTO_SECRET).update(otp).digest('hex');
}
Enter fullscreen mode Exit fullscreen mode

Demo

For this I have created a working Demo - https://db-less-otp.netlify.app/

Backend Github Repo - https://github.com/SBRakeshRath/DB-less-OTP-Backend
Frontend Github Repo - https://github.com/SBRakeshRath/DB-less-OTP-Frontend

** I am Using a gmail account so I don't want you to spam this as you can send only 500 emails

Please point-out the vulnerabilities and how to fix them.

systemdesign Article's
30 articles in total
Favicon
Rate limiting : Global, Tumbling Window, and Sliding Window
Favicon
Designing the Spotify Top K
Favicon
Building RelaxTube: A Scalable Video Transcoding and Streaming Application
Favicon
Token Bucket Rate Limiter (Redis & Java)
Favicon
RabbitMQ: conceitos fundamentais
Favicon
CDNs in Distributed Systems: Beyond Caching for Better Performance
Favicon
Designing an Internet Credit Purchase System
Favicon
Context vs. State: Why Context is King in Software Systems
Favicon
Just thought about starting
Favicon
Hinted Handoff in System Design
Favicon
System Design: The Art of Balancing Trade-Offs
Favicon
Do you want to learn about System Design? I think this is a great article for you to get started with.
Favicon
Exploring the Intersection of Systems Engineering and Artificial Intelligence: Opportunities and Challenges
Favicon
From Concept to Deployment: The Lifecycle of a Systems Engineering Project
Favicon
Database less OTP- A concept
Favicon
Telemetry and Tracing: A Comprehensive Overview
Favicon
Asynchronous transaction in distributed system
Favicon
Fixed Window Counter Rate Limiter (Redis & Java)
Favicon
Kickstarting Weekly System Design Deep Dives: Building Scalable Systems
Favicon
Database Scaling NLogN 📈
Favicon
Navigating the World of Event-Driven Process Orchestration for Technical Leaders
Favicon
Load balancer vs Gateway vs reverse proxy vs forward proxy
Favicon
Kong API Gateway Setup Basic to advance usages
Favicon
Finding the Right Microsoft Platform for Your Applications
Favicon
PRESTO card Metrolinx fare system
Favicon
A Simple Guide for Choosing the Right Database
Favicon
loved reading it. Well Researched, Crisp and Informative #SystemDesign
Favicon
HTTP Caching in Distributed Systems
Favicon
HTTP Status Codes Explained
Favicon
Understanding Networking Communication Protocols

Featured ones: