I'm implementing a JWT-based REST API for the first time and I'm trying to figure out what information to send back on the response entity when a user logs in. The strategies I am considering are:
- Just an access token, encoded with claims such as user first/last name and other items; or
- Access token and user first/last name (and other items)
So the response entity for the first strategy might look like:
{
"accessToken": <JWT-encoded-with-user-first-and-last-name-claims>
}
And the the response entity for the second strategy might look like:
{
"accessToken": <JWT>,
"userFirstName": "Jerry",
"userLastName": "Jingleheimer"
}
Ideally I'd like to go with the first option (its more succinct) but I need to verify my understanding of how JWTs work and are meant to be used.
My understanding is that JWTs are meant to be fully decodeable, meaning I can be given any old JWT, plop it into jwt.io, and see the claims and information, expiry, etc. contained in the JWT. But its read-only, and the moment it expires the server should refuse to use it for authentication. But since its meant to be decoded, its totally fine to put things like user first/last name, their roles, etc. in as token claims, and then use that information on the client-side.
My understanding is that to create a JWT, you need to sign it with a secret, and that when you present the server with a JWT for authentication, it somehow compares the secret that was used to sign the JWT with the secret the server is configured to use for creating the JWTs in the first place. I am not understanding how it does this comparison though, as I would think that if the JWT is decodeable, wouldn't that expose the secret used to sign it?
Is my understanding of JWTs correct, especially with respect to the signing + verification of JWT using secrets (and decoding them)? And are there any anti-patterns with respect to what types of info should/should not be added to a JWT claim? For instance, is it perfectly OK to put user first name and last name in the JWT claims? Otherwise I'd have to do 2 API calls: one to login and get the access token, and another to use the access token to fetch user info. Thanks in advance for any-and-all clarification!