Skip to content

Commit d145785

Browse files
authored
Merge pull request #359 from rebeckag/nest-eptid
Automagically nest eduPersonTargetedID in a NameID.
2 parents 684523a + 1891faa commit d145785

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

src/saml2/attribute_converter.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from saml2 import saml
1212
from saml2 import extension_elements_to_elements
1313
from saml2 import SAMLError
14-
from saml2.saml import NAME_FORMAT_UNSPECIFIED
14+
from saml2.saml import NAME_FORMAT_UNSPECIFIED, NAMEID_FORMAT_PERSISTENT, NameID
1515

1616
import logging
1717
logger = logging.getLogger(__name__)
@@ -491,14 +491,19 @@ def to_(self, attrvals):
491491
"""
492492
attributes = []
493493
for key, value in attrvals.items():
494-
lkey = key.lower()
495-
try:
494+
name = self._to.get(key.lower())
495+
if name:
496+
if name == "urn:oid:1.3.6.1.4.1.5923.1.1.1.10":
497+
# special case for eduPersonTargetedID
498+
attr_value = do_ava(NameID(format=NAMEID_FORMAT_PERSISTENT, text=value).to_string())
499+
else:
500+
attr_value = do_ava(value)
496501
attributes.append(factory(saml.Attribute,
497-
name=self._to[lkey],
502+
name=name,
498503
name_format=self.name_format,
499504
friendly_name=key,
500-
attribute_value=do_ava(value)))
501-
except KeyError:
505+
attribute_value=attr_value))
506+
else:
502507
attributes.append(factory(saml.Attribute,
503508
name=key,
504509
attribute_value=do_ava(value)))

tests/test_19_attribute_converter.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
from attribute_statement_data import *
66

77
from pathutils import full_path
8-
from saml2.attribute_converter import AttributeConverterNOOP
8+
from saml2.attribute_converter import AttributeConverterNOOP, from_local
99
from saml2.attribute_converter import AttributeConverter
1010
from saml2.attribute_converter import to_local
11-
from saml2.saml import attribute_from_string
11+
from saml2.saml import attribute_from_string, name_id_from_string, NameID, NAMEID_FORMAT_PERSISTENT
1212
from saml2.saml import attribute_statement_from_string
1313

1414

@@ -210,6 +210,13 @@ def test_adjust_with_no_mapping_defined(self):
210210
attr_conv.adjust()
211211
assert attr_conv._fro is None and attr_conv._to is None
212212

213+
def test_from_local_nest_eduPersonTargetedID_in_NameID(self):
214+
ava = {"edupersontargetedid": "test value"}
215+
attributes = from_local(self.acs, ava, URI_NF)
216+
assert len(attributes) == 1
217+
assert len(attributes[0].attribute_value) == 1
218+
assert attributes[0].attribute_value[0].text == NameID(format=NAMEID_FORMAT_PERSISTENT, text="test value").to_string().decode("utf-8")
219+
213220

214221
def test_noop_attribute_conversion():
215222
ava = {"urn:oid:2.5.4.4": "Roland", "urn:oid:2.5.4.42": "Hedberg"}

0 commit comments

Comments
 (0)