Skip to content

Commit

Permalink
refactor(auth): rename User class to OtfUser for better clarity
Browse files Browse the repository at this point in the history
fix(imports): update all references to User to OtfUser to match new class name
docs(auth): update __all__ to reflect the renamed OtfUser class
feat(auth): add get_tokens method to OtfUser class to retrieve tokens
  • Loading branch information
NodeJSmith committed Jul 13, 2024
1 parent 0ffb313 commit c520e39
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/otf_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
from loguru import logger

from .api import Otf
from .auth import User
from .auth import OtfUser

__version__ = "0.3.0"


__all__ = ["Otf", "User"]
__all__ = ["Otf", "OtfUser"]

logger.remove()
logger.add(sink=sys.stdout, level=os.getenv("OTF_LOG_LEVEL", "INFO"))
8 changes: 4 additions & 4 deletions src/otf_api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from loguru import logger
from yarl import URL

from otf_api.auth import User
from otf_api.auth import OtfUser
from otf_api.models.responses.body_composition_list import BodyCompositionList
from otf_api.models.responses.book_class import BookClass
from otf_api.models.responses.cancel_booking import CancelBooking
Expand Down Expand Up @@ -76,7 +76,7 @@ async def main():
"""

logger: "Logger" = logger
user: User
user: OtfUser
session: aiohttp.ClientSession

def __init__(
Expand All @@ -97,9 +97,9 @@ def __init__(
pass

if username and password:
self.user = User.login(username, password)
self.user = OtfUser.login(username, password)
elif access_token and id_token:
self.user = User.from_token(access_token, id_token)
self.user = OtfUser.from_token(access_token, id_token)
else:
raise ValueError("Either username and password or access_token and id_token must be provided.")

Expand Down
17 changes: 12 additions & 5 deletions src/otf_api/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def member_uuid(self) -> str:
return self.username


class User:
class OtfUser:
token_path: ClassVar[Path] = Path("~/.otf/.tokens").expanduser()
cognito: Cognito

Expand All @@ -76,7 +76,7 @@ def username_from_disk(cls) -> str:
return val

@classmethod
def load_from_disk(cls, username: str, password: str) -> "User":
def load_from_disk(cls, username: str, password: str) -> "OtfUser":
"""Load a User instance from disk. If the token is invalid, reauthenticate with the provided credentials.
Args:
Expand All @@ -98,7 +98,7 @@ def load_from_disk(cls, username: str, password: str) -> "User":
return user

@classmethod
def login(cls, username: str, password: str) -> "User":
def login(cls, username: str, password: str) -> "OtfUser":
"""Login and return a User instance. After a successful login, the user is saved to disk.
Args:
Expand All @@ -116,15 +116,15 @@ def login(cls, username: str, password: str) -> "User":
return user

@classmethod
def from_token(cls, access_token: str, id_token: str) -> "User":
def from_token(cls, access_token: str, id_token: str) -> "OtfUser":
"""Create a User instance from an id token."""
cognito_user = Cognito(USER_POOL_ID, CLIENT_ID, access_token=access_token, id_token=id_token)
cognito_user.verify_tokens()
cognito_user.check_token()

return cls(cognito=cognito_user)

def refresh_token(self) -> "User":
def refresh_token(self) -> "OtfUser":
"""Refresh the user's access token."""
logger.info("Checking tokens...")
if self.cognito.check_token():
Expand All @@ -148,6 +148,13 @@ def access_claims_data(self) -> AccessClaimsData:
def id_claims_data(self) -> IdClaimsData:
return IdClaimsData(**self.cognito.id_claims)

def get_tokens(self) -> dict[str, str]:
return {
"id_token": self.cognito.id_token,
"access_token": self.cognito.access_token,
"refresh_token": self.cognito.refresh_token,
}

def save_to_disk(self) -> None:
self.token_path.parent.mkdir(parents=True, exist_ok=True)
data = {
Expand Down
6 changes: 3 additions & 3 deletions src/otf_api/cli/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from rich.theme import Theme

import otf_api
from otf_api.auth import User
from otf_api.auth import OtfUser
from otf_api.cli._utilities import is_async_fn, with_cli_exception_handling

if typing.TYPE_CHECKING:
Expand Down Expand Up @@ -91,8 +91,8 @@ def set_username(self, username: str | None = None) -> None:
self.username = username
return

if User.cache_file_exists():
self.username = User.username_from_disk()
if OtfUser.cache_file_exists():
self.username = OtfUser.username_from_disk()
return

raise ValueError("Username not provided and not found in cache")
Expand Down
2 changes: 0 additions & 2 deletions src/otf_api/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from .auth import User
from .responses import (
BookClass,
BookingList,
Expand Down Expand Up @@ -29,7 +28,6 @@
)

__all__ = [
"User",
"ChallengeType",
"BookingStatus",
"EquipmentType",
Expand Down

0 comments on commit c520e39

Please sign in to comment.