2023-05-01 20:00:32 +09:00
|
|
|
import time
|
|
|
|
|
|
|
|
import jwt
|
2023-07-19 14:43:16 +09:00
|
|
|
from pydantic import TypeAdapter
|
2023-05-01 20:00:32 +09:00
|
|
|
from pydantic.dataclasses import dataclass
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
|
|
class DecodedJWT:
|
|
|
|
token_type: str
|
|
|
|
exp: int
|
|
|
|
iat: int
|
|
|
|
jti: str
|
|
|
|
user_id: int
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
|
|
class Token:
|
|
|
|
access: str
|
|
|
|
refresh: str
|
|
|
|
|
|
|
|
@property
|
|
|
|
def user_id(self) -> int:
|
|
|
|
access_decoded = self.__decode(self.access)
|
|
|
|
return access_decoded.user_id
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_expired(self) -> bool:
|
2023-05-09 19:30:07 +09:00
|
|
|
now = int(time.time())
|
2023-05-01 20:00:32 +09:00
|
|
|
refresh_decoded = self.__decode(self.refresh)
|
2023-05-09 19:30:07 +09:00
|
|
|
return (now - 10) > refresh_decoded.exp
|
2023-05-01 20:00:32 +09:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_refresh_required(self) -> bool:
|
2023-05-09 19:30:07 +09:00
|
|
|
now = int(time.time())
|
2023-05-01 20:00:32 +09:00
|
|
|
access_decoded = self.__decode(self.access)
|
|
|
|
refresh_decoded = self.__decode(self.refresh)
|
2023-05-09 19:30:07 +09:00
|
|
|
return (now + 10) > access_decoded.exp and (now - 10) < refresh_decoded.exp
|
2023-05-01 20:00:32 +09:00
|
|
|
|
|
|
|
def __decode(self, token: str) -> DecodedJWT:
|
2023-07-19 21:47:47 +09:00
|
|
|
data = jwt.decode(token, options={"verify_signature": False}) # type: ignore
|
2023-07-19 14:43:16 +09:00
|
|
|
return TypeAdapter(DecodedJWT).validate_python(data)
|
2023-05-01 20:00:32 +09:00
|
|
|
|
|
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
|
|
class User:
|
|
|
|
id: int
|
|
|
|
username: str
|
2023-05-22 13:46:19 +09:00
|
|
|
laboratory_ids: list[int]
|
|
|
|
is_reviewer: bool
|