55from django .shortcuts import render_to_response
66from django .utils .html import escape
77from django .utils .translation import gettext_lazy
8- import base64 , datetime , md5
9- import cPickle as pickle
8+ import base64 , datetime
109
1110ERROR_MESSAGE = gettext_lazy ("Please enter a correct username and password. Note that both fields are case-sensitive." )
1211LOGIN_FORM_KEY = 'this_is_the_login_form'
1312
1413def _display_login_form (request , error_message = '' ):
1514 request .session .set_test_cookie ()
16- if request .POST and request .POST .has_key ('post_data' ):
17- # User has failed login BUT has previously saved post data.
18- post_data = request .POST ['post_data' ]
19- elif request .POST :
20- # User's session must have expired; save their post data.
21- post_data = _encode_post_data (request .POST )
22- else :
23- post_data = _encode_post_data ({})
2415 return render_to_response ('admin/login.html' , {
2516 'title' : _ ('Log in' ),
2617 'app_path' : escape (request .path ),
27- 'post_data' : post_data ,
2818 'error_message' : error_message
2919 }, context_instance = template .RequestContext (request ))
3020
31- def _encode_post_data (post_data ):
32- pickled = pickle .dumps (post_data )
33- pickled_md5 = md5 .new (pickled + settings .SECRET_KEY ).hexdigest ()
34- return base64 .encodestring (pickled + pickled_md5 )
35-
36- def _decode_post_data (encoded_data ):
37- encoded_data = base64 .decodestring (encoded_data )
38- pickled , tamper_check = encoded_data [:- 32 ], encoded_data [- 32 :]
39- if md5 .new (pickled + settings .SECRET_KEY ).hexdigest () != tamper_check :
40- from django .core .exceptions import SuspiciousOperation
41- raise SuspiciousOperation , "User may have tampered with session cookie."
42- return pickle .loads (pickled )
43-
4421def staff_member_required (view_func ):
4522 """
4623 Decorator for views that checks that the user is logged in and is a staff
@@ -49,18 +26,14 @@ def staff_member_required(view_func):
4926 def _checklogin (request , * args , ** kwargs ):
5027 if request .user .is_authenticated () and request .user .is_staff :
5128 # The user is valid. Continue to the admin page.
52- if request .POST .has_key ('post_data' ):
53- # User must have re-authenticated through a different window
54- # or tab.
55- request .POST = _decode_post_data (request .POST ['post_data' ])
5629 return view_func (request , * args , ** kwargs )
5730
5831 assert hasattr (request , 'session' ), "The Django admin requires session middleware to be installed. Edit your MIDDLEWARE_CLASSES setting to insert 'django.contrib.sessions.middleware.SessionMiddleware'."
5932
6033 # If this isn't already the login page, display it.
6134 if not request .POST .has_key (LOGIN_FORM_KEY ):
6235 if request .POST :
63- message = _ ("Please log in again, because your session has expired. Don't worry: Your submission has been saved. " )
36+ message = _ ("Please log in again, because your session has expired." )
6437 else :
6538 message = ""
6639 return _display_login_form (request , message )
@@ -93,16 +66,7 @@ def _checklogin(request, *args, **kwargs):
9366 # TODO: set last_login with an event.
9467 user .last_login = datetime .datetime .now ()
9568 user .save ()
96- if request .POST .has_key ('post_data' ):
97- post_data = _decode_post_data (request .POST ['post_data' ])
98- if post_data and not post_data .has_key (LOGIN_FORM_KEY ):
99- # overwrite request.POST with the saved post_data, and continue
100- request .POST = post_data
101- request .user = user
102- return view_func (request , * args , ** kwargs )
103- else :
104- request .session .delete_test_cookie ()
105- return http .HttpResponseRedirect (request .path )
69+ return http .HttpResponseRedirect (request .path )
10670 else :
10771 return _display_login_form (request , ERROR_MESSAGE )
10872
0 commit comments