authentik.providers.saml.utils.encoding
Wrappers to de/encode and de/inflate strings
1"""Wrappers to de/encode and de/inflate strings""" 2 3import base64 4import zlib 5 6from structlog.stdlib import get_logger 7 8from authentik.common.saml.exceptions import CannotHandleAssertion 9 10LOGGER = get_logger() 11MAX_INFLATED_SIZE_BYTES = 1 * 1024 * 1024 12 13 14def decode_base64_and_inflate(encoded: str, encoding="utf-8") -> str: 15 """Base64 decode and ZLib decompress b64string""" 16 decoded_data = base64.b64decode(encoded) 17 decompressor = zlib.decompressobj(-zlib.MAX_WBITS) # raw deflate 18 try: 19 inflated = decompressor.decompress(decoded_data, MAX_INFLATED_SIZE_BYTES + 1) 20 except zlib.error: 21 return decoded_data.decode(encoding) 22 if len(inflated) > MAX_INFLATED_SIZE_BYTES: 23 raise CannotHandleAssertion( 24 f"Inflated SAML message exceeds {MAX_INFLATED_SIZE_BYTES} bytes" 25 ) 26 if not decompressor.eof: 27 LOGGER.warning("Incomplete deflate stream in SAML message") 28 return decoded_data.decode(encoding) 29 return inflated.decode(encoding) 30 31 32def deflate_and_base64_encode(inflated: str, encoding="utf-8"): 33 """Base64 and ZLib Compress b64string""" 34 zlibbed_str = zlib.compress(inflated.encode()) 35 compressed_string = zlibbed_str[2:-4] 36 return base64.b64encode(compressed_string).decode(encoding) 37 38 39def nice64(src: str) -> str: 40 """Returns src base64-encoded and formatted nicely for our XML.""" 41 return base64.b64encode(src.encode()).decode("utf-8").replace("\n", "")
LOGGER =
<BoundLoggerLazyProxy(logger=None, wrapper_class=None, processors=None, context_class=None, initial_values={}, logger_factory_args=())>
MAX_INFLATED_SIZE_BYTES =
1048576
def
decode_base64_and_inflate(encoded: str, encoding='utf-8') -> str:
15def decode_base64_and_inflate(encoded: str, encoding="utf-8") -> str: 16 """Base64 decode and ZLib decompress b64string""" 17 decoded_data = base64.b64decode(encoded) 18 decompressor = zlib.decompressobj(-zlib.MAX_WBITS) # raw deflate 19 try: 20 inflated = decompressor.decompress(decoded_data, MAX_INFLATED_SIZE_BYTES + 1) 21 except zlib.error: 22 return decoded_data.decode(encoding) 23 if len(inflated) > MAX_INFLATED_SIZE_BYTES: 24 raise CannotHandleAssertion( 25 f"Inflated SAML message exceeds {MAX_INFLATED_SIZE_BYTES} bytes" 26 ) 27 if not decompressor.eof: 28 LOGGER.warning("Incomplete deflate stream in SAML message") 29 return decoded_data.decode(encoding) 30 return inflated.decode(encoding)
Base64 decode and ZLib decompress b64string
def
deflate_and_base64_encode(inflated: str, encoding='utf-8'):
33def deflate_and_base64_encode(inflated: str, encoding="utf-8"): 34 """Base64 and ZLib Compress b64string""" 35 zlibbed_str = zlib.compress(inflated.encode()) 36 compressed_string = zlibbed_str[2:-4] 37 return base64.b64encode(compressed_string).decode(encoding)
Base64 and ZLib Compress b64string
def
nice64(src: str) -> str:
40def nice64(src: str) -> str: 41 """Returns src base64-encoded and formatted nicely for our XML.""" 42 return base64.b64encode(src.encode()).decode("utf-8").replace("\n", "")
Returns src base64-encoded and formatted nicely for our XML.