Skip to content

Commit 2e3a782

Browse files
melangerc00kiemon5ter
authored andcommitted
fix: prevent exception in attribute mapping
when internal_attributes contain nested attribute but the actual value is not nested
1 parent f4464c4 commit 2e3a782

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

src/satosa/attribute_mapping.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import logging
22
from collections import defaultdict
33
from itertools import chain
4+
from typing import Mapping
45

56
from mako.template import Template
67

@@ -97,8 +98,9 @@ def to_internal(self, attribute_profile, external_dict):
9798
continue
9899

99100
external_attribute_name = mapping[attribute_profile]
100-
attribute_values = self._collate_attribute_values_by_priority_order(external_attribute_name,
101-
external_dict)
101+
attribute_values = self._collate_attribute_values_by_priority_order(
102+
external_attribute_name, external_dict
103+
)
102104
if attribute_values: # Only insert key if it has some values
103105
logline = "backend attribute {external} mapped to {internal} ({value})".format(
104106
external=external_attribute_name, internal=internal_attribute_name, value=attribute_values
@@ -157,6 +159,8 @@ def _get_nested_attribute_value(self, nested_key, data):
157159

158160
d = data
159161
for key in keys:
162+
if not isinstance(d, Mapping):
163+
return None
160164
d = d.get(key)
161165
if d is None:
162166
return None

tests/satosa/test_attribute_mapping.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,56 @@
55
from satosa.attribute_mapping import AttributeMapper
66

77

8+
class TestAttributeMapperNestedDataDifferentAttrProfile:
9+
def test_nested_mapping_nested_data_to_internal(self):
10+
mapping = {
11+
"attributes": {
12+
"name": {
13+
"openid": ["name"]
14+
},
15+
"givenname": {
16+
"openid": ["given_name", "name.firstName"]
17+
},
18+
},
19+
}
20+
21+
data = {
22+
"name": {
23+
"firstName": "value-first",
24+
"lastName": "value-last",
25+
},
26+
"email": "[email protected]",
27+
}
28+
29+
converter = AttributeMapper(mapping)
30+
internal_repr = converter.to_internal("openid", data)
31+
assert internal_repr["name"] == [data["name"]]
32+
assert internal_repr["givenname"] == [data["name"]["firstName"]]
33+
34+
35+
def test_nested_mapping_simple_data_to_internal(self):
36+
mapping = {
37+
"attributes": {
38+
"name": {
39+
"openid": ["name"]
40+
},
41+
"givenname": {
42+
"openid": ["given_name", "name.firstName"]
43+
},
44+
},
45+
}
46+
47+
data = {
48+
"name": "value-first",
49+
"email": "[email protected]",
50+
}
51+
52+
converter = AttributeMapper(mapping)
53+
internal_repr = converter.to_internal("openid", data)
54+
assert internal_repr["name"] == [data["name"]]
55+
assert internal_repr.get("givenname") is None
56+
57+
858
class TestAttributeMapper:
959
def test_nested_attribute_to_internal(self):
1060
mapping = {

0 commit comments

Comments
 (0)