Skip to content

anujpundir999/voice-biometric-authentication-backend

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

VoiceAuth MVP πŸŽ€πŸ”

A secure, production-ready Voice-Based Authentication System with a FastAPI backend.

2026 Edition - Uses SpeechBrain ECAPA-TDNN for state-of-the-art speaker verification.


πŸ”— Related Repository

Frontend Repository: This is the Backend API only. The React frontend is maintained in a separate repository.

πŸ‘‰ VoiceAuth Frontend (React SPA)


🌟 Features

  • 🎀 Voice Enrollment: Register your unique voice signature
  • πŸ” Voice Login: Authenticate using natural speech
  • πŸ›‘οΈ Anti-Replay Protection: Challenge-response system prevents recording attacks
  • πŸ” Basic Anti-Spoofing: Detects synthetic/replayed audio
  • πŸ“Š Audit Logging: Track all authentication attempts
  • πŸ”’ Encrypted Storage: Voice embeddings encrypted at rest (AES-256)
  • 🌍 Multilingual: Supports English, Hindi, Marathi, Spanish phrases
  • 🌐 REST API: OpenAPI/Swagger documented endpoints

πŸ—οΈ System Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                   FRONTEND (React SPA)                       β”‚
β”‚           (Separate Repository - See link above)             β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”‚
β”‚  β”‚   Pages    │──│  Components  │──│  UI Components   β”‚    β”‚
β”‚  β”‚ (Routes)   β”‚  β”‚  (Business)  β”‚  β”‚  (shadcn/ui)     β”‚    β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β”‚
β”‚         β”‚                β”‚                     β”‚             β”‚
β”‚         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜             β”‚
β”‚                          β”‚                                   β”‚
β”‚                 β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                       β”‚
β”‚                 β”‚   Custom Hooks    β”‚                       β”‚
β”‚                 β”‚  (State Logic)    β”‚                       β”‚
β”‚                 β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                       β”‚
β”‚                          β”‚                                   β”‚
β”‚                 β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                       β”‚
β”‚                 β”‚  API Service      β”‚                       β”‚
β”‚                 β”‚  (api.ts)         β”‚                       β”‚
β”‚                 β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                       β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                           β”‚ HTTP/REST
                           β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                  BACKEND API (This Repository)               β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”β”‚
β”‚  β”‚  FastAPI Server (main.py)                                β”‚β”‚
β”‚  β”‚  β€’ REST API endpoints                                    β”‚β”‚
β”‚  β”‚  β€’ CORS configuration for frontend                       β”‚β”‚
β”‚  β”‚  β€’ Request validation (Pydantic)                         β”‚β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜β”‚
β”‚                          β”‚                                   β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”β”‚
β”‚  β”‚  API Routes (api/routes.py)                              β”‚β”‚
β”‚  β”‚  β€’ POST /api/enroll - Voice enrollment                   β”‚β”‚
β”‚  β”‚  β€’ POST /api/challenge - Get login challenge             β”‚β”‚
β”‚  β”‚  β€’ POST /api/verify - Verify voice response              β”‚β”‚
β”‚  β”‚  β€’ GET  /api/users/{id}/status - User status             β”‚β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜β”‚
β”‚                          β”‚                                   β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”β”‚
β”‚  β”‚  Processing Layer (utils.py)                             β”‚β”‚
β”‚  β”‚  β€’ Audio preprocessing (resample, normalize, trim)       β”‚β”‚
β”‚  β”‚  β€’ ECAPA-TDNN embedding extraction (192-dim vectors)     β”‚β”‚
β”‚  β”‚  β€’ Cosine similarity matching                            β”‚β”‚
β”‚  β”‚  β€’ Challenge-response generation                         β”‚β”‚
β”‚  β”‚  β€’ Basic anti-spoofing checks                            β”‚β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜β”‚
β”‚                          β”‚                                   β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”β”‚
β”‚  β”‚  Storage Layer (storage.py)                              β”‚β”‚
β”‚  β”‚  β€’ SQLite database (users, enrollments, challenges)      β”‚β”‚
β”‚  β”‚  β€’ Fernet encryption for embeddings at rest              β”‚β”‚
β”‚  β”‚  β€’ Account lockout protection                            β”‚β”‚
β”‚  β”‚  β€’ Audit logging                                         β”‚β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸš€ Quick Start

Prerequisites

  • Python 3.10+
  • ~500MB disk space (for ML model)

Installation

# 1. Clone the repository
git clone https://github.com/anujpundir999/voice-biometric-authentication-backend.git
cd voice-biometric-authentication-backend

# 2. Create virtual environment
python -m venv venv
source venv/bin/activate  # Linux/macOS
# OR: venv\Scripts\activate  # Windows

# 3. Install dependencies
pip install -r requirements.txt

# 4. Download ML model (first run)
python utils.py  # Downloads ECAPA-TDNN model (~90MB)

# 5. Initialize database
python storage.py  # Creates SQLite database

# 6. Start the API server
uvicorn main:app --reload --host 0.0.0.0 --port 8000

Verify Installation

# API should be running at:
# http://localhost:8000

# Swagger documentation:
# http://localhost:8000/docs

# Health check:
curl http://localhost:8000/health

πŸ“ Project Structure

voice-biometric-authentication-backend/ 
β”œβ”€β”€ main.py              # FastAPI application entry point
β”œβ”€β”€ app.py               # Application configuration
β”œβ”€β”€ utils.py             # Core ML & audio utilities
β”œβ”€β”€ storage.py           # Database & encryption layer
β”œβ”€β”€ requirements.txt     # Python dependencies
β”œβ”€β”€ pyproject.toml       # Project metadata
β”‚
β”œβ”€β”€ api/                 # API layer
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ routes.py        # API endpoint definitions
β”‚   β”œβ”€β”€ schemas.py       # Pydantic request/response models
β”‚   └── openapi.yaml     # OpenAPI specification
β”‚
β”œβ”€β”€ models/              # ML models (auto-downloaded)
β”‚   └── ecapa_tdnn/      # SpeechBrain ECAPA-TDNN
β”‚
β”œβ”€β”€ data/                # Runtime data (gitignored)
β”‚   β”œβ”€β”€ voiceauth.db     # SQLite database (auto-created)
β”‚   └── temp/            # Temporary audio files
β”‚
β”œβ”€β”€ tests/               # Test suite
β”‚   β”œβ”€β”€ test_embedding.py
β”‚   β”œβ”€β”€ test_preprocessing.py
β”‚   β”œβ”€β”€ test_storage.py
β”‚   └── test_threshold.py
β”‚
└── docs/                # Additional documentation
    β”œβ”€β”€ AGENT_DOCUMENTATION.md
    β”œβ”€β”€ FRONTEND_INTEGRATION_GUIDE.md
    └── QUICK_REFERENCE.md

πŸ”Œ API Endpoints

Method Endpoint Description
GET /health Health check
POST /api/enroll Enroll voice for new user
POST /api/challenge Get challenge phrase for login
POST /api/verify Verify voice against challenge
GET /api/users/{user_id}/status Get user enrollment status
DELETE /api/users/{user_id} Delete user and enrollments

Example: Enrollment Flow

# 1. Enroll a new user
curl -X POST http://localhost:8000/api/enroll \
  -F "user_id=john_doe" \
  -F "audio=@voice_sample.wav"

# 2. Get challenge for login
curl -X POST http://localhost:8000/api/challenge \
  -H "Content-Type: application/json" \
  -d '{"user_id": "john_doe"}'

# 3. Verify with challenge response
curl -X POST http://localhost:8000/api/verify \
  -F "user_id=john_doe" \
  -F "challenge_id=<challenge_id>" \
  -F "audio=@challenge_response.wav"

πŸ”§ Configuration

Environment Variables

Create a .env file (optional):

# Server
HOST=0.0.0.0
PORT=8000
DEBUG=false

# Security
CORS_ORIGINS=http://localhost:3000,http://localhost:5173

# Audio processing
SIMILARITY_THRESHOLD=0.75
CHALLENGE_EXPIRY_SECONDS=60

Key Settings in utils.py

Setting Default Description
SIMILARITY_THRESHOLD 0.75 Cosine similarity threshold for match
SAMPLE_RATE 16000 Audio sample rate (Hz)
MIN_AUDIO_DURATION 1.5s Minimum recording length
MAX_AUDIO_DURATION 10s Maximum recording length
CHALLENGE_EXPIRY_SECONDS 60 Challenge validity window

πŸ§ͺ Testing

# Run all tests
pytest tests/ -v

# Run specific test
pytest tests/test_embedding.py -v

# Test with coverage
pytest tests/ --cov=. --cov-report=html

πŸ“Š Performance

Metric Value
Embedding Dimension 192
Embedding Time (CPU) ~200ms
Matching Time <1ms
EER (VoxCeleb) ~1-2%
Model Size ~90MB

πŸ”’ Security Features

  1. Challenge-Response: Random phrase each login, expires in 60s
  2. One-Time Challenges: Each challenge can only be used once
  3. Encrypted Storage: Embeddings encrypted with Fernet (AES-128-CBC)
  4. Account Lockout: 15-minute lockout after 5 failed attempts
  5. Audit Logging: All attempts logged with timestamps
  6. CORS Protection: Configurable allowed origins

🚧 Known Limitations

  • Basic anti-spoofing (not production-grade)
  • No voice activity detection (VAD)
  • Single-device enrollment
  • No continuous authentication
  • No liveness detection

πŸ› οΈ Troubleshooting

Model download fails

# Manual download from HuggingFace
# https://huggingface.co/speechbrain/spkrec-ecapa-voxceleb
# Place files in models/ecapa_tdnn/

Audio format not supported

# Convert to WAV (16kHz, mono)
ffmpeg -i input.mp3 -ar 16000 -ac 1 output.wav

CORS errors from frontend

# Update CORS origins in main.py
origins = [
    "http://localhost:3000",      # React dev server
    "http://localhost:5173",      # Vite dev server
    "https://your-frontend.com",  # Production
]

Low similarity scores

  • Ensure quiet recording environment
  • Speak clearly with consistent volume
  • Use 3-5 second recordings
  • Enroll multiple samples (up to 5)

πŸ“š References


🀝 Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit changes (git commit -m 'Add amazing feature')
  4. Push to branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

πŸ“„ License

MIT License - See LICENSE file


πŸ‘₯ Authors

Built with ❀️ for secure voice authentication


Quick Commands:

# Start development server
uvicorn main:app --reload --port 8000

# Run tests
pytest tests/ -v

# Reset database (development only)
python -c "from storage import reset_database; reset_database()"

# Check API docs
open http://localhost:8000/docs

About

A secure, production-ready Voice-Based Authentication System with a FastAPI backend.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages