I'm trying to understand JWT tokens usage but I'm getting lost on the theorycal part.
I have some questions about JWT Tokens structure, in order to make this question a sort of documentation for new users, providing it with a logic order, I will write them in bold below as long as I list a sample JWT content.
I will also summary them in the end of the question
A "classic" JWT token is composed as follows:
[HEADER].[PAYLOAD].[SIGNATURE]
In detail:
HEADER
{
"alg": "HS256",
"typ": "JWT"
}
Which contains the following fields:
- ALG = Encryption algorythm (using the default HS256 could be fine)
- TYP = simply tells that it's a JWT
PAYLOAD
{
"sub": "1234567890",
"name": "MrJohnDoe",
"iat": 1516239022
}
- SUB = Is an OPTIONAL parameter. It's the subject of the token. (credits: see @cassiomolin answer)
According to: Where to store user id in jwt, it looks like you can use it to store your user ID in it.
- NAME = The username
- IAT = Token creation date and time, expressed in unix timestamp. (Thanks to @jps and @JeanRostan in the comments below)
SIGNATURE
HMACSHA256(
base64UrlEncode(header) + "." +
base64UrlEncode(payload),
secret
)
- SECRET = Unique key known just by the server. To not confuse with the current user password, which should never be used for this! (thanks to @jps in the comments below)