Skip to content

Commit

Permalink
Fixed credentials return incase of error or exception (#170)
Browse files Browse the repository at this point in the history
* Fixed credentials return incase of error or exception

* Added error description in console

* Bump new version

* upgraded jupyterhub dependency
  • Loading branch information
RupinderKaurSSB authored Aug 12, 2024
1 parent 90bacec commit 5bdc23c
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 13 deletions.
45 changes: 35 additions & 10 deletions poetry.lock

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

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "dapla-toolbelt"
version = "2.0.19"
version = "2.0.20"
description = "Dapla Toolbelt"
authors = ["Dapla Developers <[email protected]>"]
license = "MIT"
Expand Down
10 changes: 8 additions & 2 deletions src/dapla/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,16 @@ def fetch_google_token_from_oidc_exchange(
"grant_type": "urn:ietf:params:oauth:grant-type:token-exchange",
"requested_token_type": "urn:ietf:params:oauth:token-type:access_token",
"requested_issuer": "google",
"client_id": "onyxia",
"client_id": "onyxia-api",
},
)
if response.status == 200:
auth_data = json.loads(response.data)
expiry = datetime.utcnow() + timedelta(seconds=auth_data["expires_in"])
return auth_data["access_token"], expiry
else:
error = json.loads(response.data)
print("Error: ", error["error_description"])
raise AuthError

@staticmethod
Expand Down Expand Up @@ -109,6 +111,9 @@ def fetch_local_user_from_jupyter() -> dict[str, Any]:
def fetch_google_credentials() -> Credentials:
"""Fetches the Google credentials for the current user.
Raises:
AuthError: If fails to fetch credentials.
Returns:
The Google "Credentials" object.
"""
Expand All @@ -130,10 +135,11 @@ def _refresh_handler(
token_uri="https://oauth2.googleapis.com/token",
refresh_handler=_refresh_handler,
)
return credentials
except AuthError as err:
err._print_warning()
raise err

return credentials
else:
# Fetch credentials from Google Cloud SDK
credentials, _ = google.auth.default()
Expand Down
5 changes: 5 additions & 0 deletions tests/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,15 @@ def test_fetch_personal_token_error(mock_display: Mock) -> None:
@responses.activate
def test_fetch_google_token_exchange_error(mock_display: Mock) -> None:
mock_response = Mock()

mock_data = {"error_description": "Invalid token"}
mock_json = json.dumps(mock_data)
mock_response.data = mock_json
mock_response.status = 404

mock_google_request = Mock()
mock_google_request.return_value = mock_response

with mock.patch.object(
dapla.auth.GoogleAuthRequest, # type: ignore [attr-defined]
"__call__",
Expand Down

0 comments on commit 5bdc23c

Please sign in to comment.