Skip to content

12 — Authentication Architecture

Authentication (who you are) and authorization (what you can do) are fundamental to every system. A well-designed auth architecture is secure, scalable, and doesn’t add friction for legitimate users.

Analogy: Authentication is like showing your ID at airport security. Authorization is like your boarding pass — it determines which gate you can access and which seat you can sit in.


Poor auth design leads to:

  • Security breaches (stolen credentials, session hijacking)
  • Poor user experience (repeated login prompts)
  • Scalability issues (session data doesn’t distribute across servers)
  • Compliance failures (GDPR, SOC 2, PCI-DSS)

flowchart LR
User["User"] --> Client["Client App<br/>Web / Mobile"]
Client --> Auth["Auth Server<br/>Login, Token Issue"]
Client --> API["API Gateway / Backend"]
Auth --> Token["JWT / Session Token"]
Token --> Client
Client --> API
API --> Validate["Token Validation<br/>Check JWT signature<br/>or session store"]
Validate --> Allow["Allow / Deny Access"]
style User fill:#f59e0b,color:#fff
style Client fill:#3b82f6,color:#fff
style Auth fill:#7c3aed,color:#fff
style API fill:#059669,color:#fff
style Validate fill:#6366f1,color:#fff

AspectAuthenticationAuthorization
DefinitionWho are you?What can you do?
MethodPassword, OTP, SSO, biometricRoles, permissions, policies
ExampleLogin with email/passwordAdmin can delete users, user can’t
ProtocolOAuth 2.0, SAML, OpenID ConnectRBAC, ABAC, ACLs
StorageUser credentials (hashed)Role/permission mappings

flowchart TB
Methods["Authentication Methods"] --> Session["Session-Based<br/>Server stores session<br/>Cookie-based auth"]
Methods --> Token["Token-Based (JWT)<br/>Self-contained token<br/>No server state needed"]
Methods --> SSO["Single Sign-On<br/>One login for multiple apps<br/>SAML / OIDC"]
Methods --> OAuth["OAuth 2.0<br/>Third-party login<br/>Google, GitHub, Facebook"]
Methods --> MFA["Multi-Factor Auth<br/>Password + OTP/Biometric<br/>Extra security layer"]
Methods --> Passwordless["Passwordless<br/>Magic link, OTP, biometric<br/>No passwords to steal"]
style Methods fill:#7c3aed,color:#fff
style Session fill:#3b82f6,color:#fff
style Token fill:#059669,color:#fff
style SSO fill:#f59e0b,color:#fff
style OAuth fill:#ef4444,color:#fff
style MFA fill:#6366f1,color:#fff

sequenceDiagram
participant User as User
participant Client as Client App
participant Auth as Auth Server
participant API as API Server
User->>Client: Login (email + password)
Client->>Auth: POST /auth/login
Auth->>Auth: Validate credentials
Auth->>Auth: Generate JWT (signed)
Auth-->>Client: Access Token + Refresh Token
Note over Client: Stores tokens (httpOnly cookie or secure storage)
Client->>API: GET /api/orders (with JWT)
API->>API: Verify JWT signature
API->>API: Check expiration
API->>API: Extract user_id + roles
alt Valid Token
API-->>Client: 200 + order data
else Expired Token
API-->>Client: 401 Unauthorized
Client->>Auth: POST /auth/refresh (with refresh token)
Auth-->>Client: New Access Token
Client->>API: Retry with new token
end

Storage MethodSecurityUXXSS ProtectionBest For
httpOnly Cookie✅ Secure (not JS-accessible)⭐ Auto-sent✅ YesWeb apps
Local Storage⚠️ Accessible to JSManual header attach❌ Vulnerable to XSSSPA with CSRF protection
Memory (JS variable)✅ Not persistedLost on refresh✅ YesVery sensitive apps
Secure Storage (Mobile)✅ OS-protectedNative APIs✅ YesMobile apps

sequenceDiagram
participant User as User
participant App as Your App
participant Provider as OAuth Provider<br/>(Google/GitHub)
participant API as Your API
User->>App: Click "Login with Google"
App->>Provider: Redirect to Google auth URL
User->>Provider: Login to Google
Provider-->>App: Authorization code
App->>Provider: Exchange code for tokens
Provider-->>App: Access Token + ID Token
Note over App: Verify ID Token (JWT), create session
App-->>User: Logged in ✅
User->>App: Access protected resource
App->>API: Request with session/header
API-->>App: Resource data

flowchart TB
Users["Users"] --> Roles["Roles"]
Roles --> Permissions["Permissions"]
Permissions --> Resources["Resources"]
Alice["Alice"] --> Admin["Admin Role"]
Bob["Bob"] --> Editor["Editor Role"]
Charlie["Charlie"] --> Viewer["Viewer Role"]
Admin --> PermA["Create, Read, Update, Delete<br/>All resources"]
Editor --> PermE["Create, Read, Update<br/>Content only"]
Viewer --> PermV["Read only<br/>Public content"]
style Users fill:#f59e0b,color:#fff
style Roles fill:#7c3aed,color:#fff
style Permissions fill:#059669,color:#fff

StrategyHow It WorksProsCons
Sticky sessionsSame server always handles same userSimpleUneven load, server affinity
Shared session storeRedis/Memcached for sessionsStateless serversExtra network hop
JWT (stateless)No server-side sessionHighly scalableToken revocation is hard
HybridJWT for auth, Redis for blacklistBalance of scale and controlTwo systems to manage

DecisionProsCons
JWTStateless, scalable, portableCan’t revoke easily, larger payloads
SessionEasy to revoke, simple to implementServer state, horizontal scaling harder
OAuth 2.0No password handling, popularExternal dependency, redirect flow
SSOOne login for many servicesSingle point of failure
MFAMuch more secureUser friction, recovery flows

StrategyDescription
Auth service as dedicated microserviceSeparates auth concerns from business logic
JWT with short TTL (15 min) + refresh tokensBalance of security and user experience
Token blacklist (Redis)Revoke JWTs before expiration
Rate limiting on auth endpointsPrevent brute force attacks
Read replicas for authOffload token verification reads

  1. What’s the difference between authentication and authorization?
  2. How does JWT authentication work? What are its pros and cons?
  3. Explain the OAuth 2.0 authorization code flow.
  4. How would you handle session management across multiple servers?
  5. How do you revoke a JWT token before it expires?

SystemAuth Approach
GoogleOAuth 2.0 + OpenID Connect for third-party apps
GitHubPersonal access tokens, OAuth apps, GitHub Apps
Auth0Identity as a Service — handles all auth flows
AmazonFederated SSO + IAM roles for internal services

  • Authentication = proving who you are; Authorization = what you’re allowed to do
  • JWT = self-contained token — no server session needed, but hard to revoke
  • Session = server-stored state — easy to revoke, but harder to scale horizontally
  • OAuth 2.0 = let users log in with Google/GitHub — you never handle passwords
  • MFA = the single most effective security measure (password + something you have)
  • Store tokens in httpOnly cookies for web apps (XSS-safe)
  • Always hash passwords with bcrypt/argon2 — never store plaintext passwords