Skip to content

Commit

Permalink
python/authn: Add AuthN Client Logout
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Koo <[email protected]>
  • Loading branch information
rkoo19 committed Aug 20, 2024
1 parent 100bf6b commit 4a4b131
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
18 changes: 18 additions & 0 deletions python/aistore/sdk/authn/authn_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,24 @@ def login(
logger.error("Login failed for username: %s, error: %s", username, err)
raise

def logout(self) -> None:
"""
Logs out and revokes current token from the AuthN Server.
Raises:
AISError: If the logout request fails.
"""
if not self.client.token:
raise ValueError("Must be logged in first (no token)")

try:
logger.info("Logging out")
self.token_manager().revoke(token=self.client.token)
self.client.token = None
except Exception as err:
logger.error("Logout failed, error: %s", err)
raise

def cluster_manager(self) -> ClusterManager:
"""
Factory method to create a ClusterManager instance.
Expand Down
7 changes: 1 addition & 6 deletions python/aistore/sdk/request_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,8 @@ def token(self, token: str):
Set the token for Authorization.
Args:
token (str): Token for Authorization. Must be a non-empty string.
Raises:
ValueError: If the provided token is empty.
token (str): Token for Authorization.
"""
if not token:
raise ValueError("Token must be a non-empty string.")
self._token = token

def request_deserialize(
Expand Down
8 changes: 8 additions & 0 deletions python/tests/integration/sdk/authn/test_authn_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ def test_login_success(self):
token = self.authn_client.login(AIS_AUTHN_SU_NAME, AIS_AUTHN_SU_PASS)
self.assertIsNotNone(token)

@pytest.mark.authn
def test_logout(self):
token = self.authn_client.login(AIS_AUTHN_SU_NAME, AIS_AUTHN_SU_PASS)
self.assertIsNotNone(token)

self.authn_client.logout()
self.assertIsNone(self.authn_client.client.token)

@pytest.mark.authn
def test_create_bucket_without_token(self):
ais_client = Client(CLUSTER_ENDPOINT)
Expand Down

0 comments on commit 4a4b131

Please sign in to comment.