@@ -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,109 @@ 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
+ )
144
+ resp = dict (parse_qsl (urlparse (
145
+ dict (http_args ["headers" ])["Location" ]).query ))
146
+
147
+ return resp
148
+
149
+ def handle_auth_req (self , saml_request , relay_state , binding , userid ,
150
+ response_binding = BINDING_HTTP_POST ):
151
+ """
152
+ Handles a SAML request, validates and creates a SAML response.
153
+ :type saml_request: str
154
+ :type relay_state: str
155
+ :type binding: str
156
+ :type userid: str
157
+ :rtype: tuple
158
+
159
+ :param saml_request:
160
+ :param relay_state: RelayState is a parameter used by some SAML
161
+ protocol implementations to identify the specific resource at the
162
+ resource provider in an IDP initiated single sign on scenario.
163
+ :param binding:
164
+ :param userid: The user identification.
165
+ :return: A tuple with the destination and encoded response as a string
166
+ """
167
+
168
+ destination , _resp = self .__create_authn_response (
169
+ saml_request ,
170
+ relay_state ,
171
+ binding ,
172
+ userid ,
173
+ response_binding
174
+ )
175
+
176
+ resp = self .__apply_binding_to_authn_response (
177
+ _resp ,
178
+ response_binding ,
179
+ relay_state ,
180
+ destination
181
+ )
182
+
183
+ return destination , resp
184
+
185
+ def handle_auth_req_no_name_id (self , saml_request , relay_state , binding ,
186
+ userid , response_binding = BINDING_HTTP_POST ):
187
+ """
188
+ Handles a SAML request, validates and creates a SAML response but
189
+ without a <NameID> element.
190
+ :type saml_request: str
191
+ :type relay_state: str
192
+ :type binding: str
193
+ :type userid: str
194
+ :rtype: tuple
195
+
196
+ :param saml_request:
197
+ :param relay_state: RelayState is a parameter used by some SAML
198
+ protocol implementations to identify the specific resource at the
199
+ resource provider in an IDP initiated single sign on scenario.
200
+ :param binding:
201
+ :param userid: The user identification.
202
+ :return: A tuple with the destination and encoded response as a string
203
+ """
204
+
205
+ destination , _resp = self .__create_authn_response (
206
+ saml_request ,
207
+ relay_state ,
208
+ binding ,
209
+ userid ,
210
+ response_binding
211
+ )
212
+
213
+ # Remove the <NameID> element from the response.
214
+ _resp .assertion .subject .name_id = None
215
+
216
+ resp = self .__apply_binding_to_authn_response (
217
+ _resp ,
218
+ response_binding ,
219
+ relay_state ,
220
+ destination
221
+ )
128
222
129
223
return destination , resp
130
224
0 commit comments