Microservices auth pattern: API Gateway validates JWT β extracts claims β passes user context in headers β downstream services trust gateway. Token exchange for service-to-service calls (audience-restricted tokens). Service accounts for machine identity.
Anti-patterns:Rolling your own auth β use battle-tested IdPs. Long-lived tokens without rotation β compromised token valid forever. Symmetric JWT (HS256) across services β shared secret = any service can forge tokens. No rate-limiting on login β credential stuffing.
Real-world:Google β OIDC tokens for all API access. Stripe β scoped API keys with restricted permissions. GitHub β fine-grained PATs replacing classic tokens. Cloudflare β Access replaces VPN with identity-aware proxy.
Authorization
Verifying permissions β "What can you do?" β enforce least privilege at every layer
Principles:Least privilege β minimal permissions needed. Deny by default β explicit grants only. Separation of duties β no single role can do everything. Auditability β log every access decision (who, what, when, allowed/denied).
Multi-tenant authorization: Every query must include tenant_id filter. Use row-level security (Postgres RLS) as defense-in-depth. Tenant isolation at every layer: API β service β DB. Test with cross-tenant access attempts.
Anti-patterns:Checking permissions only in UI β API must enforce independently. God role β one role with all permissions (audit nightmare). Hardcoded permissions in code β can't change without deploy. No tenant isolation β one customer sees another's data.
Real-world:Google β Zanzibar for all products (Drive, YouTube, Cloud). GitHub β RBAC (owner/admin/write/read) + fine-grained permissions. AWS β IAM policies (ABAC with conditions). Notion β workspace β team β page hierarchy (ReBAC).
Encryption
Protecting data at rest and in transit β the foundation of confidentiality and integrity
Use bcrypt/Argon2id for passwords (cost factor β₯ 12)
Use envelope encryption (KMS) for data at rest
Rotate keys regularly (90 days for data keys)
Enable TLS 1.3 everywhere, disable TLS 1.0/1.1
Use forward secrecy (ephemeral key exchange)
Store secrets in Vault/KMS, never in code/env vars
Never Do
MD5/SHA1 for passwords β rainbow table attacks
ECB mode β reveals patterns in ciphertext
Hardcoded keys in source code or Docker images
Reuse IVs/nonces β breaks AES-GCM completely
Roll your own crypto β use vetted libraries
Encrypt without authenticating β use AEAD modes
Log sensitive data β PII, tokens, keys in logs
Key rotation:Envelope encryption makes rotation easy β rotate the master key (KEK), re-wrap data keys. Data itself doesn't need re-encryption. Automatic rotation via KMS (AWS: every 365 days, configurable). Old key versions kept for decryption of existing data.
Compliance:PCI-DSS β encrypt cardholder data, rotate keys annually. HIPAA β encrypt PHI at rest and in transit. GDPR β encryption as a technical safeguard. SOC 2 β demonstrate encryption controls in audit.
Anti-patterns:Secrets in .env committed to git β scan with truffleHog/gitleaks. Same key for all environments β dev key compromise = prod compromise. No key rotation β compromised key valid forever. Client-side encryption without key escrow β data lost if key lost.
Real-world:AWS β KMS + envelope encryption for S3, EBS, RDS (default). Google β default encryption at rest with Google-managed keys + CMEK option. Stripe β PGP for API key delivery, AES-256 for card data. Signal β Double Ratchet protocol (forward secrecy per message).