Skip to content

Commit 161dc2d

Browse files
jgeerdstomchristie
authored andcommitted
Call Django's authenticate function with the request object (#5295)
As of Django 1.11 the `authenticate` function accepts a request as an additional argument. This commit fixes compatibility between newer Django versions and custom authentication backends which already depend on the request object. See also: [Django 1.11 release](https://docs.djangoproject.com/en/1.11/releases/1.11/) ``` authenticate() now passes a request argument to the authenticate() method of authentication backends. Support for methods that don’t accept request as the first positional argument will be removed in Django 2.1. ```
1 parent d138f30 commit 161dc2d

File tree

4 files changed

+20
-8
lines changed

4 files changed

+20
-8
lines changed

rest_framework/authentication.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66
import base64
77
import binascii
88

9-
from django.contrib.auth import authenticate, get_user_model
9+
from django.contrib.auth import get_user_model
1010
from django.middleware.csrf import CsrfViewMiddleware
1111
from django.utils.six import text_type
1212
from django.utils.translation import ugettext_lazy as _
1313

1414
from rest_framework import HTTP_HEADER_ENCODING, exceptions
15+
from rest_framework.compat import authenticate
1516

1617

1718
def get_authorization_header(request):
@@ -83,17 +84,18 @@ def authenticate(self, request):
8384
raise exceptions.AuthenticationFailed(msg)
8485

8586
userid, password = auth_parts[0], auth_parts[2]
86-
return self.authenticate_credentials(userid, password)
87+
return self.authenticate_credentials(userid, password, request)
8788

88-
def authenticate_credentials(self, userid, password):
89+
def authenticate_credentials(self, userid, password, request=None):
8990
"""
90-
Authenticate the userid and password against username and password.
91+
Authenticate the userid and password against username and password
92+
with optional request for context.
9193
"""
9294
credentials = {
9395
get_user_model().USERNAME_FIELD: userid,
9496
'password': password
9597
}
96-
user = authenticate(**credentials)
98+
user = authenticate(request=request, **credentials)
9799

98100
if user is None:
99101
raise exceptions.AuthenticationFailed(_('Invalid username/password.'))

rest_framework/authtoken/serializers.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
from django.contrib.auth import authenticate
21
from django.utils.translation import ugettext_lazy as _
32

43
from rest_framework import serializers
4+
from rest_framework.compat import authenticate
55

66

77
class AuthTokenSerializer(serializers.Serializer):
@@ -17,7 +17,8 @@ def validate(self, attrs):
1717
password = attrs.get('password')
1818

1919
if username and password:
20-
user = authenticate(username=username, password=password)
20+
user = authenticate(request=self.context.get('request'),
21+
username=username, password=password)
2122

2223
if user:
2324
# From Django 1.10 onwards the `authenticate` call simply

rest_framework/authtoken/views.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ class ObtainAuthToken(APIView):
1313
serializer_class = AuthTokenSerializer
1414

1515
def post(self, request, *args, **kwargs):
16-
serializer = self.serializer_class(data=request.data)
16+
serializer = self.serializer_class(data=request.data,
17+
context={'request': request})
1718
serializer.is_valid(raise_exception=True)
1819
user = serializer.validated_data['user']
1920
token, created = Token.objects.get_or_create(user=user)

rest_framework/compat.py

+8
Original file line numberDiff line numberDiff line change
@@ -407,3 +407,11 @@ def include(module, namespace=None, app_name=None):
407407
return include(module, namespace, app_name)
408408
else:
409409
return include((module, app_name), namespace)
410+
411+
412+
def authenticate(request=None, **credentials):
413+
from django.contrib.auth import authenticate
414+
if django.VERSION < (1, 11):
415+
return authenticate(**credentials)
416+
else:
417+
return authenticate(request=request, **credentials)

0 commit comments

Comments
 (0)