Skip to content

Commit ef717d8

Browse files
skorandac00kiemon5ter
authored andcommitted
Add tests for client signature requirements
Add logic to test client configuration options want_response_signed, want_assertions_signed, and want_assertions_or_response_signed.
1 parent 23fe514 commit ef717d8

File tree

1 file changed

+155
-0
lines changed

1 file changed

+155
-0
lines changed

tests/test_51_client.py

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
from saml2.sigver import pre_encryption_part, pre_encrypt_assertion
3838
from saml2.sigver import rm_xmltag
3939
from saml2.sigver import verify_redirect_signature
40+
from saml2.sigver import SignatureError, SigverError
4041
from saml2.s_utils import do_attribute_statement
4142
from saml2.s_utils import factory
4243
from saml2.time_util import in_a_while, a_while_ago
@@ -1487,6 +1488,160 @@ def test_do_logout_session_expired(self):
14871488
BINDING_HTTP_POST)
14881489
assert b'<ns0:SessionIndex>_foo</ns0:SessionIndex>' in res.xmlstr
14891490

1491+
def test_signature_wants(self):
1492+
1493+
ava = {
1494+
"givenName": ["Derek"],
1495+
"sn": ["Jeter"],
1496+
"mail": ["[email protected]"],
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+
"userid": "[email protected]",
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+
14901645

14911646
class TestClientNonAsciiAva:
14921647
def setup_class(self):

0 commit comments

Comments
 (0)