Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed credentials return incase of error or exception #170

Merged
merged 11 commits into from
Aug 12, 2024
7 changes: 5 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 @@ -130,10 +132,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
Loading