@@ -83,23 +83,25 @@ def __init__(self, user_db, config):
83
83
server .Server .__init__ (self , config = config )
84
84
self .user_db = user_db
85
85
86
- def handle_auth_req (self , saml_request , relay_state , binding , userid ,
87
- response_binding = BINDING_HTTP_POST ):
86
+ def __create_authn_response (self , saml_request , relay_state , binding ,
87
+ userid , response_binding = BINDING_HTTP_POST ):
88
88
"""
89
- Handles a SAML request, validates and creates a SAML response.
89
+ Handles a SAML request, validates and creates a SAML response but
90
+ does not apply the binding to encode it.
90
91
:type saml_request: str
91
92
:type relay_state: str
92
93
:type binding: str
93
94
:type userid: str
94
- :rtype:
95
+ :rtype: tuple [string, saml2.samlp.Response]
95
96
96
97
:param saml_request:
97
- :param relay_state: RelayState is a parameter used by some SAML protocol implementations to
98
- identify the specific resource at the resource provider in an IDP initiated single sign on
99
- scenario.
98
+ :param relay_state: RelayState is a parameter used by some SAML
99
+ protocol implementations to identify the specific resource at the
100
+ resource provider in an IDP initiated single sign on scenario.
100
101
:param binding:
101
102
:param userid: The user identification.
102
- :return: A tuple with
103
+ :return: A tuple containing the destination and instance of
104
+ saml2.samlp.Response
103
105
"""
104
106
auth_req = self .parse_authn_request (saml_request , binding )
105
107
binding_out , destination = self .pick_binding (
@@ -114,17 +116,104 @@ def handle_auth_req(self, saml_request, relay_state, binding, userid,
114
116
authn_broker .get_authn_by_accr (PASSWORD )
115
117
resp_args ['authn' ] = authn_broker .get_authn_by_accr (PASSWORD )
116
118
117
- _resp = self .create_authn_response (self .user_db [userid ],
118
- userid = userid ,
119
- ** resp_args )
119
+ resp = self .create_authn_response (self .user_db [userid ],
120
+ userid = userid ,
121
+ ** resp_args )
122
+
123
+ return destination , resp
120
124
125
+ def __apply_binding_to_authn_response (self ,
126
+ resp ,
127
+ response_binding ,
128
+ relay_state ,
129
+ destination ):
130
+ """
131
+ Applies the binding to the response.
132
+ """
121
133
if response_binding == BINDING_HTTP_POST :
122
- saml_response = base64 .b64encode (str (_resp ).encode ("utf-8" ))
134
+ saml_response = base64 .b64encode (str (resp ).encode ("utf-8" ))
123
135
resp = {"SAMLResponse" : saml_response , "RelayState" : relay_state }
124
136
elif response_binding == BINDING_HTTP_REDIRECT :
125
- http_args = self .apply_binding (response_binding , '%s' % _resp ,
126
- destination , relay_state , response = True )
127
- resp = dict (parse_qsl (urlparse (dict (http_args ["headers" ])["Location" ]).query ))
137
+ http_args = self .apply_binding (
138
+ response_binding ,
139
+ '%s' % resp ,
140
+ destination ,
141
+ relay_state ,
142
+ response = True )
143
+ resp = dict (parse_qsl (urlparse (
144
+ dict (http_args ["headers" ])["Location" ]).query ))
145
+
146
+ return resp
147
+
148
+ def handle_auth_req (self , saml_request , relay_state , binding , userid ,
149
+ response_binding = BINDING_HTTP_POST ):
150
+ """
151
+ Handles a SAML request, validates and creates a SAML response.
152
+ :type saml_request: str
153
+ :type relay_state: str
154
+ :type binding: str
155
+ :type userid: str
156
+ :rtype: tuple
157
+
158
+ :param saml_request:
159
+ :param relay_state: RelayState is a parameter used by some SAML
160
+ protocol implementations to identify the specific resource at the
161
+ resource provider in an IDP initiated single sign on scenario.
162
+ :param binding:
163
+ :param userid: The user identification.
164
+ :return: A tuple with the destination and encoded response as a string
165
+ """
166
+
167
+ destination , _resp = self .__create_authn_response (
168
+ saml_request ,
169
+ relay_state ,
170
+ binding ,
171
+ userid ,
172
+ response_binding )
173
+
174
+ resp = self .__apply_binding_to_authn_response (
175
+ _resp ,
176
+ response_binding ,
177
+ relay_state ,
178
+ destination )
179
+
180
+ return destination , resp
181
+
182
+ def handle_auth_req_no_name_id (self , saml_request , relay_state , binding ,
183
+ userid , response_binding = BINDING_HTTP_POST ):
184
+ """
185
+ Handles a SAML request, validates and creates a SAML response but
186
+ without a <NameID> element.
187
+ :type saml_request: str
188
+ :type relay_state: str
189
+ :type binding: str
190
+ :type userid: str
191
+ :rtype: tuple
192
+
193
+ :param saml_request:
194
+ :param relay_state: RelayState is a parameter used by some SAML
195
+ protocol implementations to identify the specific resource at the
196
+ resource provider in an IDP initiated single sign on scenario.
197
+ :param binding:
198
+ :param userid: The user identification.
199
+ :return: A tuple with the destination and encoded response as a string
200
+ """
201
+
202
+ destination , _resp = self .__create_authn_response (
203
+ saml_request ,
204
+ relay_state ,
205
+ binding ,
206
+ userid ,
207
+ response_binding )
208
+
209
+ # Remove the <NameID> element from the response.
210
+ _resp .assertion .subject .name_id = None
211
+
212
+ resp = self .__apply_binding_to_authn_response (
213
+ _resp ,
214
+ response_binding ,
215
+ relay_state ,
216
+ destination )
128
217
129
218
return destination , resp
130
219
0 commit comments