Lesson 08 of 14 · Human and application identity
Validating tokens and propagating identity
Validate a token the way an attacker hopes you won't: signature with pinned algorithms, issuer, audience, expiry and tenant. Then carry the end user's identity safely across a chain of services with forwarded or exchanged tokens, alongside mTLS workload identity.
Reading a token is not validating it
A JWT is three base64url parts: header.payload.signature. Anyone can decode the first two. Validation means proving it was issued by whom you trust, for you, and is still valid.
A permission slip is only good if it carries the head teacher's real signature, was written for this trip (not last week's football match), is dated today, and is for your class. Checking only that it "looks like a permission slip" lets anyone in.
The validation checklist
1. Parse the header. Look up the key by kid in the issuer's JWKS (cached; refresh on unknown kid).
2. Verify the signature with an algorithm from YOUR allow-list (e.g. RS256, ES256).
Reject alg=none. Never let the header choose between RSA and HMAC.
3. iss == the issuer you expect (exact match).
4. aud contains THIS API's identifier.
5. exp in the future, nbf/iat sensible, small clock skew (≤ 60 s).
6. Required scopes / roles present for this operation.
7. Tenant claim matches the tenant of the resource being accessed.
8. Token type is what you expect (an access token, not an ID token sent to an API).
Use a maintained library for 1-5. Steps 6-7 are your business logic and the place where real breaches happen.
The two classic bypasses
alg = none: some old libraries accepted unsigned tokens. Algorithm confusion: a server expecting RS256 is tricked into HS256 and verifies the attacker's HMAC using the public key as the secret. Both are prevented by pinning algorithms per issuer.
Lifetimes and revocation
- Access tokens: minutes (5-60). Short enough that you rarely need revocation.
- Refresh tokens: longer, rotated on every use, bound to the client; revoked on logout or risk.
- High-risk actions (changing payment details, admin operations): check freshness, require recent MFA (
auth_time,acr/amrclaims), or use token introspection to ask the IdP whether the token is still active.
Where to validate
- At the edge (gateway): reject bad tokens early, apply rate limits by client or tenant.
- In each service (or its proxy): defence in depth; the gateway might be bypassed from inside.
- Strip identity headers such as
X-User-Idfrom external requests at the edge. Never trust a user identity that arrives as a plain header unless your own gateway set it after validation, and only over mTLS.
In a mesh, the proxy can do the generic checks:
apiVersion: security.istio.io/v1
kind: RequestAuthentication
metadata:
name: jwt
namespace: shop
spec:
selector:
matchLabels: { app: orders }
jwtRules:
- issuer: "https://login.example.com/"
jwksUri: "https://login.example.com/.well-known/jwks.json"
audiences: [ "orders-api" ]
forwardOriginalToken: true
A RequestAuthentication rejects invalid tokens but still lets requests without a token through. Pair it with an AuthorizationPolicy that requires requestPrincipals (and specific claims) to make a token mandatory.
Carrying user identity across services
A request often passes through several services. Each hop has two separate facts:
user ─token(aud=gateway)─► gateway ─► orders ─► payments ─► ledger
mTLS: "orders is calling" (workload identity)
token: "on behalf of user u-42" (user identity)
Options for the user part:
- Forward the original token. Simple. But the token's audience must be accepted by every downstream service, so a token stolen from one service works everywhere.
- Token exchange (RFC 8693). Each service swaps the incoming token at the IdP for a new one: audience = the next service, narrower scopes, an
act(actor) claim recording who is acting for the user. Better containment, more IdP traffic. - Internal signed context. The gateway validates the external token and issues a short-lived internal token for the cluster. Common in large platforms.
The confused deputy
If payments trusts orders completely, an attacker who tricks orders can make payments act on any user's behalf. Requiring the user's token (or an exchanged one) at payments means it checks who the request is for, not just who is sending it.
Try it
Using the Keycloak setup from the previous lesson, create two clients, orders-api and reports-api. Get a token for reports-api and call a small API that checks the audience as orders-api: it must reject it. Then remove the audience check and see it accepted. Finally, decode a token and confirm everything in it is readable.
Recap
- Validate signature (pinned algorithms, JWKS by kid), issuer, audience, expiry, scopes and tenant; don't trust the header.
- Short access tokens, rotated refresh tokens, introspection or fresh MFA for high-risk actions.
- Validate at the edge and in services; strip client-supplied identity headers.
- mTLS proves the calling workload; a forwarded or exchanged token proves the user.
This site is a public version of my personal engineering knowledge hub. It intentionally excludes confidential company information and internal operational details.