Authenticating users through token-based authentication is becoming increasingly popular as the client-server model is widely used. While session or cookies were commonly used to identify user sessions in the past, now we have another option which is token-based authentication, especially jwt.
Jwt is a string used for identification, usually the user's login session. Jwt works by digitally signing the attached token payload for authentication.
For example, if we need to store basic information to identify a user:
{
"user_id": 1,
"role": "user",
}
Jwt will encode that information along with a "signature", which can only be verified by the server. A valid jwt also means the accompanying information is accurate.
In the jwt, the exp
attribute is the expiration time of the token. A signed token will have an expiration time. The token becomes effectively invalidated after that time. Jwt is designed to reduce querying the database for identity information. The server only needs to authenticate the token and retrieve the necessary information.
Therefore, jwt is only truly invalidated when the exp
has expired. So how do we revoke a jwt token when a user logs out? Since logging out essentially means ending the session, the token must no longer be valid.
Clearly, this method does not have any effect on server-side security, but it is the simplest approach. When applying this method, pray that hackers do not get hold of your token before you log out. Just kidding, you should only apply this method when security is not too strict.
You can store invalid tokens until their signed expiration date and compare them with incoming requests.
However, this seems to break the principle of not querying the database from the beginning, as you will need to query the database for every request.
But the storage data may be lower, as you only need to store the tokens that users have logged out of.
This method also brings a major risk as you are still unsure if the token was stolen before you logged out. Or suppose before making the logout request, the client had an error and couldn't query, but it already deleted the data on the client, causing the token to not really be added to the blacklist on the server.
If you have heard of refresh tokens, this is the method that uses refresh tokens to reduce the expiration time of the tokens to the lowest possible.
Keeping the expiration time of the token short makes the token only exist for a short period of time, forcing the use of a refresh token to obtain a new token and keep rotating like that. If the token or refresh token is leaked, we can simply add that refresh token to the blacklist. This also helps reduce the storage data in the blacklist compared to keeping the access token.
In any case, be prepared to face a user's token being leaked and you have to handle them. The simplest solution is to invalidate the identifier id stored in the token's payload. For example, if the payload you sign for the token includes:
{
"user_id": 1,
"role": "user",
}
Then change the id of the user to 1 in the database. Or if it is serious, change the secret used to sign the token to immediately invalidate all issued tokens.
Anyway, this method is only a last resort. What I want to say is, be prepared with backup plans to deal with token leaks at any time. Jwt is designed to reduce server load, but consider the trade-off between performance and security.
Access Token is a string used to identify a user's session. When the access token is a token created according to the jwt standard, we can identify the user without querying the database. Every jwt token comes with an expiration time, and if it is leaked or handled incorrectly when the user logs out, the user's data is vulnerable to attacks. Therefore, be cautious when handling tokens when a user logs out, and have contingency plans to deal with token leaks at any time.
5 profound lessons
Every product comes with stories. The success of others is an inspiration for many to follow. 5 lessons learned have changed me forever. How about you? Click now!
Subscribe to receive new article notifications
Hello, my name is Hoai - a developer who tells stories through writing ✍️ and creating products 🚀. With many years of programming experience, I have contributed to various products that bring value to users at my workplace as well as to myself. My hobbies include reading, writing, and researching... I created this blog with the mission of delivering quality articles to the readers of 2coffee.dev.Follow me through these channels LinkedIn, Facebook, Instagram, Telegram.
Comments (1)