This commit is contained in:
1 parent
a603566235
commit
7280c5dd07
5 files changed
+66
-10
No files matched your search
+14
-3
@@ -1,4 +1,5 @@
|
||||
import time
|
||||
from typing import Literal
|
||||
|
||||
import jwt
|
||||
|
||||
@@ -8,13 +9,23 @@ JWT_SECRET = settings.jwt.secret
|
||||
JWT_ALGORITHM = "HS256"
|
||||
|
||||
|
||||
def generate_token(user_id: int) -> str:
|
||||
def _encode_jwt(user_id: int, ttl: int, typ: Literal["ws", "web"]) -> str:
|
||||
payload = {
|
||||
"user_id": user_id,
|
||||
"exp": int(time.time()) + 60 * 60 * 24,
|
||||
"exp": int(time.time()) + ttl,
|
||||
"typ": typ,
|
||||
}
|
||||
return jwt.encode(payload, JWT_SECRET, algorithm=JWT_ALGORITHM)
|
||||
|
||||
|
||||
def generate_token(user_id: int) -> str:
|
||||
return _encode_jwt(user_id, settings.jwt.access_ttl, "web")
|
||||
|
||||
|
||||
def generate_ws_token(user_id: int) -> str:
|
||||
return _encode_jwt(user_id, settings.jwt.ws_ttl, "ws")
|
||||
|
||||
|
||||
def decode_token(token: str) -> dict:
|
||||
payload = jwt.decode(token, JWT_SECRET)
|
||||
return payload
|
||||
return payload
|
||||
Reference in new issue
Block a user