Authentication is required to secure user data and to provide access to certain resources that are only accessible to authorized users. One of the most widely adopted solutions being used today to manage authentication in modern applications is JSON Web Tokens. Let us have a detailed discussion on the concept of JWT authentication, how JWTs work, and access and refresh tokens.
A JSON web token is defined as a compact, self-contained way to securely transmit information between two parties as a JSON object. The majority of the usage of JWTs is in authentication and authorization. How?
JWT Structure
A JWT consists of three parts:
Each of the parts is encoded using Base64Url and joined using periods (.) thus forming a string such as ‘header.payload.signature’
The header consists of mainly two parts.
The payload is composed of claims. Claims are statements about an entity, usually the user, and some supplementary metadata.
There are 3 types of claims. These include:
Registered Claims: These are pre-defined claims like 'sub' - Subject, 'exp' - Expiration Time, and 'iat' - Issued At. These are recommended by the JWT standard to provide a set of useful, interoperable claims.
Public Claims: These are custom claims. Anybody can define them. They should be collision-resistant. It contains user-defined data, for example, user roles.
Private Claims: These are custom claims designed for sharing information between parties that have agreed to use them. They are used in application-specific requirements.
First of all, one should take the encoded header, encoded payload, a secret key, and the algorithm specified in the header. For example, if you are using HMAC SHA256 (HS256), then the signature will be created like this:
The secret key used to sign the JWT forms an integral part of ensuring both the integrity and authenticity of the token. This key should never be shared with the client and only be kept secure on the server.
Within JWT-based authentication, there are two major, different roles for access and refresh tokens on user session management.
Access Token
Refresh Token
What Happens When an Access Token Expires?
If an access token has expired:
Whenever a refresh token is used to obtain a new access token, a new refresh token is usually generated and returned to the client. The practice provides a rotation of refresh tokens too. Therefore, that adds some more security to them.
How JWT Authentication Works?
The user logs in with his credentials (username and password) through the exposed login endpoint.
On successful authentication, JWTs (access token and refresh token) are generated by the server, which comprises the user's information and claims. Again, these tokens will be sent back to the client.
The client will store the received tokens in local storage or cookies.
For subsequent requests, the client includes the access token in the Authorization header using the Bearer schema and sends requests to the server.
The server verifies the signature of the token, extracts the payload, and grants access. In case the token is valid, it will process the request; otherwise, it returns an error.
Some Best practices are,
The secret key should not be exposed to any client-side code or be in version control. To manage it, store it as an environment variable or use a secure storage solution.
Include mechanisms that would gracefully handle token expiration. Use short-lived access tokens with long-lived refresh tokens, and provide a clear process by which users could refresh the tokens or re-authenticate when necessary.
Send JWTs only over HTTPS. This will keep them encrypted during transit and safe from intercepting attackers.