Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/anaconda_auth/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from anaconda_auth.actions import login # noqa: E402
from anaconda_auth.actions import logout # noqa: E402
from anaconda_auth.actions import load_token_info # noqa: E402
from anaconda_auth.client import client_factory # noqa: E402

__all__ = ["__version__", "login", "logout", "client_factory"]
__all__ = ["__version__", "login", "logout", "load_token_info", "client_factory"]
7 changes: 5 additions & 2 deletions src/anaconda_auth/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,14 @@ def logout(config: Optional[AnacondaAuthConfig] = None) -> None:
pass


def is_logged_in() -> bool:
def load_token_info() -> Optional[TokenInfo]:
Copy link
Collaborator

@mattkram mattkram Mar 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the idea of a simple function. Also, just FYI, I think a lot of this desired behavior is now captured in the updated implementation of the create=True flag in TokenInfo.load(). See here.

In other words, if you do TokenInfo.load(create=True), it will return a TokenInfo object associated with the default domain from AnacondaAuthConfig().domain (by default, unless you explicitly pass a domain in. If it can be loaded from the keyring, it will be. If not, it will create a new empty instance. That object, if it was created and not loaded from the keyring, will have token_info.api_key is None.

But I'm totally open for feedback and to add a convenience wrapper as a simple function, if the @classmethod API is too confusing.

Does this align with what you are thinking?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your input, Matt! TokenInfo.load() does the job and meets all our needs. The idea for the PR actually came after using the CLI and noticing there was a straightforward way to retrieve the token. When I looked for the equivalent in the Python interface, I couldn’t find something at the same "level" of login and logout.

The wrapper simply makes it more convenient and intuitive, but it’s more of a nice-to-have rather than a necessity. Appreciate the discussion!

config = AnacondaAuthConfig()
try:
token_info = TokenInfo.load(domain=config.domain)
except TokenNotFoundError:
token_info = None
return token_info


return token_info is not None
def is_logged_in() -> bool:
return load_token_info() is not None
6 changes: 5 additions & 1 deletion src/anaconda_auth/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from anaconda_auth import __version__
from anaconda_auth.actions import login
from anaconda_auth.actions import logout
from anaconda_auth.actions import load_token_info
from anaconda_auth.client import BaseClient
from anaconda_auth.config import AnacondaAuthConfig
from anaconda_auth.exceptions import TokenExpiredError
Expand Down Expand Up @@ -272,7 +273,10 @@ def auth_key() -> None:
print(config.api_key)
return

token_info = TokenInfo.load(domain=config.domain)
token_info = load_token_info()
if token_info is None:
raise TokenNotFoundError

if not token_info.expired:
print(token_info.api_key)
return
Expand Down