summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
authorTim Graham <timograham@gmail.com>2015-02-11 14:38:52 -0500
committerTim Graham <timograham@gmail.com>2015-02-12 07:38:16 -0500
commit0f7f5bc9e7a94ab91c2b3db29ef7cf000eff593f (patch)
tree17168b2dcd85eab4a78cba0596ee2120f889baab /django
parent93b3ef9b2e191101c1a49b332d042864df74a658 (diff)
Fixed #24161 -- Stored the user primary key as a serialized value in the session.
This allows using a UUIDField primary key along with the JSON session serializer. Thanks to Trac alias jamesbeith for the report and Simon Charette for the initial patch.
Diffstat (limited to 'django')
-rw-r--r--django/contrib/auth/__init__.py12
1 files changed, 9 insertions, 3 deletions
diff --git a/django/contrib/auth/__init__.py b/django/contrib/auth/__init__.py
index a9eb41a609..be0ab52761 100644
--- a/django/contrib/auth/__init__.py
+++ b/django/contrib/auth/__init__.py
@@ -53,6 +53,12 @@ def _clean_credentials(credentials):
return credentials
+def _get_user_session_key(request):
+ # This value in the session is always serialized to a string, so we need
+ # to convert it back to Python whenever we access it.
+ return get_user_model()._meta.pk.to_python(request.session[SESSION_KEY])
+
+
def authenticate(**credentials):
"""
If the given credentials are valid, return a User object.
@@ -93,7 +99,7 @@ def login(request, user):
session_auth_hash = user.get_session_auth_hash()
if SESSION_KEY in request.session:
- if request.session[SESSION_KEY] != user.pk or (
+ if _get_user_session_key(request) != user.pk or (
session_auth_hash and
request.session.get(HASH_SESSION_KEY) != session_auth_hash):
# To avoid reusing another user's session, create a new, empty
@@ -102,7 +108,7 @@ def login(request, user):
request.session.flush()
else:
request.session.cycle_key()
- request.session[SESSION_KEY] = user.pk
+ request.session[SESSION_KEY] = user._meta.pk.value_to_string(user)
request.session[BACKEND_SESSION_KEY] = user.backend
request.session[HASH_SESSION_KEY] = session_auth_hash
if hasattr(request, 'user'):
@@ -158,7 +164,7 @@ def get_user(request):
from .models import AnonymousUser
user = None
try:
- user_id = request.session[SESSION_KEY]
+ user_id = _get_user_session_key(request)
backend_path = request.session[BACKEND_SESSION_KEY]
except KeyError:
pass