浏览代码

feat: 支持新版本的itsdangerous

SongZihuan 3 年之前
父节点
当前提交
eab0ae46fb
共有 1 个文件被更改,包括 6 次插入5 次删除
  1. 6 5
      core/user.py

+ 6 - 5
core/user.py

@@ -1,6 +1,7 @@
 from flask_login import UserMixin, AnonymousUserMixin
 from flask_login import UserMixin, AnonymousUserMixin
 from werkzeug.security import generate_password_hash, check_password_hash
 from werkzeug.security import generate_password_hash, check_password_hash
-from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
+from itsdangerous import URLSafeTimedSerializer as Serializer
+from itsdangerous.exc import BadData
 from typing import Optional
 from typing import Optional
 
 
 from configure import conf
 from configure import conf
@@ -107,16 +108,16 @@ class User(UserMixin):
 
 
     @staticmethod
     @staticmethod
     def creat_token(email: str, passwd_hash: str):
     def creat_token(email: str, passwd_hash: str):
-        s = Serializer(conf["secret-key"], expires_in=3600)
+        s = Serializer(conf["secret-key"])
         return s.dumps({"email": email, "passwd_hash": passwd_hash})
         return s.dumps({"email": email, "passwd_hash": passwd_hash})
 
 
     @staticmethod
     @staticmethod
     def load_token(token: str):
     def load_token(token: str):
-        s = Serializer(conf["secret-key"], expires_in=3600)
+        s = Serializer(conf["secret-key"])
         try:
         try:
-            token = s.loads(token)
+            token = s.loads(token, max_age=3600)
             return token['email'], token['passwd_hash']
             return token['email'], token['passwd_hash']
-        except Exception:
+        except BadData:
             return None
             return None
 
 
     @staticmethod
     @staticmethod