jwt-decode-debug
This commit is contained in:
parent
e68272bcc2
commit
79e8677975
|
@ -2,27 +2,22 @@ from datetime import datetime
|
|||
|
||||
import jwt
|
||||
|
||||
from validations.auth import TokenPayload, AuthInput
|
||||
from validations.auth import TokenPayload
|
||||
from settings import JWT_ALGORITHM, JWT_SECRET_KEY
|
||||
|
||||
|
||||
class JWTCodec:
|
||||
@staticmethod
|
||||
def encode(user: AuthInput, exp: datetime) -> str:
|
||||
def encode(user_id: int, exp: datetime) -> str:
|
||||
payload = {
|
||||
"user_id": user.id,
|
||||
"user_id": user_id,
|
||||
# "user_email": user.email, # less secure
|
||||
# "device": device, # no use cases
|
||||
"exp": exp,
|
||||
"iat": datetime.utcnow()
|
||||
}
|
||||
try:
|
||||
r = jwt.encode(
|
||||
payload,
|
||||
JWT_SECRET_KEY,
|
||||
JWT_ALGORITHM
|
||||
)
|
||||
return r
|
||||
return jwt.encode(payload, JWT_SECRET_KEY, JWT_ALGORITHM)
|
||||
except Exception as e:
|
||||
print('[jwtcodec] JWT encode error %r' % e)
|
||||
|
||||
|
@ -35,6 +30,8 @@ class JWTCodec:
|
|||
options={"verify_exp": verify_exp},
|
||||
algorithms=[JWT_ALGORITHM],
|
||||
)
|
||||
return TokenPayload(**payload)
|
||||
r = TokenPayload(**payload)
|
||||
print('[jwtcodec] debug payload %r' % r)
|
||||
return r
|
||||
except Exception as e:
|
||||
print('[jwtcodec] JWT decode error %r' % e)
|
||||
|
|
|
@ -22,7 +22,7 @@ class TokenStorage:
|
|||
async def create_onetime(user: AuthInput) -> str:
|
||||
life_span = ONETIME_TOKEN_LIFE_SPAN
|
||||
exp = datetime.utcnow() + timedelta(seconds=life_span)
|
||||
one_time_token = JWTCodec.encode(user, exp=exp)
|
||||
one_time_token = JWTCodec.encode(user.id, exp)
|
||||
await save(f"{user.id}-{one_time_token}", life_span)
|
||||
return one_time_token
|
||||
|
||||
|
@ -30,7 +30,7 @@ class TokenStorage:
|
|||
async def create_session(user: AuthInput) -> str:
|
||||
life_span = SESSION_TOKEN_LIFE_SPAN
|
||||
exp = datetime.utcnow() + timedelta(seconds=life_span)
|
||||
session_token = JWTCodec.encode(user, exp=exp)
|
||||
session_token = JWTCodec.encode(user.id, exp)
|
||||
await save(f"{user.id}-{session_token}", life_span)
|
||||
return session_token
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user