diff options
| author | Abeer Upadhyay <ab.esquarer@gmail.com> | 2018-03-25 13:39:32 +0530 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2018-03-28 10:10:18 -0400 |
| commit | 1bf4646f9133f26547a0dccf2f8a4526d85f2ab3 (patch) | |
| tree | b0daff8bfb53294549d1270d73318d0bf4e318ca | |
| parent | 76ae1e9a94cbfddbbf115ad96f26901ba5c02d44 (diff) | |
Fixed #29258 -- Added type checking for login()'s backend argument.
| -rw-r--r-- | AUTHORS | 1 | ||||
| -rw-r--r-- | django/contrib/auth/__init__.py | 3 | ||||
| -rw-r--r-- | tests/auth_tests/test_auth_backends.py | 9 |
3 files changed, 13 insertions, 0 deletions
@@ -8,6 +8,7 @@ answer newbie questions, and generally made Django that much better: Aaron Cannon <cannona@fireantproductions.com> Aaron Swartz <http://www.aaronsw.com/> Aaron T. Myers <atmyers@gmail.com> + Abeer Upadhyay <ab.esquarer@gmail.com> Abhishek Gautam <abhishekg1128@yahoo.com> Adam BogdaĆ <adam@bogdal.pl> Adam Johnson <https://github.com/adamchainz> diff --git a/django/contrib/auth/__init__.py b/django/contrib/auth/__init__.py index 590f85442c..6aa900fbe4 100644 --- a/django/contrib/auth/__init__.py +++ b/django/contrib/auth/__init__.py @@ -119,6 +119,9 @@ def login(request, user, backend=None): 'therefore must provide the `backend` argument or set the ' '`backend` attribute on the user.' ) + else: + if not isinstance(backend, str): + raise TypeError('backend must be a dotted import path string (got %r).' % backend) request.session[SESSION_KEY] = user._meta.pk.value_to_string(user) request.session[BACKEND_SESSION_KEY] = backend diff --git a/tests/auth_tests/test_auth_backends.py b/tests/auth_tests/test_auth_backends.py index 1a1950e989..25a910cdf1 100644 --- a/tests/auth_tests/test_auth_backends.py +++ b/tests/auth_tests/test_auth_backends.py @@ -695,6 +695,15 @@ class SelectingBackendTests(TestCase): with self.assertRaisesMessage(ValueError, expected_message): self.client._login(user) + def test_non_string_backend(self): + user = User.objects.create_user(self.username, 'email', self.password) + expected_message = ( + 'backend must be a dotted import path string (got ' + '<class \'django.contrib.auth.backends.ModelBackend\'>).' + ) + with self.assertRaisesMessage(TypeError, expected_message): + self.client._login(user, backend=ModelBackend) + @override_settings(AUTHENTICATION_BACKENDS=[backend, other_backend]) def test_backend_path_login_with_explicit_backends(self): user = User.objects.create_user(self.username, 'email', self.password) |
