core/auth/jwtcodec.py

51 lines
1.8 KiB
Python
Raw Normal View History

2022-11-23 14:09:35 +00:00
from datetime import datetime, timezone
import jwt
2022-10-31 21:17:00 +00:00
from base.exceptions import ExpiredToken, InvalidToken
2022-10-31 21:25:25 +00:00
from validations.auth import TokenPayload, AuthInput
from settings import JWT_ALGORITHM, JWT_SECRET_KEY
class JWTCodec:
2022-09-03 10:50:14 +00:00
@staticmethod
2022-10-31 21:25:25 +00:00
def encode(user: AuthInput, exp: datetime) -> str:
2022-09-03 10:50:14 +00:00
payload = {
2022-10-31 21:25:25 +00:00
"user_id": user.id,
"username": user.email or user.phone,
2022-11-23 14:09:35 +00:00
"exp": exp,
2022-11-24 14:31:52 +00:00
"iat": datetime.now(tz=timezone.utc),
2022-10-31 21:05:10 +00:00
"iss": "discours"
2022-09-03 10:50:14 +00:00
}
2022-10-23 09:33:28 +00:00
try:
2022-10-31 18:38:41 +00:00
return jwt.encode(payload, JWT_SECRET_KEY, JWT_ALGORITHM)
2022-10-23 09:33:28 +00:00
except Exception as e:
2022-11-23 11:56:09 +00:00
print('[auth.jwtcodec] JWT encode error %r' % e)
2022-09-03 10:50:14 +00:00
@staticmethod
def decode(token: str, verify_exp: bool = True) -> TokenPayload:
2023-01-31 06:57:35 +00:00
r = None
2023-02-20 16:09:55 +00:00
payload = None
2022-10-23 09:33:28 +00:00
try:
payload = jwt.decode(
token,
key=JWT_SECRET_KEY,
2022-10-31 19:53:48 +00:00
options={
"verify_exp": verify_exp,
2022-10-31 21:05:10 +00:00
# "verify_signature": False
2022-10-31 19:53:48 +00:00
},
2022-10-23 09:33:28 +00:00
algorithms=[JWT_ALGORITHM],
2022-10-31 21:05:10 +00:00
issuer="discours"
2022-10-23 09:33:28 +00:00
)
2022-10-31 18:38:41 +00:00
r = TokenPayload(**payload)
2023-01-31 06:57:35 +00:00
print('[auth.jwtcodec] debug token %r' % r)
2022-10-31 18:38:41 +00:00
return r
2022-11-13 23:38:06 +00:00
except jwt.InvalidIssuedAtError:
2023-01-31 06:57:35 +00:00
print('[auth.jwtcodec] invalid issued at: %r' % payload)
2022-11-13 23:38:06 +00:00
raise ExpiredToken('check token issued time')
2022-10-31 21:05:10 +00:00
except jwt.ExpiredSignatureError:
2023-01-31 06:57:35 +00:00
print('[auth.jwtcodec] expired signature %r' % payload)
2022-10-31 21:17:00 +00:00
raise ExpiredToken('check token lifetime')
except jwt.InvalidTokenError:
raise InvalidToken('token is not valid')
except jwt.InvalidSignatureError:
raise InvalidToken('token is not valid')