简体   繁体   中英

Decode sessiondata of Django user which is encoded into base64 in Lua

I am using redis with Lua to fetch sessiondata of any admin from Django project .In Django project sessiondata is encoded into base64 form.
sessiondata value is :

session_data = "NzlmZjZmNWQxMGIzNTQzMDZhNDZjNzJiZGQ4OWZiY2NjNDg0NDVlZTqAAn1xAShVEl9hdXRoX3VzZXJfYmFja2VuZHECVSlkamFuZ28uY29udHJpYi5hdXRoLmJhY2tlbmRzLk1vZGVsQmFja2VuZHEDVQ1fYXV0aF91c2VyX2lkcQSKAgEKdS4="
My Lua code to decode session data is

output = 79ff6f5d10b354306a46c72bdd89fbccc48445ee:�}q(U_auth_user_backendqU)django.contrib.auth.backends.ModelBackendqU _auth_user_idq� u.

when I am running print(dec(session_data)) I am getting

{'_auth_user_id': 2561L, '_auth_user_backend': 'django.contrib.auth.backends.ModelBackend'}

while output suppose to be

\n{'_auth_user_id': 2561L, '_auth_user_backend': 'django.contrib.auth.backends.ModelBackend'} \n
like Django we had.

Please let me know what I am doing wrong .

A quick google search yields this blog post .

This is the (abridged, nonverifying) python code he has to do the decoding of sessiondata:

def decode(session_data, secret_key, class_name='SessionStore'):
    encoded_data = base64.b64decode(session_data)
    utoken, pickled = encoded_data.split(b':', 1)
    return pickle.loads(pickled)

In other words, your expectations are wrong. The un-base64-ed data contains a checksum hash ( 79ff6...445ee ) followed by a : followed by serialized (via pickle) python data ( }q(U_auth..._user_idq u. ).

If you really want to understand how to decode python picked data, see PEP 307 .

如果您使用的是django 1.5.3+,则可以使用json序列化程序,这样您就不必尝试在lua中解码python泡菜了;)django 1.6+的默认Json序列化程序。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM