jsonwebtoken, v8.5.0
node v10.13.0
npm 6.4.1
If i create a token several times with:
jwt.sign({ user_email: user_email, user_id: user_id, username: username }, 'RESTFULAPIs')
Question 01:
It seems the first 2 parts of the string are always the same (the base64 encoded header
and payload
values), but the third part (the signature
) is different.
Why is the signature
different when the original values are the same?
What I've Tried:
I have read the signature
section at jwt.io/introduction:
To create the signature part you have to take:
- the encoded header
- the encoded payload
- a secret
- the algorithm specified in the header
and sign that.
So, as a guess:
Is the signature
the result of encrypting the base64 encoded header
and payload
values using the HS256
algorithm and secret
, which in this case is the string RESTFULAPIs
, which produces a different result each time it is encrypted, whilst the decoded result is always the same?
Question 02:
The decoded value of the different tokens is always the same, except for an object property called iat
. What does that property represent?
{
iat: 1561358034
user_id: "25423537fshsdgA"
user_email: "[email protected]"
username: "bob"
}
{
iat: 1561358156
user_id: "25423537fshsdgA"
user_email: "[email protected]"
username: "bob"
}
Actually, after researching this second question more, I came across this:
The "iat" (issued at) claim identifies the time at which the JWT was issued. This claim can be used to determine the age of the JWT. Its value MUST be a number containing a NumericDate value.
Source: https://www.rfc-editor.org/rfc/rfc7519#section-4.1.6
payload
changing due to the automatically addediat
, i hadn't thought of that, however i have just double checked - when generating multiple tokens with the same header and payload (excluding theiat
property that i assumejwt
adds automatically when usingjwt.sign()
), the first two parts of all the tokens are the same (the encodedheader
andpayload
), whereas the third part is different (thesignature
). – Croomiat
property, which was causing the signature to be different, seems to be correct. – Croom