File size: 1,613 Bytes
21b7374 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | import logging
from typing import Optional
from pydantic import BaseModel
from .config import get_auth_path
logger = logging.getLogger("fastapi_cli")
class AuthConfig(BaseModel):
access_token: str
def write_auth_config(auth_data: AuthConfig) -> None:
auth_path = get_auth_path()
logger.debug("Writing auth config to: %s", auth_path)
auth_path.write_text(auth_data.model_dump_json(), encoding="utf-8")
logger.debug("Auth config written successfully")
def delete_auth_config() -> None:
auth_path = get_auth_path()
logger.debug("Deleting auth config at: %s", auth_path)
if auth_path.exists():
auth_path.unlink()
logger.debug("Auth config deleted successfully")
else:
logger.debug("Auth config file doesn't exist, nothing to delete")
def read_auth_config() -> Optional[AuthConfig]:
auth_path = get_auth_path()
logger.debug("Reading auth config from: %s", auth_path)
if not auth_path.exists():
logger.debug("Auth config file doesn't exist")
return None
logger.debug("Auth config loaded successfully")
return AuthConfig.model_validate_json(auth_path.read_text(encoding="utf-8"))
def get_auth_token() -> Optional[str]:
logger.debug("Getting auth token")
auth_data = read_auth_config()
if auth_data is None:
logger.debug("No auth data found")
return None
logger.debug("Auth token retrieved successfully")
return auth_data.access_token
def is_logged_in() -> bool:
result = get_auth_token() is not None
logger.debug("Login status: %s", result)
return result
|