Reputation: 11966
I'm using Flask with itsdangerous to implement token based login. It works fine but when user logs out with logout_user()
call the token is still valid, causing inconsistency. So I'm trying to make the token expire too but couldn't find a proper way. Here is what I came up with:
def generate_auth_token(self, expiration):
s = Serializer(current_app.config['SECRET_KEY'],
expires_in=expiration)
return s.dumps({'id': self.id}).decode('ascii')
@staticmethod
def verify_auth_token(token):
import flask_login
u = flask_login._get_user()
if not u or isinstance(u, flask_login.AnonymousUserMixin):
return None
s = Serializer(current_app.config['SECRET_KEY'])
try:
data = s.loads(token)
except:
return None
return User.query.get(data['id'])
But I don't like this solution. Could somebody suggest a better way?
Upvotes: 1
Views: 2878
Reputation: 159905
Ultimately, token-based authentication doesn't admit of easy particular invalidation. (Universal invalidation is easy - just change the SECRET_KEY
used to sign the sessions - every session is automatically invalidated). There are several ways to make particular invalidation work:
Upvotes: 7