Introduction To JWT
Introduction To JWT
Header: Contains metadata about the token, including the type (JWT) and
the signing algorithm (e.g., HMAC, RSA).
Introduction to JWT 1
npm install jsonwebtoken
Structure of JWT
A valid JWT consists of three parts (header, payload, and signature) separated by
periods ( . ).
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZ
// Header
{
"alg": "HS256",
"typ": "JWT"
}
// Payload
{
"sub": "1234567890",
"name": "John Doe",
"iat": 1516239022
}
// Signature
HMACSHA256(
base64UrlEncode(header) + "." +
base64UrlEncode(payload),
your-256-bit-secret
)
Introduction to JWT 2
In the context of JSON Web Tokens (JWTs), the signature is a cryptographic
element that ensures the integrity and authenticity of the token. It is created using
a signing algorithm and a secret key (for HMAC) or a private key (for asymmetric
algorithms like RSA).
2. Authenticity: The signature confirms that the token was issued by a legitimate
source. Only someone with access to the secret key (for HMAC) or private key
(for RSA) can generate a valid signature.
1. Combining the Header and Payload: The header and payload are Base64-
URL encoded and concatenated with a period ( . ) in between.
Copy
signatureInput = base64UrlEncode(header) + "." + base64UrlEncode(payload)
2. Applying the Signing Algorithm: The combined string is then processed with
a cryptographic signing algorithm (e.g., HMAC SHA-256) along with the secret
or private key.
Copy
signature = HMACSHA256(signatureInput, your-256-bit-secret)
Introduction to JWT 3
We can use the sign method from JWT to generate a JWT by providing three main
information to the method.
The JWT Secret key that is used for signature and later used to decode the
payload from the JWT
The expiresIn option for setting the expiration time of a JSON Web Token (JWT) can
be specified in various formats when using libraries like jsonwebtoken . Here are the
common formats you can use:
2. String Format:
Introduction to JWT 4
You can use a string to specify the expiration time in a more readable way.
/**
* Generate or sign token
* @param {string} id
* @returns {string} token
*/
export default (id: string, role: string) => {
return sign({ id, role }, config.jwt.secret, {
expiresIn: "90d",
});
};
Token
JWT Secret
The response of the verify method or the decoded data includes the following key
value pairs.
Introduction to JWT 5
exp - Token expire date provided during token creation
/**
* Verify token
* @param {string} token
* @returns {JwtPayload} Jwt payload
*/
export default (token: string): ICustomJwtPayload => {
return verify(token, config.jwt.secret) as ICustomJwtPayload;
};
Causes of TokenExpiredError
1. Expired Token: The primary reason for this error is that the token has
exceeded its allowed lifespan. If the exp claim in the token is set to a time in
the past, any attempt to verify or use that token will result in a TokenExpiredError .
Introduction to JWT 6
2. Incorrect System Time: If the server's system time is out of sync (e.g., due to
misconfiguration), it may incorrectly interpret the expiration time.
problem with the JWT that is being verified or decoded. This error indicates that
the token is not valid for some reason, but it is not specifically about expiration.
The token's signature does not match the expected signature when
verified with the secret key or public key. This could happen if the token
has been tampered with.
2. Malformed Token:
The token does not conform to the JWT format. A valid JWT consists of
three parts (header, payload, and signature) separated by periods ( . ).
3. No Signature:
The secret or public key used for verification does not match the one used
to sign the token, or the specified algorithm during verification is incorrect.
Introduction to JWT 7