Skip to content

Commit 820cd42

Browse files
committed
Added unit test for new form check, and updated minor stylings based on prcheck results.
1 parent 692189e commit 820cd42

File tree

2 files changed

+57
-12
lines changed

2 files changed

+57
-12
lines changed

awsprocesscreds/saml.py

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -175,13 +175,17 @@ def _parse_form_from_html(self, html):
175175

176176
def _fill_in_form_values(self, config, form_data):
177177
username = config['saml_username']
178-
username_field = set(self.USERNAME_FIELDS).intersection(form_data.keys())
178+
username_field = set(self.USERNAME_FIELDS).intersection(
179+
form_data.keys()
180+
)
179181
if not username_field:
180182
raise SAMLError(
181183
self._ERROR_MISSING_FORM_FIELD % self.USERNAME_FIELDS)
182-
else:
183-
form_data[username_field.pop()] = username
184-
password_field = set(self.PASSWORD_FIELDS).intersection(form_data.keys())
184+
form_data[username_field.pop()] = username
185+
186+
password_field = set(self.PASSWORD_FIELDS).intersection(
187+
form_data.keys()
188+
)
185189
if password_field:
186190
form_data[password_field.pop()] = self._password_prompter(
187191
"Password: ")
@@ -252,17 +256,27 @@ def retrieve_saml_assertion(self, config):
252256
return r
253257

254258
def is_suitable(self, config):
255-
return (config.get('saml_authentication_type') == 'form' and
256-
config.get('saml_provider') == 'okta')
259+
return (
260+
config.get('saml_authentication_type') == 'form'
261+
and config.get('saml_provider') == 'okta'
262+
)
257263

258264

259265
class ADFSFormsBasedAuthenticator(GenericFormsBasedAuthenticator):
260-
USERNAME_FIELDS = ('ctl00$ContentPlaceHolder1$UsernameTextBox', 'UserName',)
261-
PASSWORD_FIELDS = ('ctl00$ContentPlaceHolder1$PasswordTextBox', 'Password',)
266+
USERNAME_FIELDS = (
267+
'ctl00$ContentPlaceHolder1$UsernameTextBox',
268+
'UserName',
269+
)
270+
PASSWORD_FIELDS = (
271+
'ctl00$ContentPlaceHolder1$PasswordTextBox',
272+
'Password',
273+
)
262274

263275
def is_suitable(self, config):
264-
return (config.get('saml_authentication_type') == 'form' and
265-
config.get('saml_provider') == 'adfs')
276+
return (
277+
config.get('saml_authentication_type') == 'form'
278+
and config.get('saml_provider') == 'adfs'
279+
)
266280

267281

268282
class FormParser(six.moves.html_parser.HTMLParser):

tests/unit/test_saml.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -423,8 +423,8 @@ def test_non_adfs_not_suitable(self, adfs_auth):
423423
}
424424
assert not adfs_auth.is_suitable(config)
425425

426-
def test_uses_adfs_fields(self, adfs_auth, mock_requests_session,
427-
adfs_config):
426+
def test_uses_adfs_fields_newer(self, adfs_auth, mock_requests_session,
427+
adfs_config):
428428
adfs_login_form = (
429429
'<html>'
430430
'<form action="login">'
@@ -454,6 +454,37 @@ def test_uses_adfs_fields(self, adfs_auth, mock_requests_session,
454454
}
455455
)
456456

457+
def test_uses_adfs_fields_older(self, adfs_auth, mock_requests_session,
458+
adfs_config):
459+
adfs_login_form = (
460+
'<html>'
461+
'<form action="login">'
462+
'<input name="UserName"/>'
463+
'<input name="Password"/>'
464+
'</form>'
465+
'</html>'
466+
)
467+
mock_requests_session.get.return_value = mock.Mock(
468+
spec=requests.Response, status_code=200, text=adfs_login_form
469+
)
470+
mock_requests_session.post.return_value = mock.Mock(
471+
spec=requests.Response, status_code=200, text=(
472+
'<form><input name="SAMLResponse" '
473+
'value="fakeassertion"/></form>'
474+
)
475+
)
476+
477+
saml_assertion = adfs_auth.retrieve_saml_assertion(adfs_config)
478+
assert saml_assertion == 'fakeassertion'
479+
480+
mock_requests_session.post.assert_called_with(
481+
"https://example.com/login", verify=True,
482+
data={
483+
'UserName': 'monty',
484+
'Password': 'mypassword'
485+
}
486+
)
487+
457488

458489
class TestFormParser(object):
459490
def test_parse_form(self, basic_form):

0 commit comments

Comments
 (0)