Skip to content

Commit a442e03

Browse files
author
Roland Hedberg
committed
Methods creating request changed to return a tuple consisting of request id and request.
1 parent 53df90f commit a442e03

13 files changed

+121
-94
lines changed

src/saml2/client.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def prepare_for_authenticate(self, entityid=None, relay_state="",
7979

8080
destination = self._sso_location(entityid, binding)
8181

82-
req = self.create_authn_request(destination, vorg, scoping,
82+
reqid, req = self.create_authn_request(destination, vorg, scoping,
8383
response_binding, nameid_format,
8484
consent=consent, extensions=extensions,
8585
sign=sign, **kwargs)
@@ -89,7 +89,7 @@ def prepare_for_authenticate(self, entityid=None, relay_state="",
8989

9090
info = self.apply_binding(binding, _req_str, destination, relay_state)
9191

92-
return req.id, info
92+
return reqid, info
9393

9494
def global_logout(self, name_id, reason="", expire=None, sign=None):
9595
""" More or less a layer of indirection :-/
@@ -161,10 +161,9 @@ def do_logout(self, name_id, entity_ids, reason, expire, sign=None,
161161

162162
destination = destinations(srvs)[0]
163163
logger.info("destination to provider: %s" % destination)
164-
request = self.create_logout_request(destination, entity_id,
165-
name_id=name_id,
166-
reason=reason,
167-
expire=expire)
164+
req_id, request = self.create_logout_request(
165+
destination, entity_id, name_id=name_id, reason=reason,
166+
expire=expire)
168167

169168
#to_sign = []
170169
if binding.startswith("http://"):
@@ -178,7 +177,7 @@ def do_logout(self, name_id, entity_ids, reason, expire, sign=None,
178177
else:
179178
srequest = "%s" % request
180179

181-
relay_state = self._relay_state(request.id)
180+
relay_state = self._relay_state(req_id)
182181

183182
http_info = self.apply_binding(binding, srequest, destination,
184183
relay_state)
@@ -196,7 +195,7 @@ def do_logout(self, name_id, entity_ids, reason, expire, sign=None,
196195
logger.info("NOT OK response from %s" % destination)
197196

198197
else:
199-
self.state[request.id] = {"entity_id": entity_id,
198+
self.state[req_id] = {"entity_id": entity_id,
200199
"operation": "SLO",
201200
"entity_ids": entity_ids,
202201
"name_id": name_id,
@@ -264,7 +263,7 @@ def _use_soap(self, destination, query_type, **kwargs):
264263
except KeyError:
265264
response_args = None
266265

267-
query = _create_func(destination, **kwargs)
266+
qid, query = _create_func(destination, **kwargs)
268267

269268
response = self.send_using_soap(query, destination)
270269

src/saml2/client_base.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ def create_authn_request(self, destination, vorg="", scoping=None,
230230
of fulfilling the request, to create a new identifier to represent
231231
the principal.
232232
:param kwargs: Extra key word arguments
233-
:return: <samlp:AuthnRequest> instance
233+
:return: tuple of request ID and <samlp:AuthnRequest> instance
234234
"""
235235
client_crt = None
236236
if "client_crt" in kwargs:
@@ -304,13 +304,14 @@ def create_authn_request(self, destination, vorg="", scoping=None,
304304
except KeyError:
305305
pass
306306

307+
rid = ""
307308
if (sign and self.sec.cert_handler.generate_cert()) or client_crt is not None:
308309
with self.lock:
309310
self.sec.cert_handler.update_cert(True, client_crt)
310311
if client_crt is not None:
311312
sign_prepare = True
312-
return self._message(AuthnRequest, destination, message_id, consent,
313-
extensions, sign, sign_prepare,
313+
return self._message(AuthnRequest, destination, message_id,
314+
consent, extensions, sign, sign_prepare,
314315
protocol_binding=binding,
315316
scoping=scoping, **args)
316317
return self._message(AuthnRequest, destination, message_id, consent,
@@ -343,7 +344,7 @@ def create_attribute_query(self, destination, name_id=None,
343344
:param extensions: Possible extensions
344345
:param sign: Whether the query should be signed or not.
345346
:param sign_prepare: Whether the Signature element should be added.
346-
:return: An AttributeQuery instance
347+
:return: Tuple of request ID and an AttributeQuery instance
347348
"""
348349

349350
if name_id is None:
@@ -666,7 +667,7 @@ def create_ecp_authn_request(self, entityid=None, relay_state="",
666667
# SingleSignOnService
667668
_, location = self.pick_binding("single_sign_on_service",
668669
[_binding], entity_id=entityid)
669-
authn_req = self.create_authn_request(
670+
req_id, authn_req = self.create_authn_request(
670671
location, service_url_binding=BINDING_PAOS, **kwargs)
671672

672673
# ----------------------------------------
@@ -677,7 +678,7 @@ def create_ecp_authn_request(self, entityid=None, relay_state="",
677678
[paos_request,
678679
relay_state])
679680

680-
return authn_req.id, "%s" % soap_envelope
681+
return req_id, "%s" % soap_envelope
681682

682683
def parse_ecp_authn_response(self, txt, outstanding=None):
683684
rdict = soap.class_instances_from_soap_enveloped_saml_thingies(txt,
@@ -757,7 +758,8 @@ def create_discovery_service_request(self, url, entity_id, **kwargs):
757758
params = urlencode(args)
758759
return "%s?%s" % (url, params)
759760

760-
def parse_discovery_service_response(self, url="", query="",
761+
@staticmethod
762+
def parse_discovery_service_response(url="", query="",
761763
returnIDParam="entityID"):
762764
"""
763765
Deal with the response url from a Discovery Service

src/saml2/ecp.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,8 @@ def ecp_auth_request(cls, entityid=None, relay_state="", sign=False):
115115
logger.info("entityid: %s, binding: %s" % (entityid, BINDING_SOAP))
116116

117117
location = cls._sso_location(entityid, binding=BINDING_SOAP)
118-
authn_req = cls.create_authn_request(location,
119-
binding=BINDING_PAOS,
120-
service_url_binding=BINDING_PAOS)
118+
req_id, authn_req = cls.create_authn_request(
119+
location, binding=BINDING_PAOS, service_url_binding=BINDING_PAOS)
121120

122121
body = soapenv.Body()
123122
body.extension_elements = [element_to_extension_element(authn_req)]
@@ -128,7 +127,7 @@ def ecp_auth_request(cls, entityid=None, relay_state="", sign=False):
128127

129128
soap_envelope = soapenv.Envelope(header=header, body=body)
130129

131-
return authn_req.id, "%s" % soap_envelope
130+
return req_id, "%s" % soap_envelope
132131

133132

134133
def handle_ecp_authn_response(cls, soap_message, outstanding=None):

src/saml2/entity.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import base64
22
from binascii import hexlify
3-
import copy
43
import logging
54
from hashlib import sha1
65
from saml2.metadata import ENDPOINTS
@@ -20,10 +19,10 @@
2019
from saml2 import element_to_extension_element
2120
from saml2 import extension_elements_to_elements
2221

23-
from saml2.saml import NameID, EncryptedAssertion
22+
from saml2.saml import NameID
2423
from saml2.saml import Issuer
2524
from saml2.saml import NAMEID_FORMAT_ENTITY
26-
from saml2.response import LogoutResponse, AuthnResponse
25+
from saml2.response import LogoutResponse
2726
from saml2.time_util import instant
2827
from saml2.s_utils import sid
2928
from saml2.s_utils import UnravelError
@@ -32,7 +31,9 @@
3231
from saml2.s_utils import success_status_factory
3332
from saml2.s_utils import decode_base64_and_inflate
3433
from saml2.s_utils import UnsupportedBinding
35-
from saml2.samlp import AuthnRequest, AuthzDecisionQuery, AuthnQuery, response_from_string
34+
from saml2.samlp import AuthnRequest
35+
from saml2.samlp import AuthzDecisionQuery
36+
from saml2.samlp import AuthnQuery
3637
from saml2.samlp import AssertionIDRequest
3738
from saml2.samlp import ManageNameIDRequest
3839
from saml2.samlp import NameIDMappingRequest
@@ -50,8 +51,12 @@
5051
from saml2 import class_name
5152
from saml2.config import config_factory
5253
from saml2.httpbase import HTTPBase
53-
from saml2.sigver import security_context, response_factory, SigverError, CryptoBackendXmlSec1, make_temp, \
54-
pre_encryption_part
54+
from saml2.sigver import security_context
55+
from saml2.sigver import response_factory
56+
from saml2.sigver import SigverError
57+
from saml2.sigver import CryptoBackendXmlSec1
58+
from saml2.sigver import make_temp
59+
from saml2.sigver import pre_encryption_part
5560
from saml2.sigver import pre_signature_part
5661
from saml2.sigver import signed_instance_factory
5762
from saml2.virtual_org import VirtualOrg
@@ -367,7 +372,8 @@ def _message(self, request_cls, destination=None, message_id=0,
367372
:param sign: Whether the request should be signed or not.
368373
:param sign_prepare: Whether the signature should be prepared or not.
369374
:param kwargs: Key word arguments specific to one request type
370-
:return: An instance of the request_cls
375+
:return: A tuple containing the request ID and an instance of the
376+
request_cls
371377
"""
372378
if not message_id:
373379
message_id = sid(self.seed)
@@ -377,6 +383,7 @@ def _message(self, request_cls, destination=None, message_id=0,
377383
kwargs[key] = val
378384

379385
req = request_cls(**kwargs)
386+
reqid = req.id
380387

381388
if destination:
382389
req.destination = destination
@@ -388,12 +395,13 @@ def _message(self, request_cls, destination=None, message_id=0,
388395
req.extensions = extensions
389396

390397
if sign:
391-
return self.sign(req, sign_prepare=sign_prepare)
398+
return reqid, self.sign(req, sign_prepare=sign_prepare)
392399
else:
393400
logger.info("REQUEST: %s" % req)
394-
return req
401+
return reqid, req
395402

396-
def _filter_args(self, instance, extensions=None, **kwargs):
403+
@staticmethod
404+
def _filter_args(instance, extensions=None, **kwargs):
397405
args = {}
398406
if extensions is None:
399407
extensions = []
@@ -933,7 +941,7 @@ def artifact2message(self, artifact, descriptor):
933941
raise SAMLError("Missing endpoint location")
934942

935943
_sid = sid()
936-
msg = self.create_artifact_resolve(artifact, destination, _sid)
944+
mid, msg = self.create_artifact_resolve(artifact, destination, _sid)
937945
return self.send_using_soap(msg, destination)
938946

939947
def parse_artifact_resolve(self, txt, **kwargs):

tests/test_50_server.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def test_response(self):
124124
assert status.status_code.value == samlp.STATUS_SUCCESS
125125

126126
def test_parse_faulty_request(self):
127-
authn_request = self.client.create_authn_request(
127+
req_id, authn_request = self.client.create_authn_request(
128128
destination="http://www.example.com", id="id1")
129129

130130
# should raise an error because faulty spentityid
@@ -137,7 +137,7 @@ def test_parse_faulty_request(self):
137137
_dict["SAMLRequest"][0], binding)
138138

139139
def test_parse_faulty_request_to_err_status(self):
140-
authn_request = self.client.create_authn_request(
140+
req_id, authn_request = self.client.create_authn_request(
141141
destination="http://www.example.com")
142142

143143
binding = BINDING_HTTP_REDIRECT
@@ -163,7 +163,7 @@ def test_parse_faulty_request_to_err_status(self):
163163
assert status_code.status_code.value == samlp.STATUS_UNKNOWN_PRINCIPAL
164164

165165
def test_parse_ok_request(self):
166-
authn_request = self.client.create_authn_request(
166+
req_id, authn_request = self.client.create_authn_request(
167167
message_id="id1", destination="http://localhost:8088/sso")
168168

169169
print authn_request
@@ -378,7 +378,7 @@ def test_slo_http_post(self):
378378
}
379379
self.client.users.add_information_about_person(sinfo)
380380

381-
logout_request = self.client.create_logout_request(
381+
req_id, logout_request = self.client.create_logout_request(
382382
destination="http://localhost:8088/slop", name_id=nid,
383383
issuer_entity_id="urn:mace:example.com:saml:roland:idp",
384384
reason="I'm tired of this")
@@ -404,7 +404,7 @@ def test_slo_soap(self):
404404
sp = client.Saml2Client(config_file="server_conf")
405405
sp.users.add_information_about_person(sinfo)
406406

407-
logout_request = sp.create_logout_request(
407+
req_id, logout_request = sp.create_logout_request(
408408
name_id=nid, destination="http://localhost:8088/slo",
409409
issuer_entity_id="urn:mace:example.com:saml:roland:idp",
410410
reason="I'm tired of this")
@@ -483,7 +483,7 @@ class TestServerLogout():
483483

484484
def test_1(self):
485485
server = Server("idp_slo_redirect_conf")
486-
request = _logout_request("sp_slo_redirect_conf")
486+
req_id, request = _logout_request("sp_slo_redirect_conf")
487487
print request
488488
bindings = [BINDING_HTTP_REDIRECT]
489489
response = server.create_logout_response(request, bindings)

0 commit comments

Comments
 (0)