Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def decode_flask_cookie(cookie):
"""Decode a Flask cookie."""
data = cookie.split('.')[0]
data = base64_decode(data)
data = zlib.decompress(data)
return data.decode('utf-8')
def session_cookie_decoder(session_cookie_value, secret_key=None):
""" Decode a Flask cookie """
try:
if(secret_key==None):
compressed = False
payload = session_cookie_value
if payload.startswith(b'.'):
compressed = True
payload = payload[1:]
data = payload.split(".")[0]
data = base64_decode(data)
if compressed:
data = zlib.decompress(data)
return data
else:
app = MockApp(secret_key)
si = SecureCookieSessionInterface()
s = si.get_signing_serializer(app)
return s.loads(session_cookie_value)
except Exception as e:
return "[Decoding error]{}".format(e)
auth_token_header_name = app_config.get('AUTH_TOKEN_HEADER_NAME',
AUTH_TOKEN_HEADER_NAME)
if auth_token_header_name:
token = request.headers.get(auth_token_header_name)
if not auth_header and not token:
raise Exception('Failed to get authentication information from '
'request, headers not found: {0}, {1}'
.format(auth_header_name, auth_token_header_name))
if auth_header:
auth_header = auth_header.replace('Basic ', '', 1)
print '----- GOT AUTH_HEADER: ', auth_header
try:
from itsdangerous import base64_decode
api_key = base64_decode(auth_header)
# TODO parse better, with checks and all, this is shaky
except TypeError:
pass
else:
api_key_parts = api_key.split(':')
user_id = api_key_parts[0]
password = api_key_parts[1]
auth_info = namedtuple('auth_info_type',
['user_id', 'password', 'token'])
return auth_info(user_id, password, token)
def decode(session_cookie_value, secret_key=None):
""" Decode a Flask cookie """
try:
if(secret_key==None):
compressed = False
payload = session_cookie_value
if payload.startswith('.'):
compressed = True
payload = payload[1:]
data = payload.split(".")[0]
data = base64_decode(data)
if compressed:
data = zlib.decompress(data)
return data
else:
app = MockApp(secret_key)
si = SecureCookieSessionInterface()
s = si.get_signing_serializer(app)
return s.loads(session_cookie_value)
except Exception as e:
return "[Decoding error] {}".format(e)
raise e
else: # > 3.4
def decode(cls, session_cookie_value, secret_key=None):
""" Decode a Flask cookie """
try:
if(secret_key==None):
compressed = False
payload = session_cookie_value
if payload.startswith('.'):
compressed = True
payload = payload[1:]
data = payload.split(".")[0]
data = base64_decode(data)
if compressed:
data = zlib.decompress(data)
return data
else:
app = MockApp(secret_key)
si = SecureCookieSessionInterface()
s = si.get_signing_serializer(app)
return s.loads(session_cookie_value)
except Exception as e:
return "[Decoding error] {}".format(e)
raise e
def decode(self, cookie):
if self.session_serializer is None:
payload = cookie
if payload.startswith('.'):
self.compressed = True
payload = payload[1:]
data = payload.split(".")[0]
data = base64_decode(data)
if self.compressed:
data = zlib.decompress(data)
return json.loads(data.decode("utf-8"))
else:
return self.session_serializer.loads(cookie)
def decryption(payload):
payload, sig = payload.rsplit(b'.', 1)
payload, timestamp = payload.rsplit(b'.', 1)
decompress = False
if payload.startswith(b'.'):
payload = payload[1:]
decompress = True
try:
payload = base64_decode(payload)
except Exception as e:
raise Exception('Could not base64 decode the payload because of '
'an exception')
if decompress:
try:
payload = zlib.decompress(payload)
except Exception as e:
raise Exception('Could not zlib decompress the payload before '
'decoding the payload')
return session_json_serializer.loads(payload)