debug-jwt
This commit is contained in:
parent
6c97d39e24
commit
af2b94eca4
|
@ -1,7 +1,7 @@
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
import time
|
import time
|
||||||
import jwt
|
import jwt
|
||||||
from base.exceptions import ExpiredToken
|
from base.exceptions import ExpiredToken, InvalidToken
|
||||||
from validations.auth import TokenPayload
|
from validations.auth import TokenPayload
|
||||||
from settings import JWT_ALGORITHM, JWT_SECRET_KEY
|
from settings import JWT_ALGORITHM, JWT_SECRET_KEY
|
||||||
|
|
||||||
|
@ -9,12 +9,16 @@ from settings import JWT_ALGORITHM, JWT_SECRET_KEY
|
||||||
class JWTCodec:
|
class JWTCodec:
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def encode(user_id: int, exp: datetime) -> str:
|
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 = {
|
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": expires,
|
||||||
"iat": time.mktime(datetime.now().timetuple()),
|
"iat": issued,
|
||||||
"iss": "discours"
|
"iss": "discours"
|
||||||
}
|
}
|
||||||
try:
|
try:
|
||||||
|
@ -39,4 +43,10 @@ class JWTCodec:
|
||||||
print('[jwtcodec] debug payload %r' % r)
|
print('[jwtcodec] debug payload %r' % r)
|
||||||
return r
|
return r
|
||||||
except jwt.ExpiredSignatureError:
|
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')
|
||||||
|
|
|
@ -16,6 +16,11 @@ class InvalidToken(BaseHttpException):
|
||||||
message = "403 Invalid Token"
|
message = "403 Invalid Token"
|
||||||
|
|
||||||
|
|
||||||
|
class Unauthorized(BaseHttpException):
|
||||||
|
code = 401
|
||||||
|
message = "401 Unauthorized"
|
||||||
|
|
||||||
|
|
||||||
class ObjectNotExist(BaseHttpException):
|
class ObjectNotExist(BaseHttpException):
|
||||||
code = 404
|
code = 404
|
||||||
message = "404 Object Does Not Exist"
|
message = "404 Object Does Not Exist"
|
||||||
|
@ -23,9 +28,9 @@ class ObjectNotExist(BaseHttpException):
|
||||||
|
|
||||||
class OperationNotAllowed(BaseHttpException):
|
class OperationNotAllowed(BaseHttpException):
|
||||||
code = 403
|
code = 403
|
||||||
message = "403 Operation is not allowed"
|
message = "403 Operation Is Not Allowed"
|
||||||
|
|
||||||
|
|
||||||
class InvalidPassword(BaseHttpException):
|
class InvalidPassword(BaseHttpException):
|
||||||
code = 401
|
code = 403
|
||||||
message = "401 Invalid Password"
|
message = "403 Invalid Password"
|
||||||
|
|
Loading…
Reference in New Issue
Block a user