在验证时在其危险的序列化器中重置expire_in

时间:2019-03-28 09:45:19

标签: python itsdangerous

我基本上是在验证序列化器的过程中重置过期时间,以便在有活动时保持会话活动:

 from itsdangerous import (TimedJSONWebSignatureSerializer as Serializer, BadSignature, SignatureExpired)
import time 

SECRET_KEY = "very secret key"
EXPIRES = 4 # Seconds

def check_token(token):
    s = Serializer(SECRET_KEY)

    try:
        data = s.loads(token)
    except SignatureExpired:
        return "Session Expired"
    except BadSignature:
        return "Bad Signature"
    else:
        # How can I reset expire counter here?

        return data["id"]


s = Serializer(SECRET_KEY, expires_in=EXPIRES)

token = s.dumps({"id": "logged_user"})

time.sleep(2)
print(check_token(token))
time.sleep(2)
print(check_token(token))
time.sleep(2)
print(check_token(token))

因此在此代码内,到期时间为4秒。因此,当我验证令牌时,我希望它再次将其重置为4秒,第3个check_token谁也应该返回用户ID。

那么,该怎么做?

0 个答案:

没有答案