Skip to content

Commit 9994d26

Browse files
author
Roland Hedberg
committed
Allow for filtering which entities you want to retain from the metadata.
1 parent e9b4721 commit 9994d26

File tree

3 files changed

+123
-18
lines changed

3 files changed

+123
-18
lines changed

src/saml2/filter.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
__author__ = 'roland'
2+
3+
class Filter(object):
4+
def __init__(self):
5+
pass
6+
7+
def __call__(self, *args, **kwargs):
8+
pass
9+
10+
11+
class AllowDescriptor(Filter):
12+
def __init__(self, allow):
13+
"""
14+
15+
:param allow: List of allowed descriptors
16+
:return:
17+
"""
18+
super(AllowDescriptor, self).__init__()
19+
self.allow = allow
20+
21+
def __call__(self, entity_descriptor):
22+
# get descriptors
23+
_all = []
24+
for desc in entity_descriptor.keys():
25+
if desc.endswith("_descriptor"):
26+
typ, _ = desc.rsplit("_", 1)
27+
if typ in self.allow:
28+
_all.append(typ)
29+
else:
30+
del entity_descriptor[desc]
31+
32+
if not _all:
33+
return None
34+
else:
35+
return entity_descriptor

tests/test_38_metadata_filter.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
from saml2 import md
2+
from saml2 import saml
3+
from saml2 import config
4+
from saml2 import xmldsig
5+
from saml2 import xmlenc
6+
7+
from saml2.filter import AllowDescriptor
8+
from saml2.mdstore import MetadataStore
9+
from saml2.attribute_converter import ac_factory
10+
from saml2.extension import mdui
11+
from saml2.extension import idpdisc
12+
from saml2.extension import dri
13+
from saml2.extension import mdattr
14+
from saml2.extension import ui
15+
16+
from pathutils import full_path
17+
18+
__author__ = 'roland'
19+
20+
sec_config = config.Config()
21+
22+
ONTS = {
23+
saml.NAMESPACE: saml,
24+
mdui.NAMESPACE: mdui,
25+
mdattr.NAMESPACE: mdattr,
26+
dri.NAMESPACE: dri,
27+
ui.NAMESPACE: ui,
28+
idpdisc.NAMESPACE: idpdisc,
29+
md.NAMESPACE: md,
30+
xmldsig.NAMESPACE: xmldsig,
31+
xmlenc.NAMESPACE: xmlenc
32+
}
33+
34+
ATTRCONV = ac_factory(full_path("attributemaps"))
35+
36+
METADATACONF = {
37+
"1": [{
38+
"class": "saml2.mdstore.MetaDataFile",
39+
"metadata": [(full_path("swamid-2.0.xml"), )],
40+
}],
41+
}
42+
43+
def test_swamid_sp():
44+
mds = MetadataStore(ONTS.values(), ATTRCONV, sec_config,
45+
disable_ssl_certificate_validation=True,
46+
filter=AllowDescriptor(["spsso"]))
47+
48+
mds.imp(METADATACONF["1"])
49+
sps = mds.with_descriptor("spsso")
50+
assert len(sps) == 417
51+
idps = mds.with_descriptor("idpsso")
52+
assert idps == {}
53+
54+
def test_swamid_idp():
55+
mds = MetadataStore(ONTS.values(), ATTRCONV, sec_config,
56+
disable_ssl_certificate_validation=True,
57+
filter=AllowDescriptor(["idpsso"]))
58+
59+
mds.imp(METADATACONF["1"])
60+
sps = mds.with_descriptor("spsso")
61+
assert len(sps) == 0
62+
idps = mds.with_descriptor("idpsso")
63+
assert len(idps) == 275
64+
65+
if __name__ == "__main__":
66+
test_swamid_idp()

tests/test_75_mongodb.py

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from contextlib import closing
2-
from pymongo.errors import ConnectionFailure
2+
from pymongo.errors import ConnectionFailure, ServerSelectionTimeoutError
33
from saml2 import BINDING_HTTP_POST
44
from saml2.authn_context import INTERNETPROTOCOLPASSWORD
55
from saml2.client import Saml2Client
@@ -69,23 +69,27 @@ def test_eptid_mongo_db():
6969
except ConnectionFailure:
7070
pass
7171
else:
72-
e1 = edb.get("idp_entity_id", "sp_entity_id", "user_id",
73-
"some other data")
74-
print(e1)
75-
assert e1.startswith("idp_entity_id!sp_entity_id!")
76-
e2 = edb.get("idp_entity_id", "sp_entity_id", "user_id",
77-
"some other data")
78-
assert e1 == e2
79-
80-
e3 = edb.get("idp_entity_id", "sp_entity_id", "user_2",
81-
"some other data")
82-
print(e3)
83-
assert e1 != e3
84-
85-
e4 = edb.get("idp_entity_id", "sp_entity_id2", "user_id",
86-
"some other data")
87-
assert e4 != e1
88-
assert e4 != e3
72+
try:
73+
e1 = edb.get("idp_entity_id", "sp_entity_id", "user_id",
74+
"some other data")
75+
except ServerSelectionTimeoutError:
76+
pass
77+
else:
78+
print(e1)
79+
assert e1.startswith("idp_entity_id!sp_entity_id!")
80+
e2 = edb.get("idp_entity_id", "sp_entity_id", "user_id",
81+
"some other data")
82+
assert e1 == e2
83+
84+
e3 = edb.get("idp_entity_id", "sp_entity_id", "user_2",
85+
"some other data")
86+
print(e3)
87+
assert e1 != e3
88+
89+
e4 = edb.get("idp_entity_id", "sp_entity_id2", "user_id",
90+
"some other data")
91+
assert e4 != e1
92+
assert e4 != e3
8993

9094

9195

0 commit comments

Comments
 (0)