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
▸ Authorization Models — RBAC → ABAC → ReBAC
Model
How It Works
Scalability
Use Case
Example Systems
RBAC
Users → Roles → Permissions
Simple, limited flexibility
Most apps, admin panels
GitHub, AWS IAM, K8s, PostgreSQL
ABAC
Rules on attributes (role, time, IP, resource tags)
Very flexible, complex policies
Healthcare, finance, compliance
AWS IAM Conditions, XACML
ReBAC
Graph of relationships (user→resource)
Handles sharing/hierarchy naturally
File sharing, social, multi-tenant
Google Zanzibar, AuthZed, Ory Keto
Policy-Based
Externalized rules (Rego/Cedar DSL)
Decoupled, testable, versionable
Microservices, multi-tenant SaaS
OPA, AWS Cedar, Cerbos
ACL
Per-resource access control list
Simple but doesn't scale
File systems, small apps
Linux permissions, S3 ACLs (legacy)
▸ Authorization Patterns in Microservices
Where to Enforce
API Gateway: coarse-grained (valid token? correct scope?)
Service layer: business rules (can this user edit THIS resource?)
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).