diff options
| author | Aleksej Manaev <aleksej.manaev@gmx.de> | 2016-07-11 16:40:39 +0200 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2016-09-12 20:11:53 -0400 |
| commit | 4b9330ccc04575f9e5126529ec355a450d12e77c (patch) | |
| tree | 90d340a9d28bd448b3b709b8b605bd1009bbba0a /django | |
| parent | 32c0d823e5316aa7d616a69996919b62748368cc (diff) | |
Fixed #25187 -- Made request available in authentication backends.
Diffstat (limited to 'django')
| -rw-r--r-- | django/contrib/auth/__init__.py | 24 | ||||
| -rw-r--r-- | django/contrib/auth/backends.py | 4 | ||||
| -rw-r--r-- | django/contrib/auth/forms.py | 2 | ||||
| -rw-r--r-- | django/contrib/auth/middleware.py | 2 |
4 files changed, 22 insertions, 10 deletions
diff --git a/django/contrib/auth/__init__.py b/django/contrib/auth/__init__.py index fa0ef39a32..77458e15f9 100644 --- a/django/contrib/auth/__init__.py +++ b/django/contrib/auth/__init__.py @@ -1,11 +1,13 @@ import inspect import re +import warnings from django.apps import apps as django_apps from django.conf import settings from django.core.exceptions import ImproperlyConfigured, PermissionDenied from django.middleware.csrf import rotate_token from django.utils.crypto import constant_time_compare +from django.utils.deprecation import RemovedInDjango21Warning from django.utils.module_loading import import_string from django.utils.translation import LANGUAGE_SESSION_KEY @@ -59,19 +61,29 @@ def _get_user_session_key(request): return get_user_model()._meta.pk.to_python(request.session[SESSION_KEY]) -def authenticate(**credentials): +def authenticate(request=None, **credentials): """ If the given credentials are valid, return a User object. """ for backend, backend_path in _get_backends(return_tuples=True): + args = (request,) try: - inspect.getcallargs(backend.authenticate, **credentials) + inspect.getcallargs(backend.authenticate, request, **credentials) except TypeError: - # This backend doesn't accept these credentials as arguments. Try the next one. - continue - + try: + inspect.getcallargs(backend.authenticate, **credentials) + except TypeError: + # This backend doesn't accept these credentials as arguments. Try the next one. + continue + else: + args = () + warnings.warn( + "Update authentication backend %s to accept a " + "positional `request` argument." % backend_path, + RemovedInDjango21Warning + ) try: - user = backend.authenticate(**credentials) + user = backend.authenticate(*args, **credentials) except PermissionDenied: # This backend says to stop in our tracks - this user should not be allowed in at all. break diff --git a/django/contrib/auth/backends.py b/django/contrib/auth/backends.py index f145a1929a..379f1c4450 100644 --- a/django/contrib/auth/backends.py +++ b/django/contrib/auth/backends.py @@ -9,7 +9,7 @@ class ModelBackend(object): Authenticates against settings.AUTH_USER_MODEL. """ - def authenticate(self, username=None, password=None, **kwargs): + def authenticate(self, request, username=None, password=None, **kwargs): UserModel = get_user_model() if username is None: username = kwargs.get(UserModel.USERNAME_FIELD) @@ -125,7 +125,7 @@ class RemoteUserBackend(ModelBackend): # Create a User object if not already in the database? create_unknown_user = True - def authenticate(self, remote_user): + def authenticate(self, request, remote_user): """ The username passed as ``remote_user`` is considered trusted. This method simply returns the ``User`` object with the given username, diff --git a/django/contrib/auth/forms.py b/django/contrib/auth/forms.py index ddd915ae69..7a6a8d74aa 100644 --- a/django/contrib/auth/forms.py +++ b/django/contrib/auth/forms.py @@ -189,7 +189,7 @@ class AuthenticationForm(forms.Form): password = self.cleaned_data.get('password') if username is not None and password: - self.user_cache = authenticate(username=username, password=password) + self.user_cache = authenticate(self.request, username=username, password=password) if self.user_cache is None: raise forms.ValidationError( self.error_messages['invalid_login'], diff --git a/django/contrib/auth/middleware.py b/django/contrib/auth/middleware.py index 8c56e645fe..f179894e73 100644 --- a/django/contrib/auth/middleware.py +++ b/django/contrib/auth/middleware.py @@ -88,7 +88,7 @@ class RemoteUserMiddleware(MiddlewareMixin): # We are seeing this user for the first time in this session, attempt # to authenticate the user. - user = auth.authenticate(remote_user=username) + user = auth.authenticate(request, remote_user=username) if user: # User is valid. Set request.user and persist user in the session # by logging the user in. |
