Thursday, December 3, 2026

The OIDC edge gateway: validating tokens you didn't issue

Full writeup: OIDC edge gateway JWT validation checklist

Part 4 of the OIDC series. The provider issues tokens and clients consume them — but the most interesting security engineering in the lab lives in between: an edge gateway that validates tokens it didn't issue and decides what downstream services are allowed to trust.

Why a gateway at all

Every tutorial puts JWT validation in each service — oauth2ResourceServer() everywhere, JWKS config duplicated per app. That scales into N copies of the same security-critical code with N independent drift risks. The edge pattern moves it to one chokepoint: routes never see unauthenticated traffic, and downstreams can be dumb on purpose.

The gateway runs Spring Cloud Gateway (WebFlux) + OAuth2 Resource Server: signature verified against the provider's JWKS (resolved via issuer-uri discovery — no hardcoded key material), plus issuer and expiry checks.

Route metadata as the authz contract

The design decision I'm happiest with: authorization policy lives in route config, not code:

- id: messages-api
  uri: forward:/internal/messages
  predicates: [Path=/api/messages]
  metadata:
    audience: messages-api      # required aud claim
    scopes: api:read            # required scopes
    requests-per-minute: 120    # per-identity rate limit
    introspect: true            # revocation check at provider
    cache-ttl: PT30S            # shared GET cache in Redis

Wrong aud → 403. Missing token → 401. Insufficient scope → 403. And the demo downstream's real paths live under /internal/**, which the security config flat-out denies at the edge — callers physically cannot bypass route authz by guessing the upstream path.

Trusted headers: the subtle one

How does the downstream learn who's calling? The naive answer — forward the JWT — leaks a credential to every hop. The gateway instead strips all client-supplied identity headers (X-User-*, X-Client-*, forwarding headers) and re-derives them from the validated token: X-User-Id, X-User-Name, X-User-Roles, X-Client-Id. Downstreams see token-derived identity only — a client can never self-declare a role. Spoofing attempts get silently overwritten.

Three extras that make it a real edge

  • Per-identity rate limiting. Fixed window keyed on (route, subject) — one abusive caller gets 429 + Retry-After while everyone else passes untouched. Per-IP would punish a whole office behind NAT; per-token punishes exactly the offender.
  • Introspection on demand. JWTs are stateless — a revoked token validates fine until expiry. Routes marked introspect: true call the provider's /oauth2/introspect (as a dedicated gateway-introspect client) to catch revocation; results cache 15s and the check fails closed.
  • Bounded everything. Global connect/response timeouts (stall → 504), retry filter on idempotent GETs only — no replaying POSTs.

The cache that respects the token

The Redis response cache has one detail worth stealing: cache keys fingerprint the token's data claims — sub, scopes, roles, client — but not jti/exp/iat. So a refreshed token still hits its owner's cache entries (fresh token ≠ fresh user), while a scope downgrade correctly misses. It also honors upstream no-store/private/Set-Cookie, and upstream max-age can only shorten the configured TTL. A Redis outage fails open to pass-through — cache is an optimization, never a dependency.

What it teaches about real systems

The pattern transfers directly to production gateways: validate once at the edge, derive (never forward) identity, keep revocation checks opt-in per route, and treat every client-supplied identity assertion as hostile. None of it is exotic — it's just rarely visible until you build the layer yourself.

Next: third parties — client_credentials M2M, what a consent screen actually records, and scope design for people integrating with your IdP.

Java, JavaScript, HTML, XHTML, AJAX, CSS, etc.