authz-keycloak
Authorizes requests with Keycloak's authorization services.
authz-keycloakPriority 2000What it does
authz-keycloak lets a request through only when Keycloak's authorization services grant its caller what the route asks for. The caller sends a bearer token from the Keycloak realm, and the person or client it stands for lives in Keycloak, not on a consumer. Each entry under Permissions names a resource of the realm, with a scope after a #.
The gateway asks Keycloak as the client under Client ID, so the realm's own policies decide who passes. A request without a token is answered 401 with Missing JWT token in request. Authentication explains callers who sign in at an identity provider.
When to use it
- Keycloak already holds who may read or change orders, and the gateway should apply those rules to every call without a copy of them.
- A mobile app signs its users in at Keycloak and sends their bearer token, and only users granted
orders#readreach the orders API. - Permissions that follow the request path and method, with Resolve the resources from the path and Method as the scope on, so a new endpoint needs only a new resource in Keycloak.
Not for signing people in to a web application at an identity provider — see openid-connect (Authenticates users against an OpenID Connect provider), checking a token on the gateway without asking anyone — see jwt-auth (Authenticates consumers with a JSON Web Token they present) or a decision made by an authorization service of your own — see forward-auth (Delegates authentication to an external service).
Set it up
Let through only callers Keycloak grants read access to orders
The caller sends a bearer token from the realm, and the realm's authorization services hold a resource orders with the scope read.
Under Basics set Client ID to api-gateway, set Client secret to <secret>, set Discovery to https://keycloak.example.com/realms/main/.well-known/uma2-configuration and fill Permissions as in the JSON below.
{
"client_id": "api-gateway",
"client_secret": "<secret>",
"discovery": "https://keycloak.example.com/realms/main/.well-known/uma2-configuration",
"permissions": [
"orders#read"
]
}
Things to know
- Permissions —
orders#readasks for the scopereadon the resourceorders, both held by the realm's authorization services. - Permissions — Under Enforcing, a request with no permission named here or resolved from its path is refused with 403.
- Resolve the resources from the path — On, a discovery document or a resource registration endpoint must be written too.
- Discovery — A discovery document or a token endpoint has to be written; a configuration with neither is refused when you save.
- Policy enforcement mode — Only
ENFORCINGandPERMISSIVEcan be set;LOOSEis refused when you save. - Password grant URI — A form POST to exactly this path is answered with a token for the username and password in its body, without authorization. It needs Token endpoint written: with discovery alone the POST answers 503.
- Password grant URI — A path that does not start with
/is refused when you save. - Request body limit — Read only on a form POST to the password grant address above. A body over it is answered 503.
- AdviceGive the gateway a Keycloak client of its own, used only for these checks, so that its Client secret can be replaced without touching other applications.
- On a consumer or a consumer group it runs only after the consumer has been identified by a credential; it does not identify the consumer itself.
Settings
The fields as the console shows them, section by section.
Basics
client_idstringRequiredThe client ID of this API in the identity provider, such as payments-api.
client_secretstringThe client secret from the identity provider. Stored on the engine and shown only to roles that manage this object.
discoverystringRequired unless Token endpoint is setThe identity provider's UMA 2 configuration. The endpoints below are read from it unless written there. The password grant address still needs Token endpoint written: with discovery alone its POST answers 503.
https://keycloak.example.com/realms/main/.well-known/uma2-configurationtoken_endpointstringRequired unless Discovery is setpermissionsarray of stringsdefault []Each name is a resource of the identity provider, with a scope written after a # where one is wanted.
Behavior
policy_enforcement_modestringdefault ENFORCINGgrant_typestringdefault urn:ietf:params:oauth:grant-type:uma-ticketlazy_load_pathsbooleandefault falseOn, the resources are looked up from the request path at every request and the list above is not read at all.
http_method_as_scopebooleandefault falseThe request's method is added to every permission as its scope.
access_denied_redirect_uristringA refusal is answered 307 with this address in a Location header instead of 403.
resource_registration_endpointstringpassword_grant_token_generation_incoming_uristringpattern
^/timeoutintegerdefault 3000ssl_verifybooleandefault trueAdvanced
access_token_expires_inintegerdefault 300access_token_expires_leewayintegerdefault 0refresh_token_expires_inintegerdefault 3600refresh_token_expires_leewayintegerdefault 0cache_ttl_secondsintegerdefault 86400max_req_body_sizeintegerdefault 67108864The largest client body, in bytes, read into memory while a password grant token is asked for.
keepalivebooleandefault truekeepalive_poolintegerdefault 5keepalive_timeoutintegerdefault 60000