This repository has been archived on 2024-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
authentik/passbook/providers/saml/utils/encoding.py

25 lines
818 B
Python
Raw Normal View History

"""Wrappers to de/encode and de/inflate strings"""
import base64
import zlib
2020-02-20 20:33:10 +00:00
def decode_base64_and_inflate(encoded: str, encoding="utf-8") -> str:
"""Base64 decode and ZLib decompress b64string"""
2020-02-20 20:33:10 +00:00
decoded_data = base64.b64decode(encoded)
try:
2020-02-20 20:33:10 +00:00
return zlib.decompress(decoded_data, -15).decode(encoding)
except zlib.error:
2020-02-20 20:33:10 +00:00
return decoded_data.decode(encoding)
def deflate_and_base64_encode(inflated: str, encoding="utf-8"):
"""Base64 and ZLib Compress b64string"""
zlibbed_str = zlib.compress(inflated.encode())
compressed_string = zlibbed_str[2:-4]
2020-02-20 20:33:10 +00:00
return base64.b64encode(compressed_string).decode(encoding)
def nice64(src):
"""Returns src base64-encoded and formatted nicely for our XML. """
return base64.b64encode(src).decode("utf-8").replace("\n", "")