Introduction
JWT stands for JSON Web Token, which is a popular way to do user authorization in web application and is also popular in the context of micro-services. So, when we use Long Running Actions (LRA) in any micro-service, the transaction APIs could be authorized using JWT tokens. Open industry standard specification RFC-7519 outlines how JTW is structured and how to use it. JWT works over HTTP protocol. The reason JWT is now a days preferred more is because it makes the authorization mechanism easier for micro-service applications, avoids single point of failure and also helps the application design to be more scalable.
Here is how JWT is structured: [<HEADER>.<PAYLOAD>.<SIGNATURE>]
The JWT token is divided into three parts, as we can see in the above example which are separated by two periods.
1: HEADER -> base64UrlEncode(header) 2: PAYLOAD -> base64UrlEncode(payload) 3: SIGNATURE -> encryptionAlgorithm(base64UrlEncode(header) + '.' + base64UrlEncode(payload),256-bit-SECRET)
You can create your own JWT token by visiting website jwt.io. JWT is a value token, which will only contain the user information in PAYLOAD, with the name of type of algorithm used in the HEADER and the token verification signature in the SIGNATURE part.
The above figure shows the implication of JWT. The server will create JWT token and will give it to the client, so that client can send it back on the subsequent request. Once the JWT token is created and provided to the client, we can do a REST call to as below:
curl -H "Authorization:Bearer [<HEADER>.<PAYLOAD>.<SIGNATURE>]" http://127.0.0.1:8080/app/api
Securing LRA endpoints
There are various LRA annotations used, which will internally call the REST APIs that are present in Coordinator and RecoveryCoordinator classes. So, below are the recommendations to, how to define roles for each and every APIs in order to create JWT token for client.
LRA-endPoints | Allowed-roles | |
---|---|---|
getAllLRAs | client | |
getLRAStatus | client |
|
getLRAInfo | client | |
startLRA | client |
|
renewTimeLimit | client | |
getNestedLRAStatus | client |
|
closeLRA | client | |
cancelLRA | client |
|
joinLRAViaBody | client | |
leaveLRA | client |
|
completeNestedLRA | system | |
compensateNestedLRA | system |
|
forgetNestedLRA | system | |
getCompensator | admin |
|
replaceCompensator | admin | |
getRecoveringLRAs | admin |
|
getFailedLRAs | admin | |
deleteFailedLRA | admin |
|
One of the popular tool that could be used to generate JWT tokens would be Keycloak. Keycloak is an open source identity and access management solution. For more details about Keycloak you can also visit keycloak.org.