mdrs-client-python/mdrsclient/models/user.py

51 lines
1.2 KiB
Python
Raw Permalink Normal View History

2023-05-01 20:00:32 +09:00
import time
import jwt
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:
now = int(time.time())
2023-05-01 20:00:32 +09:00
refresh_decoded = self.__decode(self.refresh)
return (now - 10) > refresh_decoded.exp
2023-05-01 20:00:32 +09:00
@property
def is_refresh_required(self) -> bool:
now = int(time.time())
2023-05-01 20:00:32 +09:00
access_decoded = self.__decode(self.access)
refresh_decoded = self.__decode(self.refresh)
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:
data = jwt.decode(token, options={"verify_signature": False}) # type: ignore
return TypeAdapter(DecodedJWT).validate_python(data)
2023-05-01 20:00:32 +09:00
@dataclass(frozen=True)
class User:
id: int
username: str
laboratory_ids: list[int]
is_reviewer: bool