16
16
import datetime
17
17
18
18
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
+ """
21
41
timestamp = datetime .datetime .now () - datetime .timedelta (seconds = 10 )
22
42
tomorrow = datetime .datetime .now () + datetime .timedelta (days = 1 )
23
43
yesterday = datetime .datetime .now () - datetime .timedelta (days = 1 )
24
44
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
+
25
56
saml_response_tpl = (
26
57
"<?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">'
28
59
'<saml:Issuer Format="urn:oasis:names:tc:SAML:2.0:nameid-format:entity">'
29
60
'https://idp.example.com/simplesaml/saml2/idp/metadata.php'
30
61
'</saml:Issuer>'
@@ -36,17 +67,17 @@ def auth_response(session_id, uid):
36
67
'https://idp.example.com/simplesaml/saml2/idp/metadata.php'
37
68
'</saml:Issuer>'
38
69
'<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 ">'
40
71
'1f87035b4c1325b296a53d92097e6b3fa36d7e30ee82e3fcb0680d60243c1f03'
41
72
'</saml:NameID>'
42
73
'<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 " />'
44
75
'</saml:SubjectConfirmation>'
45
76
'</saml:Subject>'
46
77
'<saml:Conditions NotBefore="%(yesterday)s" NotOnOrAfter="%(tomorrow)s">'
47
78
'<saml:AudienceRestriction>'
48
79
'<saml:Audience>'
49
- 'http://sp.example.com/saml2/metadata/ '
80
+ '%(audience)s '
50
81
'</saml:Audience>'
51
82
'</saml:AudienceRestriction>'
52
83
'</saml:Conditions>'
@@ -57,18 +88,15 @@ def auth_response(session_id, uid):
57
88
'</saml:AuthnContextClassRef>'
58
89
'</saml:AuthnContext>'
59
90
'</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'
67
92
'</saml:Assertion>'
68
93
'</samlp:Response>' )
69
94
return saml_response_tpl % {
70
- 'uid' : uid ,
71
95
'session_id' : session_id ,
96
+ 'audience' : audience ,
97
+ 'acs_url' : acs_url ,
98
+ 'metadata_url' : metadata_url ,
99
+ 'attribute_statements' : attribute_statements ,
72
100
'timestamp' : timestamp .strftime ('%Y-%m-%dT%H:%M:%SZ' ),
73
101
'tomorrow' : tomorrow .strftime ('%Y-%m-%dT%H:%M:%SZ' ),
74
102
'yesterday' : yesterday .strftime ('%Y-%m-%dT%H:%M:%SZ' ),
0 commit comments