Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions src/pyff/samlmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -1112,17 +1112,32 @@ def discojson_sp_attr(e):
if b64_trustinfos is None:
return None

entity_id = e.get('entityID', None)
sp = {}
sp['entityID'] = e.get('entityID', None)
sp['entityID'] = entity_id
sp['profiles'] = {}
sp['extra_md'] = {}

for b64_trustinfo in b64_trustinfos:
try:
str_trustinfo = b64decode(b64_trustinfo.encode('ascii'))
trustinfo = json.loads(str_trustinfo.decode('utf8'))
sp['profiles'].update(trustinfo['profiles'])
for name, profile in trustinfo['profiles'].items():
if name in sp['profiles']:
log.warning(f"SP Entity {entity_id} has a duplicate trust profile {name}")
else:
sp['profiles'][name] = profile
for extra_id, extra in trustinfo.get('extra_md', {}).items():
if extra_id in sp['extra_md']:
log.warning(f"SP Entity {entity_id} has a duplicate extra IdP metadata {extra_id}")
else:
sp['extra_md'][extra_id] = extra
except Exception:
log.error(f"Error parsing TrustInfo profile for {sp['entityID']}")
log.error(f"Error parsing TrustInfo profile for {entity_id}")

if not sp['profiles']:
return None

return sp


Expand Down
47 changes: 47 additions & 0 deletions src/pyff/test/test_discojson_sp_attr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import json
from base64 import b64encode
from unittest import TestCase

from pyuppsala import etree

from pyff.constants import NS
from pyff.samlmd import discojson_sp_attr

ENTITY_ID = 'https://sp.example.com/shibboleth'


def _sp(*values):
"""Build an EntityDescriptor with one entity-selection-profile value per argument."""
e = etree.Element("{{{}}}EntityDescriptor".format(NS['md']), nsmap=NS)
e.set('entityID', ENTITY_ID)
ext = etree.SubElement(e, "{{{}}}Extensions".format(NS['md']))
eas = etree.SubElement(ext, "{{{}}}EntityAttributes".format(NS['mdattr']))
attr = etree.SubElement(eas, "{{{}}}Attribute".format(NS['saml']))
attr.set('Name', 'https://refeds.org/entity-selection-profile')
for value in values:
etree.SubElement(attr, "{{{}}}AttributeValue".format(NS['saml'])).text = value
return e


def _b64(trustinfo):
return b64encode(json.dumps(trustinfo).encode('utf8')).decode('ascii')


class TestDiscojsonSpAttr(TestCase):
def test_no_valid_profile_gives_none(self):
assert discojson_sp_attr(_sp('invalidValueForAttribute')) is None
assert discojson_sp_attr(_sp(_b64({'no-profiles': {}}))) is None

def test_invalid_value_does_not_hide_valid_one(self):
sp = discojson_sp_attr(_sp('invalidValueForAttribute', _b64({'profiles': {'p1': {'strict': True}}})))
assert sp == {'entityID': ENTITY_ID, 'profiles': {'p1': {'strict': True}}, 'extra_md': {}}

def test_duplicates_keep_first_and_warn(self):
first = {'profiles': {'p1': {'strict': True}}, 'extra_md': {'https://idp.example.com': {'title': 'first'}}}
second = {'profiles': {'p1': {'strict': False}}, 'extra_md': {'https://idp.example.com': {'title': 'second'}}}
with self.assertLogs(level='WARNING') as logs:
sp = discojson_sp_attr(_sp(_b64(first), _b64(second)))
assert sp['profiles'] == first['profiles']
assert sp['extra_md'] == first['extra_md']
assert any('duplicate trust profile p1' in line for line in logs.output)
assert any('duplicate extra IdP metadata https://idp.example.com' in line for line in logs.output)
Loading