Skip to content

Commit 15358e7

Browse files
author
Roland Hedberg
committed
Added a MDX client as a Metadata class.
1 parent d84594a commit 15358e7

File tree

2 files changed

+63
-2
lines changed

2 files changed

+63
-2
lines changed

src/saml2/mdstore.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import json
44

55
from hashlib import sha1
6+
from urllib import urlencode, quote_plus
67
from saml2.httpbase import HTTPBase
78
from saml2.extension.idpdisc import BINDING_DISCO
89
from saml2.extension.idpdisc import DiscoveryResponse
@@ -525,6 +526,51 @@ def load(self):
525526
self.entity[key] = item
526527

527528

529+
class MetaDataMDX(MetaData):
530+
def __init__(self, onts, attrc, url, security, cert, http, **kwargs):
531+
"""
532+
:params onts:
533+
:params attrc:
534+
:params url:
535+
:params security: SecurityContext()
536+
:params cert:
537+
:params http:
538+
"""
539+
MetaData.__init__(self, onts, attrc, **kwargs)
540+
self.url = url
541+
self.security = security
542+
self.cert = cert
543+
self.http = http
544+
545+
def load(self):
546+
pass
547+
548+
def __getitem__(self, item):
549+
try:
550+
return self.entity[item]
551+
except KeyError:
552+
mdx_url = "%s/entities/%s" % (self.url, quote_plus(item))
553+
response = self.http.send(mdx_url)
554+
if response.status_code == 200:
555+
node_name = self.node_name \
556+
or "%s:%s" % (md.EntitiesDescriptor.c_namespace,
557+
md.EntitiesDescriptor.c_tag)
558+
559+
_txt = response.text.encode("utf-8")
560+
if self.cert:
561+
if self.security.verify_signature(_txt,
562+
node_name=node_name,
563+
cert_file=self.cert):
564+
self.parse(_txt)
565+
return self.entity[item]
566+
else:
567+
self.parse(_txt)
568+
return self.entity[item]
569+
else:
570+
logger.info("Response status: %s" % response.status_code)
571+
raise KeyError
572+
573+
528574
class MetadataStore(object):
529575
def __init__(self, onts, attrc, config, ca_certs=None,
530576
check_validity=True,

tests/test_30_mdstore.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
#!/usr/bin/env python
12
# -*- coding: utf-8 -*-
23
import datetime
34
import re
5+
from saml2.httpbase import HTTPBase
46

5-
from saml2.mdstore import MetadataStore
7+
from saml2.mdstore import MetadataStore, MetaDataMDX
68
from saml2.mdstore import destinations
79
from saml2.mdstore import name
810

@@ -223,5 +225,18 @@ def test_metadata_file():
223225
print len(mds.keys())
224226
assert len(mds.keys()) == 560
225227

228+
229+
def test_mdx():
230+
sec_config.xmlsec_binary = sigver.get_xmlsec_binary(["/opt/local/bin"])
231+
http = HTTPBase(verify=False, ca_bundle=None)
232+
233+
mdx = MetaDataMDX(ONTS.values(), ATTRCONV, "http://pyff-test.nordu.net",
234+
sec_config, None, http)
235+
foo = mdx.service("https://idp.umu.se/saml2/idp/metadata.php",
236+
"idpsso_descriptor", "single_sign_on_service")
237+
238+
assert len(foo) == 1
239+
assert foo.keys()[0] == BINDING_HTTP_REDIRECT
240+
226241
if __name__ == "__main__":
227-
test_swami_1()
242+
test_mdx()

0 commit comments

Comments
 (0)