-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjwt.py
38 lines (30 loc) · 928 Bytes
/
jwt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# python-jose
from datetime import datetime, timedelta
from jose import jwt
ALGORITHM = "HS256"
SECRET_KEY = "VlBIHQ6005uORLSmnivHPHe2-8Xu6wwCD75JpvOUUqI"
"""
iss: jwt的签发者
sub: jwt面向的用户
aud: 接收jwt的一方
exp: 过期时间
iat: 签发时间
"""
def gen_token(user_id):
expire = datetime.utcnow() + timedelta(minutes=60 * 24)
to_encode = {"exp": expire, "sub": str(user_id)}
token = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
return token
def decode_jwt(token):
options = {"verify_exp": True}
try:
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM], options=options)
except jwt.ExpiredSignatureError:
# TODO 提示前端重新登录
print("token过期")
except jwt.JWTError:
# TODO 无效签名
print("token过期")
if __name__ == '__main__':
token = gen_token("wang")
data = decode_jwt(token)