Skip to main content

Keycloak and G8: OpenID Connect at the gateway and Keycloak authorization on a Route

Put a Keycloak realm in front of a Route with openid-connect, verify bearer tokens at the gateway, then let authz-keycloak decide who may call which path.

By Akif F. Güven Updated Security

What you will have at the end

A Route called payments-api that only serves signed-in users of a Keycloak realm. A browser is sent to Keycloak to sign in, an API client sends a bearer token, and the backend receives the user’s identity in a header. On top of that, Keycloak’s own authorization decides which paths a user may call: refunds for the finance role, orders for everyone who is signed in. Every call, allowed or refused, is in Traffic Explorer, and the change that put the plugins on the Route is in the audit log with its secrets masked.

Prerequisites

  • A Keycloak realm the G8 engine can reach. This article uses the realm corp with a confidential client payments-gateway (standard flow on, service account on, Authorization on) and two users: jane.miller with the realm role finance and sam.ortiz without it.
  • An Environment in the G8 console with traffic logging turned on under Traffic Explorer › Settings and a Prometheus the Environment reads, so Analytics counts the Route.
  • A backend. Here it is payments-svc, a small service that answers under /payments/ and prints the identity it finds in the X-Userinfo header.
  • A role in the Environment that may manage Routes. The screenshots below were taken as nora.becker.

Part one: OpenID Connect on the Route

The Route

Create the Upstream payments-backend with the node payments-svc:8080, then the Route payments-api with the path /payments/*, the methods GET and POST and the Upstream as its target. Call it once without any credentials: it answers 200, because nothing protects it yet.

Add the Plugin

Open the Route, switch to the Plugins tab and select Add plugin. Search for openid-connect in the catalog, select the tile and Configure. The form opens beside the Route with its settings in sections. Basics takes everything Keycloak needs:

  • Discovery: the realm’s discovery document, http://localhost:8190/realms/corp/.well-known/openid-configuration in this lab. The engine reads the endpoints and the signing keys from it.
  • Client ID and Client secret of the client payments-gateway. The secret is stored on the engine and shown only to roles that manage the Route.
  • Bearer only stays off, so a browser without a token is sent to Keycloak instead of being refused.
  • Scope: openid profile email, so the user’s name and address come back in the identity token.

The openid-connect form on the payments-api Route: Discovery URL of the corp realm, client ID payments-gateway, the client secret masked, Bearer only off The Basics section of the form. Everything the engine needs from Keycloak is on this screen; the secret is already masked while you type it. The banner counts the two settings the form edits as JSON rather than as fields, both under Advanced and neither needed here.

One setting sits outside Basics. Session › Secret signs the cookie the browser gets after it signs in; the form refuses to save without it while Bearer only is off. Select Add plugin: the Route is saved, the engine checks the configuration, and the audit log receives the change.

A second save follows. Behavior › Use jwks is off by default, and with it off a bearer token in the Authorization header is ignored: the API client is sent to the sign-in page like a browser. Switch it on and save again; the engine then verifies the token’s signature against the keys from the discovery document, and the audit log gets a second entry.

A browser signs in

Open http://localhost:9281/payments/me in a browser. The engine finds no session and answers 302 to Keycloak’s authorization endpoint with the client ID and the scope from the form.

Keycloak's sign-in page for the corp realm, with the username jane.miller typed in The sign-in page is Keycloak’s own. The engine only sends the browser there and waits for it to come back.

After the sign-in, Keycloak sends the browser back to the Route, the engine exchanges the code for tokens, sets its session cookie and forwards the request. The backend answers with what it received:

{
  "service": "payments-svc",
  "path": "/payments/me",
  "caller": {
    "user": "jane.miller",
    "name": "Jane Miller",
    "email": "jane.miller@corp.example"
  },
  "headers": { "authorization": null, "x-userinfo": "present" }
}

The backend never saw a password or a token. It read the X-Userinfo header, which the Plugin adds with the claims from Keycloak, base64-encoded.

An API client sends a token

An API client gets a token from Keycloak’s token endpoint and sends it as a bearer token. The same Route answers 200 and the backend sees the same identity:

curl -H "Authorization: Bearer $TOKEN" http://localhost:9281/payments/me

A token the engine cannot verify is refused at the gateway, before the backend is called:

HTTP/1.1 401 Unauthorized
WWW-Authenticate: Bearer error="invalid_token", error_description="invalid jwt: invalid jwt string"

Part two: Keycloak authorization on the same Route

openid-connect answers who the caller is. Which paths that caller may use is a separate question, and Keycloak can answer it too: in the client’s Authorization tab, the resource refunds carries the URIs /payments/refunds/* and a permission that needs the role finance; the resource orders carries /payments/orders/* and a permission any signed-in user passes.

Add a second Plugin to the Route, authz-keycloak. Its Basics are the client ID, the client secret and Discovery, which here is the realm’s UMA 2 document, http://localhost:8190/realms/corp/.well-known/uma2-configuration. Under Behavior, switch Resolve the resources from the path on. The form notes what that means: the resources are looked up from the request path at every request, and the Permissions list above is not read at all.

The authz-keycloak form on the payments-api Route: client ID, the client secret masked and the UMA 2 discovery URL of the corp realm The same client, the UMA 2 discovery document instead of the OpenID one. The Permissions list stays empty because the path decides.

The Plugin runs after openid-connect (priority 2000 against 2599), so it always sees a verified token. It asks Keycloak for a decision on the path and keeps the answer in its cache. The result, measured with the two users’ tokens:

jane.miller  GET /payments/refunds/rf-2041   200
jane.miller  GET /payments/orders/ord-10421  200
sam.ortiz    GET /payments/refunds/rf-2041   403  {"error":"access_denied","error_description":"not_authorized"}
sam.ortiz    GET /payments/orders/ord-10421  200
jane.miller  GET /payments/me                403

The last line is worth a look. Under the default Policy enforcement mode ENFORCING, a path no resource covers is refused for everyone, Jane included. Register every path the Route serves as a resource in Keycloak, or put the paths that need no authorization on a Route of their own.

What Traffic Explorer shows

Every call above is a request record. Open Observe › Traffic Explorer and the list carries the 200s next to the 401 and the 403s. The Upstream node column says who answered: the backend’s node for a 200, shown as the address the engine resolved payments-svc:8080 to, and gateway for a refusal the engine made itself.

Traffic Explorer with request records of the payments-api Route: 200 rows answered by the backend node, a 403 and a 401 answered by the gateway The refusals never reached the backend. The Upstream node column reads gateway for them and the resolved backend address for the rest.

Open a record and switch to its Request tab: the headers are there as the gateway received them. The authorization header reads ********, masked by the gateway before the record was stored, because it is on the Environment’s default mask list. Two headers were not, at first: openid-connect adds x-access-token and x-userinfo to the request it forwards, both carrying the signed-in user’s claims, and the first records of this session stored them in clear. Add them under Traffic Explorer › Settings › Masking › Request headers and save; every record written from then on masks them, as the one below does.

The Request tab of a refused refund call: the authorization, x-access-token and x-userinfo headers all read masked by the gateway before the record was stored After the two headers were added to the masks. The gateway replaces the values before the record leaves it, so the log store never holds a token.

What the audit log keeps

Adding the Plugin was a change to the Route, so the audit log has an entry: who, when, which Route and the configuration before and after. The client secret and the session secret appear as •••• on both sides, and the record names the masked paths that changed. Nobody reads a secret from the audit log, not even the person who typed it.

The audit record of the Route update that added openid-connect, before and after side by side, with client_secret and session.secret masked The change as the engine received it. The two secrets are masked and listed under “Masked values changed”.

The Route’s Analytics tab counts the same refusals by code and draws the audit entries as markers on the request chart; Who changed what shows those markers.

Side by side with other identity methods

A Route can admit more than one kind of caller. The multi-auth Plugin takes a list of at least two authentication Plugins and tries them in order until one admits the request; the configurations G8 ships combine key-auth, basic-auth, jwt-auth and hmac-auth. openid-connect keeps its own session and stays a Plugin of its own.

What to do next

  • Switch Set access token header off in the openid-connect form if the backend does not need the token itself; the identity in X-Userinfo is usually enough.
  • Copy the Route to the production Environment when it works. G8 shows what the copy changes before anything is written, and the copy is an audit entry: Change control and audit.
  • The same Plugin for a machine caller with a bearer token, and jwt-auth beside it: OAuth 2.0 access tokens at the gateway.
  • Read how the Route’s rules apply to AI agents in OpenAPI to MCP, and how the gateway’s identity options fit together on API security.
Written by
Akif F. Güven
Engineer, G8
Next step

See G8 in a demo

We’ll show you the console with APIs and AI traffic like yours, and answer the questions these articles leave open.

  1. 01Demo
  2. 02Proof of conceptOn Docker for a quick start, or on the infrastructure you plan to run G8 on.
  3. 03LicenseWe prepare a quote for your edition and nodes, and you receive a signed license file.