forked from django/django
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed django#2550 -- Allow the auth backends to raise the PermissionD…
…enied exception to completely stop the authentication chain. Many thanks to namn, danielr, Dan Julius, Łukasz Rekucki, Aashu Dwivedi and umbrae for working this over the years.
- Loading branch information
Showing
4 changed files
with
52 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,8 @@ | |
from django.contrib.auth.tests.utils import skipIfCustomUser | ||
from django.contrib.auth.tests.custom_user import ExtensionUser | ||
from django.contrib.contenttypes.models import ContentType | ||
from django.core.exceptions import ImproperlyConfigured | ||
from django.core.exceptions import ImproperlyConfigured, PermissionDenied | ||
from django.contrib.auth import authenticate | ||
from django.test import TestCase | ||
from django.test.utils import override_settings | ||
|
||
|
@@ -323,3 +324,37 @@ def test_has_perm(self): | |
def test_has_module_perms(self): | ||
self.assertEqual(self.user1.has_module_perms("app1"), False) | ||
self.assertEqual(self.user1.has_module_perms("app2"), False) | ||
|
||
|
||
class PermissionDeniedBackend(object): | ||
""" | ||
Always raises PermissionDenied. | ||
""" | ||
supports_object_permissions = True | ||
supports_anonymous_user = True | ||
supports_inactive_user = True | ||
|
||
def authenticate(self, username=None, password=None): | ||
raise PermissionDenied | ||
|
||
|
||
class PermissionDeniedBackendTest(TestCase): | ||
""" | ||
Tests that other backends are not checked once a backend raises PermissionDenied | ||
""" | ||
backend = 'django.contrib.auth.tests.auth_backends.PermissionDeniedBackend' | ||
|
||
def setUp(self): | ||
self.user1 = User.objects.create_user('test', '[email protected]', 'test') | ||
self.user1.save() | ||
|
||
@override_settings(AUTHENTICATION_BACKENDS=(backend, ) + | ||
tuple(settings.AUTHENTICATION_BACKENDS)) | ||
def test_permission_denied(self): | ||
"user is not authenticated after a backend raises permission denied #2550" | ||
self.assertEqual(authenticate(username='test', password='test'), None) | ||
|
||
@override_settings(AUTHENTICATION_BACKENDS=tuple( | ||
settings.AUTHENTICATION_BACKENDS) + (backend, )) | ||
def test_authenticates(self): | ||
self.assertEqual(authenticate(username='test', password='test'), self.user1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters