This commit is contained in:
20
server/backend/auth/JWT.py
Normal file
20
server/backend/auth/JWT.py
Normal file
@@ -0,0 +1,20 @@
|
||||
|
||||
import time
|
||||
from typing import Dict
|
||||
import jwt as pyjwt
|
||||
from server.backend.schema.pydantic import settings
|
||||
|
||||
def signJWT(user_info: dict) -> str:
|
||||
payload = {
|
||||
"user_id": user_info.id,
|
||||
"admin":user_info.admin,
|
||||
"expires": time.time() + settings.ACCESS_TOKEN_EXPIRE_SECONDS
|
||||
}
|
||||
token = pyjwt.encode(payload, settings.SECRET_KEY, algorithm=settings.ALGORITHM)
|
||||
return token
|
||||
def decodeJWT(token: str) -> dict:
|
||||
try:
|
||||
decoded_token = pyjwt.decode(token, settings.SECRET_KEY, algorithms=[settings.ALGORITHM])
|
||||
return decoded_token if decoded_token["expires"] >= time.time() else None
|
||||
except:
|
||||
return {}
|
||||
0
server/backend/auth/__init__.py
Normal file
0
server/backend/auth/__init__.py
Normal file
Reference in New Issue
Block a user