From bb002262acef03be4d17f050fdc7157e89dbd5ba Mon Sep 17 00:00:00 2001 From: Petros Moisiadis Date: Tue, 19 May 2015 17:42:44 +0300 Subject: [PATCH] Support basic authentication with custom user models that change username field Support basic authentication with custom user models with a username field that is not named 'username'. --- rest_framework/authentication.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/rest_framework/authentication.py b/rest_framework/authentication.py index f0702286c..b03e7c167 100644 --- a/rest_framework/authentication.py +++ b/rest_framework/authentication.py @@ -4,6 +4,7 @@ Provides various authentication policies. from __future__ import unicode_literals import base64 from django.contrib.auth import authenticate +from django.contrib.auth import get_user_model from django.middleware.csrf import CsrfViewMiddleware from django.utils.translation import ugettext_lazy as _ from rest_framework import exceptions, HTTP_HEADER_ENCODING @@ -85,7 +86,11 @@ class BasicAuthentication(BaseAuthentication): """ Authenticate the userid and password against username and password. """ - user = authenticate(username=userid, password=password) + credentials = { + get_user_model().USERNAME_FIELD: userid, + 'password': password + } + user = authenticate(**credentials) if user is None: raise exceptions.AuthenticationFailed(_('Invalid username/password.'))