Skip to content

Commit b727e21

Browse files
Merge pull request #655 from johanlundberg/mdstore_supported_algorithms
Add convenience method to retrieve supported algorithms from metadata
2 parents e195bb9 + f6d1c87 commit b727e21

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

src/saml2/mdstore.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
from saml2.sigver import security_context
3838
from saml2.extension.mdattr import NAMESPACE as NS_MDATTR
3939
from saml2.extension.mdattr import EntityAttributes
40+
from saml2.extension.algsupport import NAMESPACE as NS_ALGSUPPORT
41+
from saml2.extension.algsupport import SigningMethod, DigestMethod
4042
from saml2.extension.mdui import NAMESPACE as NS_MDUI
4143
from saml2.extension.mdui import UIInfo
4244
from saml2.extension.mdui import DisplayName
@@ -52,6 +54,8 @@
5254
"mdattr_entityattributes": "{ns}&{tag}".format(
5355
ns=NS_MDATTR, tag=EntityAttributes.c_tag
5456
),
57+
"algsupport_signing_method": "{ns}&{tag}".format(ns=NS_ALGSUPPORT, tag=SigningMethod.c_tag),
58+
"algsupport_digest_method": "{ns}&{tag}".format(ns=NS_ALGSUPPORT, tag=DigestMethod.c_tag),
5559
"mdui_uiinfo": "{ns}&{tag}".format(ns=NS_MDUI, tag=UIInfo.c_tag),
5660
"mdui_uiinfo_display_name": "{ns}&{tag}".format(ns=NS_MDUI, tag=DisplayName.c_tag),
5761
"mdui_uiinfo_description": "{ns}&{tag}".format(ns=NS_MDUI, tag=Description.c_tag),
@@ -1282,6 +1286,36 @@ def entity_attributes(self, entity_id):
12821286
"attribute_value"]]
12831287
return res
12841288

1289+
def supported_algorithms(self, entity_id):
1290+
"""
1291+
Get all supported algorithms for an entry in the metadata.
1292+
1293+
Example return data:
1294+
1295+
{'digest_methods': ['http://www.w3.org/2001/04/xmldsig-more#sha224', 'http://www.w3.org/2001/04/xmlenc#sha256'],
1296+
'signing_methods': ['http://www.w3.org/2001/04/xmldsig-more#rsa-sha256']}
1297+
1298+
:param entity_id: Entity id
1299+
:return: dict with keys and value-lists from metadata
1300+
1301+
:type entity_id: string
1302+
:rtype: dict
1303+
"""
1304+
res = {
1305+
'digest_methods': [],
1306+
'signing_methods': []
1307+
}
1308+
try:
1309+
ext = self.__getitem__(entity_id)["extensions"]
1310+
except KeyError:
1311+
return res
1312+
for elem in ext["extension_elements"]:
1313+
if elem["__class__"] == classnames["algsupport_digest_method"]:
1314+
res['digest_methods'].append(elem['algorithm'])
1315+
elif elem["__class__"] == classnames["algsupport_signing_method"]:
1316+
res['signing_methods'].append(elem['algorithm'])
1317+
return res
1318+
12851319
def _lookup_elements_by_cls(self, root, cls):
12861320
elements = (
12871321
element

tests/test_30_mdstore.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,18 @@
5454
<EntitiesDescriptor
5555
xmlns="urn:oasis:names:tc:SAML:2.0:metadata"
5656
xmlns:md="urn:oasis:names:tc:SAML:2.0:metadata"
57+
xmlns:alg="urn:oasis:names:tc:SAML:metadata:algsupport"
5758
xmlns:shibmeta="urn:mace:shibboleth:metadata:1.0"
5859
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5960
xmlns:ds="http://www.w3.org/2000/09/xmldsig#"
6061
Name="urn:mace:example.com:test-1.0">
6162
<EntityDescriptor
6263
entityID="http://xenosmilus.umdc.umu.se/simplesaml/saml2/idp/metadata.php"
6364
xml:base="swamid-1.0/idp.umu.se-saml2.xml">
65+
<md:Extensions>
66+
<alg:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
67+
<alg:SigningMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"/>
68+
</md:Extensions>
6469
<IDPSSODescriptor protocolSupportEnumeration="urn:oasis:names:tc:SAML:2.0:protocol">
6570
<KeyDescriptor>
6671
<ds:KeyInfo>
@@ -488,6 +493,15 @@ def test_metadata_extension_algsupport():
488493
assert mds
489494

490495

496+
def test_supported_algorithms():
497+
mds = MetadataStore(ATTRCONV, sec_config,
498+
disable_ssl_certificate_validation=True)
499+
mds.imp(METADATACONF["11"])
500+
algs = mds.supported_algorithms(entity_id='http://xenosmilus.umdc.umu.se/simplesaml/saml2/idp/metadata.php')
501+
assert 'http://www.w3.org/2001/04/xmlenc#sha256' in algs['digest_methods']
502+
assert 'http://www.w3.org/2001/04/xmldsig-more#rsa-sha256' in algs['signing_methods']
503+
504+
491505
def test_extension():
492506
mds = MetadataStore(ATTRCONV, None)
493507
# use ordered dict to force expected entity to be last

0 commit comments

Comments
 (0)