0%
Why do two standards exist when one seems like it should be enough? Because they solve fundamentally different problems. OAuth 2.0 answers "what is this application allowed to do?" OpenID Connect answers "who is the human using this application?" Confusing the two is one of the most common security mistakes in system design, and interviewers test for it directly.
Authorization is about permissions. When you grant a third-party app access to your Google Drive files, OAuth 2.0 handles that. The app receives an access token scoped to specific permissions (read files, list folders) but learns nothing about who you are.
Authentication is about identity. When you click "Sign in with Google," the application needs to know your name, email, and user ID. OAuth 2.0 alone cannot provide this. OIDC extends OAuth 2.0 by adding an ID token: a signed JWT containing identity claims about the user.
The critical distinction: an access token proves what the bearer can do, not who the bearer is. An ID token proves who the user is, but should never be sent to resource servers as an authorization credential.
The most common security mistake in OAuth implementations is using access tokens as proof of identity. An access token says 'the bearer may read files'. It does not say 'the bearer is Alice.' If you need to know who the user is, you need OIDC and its ID token. If you only need to authorize API calls, OAuth 2.0 access tokens are sufficient.
Every OAuth 2.0 flow involves four actors:
Understanding these roles matters because each role has different security responsibilities. The client never sees the user's password. The resource server validates tokens but never issues them. The authorization server is the single trust anchor for the entire system.
The authorization code flow is the standard for web and mobile applications. PKCE (Proof Key for Code Exchange) is mandatory for public clients (SPAs, mobile apps) because they cannot securely store a client secret.
The flow works in six steps:
code_verifier and derives a code_challenge (SHA-256 hash)response_type=code, requested scopes, and the code_challengecode_verifier at the token endpointPKCE prevents interception attacks. Even if an attacker captures the authorization code (via a malicious redirect or browser history), they cannot exchange it without the code_verifier that only the legitimate client possesses.
OIDC is not a separate protocol; it is a layer on top of OAuth 2.0. When the client includes openid in the requested scopes, the authorization server returns an ID token alongside the access token. The ID token is a JWT containing claims like:
sub: a unique, stable identifier for the useremail: the user's email addressname: the user's display nameiss: the issuer (which authorization server created this token)aud: the audience (which client this token was issued for)exp: expiration timestamp| Token | Purpose | Audience | Lifetime |
| Access token | Authorize API calls | Resource server | Short (5-60 min) |
| ID token | Prove user identity | Client application | Short (5-60 min) |
| Refresh token | Obtain new access tokens | Authorization server | Long (hours to days) |
The refresh token exists because access tokens are intentionally short-lived. When an access token expires, the client uses the refresh token to get a new one without forcing the user to log in again. Refresh tokens are stored securely server-side and can be revoked instantly, unlike access tokens, which remain valid until expiration.
When there is no human user (a cron job calling an API, a microservice authenticating to another microservice), the client credentials flow applies. The client sends its own client_id and client_secret directly to the authorization server and receives an access token. No browser redirects, no user consent, no ID token. This flow is the bridge between OAuth 2.0 and service-to-service authentication.
JWTs matter because they solve a specific infrastructure problem: how does a resource server validate an access token without calling the authorization server on every request? In a microservices architecture handling 50,000 requests per second, adding a network round-trip to validate each token doubles latency and turns the authorization server into a bottleneck. JWTs eliminate that round-trip by making tokens self-validating.
A JWT is three base64url-encoded segments separated by dots: header.payload.signature.
Header specifies the signing algorithm and token type:
Payload carries claims: assertions about the subject and their permissions:
Key claims: sub (subject, who), iss (issuer, who created this), aud (audience, who should accept this), exp (expiration), nbf (not before), and custom claims like scope or roles for authorization decisions.
Signature is created by signing the header and payload with the issuer's private key. Anyone with the corresponding public key can verify the token was created by the trusted issuer and has not been tampered with.
RS256 (RSA + SHA-256) uses asymmetric cryptography. The authorization server signs with a private key. Resource servers verify with the public key, available at a JWKS (JSON Web Key Set) endpoint. This is the default for production systems because resource servers never need the private key; they only need the public key, which is safe to distribute.
HS256 (HMAC + SHA-256) uses a shared secret. Both the signer and verifier use the same key. This is simpler but dangerous in distributed systems: every service that validates tokens must possess the signing secret, and any compromised service can forge tokens for any other service.
ES256 (ECDSA + SHA-256) uses elliptic curve cryptography. Smaller keys (256-bit vs 2048-bit RSA) with equivalent security, and faster signature verification. Increasingly preferred for mobile and IoT scenarios where bandwidth and computation matter.
This is the core advantage of JWTs. When a resource server receives a request with a Bearer token, it validates the JWT entirely locally:
kid (key ID)exp has not passed (token not expired)nbf has passed (token is active)iss matches the expected authorization serveraud includes this service's identifierscope or roles for authorization decisionsNo database query. No network call to the authorization server. Validation takes microseconds. At 50,000 requests per second, this saves 50,000 network round-trips per second compared to opaque token introspection.
JWTs cannot be revoked before expiration. Once issued, a JWT is valid until its exp claim passes. If a user's account is compromised, you cannot invalidate their existing JWTs; you can only wait for them to expire. This is why access token lifetimes should be short (5-15 minutes). For immediate revocation, you need either a token blocklist (which sacrifices statelessness) or opaque tokens with server-side lookup.
Stateless validation has a cost: you lose the ability to revoke individual tokens. With opaque tokens stored in a database, revoking access means deleting the token record. With JWTs, the token is self-contained; there is no record to delete.
Production systems mitigate this with a layered approach:
JWTs grow with every claim you add. A token with 15 custom claims, nested role hierarchies, and multiple audience values can reach 2-4 KB. Since the token travels in every HTTP request header, this adds bandwidth overhead. Some API gateways reject headers larger than 8 KB, which limits how much data you can embed.
The payload is base64url-encoded, not encrypted. Anyone who intercepts a JWT can decode the payload and read all claims. Never put sensitive data (passwords, credit card numbers, PII beyond what is necessary) in JWT claims. If you need encrypted tokens, use JWE (JSON Web Encryption), but this adds complexity and computational cost.
A common mistake is treating base64url encoding as a form of security. It is not. Decoding a JWT payload requires zero keys or secrets. Paste any JWT into jwt.io and every claim is immediately visible. Design your claim set with the assumption that the token will be logged, cached, and inspected by intermediaries.
Standard TLS is one-sided: the client verifies the server's identity, but the server has no idea who the client is. When you visit your bank's website, your browser verifies the bank's TLS certificate, but the bank's server does not verify your browser's certificate. The bank authenticates you later via username and password at the application layer.
This asymmetry is fine for user-facing traffic, but inside a data center it creates a gap. If Service A calls Service B over standard TLS, Service B knows the connection is encrypted but cannot verify that Service A is who it claims to be. Any process on the network that can reach Service B's port can call it. mTLS closes this gap by requiring both sides to present and verify certificates.
The mTLS handshake extends the standard TLS handshake with a client certificate exchange:
After this handshake, Service B knows with cryptographic certainty that the caller is Service A (not an attacker on the same network), and the connection is encrypted end-to-end.
Each certificate is an X.509 document containing the service's identity (Common Name or Subject Alternative Name), the service's public key, the issuer's signature, and a validity period. The trust model works through a chain:
A service validates an incoming certificate by walking the chain: leaf certificate was signed by the intermediate CA, which was signed by the root CA, which is in the service's trust store. If any link breaks (expired certificate, unknown CA, revoked intermediate), the handshake fails and the connection is refused.
Manually configuring mTLS on every service is operationally painful. Service meshes like Istio and Linkerd solve this by offloading mTLS to sidecar proxies. Each pod gets a sidecar (Envoy in Istio, Linkerd-proxy in Linkerd) that handles certificate management, TLS termination, and mutual authentication transparently.
The application code makes a plain HTTP call to localhost. The sidecar intercepts the outgoing request, establishes an mTLS connection to the destination's sidecar, and forwards the request. The receiving sidecar terminates TLS and delivers the plain HTTP request to the application. Neither application needs TLS libraries, certificate loading code, or rotation logic.
This separation is powerful because it makes mTLS a platform concern rather than an application concern. A developer writing a Python Flask service does not need to understand X.509 certificates; the mesh handles it.
SPIFFE (Secure Production Identity Framework for Everyone) standardizes how workloads identify themselves across heterogeneous environments. Instead of tying identity to an IP address or hostname (which change frequently in container orchestration), SPIFFE assigns each workload a SPIFFE ID in URI format:
SPIRE (SPIFFE Runtime Environment) is the reference implementation. It runs an agent on each node that attests the identity of workloads (using Kubernetes service account tokens, AWS instance metadata, or other platform-specific attestation), obtains short-lived X.509 certificates from the SPIRE server, and delivers them to workloads via the SPIFFE Workload API. Certificates auto-rotate before expiration.
Certificates expire. In a cluster with 500 services, each with a certificate that lasts 24 hours, that is 500 certificate renewals per day, roughly one every 3 minutes. Manual rotation is impossible at this scale.
Automated rotation requires:
Real production systems rarely use a single authentication mechanism in isolation. The strongest architectures layer multiple mechanisms because each one solves a different part of the problem. mTLS proves identity at the transport layer (who is this service?), JWTs carry authorization at the application layer (what is this service allowed to do?), and cloud-native identities tie workloads to platform-level permissions.
Why use both when mTLS already proves identity? Because mTLS operates at the connection level: it tells you which service opened the connection, but not which user initiated the request or what permissions the request carries. A payment service calling an order service via mTLS proves it is the payment service, but the order service still needs to know: is this a read request or a write request? Which user's order is being accessed? Does the payment service have the "orders:read" scope?
JWTs carry this context. The payment service obtains a JWT from the authorization server (via client credentials flow) with scopes like "orders:read" and forwards it alongside the mTLS-authenticated connection. The order service validates the mTLS certificate at the transport layer and the JWT at the application layer. Two independent checks, two different attack surfaces covered.
In system design interviews, when discussing service-to-service authentication, describe both layers: mTLS for transport-level identity ('who is calling') and JWT for application-level authorization ('what are they allowed to do'). This defense-in-depth answer demonstrates security maturity and distinguishes you from candidates who mention only one mechanism.
The client credentials flow is how services obtain JWTs without user involvement. Each service is registered as an OAuth 2.0 client with its own client_id and client_secret (or uses a private key JWT for client authentication, which is more secure). The service authenticates to the authorization server and receives an access token scoped to its specific permissions.
The authorization server returns a JWT with the requested scopes. The service caches this token and refreshes it before expiration. Each service gets only the scopes it needs; the payment service cannot request "users:admin" scope because its client registration does not permit it.
Cloud providers offer identity mechanisms that eliminate the need for manually provisioned secrets:
The pattern is the same across all three clouds: the platform attests the workload's identity based on where it runs (which pod, which namespace, which node), and the workload receives short-lived tokens without managing long-lived secrets. No secrets to rotate, no credentials to leak.
What happens when a service running on AWS needs to call a GCP API? Or when a GitHub Actions workflow needs to deploy to AWS? Workload identity federation bridges trust boundaries by exchanging tokens.
The flow: (1) the workload obtains an OIDC token from its home platform (AWS, GitHub, etc.), (2) the workload presents this token to the target cloud's security token service, (3) the target cloud validates the token's issuer and claims against a pre-configured trust policy, (4) the target cloud issues a short-lived token for its own APIs. No long-lived credentials cross the boundary, only short-lived, scoped tokens that expire in minutes.
This eliminates the most dangerous pattern in multi-cloud architectures: storing AWS access keys in GCP secrets or vice versa. Leaked cross-cloud credentials are among the most common attack vectors in cloud security breaches.
The broader principle behind workload identity federation is the shift from "credentials you store" to "identity you prove." Static credentials (access keys, service account JSON files, client secrets) are liabilities: they can be copied, leaked, and used from anywhere. Platform-attested identities are tied to the workload's runtime environment and cannot be extracted or replayed from a different context. Every major cloud provider is moving in this direction because it fundamentally changes the threat model from "protect the secret" to "verify the caller."
Every authentication and token strategy involves trade-offs. There is no universally correct choice; the right answer depends on your threat model, operational maturity, and system architecture. Understanding these trade-offs is what separates a senior engineer from someone who just implements whatever the tutorial recommends.
This is the most common trade-off question in interviews.
| Dimension | JWT (stateless) | Opaque token (stateful) |
| Validation | Local: verify signature, check claims | Network: introspect at authorization server or lookup in session store |
| Revocation | Hard: valid until exp. Requires blocklist for immediate revocation | Easy: delete the token record from the store |
| Scalability | Excellent: no shared state, any server validates | Requires shared session store (Redis, database) |
| Token size | 500 bytes - 4 KB (carries claims) | 20-40 bytes (random string, claims stored server-side) |
| Information leakage | Payload is readable (base64url, not encrypted) | No information in the token itself |
| Latency | Microseconds (local crypto) | Milliseconds (network + store lookup) |
When to use JWTs: High-throughput APIs where validation speed matters. Architectures with many resource servers that would overload a central token introspection endpoint. Stateless microservices that avoid shared infrastructure.
When to use opaque tokens: Systems requiring instant revocation (financial, healthcare). Environments where token payload confidentiality matters. Simple architectures with a single API server where the overhead of a token store is negligible.
Hybrid approach (most common in production): JWT access tokens (short-lived, stateless validation) paired with opaque refresh tokens (stored server-side, instantly revocable). This combines fast validation for the hot path with instant revocation for the security path.
Short-lived tokens (5 minutes) mean a stolen token is useful for only 5 minutes, but the client must refresh frequently. At 10,000 active users with 5-minute tokens, the authorization server handles roughly 33 refresh requests per second: manageable, but not free.
Long-lived tokens (24 hours) reduce refresh overhead to once per day per user, but a stolen token grants access for up to 24 hours. For a compromised admin account, 24 hours of unauthorized access can be catastrophic.
The industry consensus has converged on 5-15 minute access tokens with refresh token rotation. Each refresh returns a new refresh token and invalidates the old one. This provides automatic credential rotation and detects token theft: if an attacker uses a stolen refresh token, the legitimate client's next refresh fails (because the token was already consumed), triggering forced re-authentication and security alerts.
mTLS provides the strongest transport-layer authentication, but the operational burden is real:
For teams without dedicated security infrastructure, the operational cost of mTLS may exceed its security benefit. API keys or JWT-based authentication are simpler alternatives for smaller deployments, with the understanding that they do not provide transport-layer identity verification.
The pragmatic approach is to adopt mTLS incrementally: start with a service mesh on a non-critical namespace, build operational confidence with certificate automation and monitoring, then expand to the full cluster. Trying to deploy mTLS across 500 services in a single rollout is a recipe for a certificate-related outage that undermines trust in the entire initiative.
| Scenario | Recommended approach | Why |
| User-facing web/mobile app | OIDC (authentication) + OAuth 2.0 JWT (authorization) | Need user identity + API access. PKCE for public clients. |
| Service-to-service (internal) | mTLS + client credentials JWT | mTLS for transport identity. JWT for fine-grained permissions. |
| External API (third-party) | OAuth 2.0 + API keys | API keys for rate limiting and billing. OAuth for delegated access. |
| CI/CD pipelines | Workload identity federation | No long-lived credentials. Platform-attested identity. |
| Single-page application | OIDC + BFF pattern | Tokens stay server-side. Browser never holds refresh tokens. |
| IoT devices | X.509 client certificates + short-lived JWT | Device cert for hardware identity. JWT for cloud API access. |
| Mechanism | What it does | Layer | Best for |
| OAuth 2.0 | Delegated authorization (scoped access tokens) | Application | Third-party API access, user-delegated permissions |
| OIDC | User authentication (ID tokens with identity claims) | Application | SSO, user login, identity verification |
| JWT | Self-contained token format (stateless validation) | Application | Access tokens, ID tokens, service tokens |
| mTLS | Mutual transport-layer authentication (certificates) | Transport | Service-to-service identity, zero-trust networks |
| SPIFFE/SPIRE | Platform-agnostic workload identity | Infrastructure | Multi-cloud, Kubernetes, dynamic environments |
| Workload Identity Federation | Cross-platform token exchange | Infrastructure | Multi-cloud access, CI/CD pipelines |
When designing authentication for a system, start with these questions:
Is there a human user?
Is it service-to-service within your infrastructure?
Is it cross-cloud or CI/CD?
Is it an external third-party integration?
Defense in depth: Layer authentication mechanisms. mTLS proves transport identity, JWT proves application-level permissions, cloud IAM restricts resource access. No single layer is sufficient alone.
Short-lived credentials: Access tokens should expire in minutes, not hours. Refresh tokens provide continuity without the security risk of long-lived access tokens. Certificates should auto-rotate in hours, not months.
Automate rotation: Manual credential rotation fails at scale. Use SPIRE for certificates, refresh token rotation for user tokens, and workload identity federation for cloud credentials. If a human must remember to rotate a credential, that credential will eventually expire in production.
Least privilege: Every token, certificate, and credential should carry the minimum permissions required. A service that reads orders should not have a token that can delete users. Scoping reduces blast radius when credentials are compromised.
In system design interviews, show the examiner you understand layered security by mentioning at least two authentication mechanisms for service-to-service calls (mTLS + JWT), explaining why short-lived tokens matter (revocation latency), and noting that cloud-native identities eliminate stored secrets. These three points cover the most common follow-up questions about auth in distributed systems.
Interview problems where this concept is central to the design.