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