jwe-decrypt

Description

The jwe-decrypt Plugin is used to decrypt JWE authorization headers in requests to an APISIX Service or Route.

This Plugin adds an endpoint /apisix/plugin/jwe/encrypt for JWE encryption. For decryption, the key should be configured in Consumer.

Attributes

For Consumer:

NameTypeRequiredDefaultValid valuesDescription
keystringTrueUnique key for a Consumer.
secretstringTrueThe decryption key. The key could be saved in a secret manager using the Secret resource.
is_base64_encodedbooleanFalsefalseSet to true if the secret is base64 encoded.

For Route:

NameTypeRequiredDefaultDescription
headerstringFalseauthorizationThe header to get the token from.
forward_headerstringFalseauthorizationSet the header name that passes the plaintext to the Upstream.
strictbooleanFalsetrueIf true, throw a 403 error if JWE token is missing from the request. If false, do not throw an error if JWE token cannot be found.

Example usage

First, create a Consumer with jwe-decrypt and configure the decryption key:

  1. curl http://127.0.0.1:9180/apisix/admin/consumers -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  2. {
  3. "username": "jack",
  4. "plugins": {
  5. "jwe-decrypt": {
  6. "key": "user-key",
  7. "secret": "key-length-must-be-at-least-32-bytes"
  8. }
  9. }
  10. }'

Next, create a Route with jwe-decrypt enabled to decrypt the authorization header:

  1. curl http://127.0.0.1:9180/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  2. {
  3. "methods": ["GET"],
  4. "uri": "/anything*",
  5. "plugins": {
  6. "jwe-decrypt": {}
  7. },
  8. "upstream": {
  9. "type": "roundrobin",
  10. "nodes": {
  11. "httpbin.org:80": 1
  12. }
  13. }
  14. }'

Encrypt Data with JWE

The Plugin creates an internal endpoint /apisix/plugin/jwe/encrypt to encrypt data with JWE. To expose it publicly, create a Route with the public-api Plugin:

  1. curl http://127.0.0.1:9180/apisix/admin/routes/jwenew -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  2. {
  3. "uri": "/apisix/plugin/jwe/encrypt",
  4. "plugins": {
  5. "public-api": {}
  6. }
  7. }'

Send a request to the endpoint passing the key configured in Consumer to the URI parameter to encrypt some sample data in the payload:

  1. curl -G --data-urlencode 'payload={"uid":10000,"uname":"test"}' 'http://127.0.0.1:9080/apisix/plugin/jwe/encrypt?key=user-key' -i

You should see a response similar to the following, with the JWE encrypted data in the response body:

  1. HTTP/1.1 200 OK
  2. Date: Mon, 25 Sep 2023 02:38:16 GMT
  3. Content-Type: text/plain; charset=utf-8
  4. Transfer-Encoding: chunked
  5. Connection: keep-alive
  6. Server: APISIX/3.5.0
  7. Apisix-Plugins: public-api
  8. eyJhbGciOiJkaXIiLCJraWQiOiJ1c2VyLWtleSIsImVuYyI6IkEyNTZHQ00ifQ..MTIzNDU2Nzg5MDEy.hfzMJ0YfmbMcJ0ojgv4PYAHxPjlgMivmv35MiA.7nilnBt2dxLR_O6kf-HQUA

Decrypt Data with JWE

Send a request to the route with the JWE encrypted data in the Authorization header:

  1. curl http://127.0.0.1:9080/anything/hello -H 'Authorization: eyJhbGciOiJkaXIiLCJraWQiOiJ1c2VyLWtleSIsImVuYyI6IkEyNTZHQ00ifQ..MTIzNDU2Nzg5MDEy.hfzMJ0YfmbMcJ0ojgv4PYAHxPjlgMivmv35MiA.7nilnBt2dxLR_O6kf-HQUA' -i

You should see a response similar to the following, where the Authorization header shows the plaintext of the payload:

  1. HTTP/1.1 200 OK
  2. Content-Type: application/json
  3. Content-Length: 452
  4. Connection: keep-alive
  5. Date: Mon, 25 Sep 2023 02:38:59 GMT
  6. Access-Control-Allow-Origin: *
  7. Access-Control-Allow-Credentials: true
  8. Server: APISIX/3.5.0
  9. Apisix-Plugins: jwe-decrypt
  10. {
  11. "args": {},
  12. "data": "",
  13. "files": {},
  14. "form": {},
  15. "headers": {
  16. "Accept": "*/*",
  17. "Authorization": "{\"uid\":10000,\"uname\":\"test\"}",
  18. "Host": "127.0.0.1",
  19. "User-Agent": "curl/8.1.2",
  20. "X-Amzn-Trace-Id": "Root=1-6510f2c3-1586ec011a22b5094dbe1896",
  21. "X-Forwarded-Host": "127.0.0.1"
  22. },
  23. "json": null,
  24. "method": "GET",
  25. "origin": "127.0.0.1, 119.143.79.94",
  26. "url": "http://127.0.0.1/anything/hello"
  27. }

Delete Plugin

To remove the jwe-decrypt 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.

  1. curl http://127.0.0.1:9180/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  2. {
  3. "methods": ["GET"],
  4. "uri": "/anything*",
  5. "plugins": {},
  6. "upstream": {
  7. "type": "roundrobin",
  8. "nodes": {
  9. "httpbin.org:80": 1
  10. }
  11. }
  12. }'