5
5
from django.shortcuts import render_to_response
6
6
from django.utils.html import escape
7
7
from django.utils.translation import gettext_lazy
8
-
import base64, datetime, md5
9
-
import cPickle as pickle
8
+
import base64, datetime
10
9
11
10
ERROR_MESSAGE = gettext_lazy("Please enter a correct username and password. Note that both fields are case-sensitive.")
12
11
LOGIN_FORM_KEY = 'this_is_the_login_form'
13
12
14
13
def _display_login_form(request, error_message=''):
15
14
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({})
24
15
return render_to_response('admin/login.html', {
25
16
'title': _('Log in'),
26
17
'app_path': escape(request.path),
27
-
'post_data': post_data,
28
18
'error_message': error_message
29
19
}, context_instance=template.RequestContext(request))
30
20
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
-
44
21
def staff_member_required(view_func):
45
22
"""
46
23
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):
49
26
def _checklogin(request, *args, **kwargs):
50
27
if request.user.is_authenticated() and request.user.is_staff:
51
28
# 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'])
56
29
return view_func(request, *args, **kwargs)
57
30
58
31
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'."
59
32
60
33
# If this isn't already the login page, display it.
61
34
if not request.POST.has_key(LOGIN_FORM_KEY):
62
35
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.")
64
37
else:
65
38
message = ""
66
39
return _display_login_form(request, message)
@@ -93,16 +66,7 @@ def _checklogin(request, *args, **kwargs):
93
66
# TODO: set last_login with an event.
94
67
user.last_login = datetime.datetime.now()
95
68
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)
106
70
else:
107
71
return _display_login_form(request, ERROR_MESSAGE)
108
72
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4