|
1 | 1 | #!/usr/bin/env python
|
2 | 2 | # -*- coding: utf-8 -*-
|
3 |
| -from saml2.response import authn_response |
| 3 | +from saml2.response import authn_response, VerificationError |
4 | 4 | from saml2.config import config_factory
|
5 | 5 |
|
6 | 6 | from pathutils import dotname, full_path
|
7 | 7 |
|
8 |
| -HOLDER_OF_KEY_RESPONSE_FILE = full_path("saml_hok.xml") |
| 8 | +HOLDER_OF_KEY_RESPONSE_FILE = full_path("saml_hok.xml") |
| 9 | +INVALID_HOLDER_OF_KEY_RESPONSE_FILE = full_path("saml_hok_invalid.xml") |
9 | 10 |
|
10 | 11 |
|
11 | 12 | class TestHolderOfKeyResponse:
|
12 |
| - def test_hok_response_is_parsed(self): |
| 13 | + def test_valid_hok_response_is_parsed(self): |
13 | 14 | """Verifies that response with 'holder-of-key' subject confirmations is parsed successfully."""
|
14 |
| - conf = config_factory("idp", dotname("server_conf")) |
15 |
| - resp = authn_response(conf, "https://sp:443/.auth/saml/login", asynchop=False, allow_unsolicited=True) |
16 |
| - with open(HOLDER_OF_KEY_RESPONSE_FILE, 'r') as fp: |
17 |
| - authn_response_xml = fp.read() |
18 |
| - resp.loads(authn_response_xml, False) |
| 15 | + resp = self._get_test_response(HOLDER_OF_KEY_RESPONSE_FILE) |
19 | 16 | resp.do_not_verify = True
|
20 |
| - |
21 | 17 | resp.parse_assertion()
|
22 | 18 |
|
23 | 19 | assert resp.get_subject() is not None
|
@@ -56,6 +52,25 @@ def _expected_hok_certs(self):
|
56 | 52 | certs[index] = item
|
57 | 53 | return certs
|
58 | 54 |
|
| 55 | + def test_invalid_hok_response_fails_verification(self): |
| 56 | + """Verifies that response with invalid 'holder-of-key' subject confirmations is parsed successfully.""" |
| 57 | + resp = self._get_test_response(INVALID_HOLDER_OF_KEY_RESPONSE_FILE) |
| 58 | + resp.do_not_verify = True |
| 59 | + |
| 60 | + try: |
| 61 | + resp.parse_assertion() |
| 62 | + assert False, "parse_assertion() did not fail as expected" |
| 63 | + except VerificationError as e: |
| 64 | + assert e is not None |
| 65 | + |
| 66 | + def _get_test_response(self, path): |
| 67 | + conf = config_factory("idp", dotname("server_conf")) |
| 68 | + resp = authn_response(conf, "https://sp:443/.auth/saml/login", asynchop=False, allow_unsolicited=True) |
| 69 | + with open(path, 'r') as fp: |
| 70 | + authn_response_xml = fp.read() |
| 71 | + resp.loads(authn_response_xml, False) |
| 72 | + return resp |
| 73 | + |
59 | 74 |
|
60 | 75 | if __name__ == "__main__":
|
61 | 76 | t = TestHolderOfKeyResponse()
|
|
0 commit comments