A production-ready backend authentication security system that dynamically evaluates login risk signals and adjusts user access levels based on trust scores, device familiarity, and verification outcomes.
AADE provides sophisticated risk-based authentication that adapts to user behavior and context. It evaluates multiple risk factors in real-time to assign trust levels to sessions, requiring additional verification only when necessary. This approach balances security with user experience.
- Multi-factor authentication (MFA) with email/password
- Real-time risk scoring based on device, location, IP reputation, and behavioral signals
- Dynamic session trust levels: FULL_TRUST, LIMITED_TRUST, UNVERIFIED, HIGH_RISK
- Step-up authentication for privilege escalation
- JWT-based token management with RS256 signing
- Refresh token rotation for enhanced security
- Argon2id password hashing
- Device fingerprinting and recognition
- Device trust registry with TRUSTED/UNTRUSTED/PENDING states
- Device revocation with session invalidation
- Device metadata tracking (browser, OS, IP address)
- Stateless session design with Redis caching
- Multiple concurrent session support
- Session termination and management
- Trust level enforcement per endpoint
- Comprehensive audit logging for all security events
- Rate limiting on all endpoints
- CORS and Helmet security headers
- Input validation with express-validator
- Prometheus metrics for monitoring
- Health check endpoints
- Node.js 18+ or 20+
- PostgreSQL 14+
- Redis 6+
- npm or yarn
# Clone the repository
git clone git@github.com:Victoryoola/Auth-System.git
cd authsystem
# Install dependencies
npm install
# Configure environment
cp .env.example .env
# Edit .env with your configuration
# Run database migrations
npm run migrate
# Start the development server
npm run devVisit http://localhost:3000/health to verify the server is running.
npm installCopy .env.example to .env and configure:
# Server Configuration
NODE_ENV=development
PORT=3000
# Database Configuration
DB_HOST=localhost
DB_PORT=5432
DB_NAME=aade_db
DB_USER=postgres
DB_PASSWORD=your_secure_password
# Redis Configuration
REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_PASSWORD=
# JWT Configuration
JWT_ACCESS_SECRET=your_jwt_access_secret_here
JWT_REFRESH_SECRET=your_jwt_refresh_secret_here
JWT_ACCESS_EXPIRY=15m
JWT_REFRESH_EXPIRY=7d
# Security Configuration
BCRYPT_ROUNDS=12
RATE_LIMIT_WINDOW_MS=900000
RATE_LIMIT_MAX_REQUESTS=100# Create PostgreSQL database
createdb aade_db
# Run migrations
npm run migrate# Development mode with hot reload
npm run dev
# Production mode
npm run build
npm start# Development
npm run dev # Start development server with hot reload
# Testing
npm test # Run all tests
npm run test:watch # Run tests in watch mode
npm run test:coverage # Run tests with coverage report
# Code Quality
npm run lint # Lint code
npm run lint:fix # Fix linting issues
npm run format # Format code with Prettier
npm run format:check # Check code formatting
# Build
npm run build # Build for production
npm start # Start production server
# Database
npm run migrate # Run database migrationsadaptive-risk-auth-engine/
├── src/
│ ├── config/ # Configuration files (database, redis)
│ ├── controllers/ # API route controllers
│ ├── middleware/ # Express middleware (auth, error handling)
│ ├── models/ # Data models and database schemas
│ ├── routes/ # API route definitions
│ ├── services/ # Business logic services
│ ├── types/ # TypeScript type definitions
│ ├── utils/ # Utility functions
│ └── index.ts # Application entry point
├── migrations/ # Database migration scripts
├── docs/ # Documentation
│ ├── api-reference.md
│ ├── integration-guide.md
│ ├── deployment-guide.md
│ ├── setup-guide.md
│ ├── database-schema.md
│ └── openapi.yaml
├── scripts/ # Utility scripts
└── tests/ # Test files
Sessions are assigned trust levels based on risk assessment:
- FULL_TRUST: Low risk, trusted device - full access to all operations
- LIMITED_TRUST: Moderate risk - restricted access to sensitive operations
- UNVERIFIED: Elevated risk or unknown device - requires step-up authentication
- HIGH_RISK: High risk - minimal or read-only access
The risk engine evaluates multiple factors:
- Device familiarity (known vs. unknown devices)
- Geographic anomalies (unusual locations)
- IP reputation (VPN, proxy, known bad actors)
- Login velocity (rapid login attempts)
- Failed authentication attempts
- Behavioral patterns
When a user attempts a sensitive operation with insufficient trust level, they can increase their session trust through step-up authentication:
- User initiates step-up challenge (EMAIL_OTP, SMS_OTP, or AUTHENTICATOR_APP)
- System sends verification code
- User verifies with OTP
- Session trust level is elevated to FULL_TRUST
| Endpoint | Method | Description | Auth Required |
|---|---|---|---|
/auth/login |
POST | User login with email/password | No |
/auth/mfa/verify |
POST | Verify MFA code | No |
/auth/refresh |
POST | Refresh access token | No |
/auth/logout |
POST | Logout and terminate session | Yes |
| Endpoint | Method | Description | Auth Required |
|---|---|---|---|
/auth/step-up/initiate |
POST | Initiate verification challenge | Yes (requireVerified) |
/auth/step-up/verify |
POST | Verify OTP | No |
/auth/step-up/resend |
POST | Resend OTP | No |
| Endpoint | Method | Description | Auth Required |
|---|---|---|---|
/devices |
GET | List user devices | Yes (requireVerified) |
/devices/:id/trust |
PUT | Update device trust status | Yes (requireVerified) |
/devices/:id |
DELETE | Revoke device | Yes (requireVerified) |
| Endpoint | Method | Description | Auth Required |
|---|---|---|---|
/sessions |
GET | List active sessions | Yes (requireVerified) |
/sessions/:id |
DELETE | Terminate session | Yes (requireVerified) |
| Endpoint | Method | Description | Auth Required |
|---|---|---|---|
/audit-logs |
GET | Retrieve audit logs | Yes (requireVerified) |
# 1. Login with credentials
curl -X POST http://localhost:3000/auth/login \
-H "Content-Type: application/json" \
-d '{
"email": "user@example.com",
"password": "SecurePassword123!",
"deviceInfo": {
"userAgent": "Mozilla/5.0...",
"screenResolution": "1920x1080",
"timezone": "America/New_York",
"language": "en-US"
}
}'
# Response (if MFA not required):
{
"accessToken": "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9...",
"refreshToken": "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9...",
"trustLevel": "FULL_TRUST",
"expiresIn": 900,
"requiresMFA": false
}
# 2. Make authenticated requests
curl -X GET http://localhost:3000/devices \
-H "Authorization: Bearer <access_token>"
# 3. Refresh token when expired
curl -X POST http://localhost:3000/auth/refresh \
-H "Content-Type: application/json" \
-d '{
"refreshToken": "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9..."
}'See the API Reference for detailed documentation and examples.
- Setup Guide - Installation, configuration, and initial setup
- Database Schema - Database structure, models, and migrations
- API Reference - Complete REST API documentation with examples
- OpenAPI Specification - OpenAPI 3.0 specification for API tools and code generation
- Integration Guide - How to integrate AADE into your application
- Web application integration (React, Vue, Angular)
- Mobile application integration (React Native, Flutter)
- Backend-to-backend integration
- Common scenarios and code examples
- Client libraries and SDKs
- Deployment Guide - Production deployment instructions
- Infrastructure requirements
- Docker deployment with docker-compose
- Kubernetes deployment with Helm
- Cloud deployments (AWS ECS/EKS, GCP Cloud Run/GKE, Azure AKS)
- Horizontal and vertical scaling
- Monitoring, logging, and observability
- Security hardening
- Backup and disaster recovery
- Node.js 18+ with TypeScript
- Express.js - Web framework
- PostgreSQL 14+ - Primary database
- Redis 6+ - Session caching and rate limiting
- Argon2id - Password hashing
- jsonwebtoken - JWT token management with RS256
- Helmet - Security headers
- express-rate-limit - Rate limiting
- express-validator - Input validation
- prom-client - Prometheus metrics
- Winston - Structured logging
- Jest - Testing framework
- Supertest - HTTP assertions
- fast-check - Property-based testing
- Argon2id password hashing with configurable cost
- JWT tokens signed with RS256 (asymmetric encryption)
- Refresh token rotation to prevent token replay attacks
- MFA support with TOTP/OTP
- Account lockout after failed attempts
- CORS configuration
- Helmet security headers (CSP, HSTS, X-Frame-Options, etc.)
- Rate limiting on all endpoints
- Input validation and sanitization
- Encrypted database connections (SSL/TLS)
- Secure Redis connections
- Environment variable management
- No sensitive data in logs
- Comprehensive audit logging for all security events
- Failed authentication tracking
- Suspicious activity detection
- Real-time metrics and alerting
- Connection pooling for PostgreSQL
- Redis caching for session data
- Stateless design for horizontal scaling
- Efficient database indexes
- Async/await for non-blocking I/O
- Login: ~100-200ms (including risk evaluation)
- Token refresh: ~50-100ms
- Device lookup: ~20-50ms (cached)
- Risk evaluation: ~50-100ms
- Horizontal scaling: Add more application instances
- Database: Read replicas, connection pooling
- Redis: Cluster mode for high availability
- Load balancing: Round-robin or least-connections
# Build image
docker build -t aade:latest .
# Run with docker-compose
docker-compose up -d# Deploy to Kubernetes
kubectl apply -f k8s/
# Scale deployment
kubectl scale deployment aade --replicas=5- AWS: ECS, EKS, or Lambda with API Gateway
- Google Cloud: Cloud Run, GKE, or Cloud Functions
- Azure: Container Instances, AKS, or Azure Functions
See the Deployment Guide for detailed instructions.
GET /health- Basic health checkGET /ready- Readiness check (includes DB and Redis connectivity)
Prometheus metrics available at /metrics:
http_requests_total- Total HTTP requestshttp_request_duration_seconds- Request latencyauth_attempts_total- Authentication attemptsrisk_evaluation_duration_seconds- Risk evaluation latencysession_creation_total- Session creationstoken_refresh_total- Token refreshes
Structured JSON logging with Winston:
- Request/response logging
- Error logging with stack traces
- Security event logging
- Performance metrics
| Variable | Description | Default | Required |
|---|---|---|---|
NODE_ENV |
Environment mode | development |
No |
PORT |
Server port | 3000 |
No |
DB_HOST |
PostgreSQL host | localhost |
Yes |
DB_PORT |
PostgreSQL port | 5432 |
No |
DB_NAME |
Database name | - | Yes |
DB_USER |
Database user | - | Yes |
DB_PASSWORD |
Database password | - | Yes |
REDIS_HOST |
Redis host | localhost |
Yes |
REDIS_PORT |
Redis port | 6379 |
No |
REDIS_PASSWORD |
Redis password | - | No |
JWT_ACCESS_SECRET |
JWT access token secret | - | Yes |
JWT_REFRESH_SECRET |
JWT refresh token secret | - | Yes |
JWT_ACCESS_EXPIRY |
Access token expiry | 15m |
No |
JWT_REFRESH_EXPIRY |
Refresh token expiry | 7d |
No |
BCRYPT_ROUNDS |
Argon2 cost parameter | 12 |
No |
RATE_LIMIT_WINDOW_MS |
Rate limit window | 900000 |
No |
RATE_LIMIT_MAX_REQUESTS |
Max requests per window | 100 |
No |
LOG_LEVEL |
Logging level | info |
No |
See .env.example for a complete configuration template.
# Run all tests
npm test
# Run tests in watch mode
npm run test:watch
# Run tests with coverage
npm run test:coverageThe project maintains high test coverage:
- Unit tests for all services
- Integration tests for API endpoints
- Property-based tests for critical logic
- Mock implementations for external dependencies
src/
├── services/
│ ├── AuthService.ts
│ ├── AuthService.test.ts
│ ├── RiskEngine.ts
│ └── RiskEngine.test.ts
└── index.test.ts
- Fork the repository
- Create a feature branch:
git checkout -b feature/my-feature - Make your changes
- Run tests:
npm test - Run linting:
npm run lint - Commit your changes:
git commit -am 'Add new feature' - Push to the branch:
git push origin feature/my-feature - Submit a pull request
- Follow TypeScript best practices
- Use ESLint and Prettier for code formatting
- Write tests for new features
- Document public APIs
- Keep functions small and focused
Database connection errors
# Check PostgreSQL is running
pg_isready -h localhost -p 5432
# Verify credentials in .envRedis connection errors
# Check Redis is running
redis-cli ping
# Should return: PONGPort already in use
# Change PORT in .env or kill the process
lsof -ti:3000 | xargs kill -9Migration errors
# Reset database and re-run migrations
dropdb aade_db
createdb aade_db
npm run migrateSee the Setup Guide for more troubleshooting tips.
- WebAuthn/FIDO2 support
- Biometric authentication
- Advanced anomaly detection with ML
- GraphQL API
- Admin dashboard
- Multi-tenancy support
- OAuth2/OIDC provider
- Passwordless authentication
MIT License - see LICENSE file for details.
- Documentation: docs/
- Issues: GitHub Issues
- Email: support@example.com
Built with modern security best practices and inspired by industry-leading authentication systems.