Skip to content

Commit

Permalink
feat(pyproject.toml): add mypy-boto3-cognito-idp dependency for type …
Browse files Browse the repository at this point in the history
…checking support

feat(auth.py, user.py): add has_cached_credentials method to check for cached credentials

Add a static method in OtfAuth and a class method in OtfUser to verify
the presence of cached credentials, enhancing the authentication
process by allowing pre-checks for cached data.
  • Loading branch information
NodeJSmith committed Jan 7, 2025
1 parent 5958aee commit b7eb1fd
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
16 changes: 15 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pint = "0.24.*"
pycognito = "2024.5.1"
pydantic = "2.7.3"
yarl = "^1.18.3"
mypy-boto3-cognito-idp = "^1.35.93"

[tool.poetry.group.dev.dependencies]
build = "1.2.1"
Expand Down
13 changes: 13 additions & 0 deletions src/otf_api/auth/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@ def __attrs_post_init__(self) -> None:
if not hasattr(self, "config"):
self.config = OtfAuthConfig()

@staticmethod
def has_cached_credentials(config: OtfAuthConfig | None = None) -> bool:
"""Check if there are cached credentials.
Args:
config (OtfAuthConfig, optional): The configuration. Defaults to None.
Returns:
bool: True if there are cached credentials, False otherwise.
"""
config = config or OtfAuthConfig()
return config.token_cache.get_cached_data() is not None

@staticmethod
def from_cache(config: OtfAuthConfig | None = None) -> "OtfTokenAuth":
"""Attempt to get an authentication object from the cache. If no tokens are found, raise a ValueError.
Expand Down
12 changes: 12 additions & 0 deletions src/otf_api/auth/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,15 @@ def from_cache(cls, config: OtfAuthConfig | None = None) -> "OtfUser":
"""
auth = OtfAuth.from_cache(config)
return cls(auth)

@classmethod
def has_cached_credentials(cls, config: OtfAuthConfig | None = None) -> bool:
"""Check if there are cached credentials.
Args:
config (OtfAuthConfig, optional): The configuration. Defaults to None.
Returns:
bool: True if there are cached credentials, False otherwise.
"""
return OtfAuth.has_cached_credentials(config)

0 comments on commit b7eb1fd

Please sign in to comment.