Skip to content

Commit 3c790c0

Browse files
committed
raise AuthenticationError on failed login
1 parent bc82208 commit 3c790c0

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Unreleased
99

1010
**Improvements**
1111
- Added `copy_analytic_store` method to `model_repository` service
12+
- `AuthenticationError` returned instead of `HTTPError` if session authentication fails.
1213

1314

1415
v0.9.7 (2019-07-18)

src/sasctl/core.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,11 +484,29 @@ def _get_token_with_password(self):
484484
data=data,
485485
headers=headers,
486486
auth=('sas.ec', ''))
487-
r.raise_for_status()
487+
488+
if r.status_code == 401:
489+
raise AuthenticationError("Authentication failed for user '%s'." % username)
490+
else:
491+
r.raise_for_status()
488492

489493
return r.json().get('access_token')
490494

491495
def get_token(self):
496+
"""Authenticates with the session host and retrieves an
497+
authorization token for use by subsequent requests.
498+
499+
Returns
500+
-------
501+
str
502+
a bearer token for :class:`HTTPBearerAuth`
503+
504+
Raises
505+
------
506+
AuthenticationError
507+
authentication with the host failed
508+
509+
"""
492510
username = self._settings['username']
493511
password = self._settings['password']
494512

@@ -888,3 +906,6 @@ class SasctlError(Exception):
888906

889907
class TimeoutError(SasctlError):
890908
pass
909+
910+
class AuthenticationError(SasctlError):
911+
pass

tests/unit/test_session.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,6 @@ def test_ssl_context():
218218

219219

220220
def test_verify_ssl():
221-
222221
with mock.patch('sasctl.core.Session.get_token', return_value='token'):
223222
# Should verify SSL by default
224223
s = Session('hostname', 'username', 'password')
@@ -261,3 +260,13 @@ def test_kerberos():
261260

262261
s = Session('hostname', 'username@REALM')
263262
assert s.auth.token == 'token'
263+
264+
def test_authentication_failure():
265+
from sasctl.core import AuthenticationError
266+
267+
with mock.patch('sasctl.core.Session.request') as request:
268+
request.return_value.status_code = 401
269+
270+
with pytest.raises(AuthenticationError):
271+
Session('hostname', 'username', 'password')
272+

0 commit comments

Comments
 (0)