skip to Main Content

I’ve read several articles (such as this and this, and this SO answer) that suggest storing refresh tokens server-side in a database, or in-memory (or an in-memory store, such as Redis). From what I understand this is so they can be revoked.

Is there a good reason for storing all tokens as these articles suggest, rather than just storing blacklisted tokens on logout? If I understand the reasons for storing tokens, surely I could achieve the same effect by storing a token id in Redis (with a TTL as long as the expiry, so that the table doesn’t grow unweildy).

Are there downsides to this approach, and if so, what are they (or conversely, what are the upsides of storing all tokens, vs just a list of revoked tokens?)


To elaborate, and why I think it should be fine to have a revocation list, here’s the process I am imagining:

  • Issue tokens out
  • Once revoked (say, on logout), add an entry to a blacklist for a unique id (say, public_user_id if there are many tokens for different devices) with the revocation time, and add a TTL until the token’s expiry
  • When a token is provided for auth, when there is an existing blacklist entry:
    • if a valid token is used, it’ll be before it’s expiry and have a creation time after the blacklist entry creation time for the unique id
    • if an invalid token is used, it’ll either be expired or within the blacklist with the unique identifier before the entry creation

Am I missing something critical in that flow that would require a list of all tokens instead?

3

Answers


  1. The advantage of having a list of all issued tokens is that you can have a full view of who has been already authenticated and has currently access to the system. You can then choose to revoke some tokens from this list based on any criteria (e.g. the age of the token, the roles associated with the user of the token, the IP address ranges).

    If you only have a list of revoked tokens, it would be impossible to choose at runtime, an arbitrary criteria, to revoke a subset of the valid tokens. Stated otherwise, if you don’t have a list of all issued tokens, the revocation criteria cannot be enforced globally at once, but only when a token is presented to a resource server.

    Login or Signup to reply.
  2. I would aim for a direction of technical simplicity here, since the Authorization Server (AS) should do the hard work for you. Here are some end to end notes which explain some tricky aspects and suggest a simple direction.

    1. TOKEN ISSUING

    The user authenticates (and optionally consents) resulting in a token ‘grant’. The AS should then store refresh tokens for you, in a database table that might be named ‘delegations’. Typically the stored ‘token’ will be a hash rather than the real value, and will be linked to the application (client_id) and user (subject). Tokens issued might have these lifetimes:

    • Refresh token: 4 hours
    • Access token: 30 minutes

    2. TOKEN REFRESH

    OAuth clients such as mobile apps will silently renew access tokens during the lifetime of the grant or ‘user session’. This involves sending a refresh token to the AS and getting back a new access token. For this to work the AS needs to store a hash of the refresh token in order to be able to validate the input.

    3. DEFAULT REMOVAL BEHAVIOUR

    When a user logs out, tokens are cleared from the client app, so they are gone. Newer OAuth 2.1 recommendations are to use rotating refresh tokens, where each access token refresh also renews the refresh token and invalidates the previous one. In our example this now means that the lifetime of a stolen refresh token is likely to be reduced – perhaps to only 30 minutes.

    4. MANUAL REVOCATION BY ADMINISTRATOR

    If for some reason you want to explicitly deny access to a particular user and application, an administrator could use the AS database and issue a command like this, though the Admin UI may provide more visual options.

    • delete from delegations where client_id=[value] and subject=[value]

    Whether manual revocation is likely to be manageable at a people level is questionable but it is a good capability to have, eg in security reviews.

    5. REVOCATION OF REFRESH TOKENS ON LOGOUT

    Of course the client can revoke its own refresh tokens on logout if required, before clearing tokens. This should also ensure that any access tokens for the same grant are rejected by the Authorization Server.

    6. ACCESS TOKEN VALIDITY

    Access tokens are most commonly JWTs. Revocation or logout may occur when the JWT still has 25 minutes to live. If an attacker has somehow intercepted an access token (which shouldn’t usually be possible) they can continue to use it against your APIs – during this period the AS will never see the access token.

    7. API GATEWAY SETUPS

    In a more sophisticated setup, opaque access tokens are issued to internet clients, then sent to an API gateway, which introspects them, as in the Phantom Token Pattern. The gateway also maintains a cache of the access token result.

    At the time of revocation the AS can raise a Custom Event to inform the API Gateway, which can then clear any cached access tokens for the user. This should ensure that the very next request with an access token for the revoked (or logged out) user is rejected.

    SUMMARY

    Unless you are dealing with a very high security domain I would aim for simplicity and follow these two principles:

    • Leave token storage and revocation to the Authorization Server
    • Keep tokens short lived so that revocation becomes less of an issue
    Login or Signup to reply.
  3. The only deciding factor I can think of, that will require a list of all refresh tokens is the following:

    Do you / will you, at any point, need to have a functionality where you can dynamically revoke valid refresh tokens, based on some arbitrary, regulatory, legal, integrity, security etc. criteria?

    If so, the least you will need is a list of all issued tokens, plus any metadata required to implement the criteria logic.

    Example: "Due to regulation, I need to ban all EU users" equates to delete from refresh_tokens were user_ip in <... eu logic ...>

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search