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 = What is SUB? Can you please tell me an example of what it could be in a common usage?
See @CassioMazzocchiMolin answer below for this- SUB = Is an OPTIONAL parameter. It's the subject of the token. (credits: @CassioMazzocchiMolin)
According to: Where to store user id in jwt looks like you can use it to store your user ID in it. - NAME = The username
- IAT =
Token expiration time. Is it expressed in ms?
Thanks to @jps and @JeanRostan in the comments below.- IAT = Token creation date and time, expressed in unix timestamp.
SIGNATURE
HMACSHA256(
base64UrlEncode(header) + "." +
base64UrlEncode(payload),
secret
)
What's actually the "secret"? Should I use user's password as secret?
Thanks to @jps in the comments below.- SECRET = Unique key known just by the server. To not confuse with the current user password, which should never be used for this!