Skip to content

Instantly share code, notes, and snippets.

@atuchak
Created April 12, 2021 09:58
Show Gist options
  • Save atuchak/d712b618cf68b524930500a8d16418dc to your computer and use it in GitHub Desktop.
Save atuchak/d712b618cf68b524930500a8d16418dc to your computer and use it in GitHub Desktop.
import base64
import zlib
def b64_decode(s):
pad = b'=' * (-len(s) % 4)
return base64.urlsafe_b64decode(s + pad)
def decode_session_data(session_data: str):
session_data = session_data.encode()
is_compressed = True if session_data.startswith(b'.') else False
decoded_data = b64_decode(session_data[1:])
if is_compressed:
return zlib.decompress(decoded_data)
return decoded_data
def test_decode_session_data():
test_session_data = '.eJxVjkEOwiAQRe_CWpoCA-O404s0wxTSxgYTS1fGu0tNF7qcP__9vJcaeKvTsK3pOcyjuihAQuIImtiCBomoCa3ohBYsc4gmoTr9YpHlnsrO8rLscccij63U7ts53mt3bVcqdRau86PcDupvauJ12h2Ck8xifbBNxoyAeSQmIxYEjHcMzhN5Hykm7lvcnHwfzrlnzMROvT_zl0Tb:1lRz1Z:859enAty6xE12vX17o0fySraDDrP1fo2Sgu1lalUmN'
expected_result = b'{"_auth_user_id":"47979ab4-9a24-4cb7-972c-e7242aa6b1e7","_auth_user_backend":"allauth.account.auth_backends.AuthenticationBackend","_auth_user_hash":"463cfac25629791d47fd9a91c24c4153a4359955b9bea01c21e75068f0a7f9a3"}'
assert decode_session_data(test_session_data) == expected_result
test_decode_session_data()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment