Skip to content

Commit fcd34dc

Browse files
author
Roland Hedberg
committed
Should generate 'true'/'false' in xml not 'True'/'False'.
1 parent 478a78c commit fcd34dc

File tree

1 file changed

+51
-18
lines changed

1 file changed

+51
-18
lines changed

example/idp2/idp.py

Lines changed: 51 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#!/usr/bin/env python
22
import argparse
33
import base64
4-
import xmldsig as ds
54
import re
65
import logging
76
import time
@@ -10,6 +9,7 @@
109
from urlparse import parse_qs
1110
from Cookie import SimpleCookie
1211
import os
12+
from saml2.profile import ecp
1313

1414
from saml2 import server
1515
from saml2 import BINDING_HTTP_ARTIFACT
@@ -24,7 +24,6 @@
2424
from saml2.authn_context import PASSWORD
2525
from saml2.authn_context import UNSPECIFIED
2626
from saml2.authn_context import authn_context_class_ref
27-
from saml2.extension import pefim
2827
from saml2.httputil import Response
2928
from saml2.httputil import NotFound
3029
from saml2.httputil import geturl
@@ -35,11 +34,13 @@
3534
from saml2.httputil import ServiceError
3635
from saml2.ident import Unknown
3736
from saml2.metadata import create_metadata_string
38-
from saml2.s_utils import rndstr, exception_trace
37+
from saml2.s_utils import rndstr
38+
from saml2.s_utils import exception_trace
3939
from saml2.s_utils import UnknownPrincipal
4040
from saml2.s_utils import UnsupportedBinding
4141
from saml2.s_utils import PolicyError
42-
from saml2.sigver import verify_redirect_signature, cert_from_instance, encrypt_cert_from_item
42+
from saml2.sigver import verify_redirect_signature
43+
from saml2.sigver import encrypt_cert_from_item
4344

4445
logger = logging.getLogger("saml2.idp")
4546

@@ -239,6 +240,7 @@ def __init__(self, environ, start_response, user=None):
239240
self.binding_out = None
240241
self.destination = None
241242
self.req_info = None
243+
self.op_type = ""
242244

243245
def verify_request(self, query, binding):
244246
"""
@@ -258,10 +260,14 @@ def verify_request(self, query, binding):
258260
_authn_req = self.req_info.message
259261
logger.debug("%s" % _authn_req)
260262

261-
self.binding_out, self.destination = IDP.pick_binding(
262-
"assertion_consumer_service",
263-
bindings=self.response_bindings,
264-
entity_id=_authn_req.issuer.text)
263+
try:
264+
self.binding_out, self.destination = IDP.pick_binding(
265+
"assertion_consumer_service",
266+
bindings=self.response_bindings,
267+
entity_id=_authn_req.issuer.text)
268+
except Exception as err:
269+
logger.error("Couldn't find receiver endpoint: %s" % err)
270+
raise
265271

266272
logger.debug("Binding: %s, destination: %s" % (self.binding_out,
267273
self.destination))
@@ -270,23 +276,31 @@ def verify_request(self, query, binding):
270276
try:
271277
resp_args = IDP.response_args(_authn_req)
272278
_resp = None
273-
except UnknownPrincipal, excp:
279+
except UnknownPrincipal as excp:
274280
_resp = IDP.create_error_response(_authn_req.id,
275281
self.destination, excp)
276-
except UnsupportedBinding, excp:
282+
except UnsupportedBinding as excp:
277283
_resp = IDP.create_error_response(_authn_req.id,
278284
self.destination, excp)
279285

280286
return resp_args, _resp
281287

282288
def do(self, query, binding_in, relay_state="", encrypt_cert=None):
289+
"""
290+
291+
:param query: The request
292+
:param binding_in: Which binding was used when receiving the query
293+
:param relay_state: The relay state provided by the SP
294+
:param encrypt_cert: Cert to use for encryption
295+
:return: A response
296+
"""
283297
try:
284298
resp_args, _resp = self.verify_request(query, binding_in)
285-
except UnknownPrincipal, excp:
299+
except UnknownPrincipal as excp:
286300
logger.error("UnknownPrincipal: %s" % (excp,))
287301
resp = ServiceError("UnknownPrincipal: %s" % (excp,))
288302
return resp(self.environ, self.start_response)
289-
except UnsupportedBinding, excp:
303+
except UnsupportedBinding as excp:
290304
logger.error("UnsupportedBinding: %s" % (excp,))
291305
resp = ServiceError("UnsupportedBinding: %s" % (excp,))
292306
return resp(self.environ, self.start_response)
@@ -299,19 +313,34 @@ def do(self, query, binding_in, relay_state="", encrypt_cert=None):
299313
if REPOZE_ID_EQUIVALENT:
300314
identity[REPOZE_ID_EQUIVALENT] = self.user
301315
try:
316+
try:
317+
metod = self.environ["idp.authn_ref"]
318+
except KeyError:
319+
pass
320+
else:
321+
resp_args["authn"] = metod
322+
302323
_resp = IDP.create_authn_response(
303324
identity, userid=self.user,
304-
authn=AUTHN_BROKER[self.environ["idp.authn_ref"]], encrypt_cert=encrypt_cert,
325+
encrypt_cert=encrypt_cert,
305326
**resp_args)
306-
except Exception, excp:
327+
except Exception as excp:
307328
logging.error(exception_trace(excp))
308329
resp = ServiceError("Exception: %s" % (excp,))
309330
return resp(self.environ, self.start_response)
310331

311332
logger.info("AuthNResponse: %s" % _resp)
333+
if self.op_type == "ecp":
334+
kwargs = {"soap_headers": [
335+
ecp.Response(
336+
assertion_consumer_service_url=self.destination)]}
337+
else:
338+
kwargs = {}
339+
312340
http_args = IDP.apply_binding(self.binding_out,
313341
"%s" % _resp, self.destination,
314-
relay_state, response=True)
342+
relay_state, response=True, **kwargs)
343+
315344
logger.debug("HTTPargs: %s" % http_args)
316345
return self.response(self.binding_out, http_args)
317346

@@ -412,6 +441,9 @@ def ecp(self):
412441
if PASSWD[user] != passwd:
413442
resp = Unauthorized()
414443
self.user = user
444+
self.environ[
445+
"idp.authn_ref"] = AUTHN_BROKER.get_authn_by_accr(
446+
PASSWORD)
415447
except ValueError:
416448
resp = Unauthorized()
417449
else:
@@ -425,6 +457,7 @@ def ecp(self):
425457
_dict = self.unpack_soap()
426458
self.response_bindings = [BINDING_PAOS]
427459
# Basic auth ?!
460+
self.op_type = "ecp"
428461
return self.operation(_dict, BINDING_SOAP)
429462

430463
# -----------------------------------------------------------------------------
@@ -542,7 +575,7 @@ def do(self, request, binding, relay_state="", encrypt_cert=None):
542575
_, body = request.split("\n")
543576
logger.debug("req: '%s'" % body)
544577
req_info = IDP.parse_logout_request(body, binding)
545-
except Exception, exc:
578+
except Exception as exc:
546579
logger.error("Bad request: %s" % exc)
547580
resp = BadRequest("%s" % exc)
548581
return resp(self.environ, self.start_response)
@@ -559,7 +592,7 @@ def do(self, request, binding, relay_state="", encrypt_cert=None):
559592
# remove the authentication
560593
try:
561594
IDP.session_db.remove_authn_statements(msg.name_id)
562-
except KeyError, exc:
595+
except KeyError as exc:
563596
logger.error("ServiceError: %s" % exc)
564597
resp = ServiceError("%s" % exc)
565598
return resp(self.environ, self.start_response)
@@ -568,7 +601,7 @@ def do(self, request, binding, relay_state="", encrypt_cert=None):
568601

569602
try:
570603
hinfo = IDP.apply_binding(binding, "%s" % resp, "", relay_state)
571-
except Exception, exc:
604+
except Exception as exc:
572605
logger.error("ServiceError: %s" % exc)
573606
resp = ServiceError("%s" % exc)
574607
return resp(self.environ, self.start_response)

0 commit comments

Comments
 (0)