|
37 | 37 | from saml2.sigver import pre_encryption_part, pre_encrypt_assertion
|
38 | 38 | from saml2.sigver import rm_xmltag
|
39 | 39 | from saml2.sigver import verify_redirect_signature
|
| 40 | +from saml2.sigver import SignatureError, SigverError |
40 | 41 | from saml2.s_utils import do_attribute_statement
|
41 | 42 | from saml2.s_utils import factory
|
42 | 43 | from saml2.time_util import in_a_while, a_while_ago
|
@@ -1487,6 +1488,160 @@ def test_do_logout_session_expired(self):
|
1487 | 1488 | BINDING_HTTP_POST)
|
1488 | 1489 | assert b'<ns0:SessionIndex>_foo</ns0:SessionIndex>' in res.xmlstr
|
1489 | 1490 |
|
| 1491 | + def test_signature_wants(self): |
| 1492 | + |
| 1493 | + ava = { |
| 1494 | + "givenName": ["Derek"], |
| 1495 | + "sn": ["Jeter"], |
| 1496 | + |
| 1497 | + "title": ["The man"] |
| 1498 | + } |
| 1499 | + |
| 1500 | + nameid_policy = samlp.NameIDPolicy( |
| 1501 | + allow_create="false", |
| 1502 | + format=saml.NAMEID_FORMAT_PERSISTENT) |
| 1503 | + |
| 1504 | + kwargs = { |
| 1505 | + "identity": ava, |
| 1506 | + "in_response_to": "id1", |
| 1507 | + "destination": "http://lingon.catalogix.se:8087/", |
| 1508 | + "sp_entity_id": "urn:mace:example.com:saml:roland:sp", |
| 1509 | + "name_id_policy": nameid_policy, |
| 1510 | + |
| 1511 | + "authn": AUTHN |
| 1512 | + } |
| 1513 | + |
| 1514 | + outstanding = {"id1": "http://foo.example.com/service"} |
| 1515 | + |
| 1516 | + def create_authn_response(**kwargs): |
| 1517 | + return encode_fn( |
| 1518 | + str(self.server.create_authn_response(**kwargs)).encode()) |
| 1519 | + |
| 1520 | + def parse_authn_response(response): |
| 1521 | + self.client.parse_authn_request_response(response, |
| 1522 | + BINDING_HTTP_POST, outstanding) |
| 1523 | + |
| 1524 | + def set_client_want(response, assertion, either): |
| 1525 | + self.client.want_response_signed = response |
| 1526 | + self.client.want_assertions_signed = assertion |
| 1527 | + self.client.want_assertions_or_response_signed = either |
| 1528 | + |
| 1529 | + # Response is signed but assertion is not. |
| 1530 | + kwargs["sign_response"] = True |
| 1531 | + kwargs["sign_assertion"] = False |
| 1532 | + response = create_authn_response(**kwargs) |
| 1533 | + |
| 1534 | + set_client_want(True, True, True) |
| 1535 | + raises(SignatureError, parse_authn_response, response) |
| 1536 | + |
| 1537 | + set_client_want(True, True, False) |
| 1538 | + raises(SignatureError, parse_authn_response, response) |
| 1539 | + |
| 1540 | + set_client_want(True, False, True) |
| 1541 | + parse_authn_response(response) |
| 1542 | + |
| 1543 | + set_client_want(True, False, False) |
| 1544 | + parse_authn_response(response) |
| 1545 | + |
| 1546 | + set_client_want(False, True, True) |
| 1547 | + raises(SignatureError, parse_authn_response, response) |
| 1548 | + |
| 1549 | + set_client_want(False, True, False) |
| 1550 | + raises(SignatureError, parse_authn_response, response) |
| 1551 | + |
| 1552 | + set_client_want(False, False, True) |
| 1553 | + parse_authn_response(response) |
| 1554 | + |
| 1555 | + set_client_want(False, False, False) |
| 1556 | + parse_authn_response(response) |
| 1557 | + |
| 1558 | + # Response is not signed but assertion is signed. |
| 1559 | + kwargs["sign_response"] = False |
| 1560 | + kwargs["sign_assertion"] = True |
| 1561 | + response = create_authn_response(**kwargs) |
| 1562 | + |
| 1563 | + set_client_want(True, True, True) |
| 1564 | + raises(SignatureError, parse_authn_response, response) |
| 1565 | + |
| 1566 | + set_client_want(True, True, False) |
| 1567 | + raises(SignatureError, parse_authn_response, response) |
| 1568 | + |
| 1569 | + set_client_want(True, False, True) |
| 1570 | + raises(SignatureError, parse_authn_response, response) |
| 1571 | + |
| 1572 | + set_client_want(True, False, False) |
| 1573 | + raises(SignatureError, parse_authn_response, response) |
| 1574 | + |
| 1575 | + set_client_want(False, True, True) |
| 1576 | + parse_authn_response(response) |
| 1577 | + |
| 1578 | + set_client_want(False, True, False) |
| 1579 | + parse_authn_response(response) |
| 1580 | + |
| 1581 | + set_client_want(False, False, True) |
| 1582 | + parse_authn_response(response) |
| 1583 | + |
| 1584 | + set_client_want(False, False, False) |
| 1585 | + parse_authn_response(response) |
| 1586 | + |
| 1587 | + # Both response and assertion are signed. |
| 1588 | + kwargs["sign_response"] = True |
| 1589 | + kwargs["sign_assertion"] = True |
| 1590 | + response = create_authn_response(**kwargs) |
| 1591 | + |
| 1592 | + set_client_want(True, True, True) |
| 1593 | + parse_authn_response(response) |
| 1594 | + |
| 1595 | + set_client_want(True, True, False) |
| 1596 | + parse_authn_response(response) |
| 1597 | + |
| 1598 | + set_client_want(True, False, True) |
| 1599 | + parse_authn_response(response) |
| 1600 | + |
| 1601 | + set_client_want(True, False, False) |
| 1602 | + parse_authn_response(response) |
| 1603 | + |
| 1604 | + set_client_want(False, True, True) |
| 1605 | + parse_authn_response(response) |
| 1606 | + |
| 1607 | + set_client_want(False, True, False) |
| 1608 | + parse_authn_response(response) |
| 1609 | + |
| 1610 | + set_client_want(False, False, True) |
| 1611 | + parse_authn_response(response) |
| 1612 | + |
| 1613 | + set_client_want(False, False, False) |
| 1614 | + parse_authn_response(response) |
| 1615 | + |
| 1616 | + # Neither response nor assertion is signed. |
| 1617 | + kwargs["sign_response"] = False |
| 1618 | + kwargs["sign_assertion"] = False |
| 1619 | + response = create_authn_response(**kwargs) |
| 1620 | + |
| 1621 | + set_client_want(True, True, True) |
| 1622 | + raises(SignatureError, parse_authn_response, response) |
| 1623 | + |
| 1624 | + set_client_want(True, True, False) |
| 1625 | + raises(SignatureError, parse_authn_response, response) |
| 1626 | + |
| 1627 | + set_client_want(True, False, True) |
| 1628 | + raises(SignatureError, parse_authn_response, response) |
| 1629 | + |
| 1630 | + set_client_want(True, False, False) |
| 1631 | + raises(SignatureError, parse_authn_response, response) |
| 1632 | + |
| 1633 | + set_client_want(False, True, True) |
| 1634 | + raises(SignatureError, parse_authn_response, response) |
| 1635 | + |
| 1636 | + set_client_want(False, True, False) |
| 1637 | + raises(SignatureError, parse_authn_response, response) |
| 1638 | + |
| 1639 | + set_client_want(False, False, True) |
| 1640 | + raises(SigverError, parse_authn_response, response) |
| 1641 | + |
| 1642 | + set_client_want(False, False, False) |
| 1643 | + parse_authn_response(response) |
| 1644 | + |
1490 | 1645 |
|
1491 | 1646 | class TestClientNonAsciiAva:
|
1492 | 1647 | def setup_class(self):
|
|
0 commit comments