Skip to content

Commit ea6d290

Browse files
committed
support scope as a mako filter
1 parent 1b09631 commit ea6d290

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

src/satosa/internal_data.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ def _collate_attribute_values_by_priority_order(self, attribute_names, data):
135135
return result
136136

137137
def _render_attribute_template(self, template, data):
138-
t = Template(template,cache_enabled=True)
138+
t = Template(template,cache_enabled=True,imports=["from satosa.util import scope"])
139139
return t.render(**data).split(self.multivalue_separator)
140140

141141
def _get_nested_attribute_value(self, nested_key, data):

src/satosa/util.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,3 +168,9 @@ def rndstr(size=16, alphabet=""):
168168
if not alphabet:
169169
alphabet = string.ascii_letters[0:52] + string.digits
170170
return type(alphabet)().join(rng.choice(alphabet) for _ in range(size))
171+
172+
def scope(s):
173+
if not '@' in s:
174+
raise ValueError("Unscoped string")
175+
(local_part, _, domain_part) = s.partition('@')
176+
return domain_part

tests/satosa/test_internal_data.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,29 @@ def test_simple_template_mapping(self):
272272
assert len(internal_repr["name"]) == 1
273273
assert internal_repr["name"][0] == "Valfrid Lindeman"
274274

275+
def test_scoped_template_mapping(self):
276+
mapping = {
277+
"attributes": {
278+
"unscoped_affiliation": {
279+
"p1": ["eduPersonAffiliation"]
280+
},
281+
"uid": {
282+
"p1": ["eduPersonPrincipalName"],
283+
},
284+
"affiliation": {
285+
"p1": ["eduPersonScopedAffiliation","${eduPersonAffiliation[0]}@${eduPersonPrincipalName[0] | scope}"]
286+
}
287+
}
288+
}
289+
290+
converter = DataConverter(mapping)
291+
internal_repr = converter.to_internal("p1", {
292+
"eduPersonAffiliation": ["student"],
293+
"eduPersonPrincipalName": ["[email protected]"]})
294+
assert "affiliation" in internal_repr
295+
assert len(internal_repr["affiliation"]) == 1
296+
assert internal_repr["affiliation"][0] == "[email protected]"
297+
275298

276299
@pytest.mark.parametrize("attribute_value", [
277300
{"email": "[email protected]"},

0 commit comments

Comments
 (0)