I have a Micronaut microservice that handles authentication via JsonWebTokens (JWT) from this guide.
Now I'd like to extend this code. The users in my app have some extra attributes such as email, adress, teamId etc. I have all users in the database.
How do I know in the backend controller method which user corresponds to the JWT that is sent by the client?
The guide contains this example code for the Micronaut REST controller:
@Secured(SecurityRule.IS_AUTHENTICATED)
@Controller
public class HomeController {
@Produces(MediaType.TEXT_PLAIN)
@Get
public String index(Principal principal) {
return principal.getName();
}
}
I know that I can get the name of the principal, ie. the username from the HttpRequest. But how do I get my additional attributes?
(Maybe I misunderstand JWT a bit???)
- Are these JWT "claims" ?
- Do I need to load the corresponding user by username from my DB table?
- How can I verify that the sent username is actually valid?
edit Describing my usecase in more detail:
Security requirements of my use case
- Do not expose valid information to the client
- Validate everything the client (a mobile app) sends via REST
Authentication Flow
default oauth2 flow with JWTs:
Precondition: User is already registerd. Username, hash(password) and furhter attributes (email, adress, teamId, ..) are known on the backend.
- Client POSTs username and password to /login endpoint
- Client receives JWT in return, signed with server secret
- On every future request the client sends this JWT as bearer in the Http header.
- Backend validates JWT <==== this is what I want to know how to do this in Micronaut.
Questions
- How to validate that the JWT is valid?
- How to and where in which Java class should I fetch additional information for that user (the additional attributes). What ID should I use to fetch this information. The "sub" or "name" from the decoded JWT?