Jakarta Security and REST in the Cloud Part 3: Knowing the OAuth2
Jakarta Security and REST in the Cloud Part 3: Knowing the OAuth2
Jakarta Security and REST in the Cloud: Part 2 Getting to Know the OAuth 2.0.
Join the DZone community and get the full member experience.Join For Free
Security is generally a topic that is left out when talking about software architecture, but that doesn’t mean that it is not essential. To talk more about it, we created this series on Java API security with Jakarta EE. In this third part, we will talk about the OAuth2 authentication process, moving it quickly to the cloud, and how to implement it with two MongoDB and Redis databases.
OAuth 2.0 is a protocol that allows permission to access authorization resources between systems or sites with the benefits of a better encapsulation of critical information such as username and password. An overview of OAuth 2.0:
- The first step is the authorization request. It identifies the user's authenticity
- Once authorized, the next step is to request the token
- With this access token, all requests will be made from it
This is a very short summary, but if you want to know more about OAuth 2.0, it is worth taking a look at the specification.
One of the significant advantages of this approach is low password exposure, in addition to being able to generate a large number of tokens for the same user. It is possible to create a token for each device, and if you wanted to revoke access to a specific device, you would just remove the token from this device. Another critical point is that none of these devices have access to the user's login and password, only the token. Thinking about encapsulation, the less exposed the password, the better for security.
On the other hand, we have significantly increased the complexity of the architecture; after all, before it was just a password, now, we have a large volume of tokens managed by each user.
OAuth2 in architectural terms will follow the following steps:
- The first step, following the rule, is to request to obtain a new token
- This request will result in a pair of tokens: an access_token and a refresh_token
- From that moment, every request will be made with the "access_token"
- The access_token will expire at some point making it useless
- The next step will be to update it, creating a new token, thanks to refresh_token. We will make a new request, however, this time with the refresh_token that will return the result already mentioned in step 2
- The cycle will continue again using access_token until it expires back
This cycle only applies to one device -- if it is necessary to have another access to another machine, each one will have its period with its respective token.
It is possible to separate the logic that generates tokens from the user's information logically and physically, that is, on another server. But in our example, we will simplify by demonstrating a logical, but not physical, separation. The authentication mechanism logic will be created as a subdomain of the security API since there is a dependency to authenticate the user. Since they have a TTL, we will use Redis.
When we talk about the code, in general, we will take advantage of practically all the logic of storage and user management from the second part of the article, with the difference that the mechanism will be different. We will add some others that deal with the generation of tokens with Redis.
Regarding the modeling and persistence of the tokens, three new entities will be created. One important thing is that the encapsulation rules are still relevant. And it is only for this reason that we are using private visibility and public access methods, in cases where we have no other option. It is a good rule that Effective Java speaks so much about, besides being a good practice for security. The critical point is that these entities, in addition to Jakarta NoSQL notations, also use JSONB notations. The reason for this is that Redis stores the information as text, and the storage strategy that the Redis driver uses is in JSON, using some kind of implementation from the Jakarta world.
Going to the rule for generating and updating these tokens, we have the class OAuth2Service that will manage all the logic of the storage mechanism. Data validation will be performed from Bean Validation, and, as it starts from the context, both for creating and updating the token, we create a group for each setting. Thus, as in MongoDB, the value-key API can use the Repository; however, as the operations are quite simple, the KeyValueTemplate will be used. An important point is when the refresh token is persisted, having a second parameter that defines the TTL. This means that, after a specific time, the information will be automatically deleted from Redis.
Within the OAuth2 feature, you can see that we have a method and two operations. It is one of the reasons that in the code, the getGrantType method returns an enum with two options.
The OAuth2Authentication class receives the request and searches for the “Authorization” header validating using the regex. Once the Header has been approved, the next step is to check the existence of this token within the database, in which case we use Redis. Once the refresh token is verified, we send the user ID to the storage mechanism represented by the IdentityStoreHandler.
The identification class needed some changes compared to the second part. It continues to load user information and permission rules, however, there is no password validation, as all of this is done by the user's identifier.
The critical point is that although the authentication mechanism depends on the database, they are not known, thanks to the security API. Following the domain and subdomain rule, the OAuth2 subdomain is allowed to see the security API. However, the opposite is not allowed to occur for several reasons. This cyclical dependency brings several problems, for example, if at some point we want to move the logic to a server. it will be more difficult because there is difficulty to maintain the software in this way. A simple way of thinking about cyclical addiction is to reflect on the classic chicken and egg question.
However, here's a problem: When a user has removed, it is also essential that the respective tokens will also be removed. One way to create this separation is through events. As we are using CDI, it will fire events.
Moving to the Cloud
In our series of articles, we are using a PaaS to simplify the complexity of hardware and security of access between containers. After all, it would be all in vain if we did strict control on the software, and the database had a public IP for the entire internet. Thus, we will maintain the strategy of using Platform.sh.
We will only mention changing the services file to add another database, in this case, Redis:
It will be necessary to carry out the modification within the application configuration file. The goal is to provide credentials so that the application container has access to the database container.
With that, we talk about the concepts and the design of an OAuth2 mechanism in a practical way, using Jakarta Security. The critical point is that for each request, we need to perform a search within the database, since the token is nothing more than a pointer to the information; however, it is not the information itself. There is an exciting way to use the symbol to put information, for example, by combining the token with JWT. This combination of OAuth2 and JWT will be scenes from the next chapter.
As always, the code example can be found on GitHub.
Opinions expressed by DZone contributors are their own.