You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/en/latest/plugins/forward-auth.md
+49Lines changed: 49 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -45,6 +45,7 @@ This Plugin moves the authentication and authorization logic to a dedicated exte
45
45
| extra_headers |object | False ||| Extra headers to be sent to the authorization service passed in key-value format. The value can be a variable like `$request_uri`, `$post_arg.xyz`|
46
46
| upstream_headers | array[string]| False ||| Authorization service response headers to be forwarded to the Upstream. If not set, no headers are forwarded to the Upstream service. |
47
47
| client_headers | array[string]| False ||| Authorization service response headers to be sent to the client when authorization fails. If not set, no headers will be sent to the client. |
48
+
| consumer_header | string | False ||| Authorization service response header that contains an existing APISIX Consumer username. When set and the auth response is 2xx, APISIX attaches that Consumer to the request. |
48
49
| timeout | integer | False | 3000ms |[1, 60000]ms | Timeout for the authorization service HTTP call. |
49
50
| keepalive | boolean | False | true || When set to `true`, keeps the connection alive for multiple requests. |
50
51
| keepalive_timeout | integer | False | 60000ms |[1000, ...]ms | Idle time after which the connection is closed. |
@@ -60,6 +61,8 @@ APISIX will generate and send the request headers listed below to the authorizat
If `consumer_header` is configured and the authorization service returns a 2xx response, APISIX reads the configured response header as a Consumer username and attaches the corresponding Consumer to the current request. This allows Consumer and Consumer Group plugins to take effect for the request. If the header is missing or the Consumer does not exist in APISIX, the request is rejected with HTTP `403`.
65
+
63
66
## Example usage
64
67
65
68
First, you need to setup your external authorization service. The example below uses Apache APISIX's [serverless](./serverless.md) Plugin to mock the service:
@@ -167,6 +170,52 @@ HTTP/1.1 403 Forbidden
167
170
Location: http://example.com/auth
168
171
```
169
172
173
+
### Attach an APISIX Consumer from the authorization response
174
+
175
+
Create a Consumer that already exists in APISIX:
176
+
177
+
```shell
178
+
curl -X PUT 'http://127.0.0.1:9180/apisix/admin/consumers' \
179
+
-H "X-API-KEY: $admin_key" \
180
+
-d '{
181
+
"username": "demo-consumer",
182
+
"plugins": {
183
+
"limit-count": {
184
+
"count": 1,
185
+
"time_window": 60,
186
+
"rejected_code": 429,
187
+
"key": "remote_addr",
188
+
"policy": "local"
189
+
}
190
+
}
191
+
}'
192
+
```
193
+
194
+
Have the authorization service return the Consumer username in a response header such as `X-Consumer-Username`, and configure the plugin to read it:
195
+
196
+
```shell
197
+
curl -X PUT 'http://127.0.0.1:9180/apisix/admin/routes/consumer-route' \
198
+
-H "X-API-KEY: $admin_key" \
199
+
-d '{
200
+
"uri": "/consumer-route",
201
+
"plugins": {
202
+
"forward-auth": {
203
+
"uri": "http://127.0.0.1:9080/auth",
204
+
"request_headers": ["Authorization"],
205
+
"consumer_header": "X-Consumer-Username"
206
+
}
207
+
},
208
+
"upstream": {
209
+
"nodes": {
210
+
"httpbin.org:80": 1
211
+
},
212
+
"type": "roundrobin"
213
+
}
214
+
}'
215
+
```
216
+
217
+
With this configuration, a successful auth response like `X-Consumer-Username: demo-consumer` attaches `demo-consumer` to the request. APISIX then applies Consumer-scoped plugins, such as the `limit-count` policy above.
218
+
170
219
### Using data from POST body to make decision on Authorization service
0 commit comments