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
| uri | string | True ||| URI of the authorization service. |
42
42
| ssl_verify | boolean | False | true || When set to `true`, verifies the SSL certificate. |
43
-
| request_method | string | False | GET |["GET","POST"]| HTTP method for a client to send requests to the authorization service. When set to `POST` the request body is send to the authorization service. |
43
+
| request_method | string | False | GET |["GET","POST"]| HTTP method for a client to send requests to the authorization service. When set to `POST` the request body is sent to the authorization service. (not recommended - see section on [Using data from POST body](#using-data-from-post-body-to-make-decision-on-authorization-service))|
44
44
| request_headers | array[string]| False ||| Client request headers to be sent to the authorization service. If not set, only the headers provided by APISIX are sent (for example, `X-Forwarded-XXX`). |
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`|
45
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. |
46
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. |
47
48
| timeout | integer | False | 3000ms |[1, 60000]ms | Timeout for the authorization service HTTP call. |
@@ -166,6 +167,110 @@ HTTP/1.1 403 Forbidden
166
167
Location: http://example.com/auth
167
168
```
168
169
170
+
### Using data from POST body to make decision on Authorization service
171
+
172
+
::: note
173
+
When the decision is to be made on the basis of POST body, then it is recommended to use `$post_arg.*` with `extra_headers` field and make the decision on Authorization service on basis of headers rather than using POST `request_method` to pass the entire request body to Authorization service.
174
+
:::
175
+
176
+
Create a serverless function on the `/auth` route that checks for the presence of the `tenant_id` header. If present, the route responds with HTTP 200 and sets the `X-User-ID` header to a fixed value `i-am-an-user`. If `tenant_id` is missing, it returns HTTP 400 with an error message.
177
+
178
+
```shell
179
+
curl -X PUT 'http://127.0.0.1:9180/apisix/admin/routes/auth' \
core.response.exit(400, \"tenant_id is required\")
195
+
end
196
+
end"
197
+
]
198
+
}
199
+
}
200
+
}'
201
+
```
202
+
203
+
Create a route that accepts POST requests and uses the `forward-auth` plugin to call the auth endpoint with the `tenant_id` from the request. The request is forwarded to the upstream service only if the auth check returns 200.
204
+
205
+
```shell
206
+
curl -X PUT 'http://127.0.0.1:9180/apisix/admin/routes/1' \
Send a POST request without the `tenant_id` header:
261
+
262
+
```shell
263
+
curl -i http://127.0.0.1:9080/post -X POST -d '{
264
+
"abc": 123
265
+
}'
266
+
```
267
+
268
+
You should receive an `HTTP/1.1 400 Bad Request` response with the following message:
269
+
270
+
```shell
271
+
tenant_id is required
272
+
```
273
+
169
274
## Delete Plugin
170
275
171
276
To remove the `forward-auth` Plugin, you can delete the corresponding JSON configuration from the Plugin configuration. APISIX will automatically reload and you do not have to restart for this to take effect.
0 commit comments