Skip to content

Commit 22d82a7

Browse files
committed
Fixed django#15198 -- pass request to AuthenticationForm
Thanks to Ciantic for the report, claudep and slurms for initial work
1 parent 9d2c0a0 commit 22d82a7

File tree

4 files changed

+23
-7
lines changed

4 files changed

+23
-7
lines changed

django/contrib/auth/forms.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,8 @@ class AuthenticationForm(forms.Form):
160160

161161
def __init__(self, request=None, *args, **kwargs):
162162
"""
163-
If request is passed in, the form will validate that cookies are
164-
enabled. Note that the request (a HttpRequest object) must have set a
165-
cookie with the key TEST_COOKIE_NAME and value TEST_COOKIE_VALUE before
166-
running this validation.
163+
The 'request' parameter is set for custom auth use by subclasses.
164+
The form data comes in via the standard 'data' kwarg.
167165
"""
168166
self.request = request
169167
self.user_cache = None

django/contrib/auth/tests/urls.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
from django.conf.urls import patterns, url
22
from django.contrib.auth import context_processors
3+
from django.contrib.auth.forms import AuthenticationForm
34
from django.contrib.auth.urls import urlpatterns
4-
from django.contrib.auth.views import password_reset
5+
from django.contrib.auth.views import password_reset, login
56
from django.contrib.auth.decorators import login_required
67
from django.contrib.messages.api import info
7-
from django.http import HttpResponse
8+
from django.http import HttpResponse, HttpRequest
89
from django.shortcuts import render_to_response
910
from django.template import Template, RequestContext
1011
from django.views.decorators.cache import never_cache
1112

13+
class CustomRequestAuthenticationForm(AuthenticationForm):
14+
def __init__(self, request, *args, **kwargs):
15+
assert isinstance(request, HttpRequest)
16+
super(CustomRequestAuthenticationForm, self).__init__(request, *args, **kwargs)
17+
1218
@never_cache
1319
def remote_user_auth_view(request):
1420
"Dummy view for remote user tests"
@@ -49,6 +55,9 @@ def auth_processor_messages(request):
4955
def userpage(request):
5056
pass
5157

58+
def custom_request_auth_login(request):
59+
return login(request, authentication_form=CustomRequestAuthenticationForm)
60+
5261
# special urls for auth test cases
5362
urlpatterns = urlpatterns + patterns('',
5463
(r'^logout/custom_query/$', 'django.contrib.auth.views.logout', dict(redirect_field_name='follow')),
@@ -65,6 +74,7 @@ def userpage(request):
6574
(r'^auth_processor_perms/$', auth_processor_perms),
6675
(r'^auth_processor_perm_in_perms/$', auth_processor_perm_in_perms),
6776
(r'^auth_processor_messages/$', auth_processor_messages),
77+
(r'^custom_request_auth_login/$', custom_request_auth_login),
6878
url(r'^userpage/(.+)/$', userpage, name="userpage"),
6979
)
7080

django/contrib/auth/tests/views.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,14 @@ def test_security_check(self, password='password'):
365365
self.assertTrue(good_url in response.url,
366366
"%s should be allowed" % good_url)
367367

368+
def test_login_form_contains_request(self):
369+
# 15198
370+
response = self.client.post('/custom_requestauth_login/', {
371+
'username': 'testclient',
372+
'password': 'password',
373+
}, follow=True)
374+
# the custom authentication form used by this login asserts
375+
# that a request is passed to the form successfully.
368376

369377
@skipIfCustomUser
370378
class LoginURLSettings(AuthViewsTestCase):

django/contrib/auth/views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def login(request, template_name='registration/login.html',
3535
redirect_to = request.REQUEST.get(redirect_field_name, '')
3636

3737
if request.method == "POST":
38-
form = authentication_form(data=request.POST)
38+
form = authentication_form(request, data=request.POST)
3939
if form.is_valid():
4040

4141
# Ensure the user-originating redirection url is safe.

0 commit comments

Comments
 (0)