how to find user id from session_data from django_session table?

Ketan Yekale picture Ketan Yekale · Nov 4, 2010 · Viewed 11.2k times · Source

In django_session table session_data is stored which is first pickled using pickle module of python and then encoded in base64 by using base64 module of python.

I got the decoded pickled session_data.

session_data from django_session table:

gAJ9cQEoVQ9fc2Vzc2lvbl9leHBpcnlxAksAVRJfYXV0aF91c2VyX2JhY2tlbmRxA1UpZGphbmdvLmNvbnRyaWIuYXV0aC5iYWNrZW5kcy5Nb2RlbEJhY2tlbmRxBFUNX2F1dGhfdXNlcl9pZHEFigECdS5iZmUwOWExOWI0YTZkN2M0NDc2MWVjZjQ5ZDU0YjNhZA==

after decoding it by base64.decode(session_data):

 \x80\x02}q\x01(U\x0f_session_expiryq\x02K\x00U\x12_auth_user_backendq\x03U)django.contrib.auth.backends.ModelBackendq\x04U\r_auth_user_idq\x05\x8a\x01\x02u.bfe09a19b4a6d7c44761ecf49d54b3ad

I want to find out the value of auth_user_id from auth_user_idq\x05\x8a\x01\x02u . Please help me to do so.

Answer

Dolan Antenucci picture Dolan Antenucci · Aug 21, 2012

I had trouble with Paulo's method (see my comment on his answer), so I ended up using this method from a scottbarnham.com blog post:

from django.contrib.sessions.models import Session
from django.contrib.auth.models import User

session_key = '8cae76c505f15432b48c8292a7dd0e54'

session = Session.objects.get(session_key=session_key)
uid = session.get_decoded().get('_auth_user_id')
user = User.objects.get(pk=uid)

print user.username, user.get_full_name(), user.email