Skip to content

Commit

Permalink
fix: add response object to AuthenticationException
Browse files Browse the repository at this point in the history
  • Loading branch information
derlin committed Mar 23, 2024
1 parent d80dae3 commit feccdeb
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 3 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,9 @@ HttpException: (403, {'error': 'unknown_error', 'error_description': 'For more o

## 💀 Exceptions

If an error occurs during _authentication_, mantelo will raise an `AuthenticationException` with the
`error` and `errorDescription` from Keycloak. All other exceptions are instances of `HttpException`.
If the server returns a 401 Unauthorized during the _authentication_ process, mantelo will raise an
`AuthenticationException` with the `error` and `errorDescription` from Keycloak. All other HTTP
exceptions are instances of `HttpException`.

Here are some examples:

Expand All @@ -293,6 +294,7 @@ Here are some examples:
AuthenticationException(
error='invalid_client',
error_description='Invalid client or Invalid client credentials'
response='<requests.Response>',
)

# Trying to access an endpoint without the proper permissions
Expand Down
2 changes: 1 addition & 1 deletion mantelo/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def _fetch_token(self) -> None:
# to avoid recursion errors.
resp = self.session.post(self.auth_url, data=data, auth=_NO_AUTH)
if resp.status_code == 401:
raise AuthenticationException(**resp.json())
raise AuthenticationException(**resp.json(), response=resp)
resp.raise_for_status()
self._token = Token.from_dict(resp.json(), now=now)
logger.debug(
Expand Down
1 change: 1 addition & 0 deletions mantelo/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
class AuthenticationException(Exception):
error: str
error_description: str
response: requests.Response = field(repr=False)


@frozen
Expand Down
2 changes: 2 additions & 0 deletions tests/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,12 @@ def test_openid_token_fails(openid_connection_password):

assert excinfo.value.error == "invalid_grant"
assert excinfo.value.error_description == "Invalid user credentials"
assert isinstance(excinfo.value.response, requests.Response)

openid_connection_password.username = None
with pytest.raises(AuthenticationException) as excinfo:
openid_connection_password.token()

assert excinfo.value.error == "invalid_request"
assert excinfo.value.error_description == "Missing parameter: username"
assert isinstance(excinfo.value.response, requests.Response)

0 comments on commit feccdeb

Please sign in to comment.