Design a Central Authentication Service

Difficulty: MEDIUMCategory: HLDFrequency: 1Asked On 2024-06-11

Problem Statement

Design a Central Authentication System that can securely authenticate users across multiple applications or services within an organization or ecosystem.

This service should be scalable, reusable, and support both session-based and token-based (e.g., JWT/OAuth2) authentication models.


🎯 Functional Requirements:

1.User Authentication
•Login using email/username + password
•Support for third-party logins (Google, SSO, etc.)
•Optional: Multi-Factor Authentication (MFA)
2.Token Generation
•Issue secure access tokens (JWT or opaque)
•Support access/refresh token model
3.Token Validation API
•Allow other services to validate tokens via a secure API
4.User Management
•Registration, password reset, account verification
•Store user metadata securely
5.Logout & Token Revocation
•Handle session and token revocation on logout
6.Session Management
•Optional server-side sessions (for web apps)
7.Audit Logging
•Track login attempts, token usage, password changes


⚙️ Non-Functional Requirements:

•High availability and horizontal scalability
•Secure storage of credentials (e.g., hashed with bcrypt)
•OAuth2 and/or OpenID Connect compliant
•Rate limiting and brute-force protection
•Fast token verification (<100ms typical)


🧱 Core Components:

ComponentDescription

Auth API Service

Main entry point for login, registration, token issuance

User Store

Stores user credentials and metadata securely

Token Service

Handles access/refresh token generation and validation

Session Store

Stores server-side session data (if required)

OAuth2 Provider Module

Enables 3rd party auth (e.g., Google, GitHub)

Admin Dashboard

Optional interface to manage users, sessions, policies

Audit Log System

Records auth-related events for monitoring and compliance


🛢️ Database Schema (Simplified):


users

1nginx 2CopyEdit 3user_id | email | password_hash | is_verified | created_at | last_login


refresh_tokens

1nginx 2CopyEdit 3token_id | user_id | token | expiry | revoked | created_at


audit_logs

1sql 2CopyEdit 3event_id | user_id | event_type | timestamp | ip_address


🔁 Flow (Login via Email/Password):

1.User submits login request → POST /auth/login
2.Validate credentials against hashed passwords
3.On success:
•Generate access_token (short-lived JWT)
•Generate refresh_token (longer-lived, stored in DB)
4.Return tokens to client
5.Other services validate access_token (or call GET /auth/validate)


🧰 Suggested Tech Stack:

LayerTools

API Layer

Node.js / FastAPI / Spring Boot

Token Signing

JWT + HMAC or RSA

DB

PostgreSQL / MongoDB

Cache

Redis (for session management)

Queue (optional)

Kafka (for audit logs, event-driven auth)

MFA

TOTP (Google Authenticator)


🧠 Advanced Features (Optional for HLD Rounds):

•Role-based Access Control (RBAC) or Attribute-based Access Control (ABAC)
•Device fingerprinting
•Session hijack detection
•Email + OTP login or magic link authentication
•Integration with LDAP or enterprise SSO


📈 Scalability & Reliability:

•Use Redis or memory-based session stores with expiry
•Deploy behind a load balancer
•Rate-limit login attempts per IP/user
•Encrypt tokens in transport (HTTPS) and optionally at rest
•Use stateless access tokens for microservice-scale performance


Solution

Comments