Skip to content

Commit dc75c30

Browse files
Add parameters to auth_response test helper
1 parent 560f37d commit dc75c30

File tree

1 file changed

+42
-14
lines changed

1 file changed

+42
-14
lines changed

djangosaml2/tests/auth_response.py

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,46 @@
1616
import datetime
1717

1818

19-
def auth_response(session_id, uid):
20-
"""Generates a fresh signed authentication response"""
19+
def auth_response(session_id,
20+
uid,
21+
audience='http://sp.example.com/saml2/metadata/',
22+
acs_url='http://sp.example.com/saml2/acs/',
23+
metadata_url='http://sp.example.com/saml2/metadata/',
24+
attribute_statements=None):
25+
"""Generates a fresh signed authentication response
26+
27+
Params:
28+
session_id: The session ID to generate the reponse for. Login set an
29+
outstanding session ID, i.e. djangosaml2 waits for a response for
30+
that session.
31+
uid: Unique identifier for a User (will be present as an attribute in
32+
the answer). Ignored when attribute_statements is not ``None``.
33+
audience: SP entityid (used when PySAML validates the response
34+
audience).
35+
acs_url: URL where the response has been posted back.
36+
metadata_url: URL where the SP metadata can be queried.
37+
attribute_statements: An alternative XML AttributeStatement to use in
38+
lieu of the default (uid). The uid argument is ignored when
39+
attribute_statements is not ``None``.
40+
"""
2141
timestamp = datetime.datetime.now() - datetime.timedelta(seconds=10)
2242
tomorrow = datetime.datetime.now() + datetime.timedelta(days=1)
2343
yesterday = datetime.datetime.now() - datetime.timedelta(days=1)
2444

45+
if attribute_statements is None:
46+
attribute_statements = (
47+
'<saml:AttributeStatement>'
48+
'<saml:Attribute FriendlyName="uid" Name="urn:oid:0.9.2342.19200300.100.1.1" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:uri">'
49+
'<saml:AttributeValue xsi:nil="true" xsi:type="xs:string">'
50+
'%(uid)s'
51+
'</saml:AttributeValue>'
52+
'</saml:Attribute>'
53+
'</saml:AttributeStatement>'
54+
) % {'uid': uid}
55+
2556
saml_response_tpl = (
2657
"<?xml version='1.0' encoding='UTF-8'?>"
27-
'<samlp:Response xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion" xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Destination="http://sp.example.com/saml2/acs/" ID="id-88b9f586a2a3a639f9327485cc37c40a" InResponseTo="%(session_id)s" IssueInstant="%(timestamp)s" Version="2.0">'
58+
'<samlp:Response xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion" xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Destination="%(acs_url)s" ID="id-88b9f586a2a3a639f9327485cc37c40a" InResponseTo="%(session_id)s" IssueInstant="%(timestamp)s" Version="2.0">'
2859
'<saml:Issuer Format="urn:oasis:names:tc:SAML:2.0:nameid-format:entity">'
2960
'https://idp.example.com/simplesaml/saml2/idp/metadata.php'
3061
'</saml:Issuer>'
@@ -36,17 +67,17 @@ def auth_response(session_id, uid):
3667
'https://idp.example.com/simplesaml/saml2/idp/metadata.php'
3768
'</saml:Issuer>'
3869
'<saml:Subject>'
39-
'<saml:NameID Format="urn:oasis:names:tc:SAML:2.0:nameid-format:transient" NameQualifier="" SPNameQualifier="http://sp.example.com/saml2/metadata/">'
70+
'<saml:NameID Format="urn:oasis:names:tc:SAML:2.0:nameid-format:transient" NameQualifier="" SPNameQualifier="%(metadata_url)s">'
4071
'1f87035b4c1325b296a53d92097e6b3fa36d7e30ee82e3fcb0680d60243c1f03'
4172
'</saml:NameID>'
4273
'<saml:SubjectConfirmation Method="urn:oasis:names:tc:SAML:2.0:cm:bearer">'
43-
'<saml:SubjectConfirmationData InResponseTo="%(session_id)s" NotOnOrAfter="%(tomorrow)s" Recipient="http://sp.example.com/saml2/acs/" />'
74+
'<saml:SubjectConfirmationData InResponseTo="%(session_id)s" NotOnOrAfter="%(tomorrow)s" Recipient="%(acs_url)s" />'
4475
'</saml:SubjectConfirmation>'
4576
'</saml:Subject>'
4677
'<saml:Conditions NotBefore="%(yesterday)s" NotOnOrAfter="%(tomorrow)s">'
4778
'<saml:AudienceRestriction>'
4879
'<saml:Audience>'
49-
'http://sp.example.com/saml2/metadata/'
80+
'%(audience)s'
5081
'</saml:Audience>'
5182
'</saml:AudienceRestriction>'
5283
'</saml:Conditions>'
@@ -57,18 +88,15 @@ def auth_response(session_id, uid):
5788
'</saml:AuthnContextClassRef>'
5889
'</saml:AuthnContext>'
5990
'</saml:AuthnStatement>'
60-
'<saml:AttributeStatement>'
61-
'<saml:Attribute FriendlyName="uid" Name="urn:oid:0.9.2342.19200300.100.1.1" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:uri">'
62-
'<saml:AttributeValue xsi:nil="true" xsi:type="xs:string">'
63-
'%(uid)s'
64-
'</saml:AttributeValue>'
65-
'</saml:Attribute>'
66-
'</saml:AttributeStatement>'
91+
'%(attribute_statements)s'
6792
'</saml:Assertion>'
6893
'</samlp:Response>')
6994
return saml_response_tpl % {
70-
'uid': uid,
7195
'session_id': session_id,
96+
'audience': audience,
97+
'acs_url': acs_url,
98+
'metadata_url': metadata_url,
99+
'attribute_statements': attribute_statements,
72100
'timestamp': timestamp.strftime('%Y-%m-%dT%H:%M:%SZ'),
73101
'tomorrow': tomorrow.strftime('%Y-%m-%dT%H:%M:%SZ'),
74102
'yesterday': yesterday.strftime('%Y-%m-%dT%H:%M:%SZ'),

0 commit comments

Comments
 (0)