Skip to content

Commit 108a386

Browse files
author
Roland Hedberg
committed
Changes in the MetaDataMDX class to allow for entity id transformation.
1 parent e2a07fa commit 108a386

File tree

3 files changed

+36
-19
lines changed

3 files changed

+36
-19
lines changed

example/idp2/idp.py

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ def operation(self, saml_msg, binding):
135135
saml_msg["RelayState"],
136136
encrypt_cert=_encrypt_cert)
137137
except KeyError:
138-
# Can live with no relay state
139-
return self.do(saml_msg["SAMLRequest"], binding)
138+
# Can live with no relay state # TODO or can we, for inacademia?
139+
return self.do(saml_msg["SAMLRequest"], binding, saml_msg["RelayState"])
140140

141141
def artifact_operation(self, saml_msg):
142142
if not saml_msg:
@@ -400,20 +400,29 @@ def post(self):
400400
"""
401401
logger.info("--- In SSO POST ---")
402402
saml_msg = self.unpack_either()
403-
self.req_info = IDP.parse_authn_request(
404-
saml_msg["SAMLRequest"], BINDING_HTTP_POST)
405-
_req = self.req_info.message
406-
if self.user:
407-
if _req.force_authn:
403+
404+
try:
405+
_key = saml_msg["key"]
406+
saml_msg = IDP.ticket[_key]
407+
self.req_info = saml_msg["req_info"]
408+
del IDP.ticket[_key]
409+
except KeyError:
410+
self.req_info = IDP.parse_authn_request(
411+
saml_msg["SAMLRequest"], BINDING_HTTP_POST)
412+
_req = self.req_info.message
413+
if self.user:
414+
if _req.force_authn:
415+
saml_msg["req_info"] = self.req_info
416+
key = self._store_request(saml_msg)
417+
return self.not_authn(key, _req.requested_authn_context)
418+
else:
419+
return self.operation(saml_msg, BINDING_HTTP_POST)
420+
else:
408421
saml_msg["req_info"] = self.req_info
409422
key = self._store_request(saml_msg)
410423
return self.not_authn(key, _req.requested_authn_context)
411-
else:
412-
return self.operation(saml_msg, BINDING_HTTP_POST)
413424
else:
414-
saml_msg["req_info"] = self.req_info
415-
key = self._store_request(saml_msg)
416-
return self.not_authn(key, _req.requested_authn_context)
425+
return self.operation(saml_msg, BINDING_HTTP_POST)
417426

418427
# def artifact(self):
419428
# # Can be either by HTTP_Redirect or HTTP_POST

src/saml2/mdstore.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def __init__(self, onts, attrc, metadata="", node_name=None,
121121
self.entities_descr = None
122122
self.entity_descr = None
123123
self.check_validity = check_validity
124-
124+
125125
def items(self):
126126
return self.entity.items()
127127

@@ -569,9 +569,14 @@ def load(self):
569569

570570

571571
class MetaDataMDX(MetaData):
572-
573-
def __init__(self, onts, attrc, url, security, cert, http, **kwargs):
572+
""" Uses the md protocol to fetch entity information
573+
"""
574+
def __init__(self, entity_transform, onts, attrc, url, security, cert,
575+
http, **kwargs):
574576
"""
577+
:params entity_transform: function transforming (e.g. base64 or sha1
578+
hash) the entity id. It is applied to the entity id before it is
579+
concatenated with the request URL sent to the MDX server.
575580
:params onts:
576581
:params attrc:
577582
:params url:
@@ -584,6 +589,7 @@ def __init__(self, onts, attrc, url, security, cert, http, **kwargs):
584589
self.security = security
585590
self.cert = cert
586591
self.http = http
592+
self.entity_transform = entity_transform
587593

588594
def load(self):
589595
pass
@@ -592,7 +598,7 @@ def __getitem__(self, item):
592598
try:
593599
return self.entity[item]
594600
except KeyError:
595-
mdx_url = "%s/entities/%s" % (self.url, quote_plus(item))
601+
mdx_url = "%s/entities/%s" % (self.url, self.entity_transform(item))
596602
response = self.http.send(
597603
mdx_url, headers={'Accept': SAML_METADATA_CONTENT_TYPE})
598604
if response.status_code == 200:
@@ -616,7 +622,6 @@ def __getitem__(self, item):
616622
raise KeyError
617623

618624

619-
620625
class MetadataStore(object):
621626
def __init__(self, onts, attrc, config, ca_certs=None,
622627
check_validity=True,

tests/test_30_mdstore.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# -*- coding: utf-8 -*-
33
import datetime
44
import re
5+
from urllib import quote_plus
56
from saml2.httpbase import HTTPBase
67

78
from saml2.mdstore import MetadataStore, MetaDataMDX
@@ -230,7 +231,8 @@ def test_mdx_service():
230231
sec_config.xmlsec_binary = sigver.get_xmlsec_binary(["/opt/local/bin"])
231232
http = HTTPBase(verify=False, ca_bundle=None)
232233

233-
mdx = MetaDataMDX(ONTS.values(), ATTRCONV, "http://pyff-test.nordu.net",
234+
mdx = MetaDataMDX(quote_plus, ONTS.values(), ATTRCONV,
235+
"http://pyff-test.nordu.net",
234236
sec_config, None, http)
235237
foo = mdx.service("https://idp.umu.se/saml2/idp/metadata.php",
236238
"idpsso_descriptor", "single_sign_on_service")
@@ -243,7 +245,8 @@ def test_mdx_certs():
243245
sec_config.xmlsec_binary = sigver.get_xmlsec_binary(["/opt/local/bin"])
244246
http = HTTPBase(verify=False, ca_bundle=None)
245247

246-
mdx = MetaDataMDX(ONTS.values(), ATTRCONV, "http://pyff-test.nordu.net",
248+
mdx = MetaDataMDX(quote_plus, ONTS.values(), ATTRCONV,
249+
"http://pyff-test.nordu.net",
247250
sec_config, None, http)
248251
foo = mdx.certs("https://idp.umu.se/saml2/idp/metadata.php", "idpsso")
249252

0 commit comments

Comments
 (0)