Skip to Content
Deep DivesSecurity

Security

Security is a critical concern for every system. This guide covers authentication, authorization, encryption, and modern security architectures.

Authentication vs Authorization

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Security Flow β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β”‚ Authentication │───▢│ Authorization β”‚ β”‚ β”‚ β”‚ "Who are you?" β”‚ β”‚ "What can you do?"β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β–Ό β–Ό β”‚ β”‚ Username/Password Role-Based Access β”‚ β”‚ OAuth/OIDC Permission Checks β”‚ β”‚ Biometrics Policy Evaluation β”‚ β”‚ MFA Resource Access β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

OAuth 2.0

Industry-standard protocol for authorization.

OAuth Roles

RoleDescription
Resource OwnerUser who owns the data
ClientApplication requesting access
Authorization ServerIssues tokens (Google, Auth0)
Resource ServerAPI holding protected resources

Authorization Code Flow

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ User β”‚ β”‚ Authorization β”‚ β”‚ (Browser)β”‚ β”‚ Server β”‚ β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚ β”‚ 1. Click "Login with Google" β”‚ │─────────────────────────────────────────────▢│ β”‚ β”‚ β”‚ 2. Redirect to authorization URL β”‚ │◀─────────────────────────────────────────────│ β”‚ β”‚ β”‚ 3. User authenticates & consents β”‚ │─────────────────────────────────────────────▢│ β”‚ β”‚ β”‚ 4. Redirect with authorization code β”‚ │◀─────────────────────────────────────────────│ β”‚ β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β”‚ Client β”‚ β”‚ β”‚ β”‚ (Backend) β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ 5. Exchange code for tokens β”‚ β”‚ │────────────────────────────▢│ β”‚ β”‚ β”‚ β”‚ β”‚ 6. Access + Refresh tokens β”‚ β”‚ │◀────────────────────────────│ β”‚ β”‚ β”‚ β”‚ β”‚ 7. Access protected API β”‚ β”‚ │────────────────────────────▢│ Resource β”‚ β”‚ β”‚ Server

OAuth Grant Types

Grant TypeUse CaseSecurity
Authorization CodeServer-side appsMost secure
Authorization Code + PKCEMobile/SPA appsSecure for public clients
Client CredentialsMachine-to-machineNo user involved
Refresh TokenGet new access tokenLong-lived sessions

OAuth Best Practices

  • Always use HTTPS
  • Use PKCE for public clients (mobile, SPA)
  • Short-lived access tokens (15 min - 1 hour)
  • Rotate refresh tokens on use
  • Validate redirect URIs strictly
  • Use state parameter to prevent CSRF

OpenID Connect (OIDC)

Authentication layer on top of OAuth 2.0.

OAuth 2.0: Authorization (access to resources) + OpenID Connect: Authentication (user identity) = Complete auth solution

OIDC Additions

ComponentPurpose
ID TokenJWT containing user identity
UserInfo EndpointGet user profile
Standard Scopesopenid, profile, email
Standard Claimssub, name, email, picture

ID Token Structure

{ "iss": "https://auth.example.com", "sub": "user123", "aud": "client_app_id", "exp": 1699999999, "iat": 1699996399, "name": "John Doe", "email": "john@example.com" }

JWT (JSON Web Tokens)

Compact, URL-safe tokens for transmitting claims.

JWT Structure

Header.Payload.Signature eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9. eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ. SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Header β”‚ {"alg": "HS256", "typ": "JWT"} β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ Payload β”‚ {"sub": "123", "name": "John", "exp": 1699999999} β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ Signature β”‚ HMACSHA256(base64(header) + "." + base64(payload), secret) β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

JWT Validation

def validate_jwt(token, secret): header, payload, signature = token.split('.') # 1. Verify signature expected_sig = hmac_sha256(f"{header}.{payload}", secret) if signature != expected_sig: raise InvalidSignature() # 2. Decode payload claims = base64_decode(payload) # 3. Validate claims if claims['exp'] < current_time(): raise TokenExpired() if claims['iss'] != expected_issuer: raise InvalidIssuer() if claims['aud'] != our_client_id: raise InvalidAudience() return claims

JWT Best Practices

PracticeRecommendation
AlgorithmRS256 (asymmetric) for distributed systems
ExpirationShort-lived (15 min for access tokens)
StorageHttpOnly cookies (not localStorage)
PayloadMinimal claims, no sensitive data
ValidationAlways verify signature, exp, iss, aud

JWT vs Sessions

AspectJWTSessions
StorageClient-sideServer-side
ScalabilityStateless, scales easilyRequires session store
RevocationDifficult (need blocklist)Easy (delete session)
SizeCan grow largeJust session ID
Use CaseAPIs, microservicesTraditional web apps

Zero Trust Architecture

β€œNever trust, always verify” - every request must be authenticated and authorized.

Traditional vs Zero Trust

Traditional (Perimeter-based): β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Corporate Network β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β”‚ Service │──│ Service │──│ Service β”‚ β”‚ ← Trust inside β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ Firewall ← Trust boundary β”‚ Internet (untrusted) Zero Trust: β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β”‚ Service │─?─│ Service │─?─│ Service β”‚ β”‚ ← Verify everything β”‚ β”‚ + Auth β”‚ β”‚ + Auth β”‚ β”‚ + Auth β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ Every request authenticated & authorized

Zero Trust Principles

PrincipleImplementation
Verify explicitlyAuthenticate every request
Least privilegeMinimal permissions needed
Assume breachLimit blast radius
Micro-segmentationNetwork isolation per service
Continuous validationRe-verify throughout session

Zero Trust Components

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Zero Trust Architecture β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β”‚ Identity β”‚ β”‚ Device β”‚ β”‚ Network β”‚ β”‚ β”‚ β”‚ Provider β”‚ β”‚ Trust β”‚ β”‚ Micro-seg β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚ β”‚ β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β”‚ Policy β”‚ β”‚ β”‚ β”‚ Engine β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚ β”‚ β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β”‚ Access β”‚ β”‚ β”‚ β”‚ Decision β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Encryption

Encryption at Rest

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Data at Rest β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ β”‚ β”‚ Application Data β”‚ β”‚ β”‚ β”‚ β”‚ β–Ό β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β”‚ DEK β”‚ Data Encryption Key β”‚ β”‚ β”‚ (per-data) β”‚ Encrypts actual data β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚ β”‚ β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β”‚ KEK β”‚ Key Encryption Key β”‚ β”‚ β”‚ (per-user) β”‚ Encrypts DEK β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚ β”‚ β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β”‚ Master Key β”‚ Stored in HSM/KMS β”‚ β”‚ β”‚ (KMS) β”‚ (AWS KMS, HashiCorp Vault) β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Encryption in Transit

ProtocolUse Case
TLS 1.3HTTPS, API calls
mTLSService-to-service
SSHServer access
VPN/WireGuardNetwork tunneling

Symmetric vs Asymmetric

TypeAlgorithmsUse Case
SymmetricAES-256-GCMBulk data encryption
AsymmetricRSA, ECDSAKey exchange, signatures
HybridBothTLS (asymmetric for key exchange, symmetric for data)

API Security

API Authentication Methods

MethodSecurityUse Case
API KeyLowSimple integrations
OAuth 2.0HighUser-authorized access
mTLSVery HighService-to-service
HMAC SignaturesHighWebhook verification

Rate Limiting

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Rate Limiting β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ β”‚ β”‚ Token Bucket: β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β”‚ Bucket: [●●●●●○○○○○] β”‚ β”‚ β”‚ β”‚ β–² β”‚ β”‚ β”‚ β”‚ Tokens added at fixed rate β”‚ β”‚ β”‚ β”‚ Request consumes 1 token β”‚ β”‚ β”‚ β”‚ Empty bucket = rate limited β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚ β”‚ β”‚ Limits by: β”‚ β”‚ - IP address β”‚ β”‚ - User ID β”‚ β”‚ - API key β”‚ β”‚ - Endpoint β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Input Validation

# Always validate and sanitize def create_user(request): # 1. Schema validation validate_schema(request.body, UserSchema) # 2. Type checking email = str(request.body.get('email', '')) # 3. Format validation if not is_valid_email(email): raise ValidationError("Invalid email") # 4. Business rules if User.exists(email): raise ValidationError("Email already registered") # 5. Sanitize before storage name = sanitize_html(request.body.get('name'))

OWASP Top 10

Critical Vulnerabilities

RankVulnerabilityPrevention
1Broken Access ControlEnforce authorization on every request
2Cryptographic FailuresUse strong encryption, proper key management
3InjectionParameterized queries, input validation
4Insecure DesignThreat modeling, secure patterns
5Security MisconfigurationHardened defaults, security headers
6Vulnerable ComponentsRegular updates, dependency scanning
7Auth FailuresMFA, strong passwords, secure sessions
8Data Integrity FailuresVerify signatures, validate updates
9Logging FailuresComprehensive logging, monitoring
10SSRFValidate URLs, allowlist destinations

SQL Injection Prevention

// ❌ Vulnerable String query = "SELECT * FROM users WHERE id = " + userId; // βœ… Safe - Parameterized query PreparedStatement stmt = conn.prepareStatement( "SELECT * FROM users WHERE id = ?" ); stmt.setString(1, userId);

XSS Prevention

// ❌ Vulnerable element.innerHTML = userInput; // βœ… Safe - Text content element.textContent = userInput; // βœ… Safe - Sanitize if HTML needed element.innerHTML = DOMPurify.sanitize(userInput);

Security Headers

# Prevent XSS Content-Security-Policy: default-src 'self'; script-src 'self' # Prevent clickjacking X-Frame-Options: DENY # Force HTTPS Strict-Transport-Security: max-age=31536000; includeSubDomains # Prevent MIME sniffing X-Content-Type-Options: nosniff # Control referrer Referrer-Policy: strict-origin-when-cross-origin

Interview Quick Reference

Common Questions

  1. β€œHow would you secure a REST API?”

    • OAuth 2.0 / JWT authentication
    • HTTPS everywhere
    • Rate limiting
    • Input validation
    • Security headers
    • Logging and monitoring
  2. β€œExplain OAuth 2.0 Authorization Code flow”

    • User clicks login
    • Redirect to auth server
    • User authenticates
    • Auth code returned
    • Backend exchanges code for tokens
    • Access protected resources
  3. β€œJWT vs Sessions - when to use which?”

    • JWT: APIs, microservices, stateless needs
    • Sessions: Traditional web apps, easy revocation needed

Security Checklist

  • Authentication implemented? (OAuth, OIDC)
  • Authorization on every endpoint?
  • Input validation and sanitization?
  • Encryption at rest and in transit?
  • Secure headers configured?
  • Rate limiting in place?
  • Logging and monitoring?
  • Dependencies regularly updated?
  • Secrets management? (not in code)
  • Security testing? (SAST, DAST, pen testing)
Last updated on