forward-auth
Delegates authentication to an external service.
forward-authPriority 2002What it does
forward-auth asks an authorization service of your own about every request before it goes on to the route's backend. The gateway calls URI with the headers listed under Request headers, the client's Authorization header for example. Whatever the client signs in with is checked by that service, not by a consumer or a credential on the gateway.
When the service answers with a 2xx, the request reaches the backend with the headers listed under Upstream headers copied from that answer. Any other answer goes back to the client with the service's own status, a 401 or a 403 for example, and the headers listed under Client headers. Authentication shows the other ways a request is checked.
When to use it
- Your company already runs a service that checks its session tokens or cookies, and every API behind the gateway should ask it instead of checking them again.
- The backend needs to know who called and with which roles: the service answers with
X-User-IdandX-User-Roles, and the gateway copies them onto the request. - The decision depends on the body, which order a partner may touch for example: with Request method set to
POST, the service receives the client's body. - A sign-in scheme no other plugin speaks, kept in a service your team writes and changes on its own schedule.
Not for a credential the gateway checks itself, an API key for example — see key-auth (Authenticates consumers with an API key sent in a header or query parameter), permissions kept in Keycloak's authorization services — see authz-keycloak (Authorizes requests with Keycloak's authorization services) or tokens from an OpenID Connect identity provider — see openid-connect (Authenticates users against an OpenID Connect provider).
Set it up
Ask an authorization service before the request goes on and pass its verdict along
A 2xx from the service lets the request on with the service's X-User headers; any other answer is sent back to the client with the Www-Authenticate header the service gave.
Under Basics set URI to https://auth.example.com/verify. Under Behavior fill Request headers as in the JSON below, fill Upstream headers as in the JSON below, fill Client headers as in the JSON below and set Timeout to 2000 milliseconds.
{
"uri": "https://auth.example.com/verify",
"request_headers": [
"Authorization",
"Cookie"
],
"upstream_headers": [
"X-User-Id",
"X-User-Roles"
],
"client_headers": [
"Www-Authenticate"
],
"timeout": 2000
}
More scenarios
Send the request body to the authorization service as a POST
Under Basics set URI to https://auth.example.com/verify, set Request method to POST and set Request body limit to 65536. Under Behavior fill Request headers as in the JSON below.
{
"uri": "https://auth.example.com/verify",
"request_method": "POST",
"max_req_body_size": 65536,
"request_headers": [
"Authorization"
]
}
Things to know
- Upstream headers — With
X-User-Idlisted, the backend receives the value the service answered for that client,alicein the example. - Client headers — With
Www-Authenticatelisted, a client the service refuses receives the service's401together with itsWww-Authenticate: Bearer realm="orders". - Request method — With
POST, the service reads the client's body and decides by it. An order it allows is answered200, and any other order the403the service gave. - Request method — Only
GETandPOSTcan be set;PUTis refused when you save. - URI — An address without its scheme,
http://orhttps://, is refused when you save. - Extra headers — A header name that carries a colon is refused when you save.
- Status on error — Read only when the service cannot be reached: one that answers a refusal reaches the client with its own status and body.
- Allow degradation — On, a request the service could not be asked about travels to the upstream unauthorized, and the status above is never used.
- AdviceKeep Timeout short and the authorization service near the gateway, since it is asked about every request.
- 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. A field under “When … is …” is there only while that value is set.
Basics
uristringRequiredThe address of the authorization service, scheme included. Every request is announced to it with the five X-Forwarded headers.
pattern
^https?://http://auth.internal:8080/verifyrequest_methodstringdefault GETGET sends the request's headers only. POST also sends the client's body.
When Request method is POST
max_req_body_sizeintegerdefault 67108864The largest client body, in bytes, forwarded to the service. A larger request is answered 413.
Behavior
request_headersarray of stringsdefault []Request headers copied to the service, such as Authorization. X-Forwarded-* names keep the plugin's own values.
upstream_headersarray of stringsdefault []Headers copied from the service's answer onto the request to the upstream, such as X-User-Id. One the answer lacks is removed.
client_headersarray of stringsdefault []Read from a refusal and sent on to the client with it.
extra_headersobjectExtra headers sent to the service, such as X-Route: $uri. A $name is read from the request, and these replace the plugin's X-Forwarded-* values. An object whose keys are a name without a colon, each with a string as its value.
status_on_errorintegerdefault 403timeoutintegerdefault 3000ssl_verifybooleandefault trueAdvanced
allow_degradationbooleandefault falsekeepalivebooleandefault truekeepalive_poolintegerdefault 5keepalive_timeoutintegerdefault 60000