|
| 1 | +--- |
| 2 | +id: jwt-blacklisting |
| 3 | +title: JWT Blacklisting |
| 4 | +sidebar_label: JWT Blacklisting |
| 5 | +--- |
| 6 | + |
| 7 | +[JWT](https://jwt.io/) Blacklisting is one of the RIG's core features. Imagine a use case where someone does a malicious action using specific JWT. By blacklisting this JWT, you can prevent any other malicious actions. Once it's blacklisted, user is not able to do any action within the RIG (unless it's an unsecured action -- e.g. unsecured reverse proxy endpoint). |
| 8 | + |
| 9 | +You can blacklist a JWT via REST API call to `POST :4010/v3/session-blacklist` and in body specify the `sessionId` and `validityInSeconds`. `sessionId` is by default expecting [JWT ID - JTI](https://tools.ietf.org/html/rfc7519#page-10), but you can change it via `JWT_SESSION_FIELD` env var. |
| 10 | + |
| 11 | +Blacklist is using so called [ETS](http://erlang.org/doc/man/ets.html) tables to store JTIs and their expiration time. These information are automatically synchronized across RIG cluster. That means you can blacklist a JWT via whatever RIG node and it will apply to all RIG nodes. Blacklisted JTIs in ETS tables are cleaned up based on the `validityInSeconds` property provided in a request. |
| 12 | + |
| 13 | +> `validityInSeconds` should be ideally set to at least _**JWT expiration time - current time**_. |
| 14 | +
|
| 15 | +## API |
| 16 | + |
| 17 | +There are 2 APIs that are easily accessible via built-in Swagger UI (`your_host:4010/swagger-ui`). |
| 18 | + |
| 19 | +- `POST :4010/v3/session-blacklist` - to blacklist a JWT |
| 20 | +- `GET :4010/v3/session-blacklist/{sessionId}` - to check whether JWT is blacklisted at the moment |
| 21 | + |
| 22 | +## Example |
| 23 | + |
| 24 | +JWT used below has the following payload: |
| 25 | + |
| 26 | +```json |
| 27 | +{ |
| 28 | + "sub": "1234567890", |
| 29 | + "name": "John Doe", |
| 30 | + "iat": 1516239022, |
| 31 | + "jti": "johndoe", |
| 32 | + "exp": 4516239022 |
| 33 | +} |
| 34 | +``` |
| 35 | + |
| 36 | +Run in terminal: |
| 37 | + |
| 38 | +```bash |
| 39 | +# run RIG - note the "secured" field in the PROXY_CONFIG_FILE |
| 40 | +docker run -d --name rig \ |
| 41 | +-e PROXY_CONFIG_FILE='[{"id":"service","name":"service","auth_type":"jwt","auth":{"use_header":true,"header_name":"Authorization","use_query":false,"query_name":""},"versioned":false,"version_data":{"default":{"endpoints":[{"id":"secured","path_regex":"todos/1","method":"GET","secured":true}]}},"proxy":{"target_url":"http://jsonplaceholder.typicode.com","port":80}}]' \ |
| 42 | +-e JWT_SECRET_KEY='rigsecret' \ |
| 43 | +-p 4000:4000 \ |
| 44 | +-p 4010:4010 \ |
| 45 | +accenture/reactive-interaction-gateway |
| 46 | + |
| 47 | +# check if JWT is blacklisted - should return "Not found.", that means it's not blacklisted |
| 48 | +curl "http://localhost:4010/v3/session-blacklist/johndoe" \ |
| 49 | +-H "accept: application/json" |
| 50 | + |
| 51 | +# call an API - should return some data |
| 52 | +curl http://localhost:4000/todos/1 \ |
| 53 | +-H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyLCJqdGkiOiJqb2huZG9lIiwiZXhwIjo0NTE2MjM5MDIyfQ.gPP_Ya_QphNAas3NXqqlfwvyzy_TSN5sh_eMqX0Xnf4" |
| 54 | + |
| 55 | +# blacklist the JWT for 60 seconds |
| 56 | +curl -X POST "http://localhost:4010/v3/session-blacklist" -H "accept: application/json" -H "content-type: application/json" -d "{ \"validityInSeconds\": 60,\"sessionId\": \"johndoe\"}" |
| 57 | + |
| 58 | +# check if JWT is blacklisted - should return empty response, that means it's blacklisted |
| 59 | +curl "http://localhost:4010/v3/session-blacklist/johndoe" \ |
| 60 | +-H "accept: application/json" |
| 61 | + |
| 62 | +# call an API - should return "Authentication failed." |
| 63 | +curl http://localhost:4000/todos/1 \ |
| 64 | +-H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyLCJqdGkiOiJqb2huZG9lIiwiZXhwIjo0NTE2MjM5MDIyfQ.gPP_Ya_QphNAas3NXqqlfwvyzy_TSN5sh_eMqX0Xnf4" |
| 65 | +``` |
| 66 | + |
| 67 | +You can restrict access also to your WS/SSE/Longpolling connections/subscriptions via `SUBSCRIPTION_CHECK` env var, check the [ops guide](./rig-ops-guide.md). |
0 commit comments