Security Architecture#

Security is not a feature you bolt on after launch — it is an architectural concern woven through every layer of the system. This section covers the patterns for authentication at scale, zero-trust networking, and secrets management in distributed systems.


Authentication at Scale#

Token-Based Authentication (JWT)#

Client → Login(credentials) → Auth Service → JWT ← Client stores token
Client → API Request + JWT → API Gateway → Validates JWT → Backend Service
ComponentResponsibility
Auth ServiceValidates credentials, issues tokens
JWT (access token)Short-lived (5-15 min), carries claims (user ID, roles)
Refresh tokenLong-lived (days/weeks), used to get new access tokens
API GatewayValidates JWT signature, checks expiration, extracts claims

OAuth 2.0 / OpenID Connect Flows#

FlowUse CaseExample
Authorization Code + PKCEWeb apps, SPAs, mobile“Log in with Google”
Client CredentialsService-to-serviceBackend calling another backend
Device AuthorizationSmart TVs, CLIs“Enter this code at example.com/device”

Session vs Token#

Server-Side SessionsJWT Tokens
StorageServer (Redis, DB)Client (cookie, localStorage)
RevocationDelete session recordCannot revoke until expiry (unless denylist)
ScalingRequires shared session storeStateless — any server can validate
SizeSession ID (small)JWT payload (larger, contains claims)

Authorisation Patterns#

RBAC (Role-Based Access Control)#

User → has → Role → has → Permissions
Alice → Admin → [read, write, delete]
Bob → Editor → [read, write]
Carol → Viewer → [read]

Simple, works for most applications. Breaks down when permissions depend on the specific resource.

ABAC (Attribute-Based Access Control)#

Decisions based on attributes of the user, resource, and environment:

ALLOW if:
  user.department == resource.department
  AND user.clearance >= resource.classification
  AND environment.time is within business hours

More flexible than RBAC but harder to audit and reason about.

ReBAC (Relationship-Based Access Control)#

Permissions based on relationships in a graph (used by Google Zanzibar, AuthZed, Ory):

document:budget#viewer@user:alice    ← Alice can view the budget document
folder:finance#editor@group:finance  ← Finance group can edit the finance folder
document:budget#parent@folder:finance ← Budget inherits folder permissions

Best for: Google Docs-style sharing, hierarchical permissions, complex access graphs.


Zero-Trust Architecture#

Principle: Never trust, always verify. Every request is authenticated and authorised regardless of network location.

Traditional (Perimeter)Zero-Trust
Trust inside the firewallTrust nothing
VPN = accessIdentity = access
Flat internal networkMicrosegmented
Implicit trust between servicesmTLS between all services

Implementation#

LayerMechanism
Service identitymTLS certificates (SPIFFE/SPIRE)
Service-to-service authJWT or mTLS
Network segmentationService mesh (Istio, Linkerd), security groups
API gatewayRate limiting, WAF, JWT validation, IP filtering
User identitySSO + MFA at every entry point

Secrets Management#

The Problem#

Applications need secrets (database passwords, API keys, encryption keys). Hardcoding them is a security disaster. Environment variables are better but not enough.

Solutions#

ApproachToolHow It Works
VaultHashiCorp VaultCentralised secret store, dynamic credentials, lease/revocation
Cloud-nativeAWS Secrets Manager, GCP Secret ManagerManaged, rotation, IAM-integrated
Config injectionKubernetes Secrets, ECS task definitionInjected at deploy time
Parameter storeAWS SSM Parameter StoreHierarchical, versioned, encrypted

Secret Rotation Pattern#

1. Vault generates new DB credentials
2. Application fetches new credentials (or lease is renewed)
3. Old credentials are revoked after grace period
4. No human ever sees the password

Encryption at Rest and in Transit#

LayerMechanism
In transitTLS 1.3 (everywhere), mTLS (service-to-service)
At restAES-256 (S3 SSE, RDS encryption, EBS encryption)
Application-levelEnvelope encryption (data key encrypted by master key)
Key managementAWS KMS, GCP Cloud KMS, HashiCorp Vault

API Gateway as Security Layer#

ConcernImplementation
AuthenticationJWT validation, API key verification
Rate limitingPer-user, per-IP, per-endpoint
WAFOWASP Top 10 protection, SQL injection, XSS
IP filteringAllow/deny lists, geo-blocking
Request validationSchema validation, payload size limits
TLS terminationOffload TLS from backend services
LoggingAudit trail of all requests

Key Takeaways#

  • JWT for stateless auth at scale, sessions for simpler apps with revocation needs.
  • OAuth 2.0 + PKCE is the standard for web/mobile. Client Credentials for service-to-service.
  • RBAC for most apps, ABAC for policy-rich environments, ReBAC for document-sharing models.
  • Zero-trust: mTLS between services, identity-based access, no implicit trust from network location.
  • Never hardcode secrets. Use a vault or managed secret store with automatic rotation.
  • The API gateway is your security perimeter: auth, rate limiting, WAF, validation — all in one place.