The issue with JWTs is that there is no way to revoke them.
Except you can have a nonce in the JWT that corresponds to a field on the server… which is revokable.
Comment on How to store user's access tokens/API keys without hashing them?
towerful@programming.dev 1 year agoIs the client a web browser used by an end user? Or is it a trusted environment?
Because if it’s a trusted environment (server to server) then you could add an extra field to your user table for api_key
, and an extra tokens
table to your database.
Think of githubs legacy access token system (and, again, it’s now legacy because it’s a dated and insecure way of doing it).
Each user gets a randomly generated 16 character string as their api_key
.
Then the user is given a way to create/regenerate/delete records into the tokens
table: a friendly name, user id relation, and finally a randomly generated 16 character string as for the token.
You could even add some sort of token expiry date, to limit the timeframe of damage for a leaked key.
Another option for untrusted environments (egba we browser) is JWT. It’s used a lot for microservices.
It’s a token with a lifespan minted by your Auth server. Anyone can decode the token and inspect the payload, so it’s not secure for storing passwords but it’s great for storing user ID and perhaps access scopes. The token can be verified by anyone to ensure the token is authentic and hasn’t been tampered with.
But only servers that know the JWT secret can mint them.
The issue with JWTs is that there is no way to revoke them. If you mint a jwt that’s valid for 4 years, the only way to invalidate it is by having all servers share a list of revoked tokens - or by having all servers call back to the Auth server that minted the token (which probably maintains a revoke list) to check it’s still valid. And, there is no way to “ban” a user if they still have a valid token.
Essentially the JWT is keys to the kingdom, and they are hard to get back.
Which is why they often have lifetimes of 5-30 minutes, and - you guessed it - are issued along with a refresh token.
The issue with JWTs is that there is no way to revoke them.
Except you can have a nonce in the JWT that corresponds to a field on the server… which is revokable.
Oh, as opposed to a revocation list.
Yup, I’m an idiot
pe1uca@lemmy.pe1uca.dev 1 year ago
Oh I’ve only used JWTs with OIDC so I didn’t thought about using them directly.
It could be a good solution since the user can generate them on their own and we can validate them with the correct information (secret or public key).
About the issue of long lived or not expiring JWT, maybe a custom restriction of valid tokens with lifespans of more than X amount of minutes are rejected?
Yeah, the token could be a valid one but we could say the payload is invalid for our API.