Invalidating JSON Web Tokens

前端 未结 28 2937
夕颜
夕颜 2020-11-22 06:17

For a new node.js project I\'m working on, I\'m thinking about switching over from a cookie based session approach (by this, I mean, storing an id to a key-value store conta

28条回答
  •  闹比i
    闹比i (楼主)
    2020-11-22 07:04

    Haven't tried this yet, and it is uses a lot of information based on some of the other answers. The complexity here is to avoid a server side data store call per request for user information. Most of the other solutions require a db lookup per request to a user session store. That is fine in certain scenarios but this was created in an attempt to avoid such calls and make whatever required server side state to be very small. You will end up recreating a server side session, however small to provide all the force invalidation features. But if you want to do it here is the gist:

    Goals:

    • Mitigate use of a data store (state-less).
    • Ability to force log out all users.
    • Ability to force log out any individual at any time.
    • Ability to require password re-entry after a certain amount of time.
    • Ability to work with multiple clients.
    • Ability to force a re-log in when a user clicks logout from a particular client. (To prevent someone "un-deleting" a client token after user walks away - see comments for additional information)

    The Solution:

    • Use short lived (<5m) access tokens paired with a longer lived (few hours) client stored refresh-token.
    • Every request checks either the auth or refresh token expiration date for validity.
    • When the access token expires, the client uses the refresh token to refresh the access token.
    • During the refresh token check, the server checks a small blacklist of user ids - if found reject the refresh request.
    • When a client doesn't have a valid(not expired) refresh or auth token the user must log back in, as all other requests will be rejected.
    • On login request, check user data store for ban.
    • On logout - add that user to the session blacklist so they have to log back in. You would have to store additional information to not log them out of all devices in a multi device environment but it could be done by adding a device field to the user blacklist.
    • To force re-entry after x amount of time - maintain last login date in the auth token, and check it per request.
    • To force log out all users - reset token hash key.

    This requires you to maintain a blacklist(state) on the server, assuming the user table contains banned user information. The invalid sessions blacklist - is a list of user ids. This blacklist is only checked during a refresh token request. Entries are required to live on it as long as the refresh token TTL. Once the refresh token expires the user would be required to log back in.

    Cons:

    • Still required to do a data store lookup on the refresh token request.
    • Invalid tokens may continue to operate for access token's TTL.

    Pros:

    • Provides desired functionality.
    • Refresh token action is hidden from the user under normal operation.
    • Only required to do a data store lookup on refresh requests instead of every request. ie 1 every 15 min instead of 1 per second.
    • Minimizes server side state to a very small blacklist.

    With this solution an in memory data store like reddis isn't needed, at least not for user information as you are as the server is only making a db call every 15 or so minutes. If using reddis, storing a valid/invalid session list in there would be a very fast and simpler solution. No need for a refresh token. Each auth token would have a session id and device id, they could be stored in a reddis table on creation and invalidated when appropriate. Then they would be checked on every request and rejected when invalid.

提交回复
热议问题