When No Refresh token is used:
1.While changing password: when the user changes his password, note the change password time in the user db, so when the change password time is greater than the token creation time, then token is not valid. Hence the remaining session will get logged out soon.
2.When User logs out: When the user logs out, save the token in a seperate DB (say: InvalidTokenDB and remove the token from Db when token expires). Hence user logs out from the respective device, his sessions in other device left undisturbed.
Hence while invalidating a JWT, I follow the below steps:
- Check whether the token is valid or not.
- If valid, check it is present in invalidTokenDB (a database where logged out tokens are stored till their expiry time).
- If its not present, then check the token created time and changed password time in user db.
- If changed password time < token created time, then token is valid.
Concern with the above method:
- For each api request, I need to follow all the above steps, which might affect performance.
When Refresh token is used: with expiry of access token as 1 day, refresh token as lifetime validity
1. While changing password: When the user changes his password, change the refresh token of the user. Hence the remaining session will get logged out soon.
2. When User logs out: When the user logs out, save the token in a seperate DB (say: InvalidTokenDB and remove the token from Db when token expires). Hence user logs out from the respective device, his sessions in other device left undisturbed.
Hence while invalidating a JWT, I follow the below steps:
- check whether the token is valid or not
- If valid, check whether the token is present in InvalidTokenDB.
- If not present, check the refresh token with the refresh token in userDB.
- If equals, then its a valid token
Concern with the above method:
- For each api request, I need to follow all the above steps, which might affect performance.
- How do I invalidate the refresh token, as refresh token has no validity, if its used by hacker, still the authentication is valid one, request will be success always.
Note: Although Hanz suggested a way to secure refresh token in Using Refesh Token in Token-based Authentication is secured? , I couldn't able to understand what he is saying. Any help is appreciated.
So If anyone have nice suggestion, your comments are welcome.
UPDATE:
I am adding the answer incase your app needs no refresh token with lifetime expiry. This answer was given by Sudhanshu (https://stackoverflow.com/users/4062630/sudhanshu-gaur). Thanks Sudhanshu. So I believe this is the best way to do this,
When No Refresh token needed and no expiry of access tokens:
when user login, create a login token in his user database with no expiry time.
Hence while invalidating a JWT, follow the below steps,
- retrieve the user info and Check whether the token is in his User database. If so allow.
- When user logs out, remove only this token from his user database.
- When user changes his password, remove all tokens from his user database and ask him to login again.
So with this approach, you don't need to store neither logout tokens in database until their expiry nor storing token creation time while changing password which was needed in the above cases. However I believe this approach only valids if your app has requirements with no refresh token needed and no expiry of the tokens.
If anyone has concern with this approach, please let me know. Your comments are welcome :)