Skip to content
Merged
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
15 changes: 13 additions & 2 deletions src/firebolt/client/auth/request_auth_base.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
from time import time
from typing import Generator

from httpx import Request, Response
from httpx import HTTPStatusError, Request, Response

from firebolt.client.auth.base import Auth
from firebolt.client.constants import _REQUEST_ERRORS
from firebolt.utils.exception import AuthenticationError
from firebolt.utils.usage_tracker import get_user_agent_header


def get_auth_error(e: HTTPStatusError) -> str:
error_description = ""
if e.response.headers.get("content-type", "").lower() == "application/json":
error_description = e.response.json().get("error_description")
return error_description


class _RequestBasedAuth(Auth):
"""Base abstract class for http request based authentication."""

Expand Down Expand Up @@ -54,4 +61,8 @@ def get_new_token_generator(self) -> Generator[Request, Response, None]:
self._expires = int(time()) + int(parsed["expires_in"])

except _REQUEST_ERRORS as e:
raise AuthenticationError(repr(e)) from e
error_text = repr(e)
if isinstance(e, HTTPStatusError):
client_friendly_error = get_auth_error(e)
error_text = client_friendly_error or error_text
raise AuthenticationError(error_text) from e
18 changes: 18 additions & 0 deletions tests/unit/client/auth/test_request_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,24 @@ def http_error(*args, **kwargs):
assert "Bad Request" in errmsg, "Invalid authentication error message"
httpx_mock.reset(True)

# Username/password error
httpx_mock.add_response(
status_code=codes.FORBIDDEN,
headers={"Content-Type": "application/json"},
json={
"error": "invalid_grant",
"error_description": "Wrong email or password.",
},
)
with pytest.raises(AuthenticationError) as excinfo:
execute_generator_requests(auth.get_new_token_generator(), api_endpoint)

errmsg = str(excinfo.value)
assert (
"Wrong email or password." in errmsg
), "Invalid authentication error message"
httpx_mock.reset(True)

# Firebolt api error
httpx_mock.add_response(
status_code=codes.OK, json={"error": "", "message": "firebolt"}
Expand Down