Single Central Authentication System

Difficulty: MEDIUMCategory: HLDFrequency: 1Asked On 2024-04-12

Problem Statement

Design a Central Authentication System that can authenticate and authorize users across multiple internal or external applications/services.


This system should provide secure, scalable, and reusable identity and access management for web, mobile, and backend systems using protocols like OAuth2, JWT, SAML, etc.


🎯 Functional Requirements:

1.User Registration & Login
•Register with email/password
•Login with username/password
•Support Single Sign-On (SSO) across services
2.Token Issuance
•Generate secure access tokens (JWT) and optional refresh tokens
•Token expiry & rotation logic
3.Third-Party Identity Integration
•Support for Google, GitHub, Microsoft logins (OAuth2)
4.Token Validation API
•Services should be able to validate token authenticity
5.Role-Based Access Control (RBAC)
•Assign roles to users and restrict access accordingly
6.Multi-Factor Authentication (Optional)
•TOTP (Google Authenticator) or Email OTP
7.Logout & Token Revocation
•Invalidate refresh tokens and revoke sessions
8.Audit Trail
•Store and expose login events, password changes, failed attempts


⚙️ Non-Functional Requirements:

•High availability (HA) and fault-tolerance
•Scalable to millions of users
•Secure storage of credentials (e.g., bcrypt-hashed)
•Session timeouts, brute force protection, rate limiting
•Compliance with GDPR/CCPA where applicable


🧱 Core Components:

ComponentDescription

Auth API Gateway

Entry point for login, signup, token operations

Identity Provider (IdP)

Issues tokens and handles federation

Token Service

Issues, refreshes, and validates JWTs

User Service

Manages user data and credentials

Session Store

Stores refresh tokens or active sessions (Redis, DB)

Audit Logging System

Tracks user activity and security events

Admin Dashboard (Optional)

For managing users, roles, and policies


🛢️ Database Schema (Simplified):

users

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

tokens

1nginx 2CopyEdit 3token_id | user_id | refresh_token | expires_at | revoked

roles

1nginx 2CopyEdit 3role_id | role_name | permissions

user_roles

1nginx 2CopyEdit 3user_id | role_id


🔁 Flow: Login and Token Issuance

1.User sends POST /auth/login with credentials
2.Verify credentials → generate access_token + refresh_token
3.Return tokens
4.Other services use Authorization: Bearer <access_token> in headers
5.Validate via JWT or API call to /auth/validate


🔐 Token Format (JWT Claims):

1json 2CopyEdit 3{ 4 "sub": "user123", 5 "email": "user@example.com", 6 "roles": ["admin", "editor"], 7 "iat": 1723453435, 8 "exp": 1723457035, 9 "iss": "central-auth-service" 10}


📈 Scalability Considerations:

•Stateless authentication using JWTs
•Redis-backed refresh token/session management
•Horizontal scaling via load balancer
•API rate limiting (e.g., login throttling)


🧰 Suggested Tech Stack:

LayerTools/Technologies

API Gateway

NGINX / Kong / Envoy

Backend

Node.js / Go / Java / Python

Auth Protocols

OAuth2 / OpenID Connect / JWT

Database

PostgreSQL / MongoDB

Cache

Redis

Federation

SAML / OAuth with Google/Facebook

Solution

Comments