JWT authentication & refresh token implementation

前端 未结 3 1908
名媛妹妹
名媛妹妹 2020-12-23 10:29

I am developing a REST application with its own authentication and authorization mechanism. I want to use JSON Web Tokens for authentication. Is the following a valid and sa

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-23 11:10

    My understanding of refresh token here is:

    Due to the presence of refresh token, we can keep shorter validity period for access token and check frequently (at the expiry of access token) that the user is still authorized to login.

    Please correct me if I am wrong.

    Assuming you're talking about using JWT as Bearer-token in OAuth (and I would strongly advice to follow the OAuth 2.0 protocol), that's right.

    With an additional auth-time (timestamp of authentication) claim in your JWT, you could even drop the second token and sent your access- as a refresh-token (the auth-server could then issue a new access-token if token is valid & auth-time within allowed range)... but sure, it's also good to follow the standard ;)

    Anyway, there are certain additional aspects (that tend to get difficult or are even against the fundamental ideas of JWT) you should consider before using JWTs as refresh-token, as this basically means you introduce long-living JWT:

    • do you need to have something like forced user logout/ token revocation by subject (e.g. if user got identified as fraudulent)?
    • do you need to have something like revocation of a specific token (e.g. if a user looses a device)?
    • ...

    Dependent on your use-case you should consider all the possible implications, long-living tokens have as they usually require you to introduce some kind of state on your server-side (e.g. to allow revocation/ blacklisting). Keep in mind the beauty and security of the JWT concept lies within JWTs being short-lived.

提交回复
热议问题