mirror of
https://github.com/suyiiyii/nonebot-bison.git
synced 2025-06-06 20:06:12 +08:00
21 lines
507 B
Python
21 lines
507 B
Python
import random
|
|
import string
|
|
from typing import Optional
|
|
import jwt
|
|
import datetime
|
|
|
|
_key = ''.join(random.SystemRandom().choice(string.ascii_letters) for _ in range(16))
|
|
|
|
def pack_jwt(obj: dict) -> str:
|
|
return jwt.encode(
|
|
{'exp': datetime.datetime.utcnow() + datetime.timedelta(hours=1), **obj},
|
|
_key, algorithm='HS256'
|
|
)
|
|
|
|
def load_jwt(token: str) -> Optional[dict]:
|
|
try:
|
|
return jwt.decode(token, _key, algorithm='HS256')
|
|
except:
|
|
return None
|
|
|