Skip to content

bikram73/OTP-Generator-TOTP-HOTP-ChallengeResponse_OTP-

Repository files navigation

πŸ” OTP Generator (TOTP, HOTP, Challenge Response OTP)

Professional multi-factor authentication system supporting TOTP, HOTP, and Challenge-Response OTP protocols. Built with Next.js 15, TypeScript, and Tailwind CSS.

✨ Overview

This project is a full authentication demo and learning platform for OTP-based security. It includes user registration, login, JWT-based sessions, QR code generation, OTP verification, and a challenge-response flow for higher-security use cases.

🧩 What's Included

  • πŸ” JWT authentication with HTTP-only cookies
  • πŸ§‘β€πŸ’» User registration and login flows
  • ⏱️ TOTP generation and verification
  • πŸ”’ HOTP generation and verification
  • πŸ§ͺ Challenge-response OTP workflows
  • πŸ“· QR code generation for authenticator apps
  • πŸ—ƒοΈ Supabase Postgres user and challenge data management
  • 🎨 Tailwind-based UI pages and layouts
  • 🧱 Reusable UI components for visual polish
  • πŸ›‘οΈ Secret encryption and password hashing utilities

πŸ“± Main Pages

  • / - Home / landing page
  • /login - Login screen
  • /register - Registration screen
  • /dashboard - User dashboard after authentication
  • /verify - OTP verification page
  • /challenge - Challenge-response authentication page
  • /about - Project and OTP comparison page
  • /qrcode - QR code display page
  • /dashboard/settings - User settings page

πŸ› οΈ Core Capabilities

πŸ”‘ Authentication

  • JWT token creation and verification
  • Secure cookie-based session handling
  • Login flow for password-based access
  • Auth checks for protected pages and API routes

⏳ OTP Support

  • TOTP for time-based one-time passwords
  • HOTP for counter-based one-time passwords
  • Challenge-response OTP for transaction-level validation
  • Secret generation and verification helpers

🧰 Security Utilities

  • Password hashing with bcrypt
  • Secret storage encryption helpers
  • Token validation helpers
  • Challenge lifecycle management

🎯 User Experience

  • QR code generation for authenticator apps
  • Animated and reusable UI components
  • Responsive layout built with Tailwind CSS
  • Dedicated pages for learning and testing OTP flows

πŸš€ Quick Start

Installation

# Install dependencies (use --legacy-peer-deps for React 19 compatibility)
npm install --legacy-peer-deps
powershell -ExecutionPolicy Bypass -Command "npm install --legacy-peer-deps"

# Or use the npm script
npm run install-deps

# Run development server
npm run dev
powershell -ExecutionPolicy Bypass -Command "npm run dev"

Open http://localhost:3000 in your browser.

Build for Production

npm run build
npm start

πŸ“ Project Structure

β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ api/              # API routes
β”‚   β”‚   β”œβ”€β”€ auth/         # Authentication endpoints
β”‚   β”‚   β”œβ”€β”€ otp/          # OTP generation/verification
β”‚   β”‚   β”œβ”€β”€ challenge/    # Challenge-Response endpoints
β”‚   β”‚   └── user/         # User information endpoints
β”‚   β”œβ”€β”€ dashboard/        # Dashboard page
β”‚   β”œβ”€β”€ login/            # Login page
β”‚   β”œβ”€β”€ register/         # Registration page
β”‚   β”œβ”€β”€ verify/           # OTP verification page
β”‚   β”œβ”€β”€ challenge/        # Challenge-Response page
β”‚   β”œβ”€β”€ about/            # About page with OTP comparisons
β”‚   └── qrcode/           # QR code display
β”œβ”€β”€ lib/
β”‚   β”œβ”€β”€ otp/              # TOTP/HOTP/Challenge-Response implementations
β”‚   β”œβ”€β”€ security.ts       # Security utilities
β”‚   β”œβ”€β”€ user-manager.ts   # User management
β”‚   β”œβ”€β”€ challenge-manager.ts # Challenge lifecycle management
β”‚   └── supabase-admin.ts # Supabase server client
β”œβ”€β”€ components/
β”‚   └── ui/               # Reusable visual components
└── package.json

πŸ›‘οΈ Authentication Methods

TOTP (Time-based One-Time Password)

  • RFC 6238 compliant
  • 30-second time windows
  • Automatic expiration and refresh
  • Mobile app compatible (Google Authenticator, Authy)
  • Best for: Daily user authentication, 2FA

HOTP (HMAC-based One-Time Password)

  • RFC 4226 compliant
  • Counter-based generation
  • Manual code generation
  • No automatic expiration
  • Best for: API authentication, event-driven auth

Challenge-Response OTP (Enterprise Security)

  • Server-generated unique challenges
  • Transaction-specific authentication
  • 5-minute expiration window
  • Single-use challenges
  • Maximum security for high-value operations
  • Best for: Financial transactions, administrative actions

πŸš€ Challenge-Response OTP Usage

Registration

  1. Visit /register
  2. Select "Challenge-Response (Enterprise Security)"
  3. Complete registration with username, email, password

Authentication Flow

  1. Login with username/password
  2. Redirected to /challenge page
  3. Generate Challenge - Server creates unique challenge
  4. Get Challenge Code - Via display or QR code
  5. Generate Response - Use authenticator with challenge + secret
  6. Verify Response - Enter 6-digit response code
  7. Access Granted - Redirect to dashboard

API Endpoints

Generate Challenge

POST /api/challenge/generate
{
  "context": "Wire Transfer $10,000 to Account 12345"
}

Response:
{
  "success": true,
  "challenge": {
    "id": "uuid",
    "challenge": "8-char-hex-string",
    "context": "transaction-details",
    "expiresAt": 1234567890
  }
}

Verify Response

POST /api/challenge/verify
{
  "challengeId": "challenge-uuid",
  "response": "123456"
}

Response:
{
  "success": true,
  "message": "Challenge verified successfully"
}

Get QR Code

GET /api/challenge/qrcode?challengeId=uuid

Response:
{
  "success": true,
  "qrCode": "data:image/png;base64,..."
}

Security Features

  • Unique per request: Each challenge is cryptographically random
  • Time-limited: 5-minute expiration window
  • Single-use: Cannot be reused after verification
  • Context-aware: Can include transaction-specific data
  • No replay attacks: Each challenge is unique
  • Transaction binding: Challenge includes transaction details
  • Server control: Server initiates each authentication

Use Cases

  • High-value transactions: Wire transfers, large purchases
  • Administrative actions: User management, system configuration
  • API authentication: Critical operations, data exports
  • Enterprise security: Maximum protection for sensitive operations

πŸ” Features

  • βœ… TOTP (Time-based One-Time Password) - RFC 6238
  • βœ… HOTP (HMAC-based One-Time Password) - RFC 4226
  • βœ… Secure Authentication with JWT tokens
  • βœ… AES-256 Encryption for secret storage
  • βœ… bcrypt Password Hashing
  • βœ… QR Code Generation for mobile authenticators
  • βœ… Modern UI with Tailwind CSS
  • βœ… TypeScript for type safety

πŸ› οΈ Tech Stack

  • Next.js 15 - React framework with App Router
  • TypeScript - Type safety
  • Tailwind CSS - Modern styling
  • speakeasy - OTP generation (RFC compliant)
  • bcryptjs - Password hashing
  • jsonwebtoken - JWT authentication
  • qrcode - QR code generation
  • framer-motion - Motion and animations
  • lucide-react - Icons
  • zod - Schema validation

πŸ“ Environment Variables

Create a .env.local file:

JWT_SECRET=your-secret-key-here
NODE_ENV=development

πŸ”’ Important Note

  • Replace JWT_SECRET with a long, random value before using the app in production.
  • Keep the same secret across restarts if you want existing login tokens to remain valid.
  • Do not commit .env.local to source control.

🎯 API Endpoints

Authentication

  • POST /api/auth/register - Register new user
  • POST /api/auth/login - User login

OTP

  • GET /api/otp/generate - Generate OTP code
  • GET /api/otp/generate-next - Generate the next OTP code
  • POST /api/otp/verify - Verify OTP code

QR Code

  • GET /api/qrcode - Get QR code for mobile app

Challenge Flow

  • GET /api/challenge/active - View active challenge data
  • POST /api/challenge/generate - Create a new challenge
  • POST /api/challenge/generate-response - Generate a challenge response
  • POST /api/challenge/verify - Verify a challenge response
  • POST /api/challenge/verify-fixed - Verify using a fixed challenge flow
  • GET /api/challenge/qrcode - Get a QR code for a challenge
  • POST /api/challenge/quick-solve - Quick challenge solving helper
  • POST /api/challenge/clear-all - Clear stored challenges

User Data

  • GET /api/user/info - Get authenticated user information
  • GET /api/user/secret - Access user secret data
  • POST /api/user/convert-to-totp - Convert a user to TOTP flow

πŸ”’ Security Features

  • JWT-based authentication
  • HTTP-only cookies
  • Password strength validation
  • Encrypted secret storage
  • Session-based OTP access flow
  • Challenge replay prevention
  • Rate limiting (to be implemented)

πŸ“± Mobile Authenticator Support

Compatible with:

  • Google Authenticator
  • Microsoft Authenticator
  • Authy
  • LastPass Authenticator
  • Any TOTP/HOTP compatible app

πŸ§ͺ Helpful Scripts

  • npm run dev - Start the development server
  • npm run build - Build the production app
  • npm start - Run the production build
  • npm run install-deps - Install dependencies with legacy peer dependency support

About

πŸ”The OTP Generator Application is a professional-grade multi-factor authentication (MFA) system built with Next.js 15, TypeScript, and Tailwind CSS. The project implements industry-standard OTP protocols along with an enterprise-level challenge-response authentication mechanism, providing a secure and extensible authentication solution.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors