Skip to content

Commit 1891faa

Browse files
author
Rebecka Gulliksson
committed
Automagically nest eduPersonTargetedID in a NameID.
1 parent 17e6883 commit 1891faa

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__)
@@ -488,14 +488,19 @@ def to_(self, attrvals):
488488
"""
489489
attributes = []
490490
for key, value in attrvals.items():
491-
lkey = key.lower()
492-
try:
491+
name = self._to.get(key.lower())
492+
if name:
493+
if name == "urn:oid:1.3.6.1.4.1.5923.1.1.1.10":
494+
# special case for eduPersonTargetedID
495+
attr_value = do_ava(NameID(format=NAMEID_FORMAT_PERSISTENT, text=value).to_string())
496+
else:
497+
attr_value = do_ava(value)
493498
attributes.append(factory(saml.Attribute,
494-
name=self._to[lkey],
499+
name=name,
495500
name_format=self.name_format,
496501
friendly_name=key,
497-
attribute_value=do_ava(value)))
498-
except KeyError:
502+
attribute_value=attr_value))
503+
else:
499504
attributes.append(factory(saml.Attribute,
500505
name=key,
501506
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)