Authentication and Authorization for Backend Engineers
The evolution of identity, stateful sessions vs stateless JWTs, cookies, API keys, OAuth 2.0/OIDC delegation, RBAC, and timing attack mitigation.
Authentication determines 'Who are you?' (identity verification), while Authorization determines 'What can you do?' (permissions). Modern backends balance centralized stateful sessions (Redis + cookies) for instant revocation against stateless JWTs for distributed horizontal scale, hardening systems with generic error responses, constant-time comparisons, and OAuth 2.0 delegation.
1. Authentication vs. Authorization: The 'Who' vs. The 'What'
Every secure backend system separates access control into two distinct, sequential phases: verifying identity first, and then validating permissions.
1Authentication (AuthN — The 'Who')
2Authorization (AuthZ — The 'What')
2. The Historical Evolution of Identity & Hashing
Understanding how security evolved explains why modern backend protocols are designed the way they are:
| Era | Mechanism | Vulnerability & Innovation |
|---|---|---|
| Pre-Industrial | Implicit Village Trust | Personal vouching and handshakes. Zero technical scalability beyond local communities. |
| Medieval | Wax Seals & Physical Signets | Authentication based on possession. Gave rise to early physical forgery attacks. |
| Industrial | Telegraph Passphrases | Shifted security to 'something you know' across long-distance wire communications. |
| 1960s Mainframes | Plaintext Passwords ➔ Hashing | An accidental password file print at MIT led to the invention of one-way cryptographic hashing (irreversible mathematical transforms like bcrypt/Argon2). |
| Modern MFA | Multi-Factor Authentication | Combines Knowledge (Password), Possession (Authenticator OTP / Phone), and Inherence (Biometrics). |
3. Stateful Sessions vs. Stateless JWTs
Because HTTP is fundamentally stateless, backend engineers use two primary patterns to maintain user authentication across requests:
1Stateful Sessions (Server-Side Memory)
2Stateless JWTs (Client-Side Tokens)
| Feature | Stateful Sessions (Redis/DB) | Stateless JWTs |
|---|---|---|
| Storage Location | Server-side store (Redis / Database) | Client-side (Memory / HttpOnly Cookie) |
| Revocation | Instant (delete session from Redis) | Difficult before token expiration (requires token blocklists) |
| Microservice Scaling | Requires shared cache lookup on every hop | Zero database hits; verified locally via shared secret |
| Payload Size | Tiny (32-character opaque Session ID) | Larger (base64 encoded JSON string with claims) |
5. API Keys, OAuth 2.0 & OpenID Connect (OIDC)
1API Keys (Machine-to-Machine)
2OAuth 2.0 (Delegated Authorization)
3OpenID Connect (OIDC Identity Layer)
6. Role-Based Access Control (RBAC)
RBAC restricts system access based on assigned user roles. Backend middleware intercepts requests, extracts user claims, and checks if the role possesses the required permission before running business logic.
7. Critical Security: Generic Errors & Timing Attack Mitigation
Building secure authentication requires protecting against subtle side-channel attacks:
1Generic Error Messages (Prevent Enumeration)
2Equalized Latency & Constant-Time Comparisons (Prevent Timing Attacks)
POST /api/auth/login HTTP/1.1
Host: api.example.com
Content-Type: application/json
{
"email": "nonexistent_user@example.com",
"password": "wrong_password_123"
}HTTP/1.1 401 Unauthorized
Content-Type: application/json
{
"error": "Invalid email or password"
}19. Login & Token Issuer (JWT vs Session)
Simulates secure credential verification with constant-time timing protection, generic error responses, and dual issuance of stateless JWT and stateful Session ID.
20. Stateless JWT Signature Verification
Cryptographically verifies the HMAC-SHA256 signature and decodes token claims in memory without requiring any database lookups.
21. Role-Based Access Control (RBAC Guard)
Enforces permission boundaries across viewer, editor, and admin roles, differentiating 401 Unauthorized from 403 Forbidden.