JWT (JSON Web Token) automatic prolongation of expiration

后端 未结 12 2178
一向
一向 2020-11-22 10:56

I would like to implement JWT-based authentication to our new REST API. But since the expiration is set in the token, is it possible to automatically prolong it? I don\'t wa

12条回答
  •  孤城傲影
    2020-11-22 11:38

    In the case where you handle the auth yourself (i.e don't use a provider like Auth0), the following may work:

    1. Issue JWT token with relatively short expiry, say 15min.
    2. Application checks token expiry date before any transaction requiring a token (token contains expiry date). If token has expired, then it first asks API to 'refresh' the token (this is done transparently to the UX).
    3. API gets token refresh request, but first checks user database to see if a 'reauth' flag has been set against that user profile (token can contain user id). If the flag is present, then the token refresh is denied, otherwise a new token is issued.
    4. Repeat.

    The 'reauth' flag in the database backend would be set when, for example, the user has reset their password. The flag gets removed when the user logs in next time.

    In addition, let's say you have a policy whereby a user must login at least once every 72hrs. In that case, your API token refresh logic would also check the user's last login date from the user database and deny/allow the token refresh on that basis.

提交回复
热议问题