Skip to content

Commit 2aa63a7

Browse files
authored
Merge pull request #94 from francoisfreitag/add_logs
Log invalid model field in attribute mapping and missing fields in SAML response
2 parents 3685a96 + d02881c commit 2aa63a7

File tree

6 files changed

+64
-5
lines changed

6 files changed

+64
-5
lines changed

CHANGES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ Changes
33

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

810
0.16.10 (2017-10-02)

README.rst

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,12 @@ do to make sure it is compatible with your Django version and environment.
6565

6666
.. note::
6767

68-
When you finish the configuation you can run the djangosaml2 test suite
69-
as you run any other Django application test suite. Just type
70-
``python manage.py test djangosaml2``
68+
When you finish the configuration you can run the djangosaml2 test suite as
69+
you run any other Django application test suite. Just type ``python manage.py
70+
test djangosaml2``.
71+
72+
Python 2 users need to ``pip install djangosaml2[test]`` in order to run the
73+
tests.
7174

7275
Then you have to add the ``djangosaml2.backends.Saml2Backend``
7376
authentication backend to the list of authentications backends.

djangosaml2/backends.py

Lines changed: 6 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:
@@ -226,6 +229,9 @@ def update_user(self, user, attributes, attribute_mapping,
226229
modified = self._set_attribute(user, attr, attr_value_list[0])
227230

228231
user_modified = user_modified or modified
232+
else:
233+
logger.debug(
234+
'Could not find attribute "%s" on user "%s"', attr, user)
229235

230236
logger.debug('Sending the pre_save signal')
231237
signal_modified = any(

setup.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,22 @@
1313
# limitations under the License.
1414

1515

16-
import os
1716
import codecs
17+
import os
18+
import sys
1819
from setuptools import setup, find_packages
1920

2021

2122
def read(*rnames):
2223
return codecs.open(os.path.join(os.path.dirname(__file__), *rnames), encoding='utf-8').read()
2324

2425

26+
extra = {'test': []}
27+
if sys.version_info < (3, 4):
28+
# Necessary to use assertLogs in tests
29+
extra['test'].append('unittest2')
30+
31+
2532
setup(
2633
name='djangosaml2',
2734
version='0.16.10',
@@ -66,4 +73,5 @@ def read(*rnames):
6673
'Django>=1.8',
6774
'pysaml2==4.4.0',
6875
],
76+
extras_require=extra,
6977
)

tests/testprofiles/tests.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17+
import sys
18+
1719
from django.contrib.auth import get_user_model
1820
from django.contrib.auth.models import User as DjangoUserModel
1921
from django.test import TestCase, override_settings
@@ -22,6 +24,17 @@
2224

2325
User = get_user_model()
2426

27+
if sys.version_info < (3, 4):
28+
# Monkey-patch TestCase to add the assertLogs method introduced in
29+
# Python 3.4
30+
from unittest2.case import _AssertLogsContext
31+
32+
class LoggerTestCase(TestCase):
33+
def assertLogs(self, logger=None, level=None):
34+
return _AssertLogsContext(self, logger, level)
35+
36+
TestCase = LoggerTestCase
37+
2538

2639
class Saml2BackendTests(TestCase):
2740
def test_update_user(self):
@@ -89,11 +102,37 @@ def test_update_user_empty_attribute(self):
89102
'cn': ('John', ),
90103
'sn': (),
91104
}
92-
backend.update_user(user, attributes, attribute_mapping)
105+
with self.assertLogs('djangosaml2', level='DEBUG') as logs:
106+
backend.update_user(user, attributes, attribute_mapping)
93107
self.assertEqual(user.email, 'john@example.com')
94108
self.assertEqual(user.first_name, 'John')
95109
# empty attribute list: no update
96110
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+
)
116+
117+
def test_invalid_model_attribute_log(self):
118+
backend = Saml2Backend()
119+
120+
attribute_mapping = {
121+
'uid': ['username'],
122+
'cn': ['nonexistent'],
123+
}
124+
attributes = {
125+
'uid': ['john'],
126+
'cn': ['John'],
127+
}
128+
129+
with self.assertLogs('djangosaml2', level='DEBUG') as logs:
130+
backend.get_saml2_user(True, 'john', attributes, attribute_mapping)
131+
132+
self.assertIn(
133+
'DEBUG:djangosaml2:Could not find attribute "nonexistent" on user "john"',
134+
logs.output,
135+
)
97136

98137
def test_django_user_main_attribute(self):
99138
backend = Saml2Backend()

tox.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ deps =
1616
django110: Django>=1.10,<1.11
1717
django111: Django>=1.11,<2.0
1818
djangomaster: https://github.com/django/django/archive/master.tar.gz
19+
.[test]
1920

2021
# Waiting on upstream fix for https://code.djangoproject.com/ticket/28679
2122
ignore_outcome =

0 commit comments

Comments
 (0)