Skip to content

Commit d02881c

Browse files
Log missing fields in SAML response
When a field was not present in the SAML response, it was silently ignored during the User model update. Logging helps debugging.
1 parent f655aea commit d02881c

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

CHANGES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Changes
33

44
UNRELEASED
55
----------
6+
- Log when fields are missing in a SAML response.
67
- Log when attribute_mapping maps to nonexistent User fields.
78
- Dropped compatibility for Python < 2.7 and Django < 1.8.
89

djangosaml2/backends.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,9 @@ def update_user(self, user, attributes, attribute_mapping,
215215
for saml_attr, django_attrs in attribute_mapping.items():
216216
attr_value_list = attributes.get(saml_attr)
217217
if not attr_value_list:
218+
logger.debug(
219+
'Could not find value for "%s", not updating fields "%s"',
220+
saml_attr, django_attrs)
218221
continue
219222

220223
for attr in django_attrs:

tests/testprofiles/tests.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,17 @@ def test_update_user_empty_attribute(self):
102102
'cn': ('John', ),
103103
'sn': (),
104104
}
105-
backend.update_user(user, attributes, attribute_mapping)
105+
with self.assertLogs('djangosaml2', level='DEBUG') as logs:
106+
backend.update_user(user, attributes, attribute_mapping)
106107
self.assertEqual(user.email, 'john@example.com')
107108
self.assertEqual(user.first_name, 'John')
108109
# empty attribute list: no update
109110
self.assertEqual(user.last_name, 'Smith')
111+
self.assertIn(
112+
'DEBUG:djangosaml2:Could not find value for "sn", not '
113+
'updating fields "(\'last_name\',)"',
114+
logs.output,
115+
)
110116

111117
def test_invalid_model_attribute_log(self):
112118
backend = Saml2Backend()

0 commit comments

Comments
 (0)