debug-jwt

This commit is contained in:
tonyrewin 2022-11-01 00:17:00 +03:00
parent 6c97d39e24
commit af2b94eca4
2 changed files with 22 additions and 7 deletions

View File

@ -1,7 +1,7 @@
from datetime import datetime
import time
import jwt
from base.exceptions import ExpiredToken
from base.exceptions import ExpiredToken, InvalidToken
from validations.auth import TokenPayload
from settings import JWT_ALGORITHM, JWT_SECRET_KEY
@ -9,12 +9,16 @@ from settings import JWT_ALGORITHM, JWT_SECRET_KEY
class JWTCodec:
@staticmethod
def encode(user_id: int, exp: datetime) -> str:
issued = int(time.mktime(datetime.now().timetuple()))
print('[jwtcodec] issued at %r' % issued)
expires = time.mktime(exp.timetuple())
print('[jwtcodec] expires at %r' % expires)
payload = {
"user_id": user_id,
# "user_email": user.email, # less secure
# "device": device, # no use cases
"exp": exp,
"iat": time.mktime(datetime.now().timetuple()),
"exp": expires,
"iat": issued,
"iss": "discours"
}
try:
@ -39,4 +43,10 @@ class JWTCodec:
print('[jwtcodec] debug payload %r' % r)
return r
except jwt.ExpiredSignatureError:
raise ExpiredToken
raise ExpiredToken('check token lifetime')
except jwt.InvalidTokenError:
raise InvalidToken('token is not valid')
except jwt.InvalidSignatureError:
raise InvalidToken('token is not valid')
except jwt.InvalidIssuedAtError:
raise ExpiredToken('check token issued time')

View File

@ -16,6 +16,11 @@ class InvalidToken(BaseHttpException):
message = "403 Invalid Token"
class Unauthorized(BaseHttpException):
code = 401
message = "401 Unauthorized"
class ObjectNotExist(BaseHttpException):
code = 404
message = "404 Object Does Not Exist"
@ -23,9 +28,9 @@ class ObjectNotExist(BaseHttpException):
class OperationNotAllowed(BaseHttpException):
code = 403
message = "403 Operation is not allowed"
message = "403 Operation Is Not Allowed"
class InvalidPassword(BaseHttpException):
code = 401
message = "401 Invalid Password"
code = 403
message = "403 Invalid Password"