Authorization code
Use authorization codes to authenticate requests with an external identity provider.
For more information or other OAuth options, see the OAuth about page.
Before you begin
Follow the Get started guide to install Solo Enterprise for kgateway.
Follow the Sample app guide to create a gateway proxy with an HTTP listener and deploy the httpbin sample app.
Get the external address of the gateway and save it in an environment variable.
export INGRESS_GW_ADDRESS=$(kubectl get svc -n kgateway-system http -o jsonpath="{.status.loadBalancer.ingress[0]['hostname','ip']}") echo $INGRESS_GW_ADDRESSkubectl port-forward deployment/http -n kgateway-system 8080:8080
Step 1: Set up your Identity Provider
Set up an OpenID Connect (OIDC) compatible identity provider (IdP).
For example, you can use Keycloak as an IdP.
Step 2: Enforce authorization code
Use AuthConfig and EnterpriseKgatewayTrafficPolicy resources to apply the auth rules to the routes that you want to secure with authorization code OAuth.
Create a Kubernetes secret to store your Keycloak client credentials.
kubectl apply -f - <<EOF apiVersion: v1 kind: Secret metadata: name: oauth-keycloak namespace: kgateway-system type: extauth.solo.io/oauth stringData: client-secret: ${KEYCLOAK_SECRET} EOFCreate an AuthConfig resource with your authorization code OAuth rules.
kubectl apply -f- <<EOF apiVersion: extauth.solo.io/v1 kind: AuthConfig metadata: name: oauth-authorization-code namespace: httpbin spec: configs: - oauth2: oidcAuthorizationCode: appUrl: "http://${INGRESS_GW_ADDRESS}:80" callbackPath: /callback clientId: ${KEYCLOAK_CLIENT} clientSecretRef: name: oauth-keycloak namespace: kgateway-system issuerUrl: "${KEYCLOAK_URL}/realms/master/" scopes: - email session: failOnFetchFailure: true redis: cookieName: keycloak-session options: host: ext-cache-enterprise-kgateway:6379 headers: idTokenHeader: jwt EOFReview the following table to understand this configuration. For more authorization code options, see the Gloo Edge API docs.
Field Description oauth2.oidcAuthorizationCodeSet up the OAuth policy to authenticate requests with an authorization code. appUrlThe public URL of the app that you want to set up external auth for. This setting is used in combination with the callbackPathattribute.callbackPathThe callback path, relative to the appUrlsetting. After a user authenticates, the IdP redirects the user to this callback URL. Solo Enterprise for kgateway intercepts requests with this path, exchanges the authorization code received from the IdP for an ID token, places the ID token in a cookie on the request, and forwards the request to its original destination. Note: The callback path must have a matching route that the EnterpriseKgatewayTrafficPolicy applies to. For example, you could simply have a/path-prefix route which would match any callback path. The important part of this callback “catch all” route is that the request goes through the routing filters, including external auth.clientIdThe client ID token that you got when you registered your app with the IdP. In this example, you set the client ID when you set up Keycloak. clientSecretRefThe Kubernetes secret that has the client secret that you got when you registered your app with the identity provider. The secret must exist on the same cluster as the external auth service that enforces this policy. In this example, you created the secret in an earlier step. issuerUrlThe URL of the OpenID Connect IdP. Solo Enterprise for kgateway automatically discovers OIDC configuration by querying the .well-known/openid-configurationendpoint on theissuer_url. In this example, Solo Enterprise for kgateway expects to find OIDC discovery information at"${KEYCLOAK_URL}/realms/master/".scopesScopes to request in addition to the openidscope, such asemailin this example.sessionDetails on how to store the user session details. In this example, the cookie is stored as by the name keycloak-sessionin a Redis instance that is set up for the external auth service by default. The Redis service name is in the formatext-cache-<gateway-class-name>, such asext-cache-enterprise-kgateway. If your Redis instance is for a different GatewayClass, update the name accordingly.headersForward the ID token to the destination after successful authentication. In this example, the ID token is sent as a JWT. Create an EnterpriseKgatewayTrafficPolicy resource that refers to the AuthConfig that you created. The following policy applies external auth to all routes that the Gateway serves.
kubectl apply -f - <<EOF apiVersion: enterprisekgateway.solo.io/v1alpha1 kind: EnterpriseKgatewayTrafficPolicy metadata: name: oauth-authorization-code namespace: kgateway-system spec: targetRefs: - name: http group: gateway.networking.k8s.io kind: Gateway entExtAuth: authConfigRef: name: oauth-authorization-code namespace: httpbin EOFVerify that the AuthConfig is
ACCEPTED.kubectl get authconfig oauth-authorization-code -n httpbin -o yamlIf you see a
REJECTEDerror similar toinvalid character 'k' looking for beginning of object key string, try copying the values of your environment variables manually into the AuthConfig resource.
Step 3: Verify access token validation
Sign in through your IdP to access your protected app.
Open the path in your web browser to access the httpbin app. You are redirected to the authentication page for your IdP, such as Keycloak.
open "http://$INGRESS_GW_ADDRESS:80/get"Enter the user credentials from the IdP, such as the following values from the Keycloak setup.
- Username:
user1 - Password:
password
- Username:
You are authenticated and returned back to the httpbin home page.
Send a request to the httpbin app.
curl -i localhost:8080/status/200 -H "host: www.example.com"In the output, verify that the request is redirected to your IdP, such as Keycloak in the following example.
HTTP/1.1 302 Found location: http://localhost:8080/realms/master/protocol/openid-connect/auth?client_id=$KEYCLOAK_CLIENT&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2Fcallback&response_type=code&scope=email+openid&state=$JWT_STATE date: Wed, 09 Jul 2025 20:19:53 GMT server: envoy content-length: 0
Cleanup
You can optionally remove the resources that you set up as part of this guide.kubectl delete authconfig oauth-authorization-code -n httpbin
kubectl delete EnterpriseKgatewayTrafficPolicy oauth-authorization-code -n kgateway-system
kubectl delete secret oauth-keycloak -n kgateway-system