authentik.common.saml.utils

Shared SAML XML helpers

 1"""Shared SAML XML helpers"""
 2
 3import base64
 4from collections.abc import Iterator
 5from typing import cast
 6
 7from cryptography.hazmat.primitives.serialization import Encoding
 8from cryptography.x509 import Certificate
 9from lxml.etree import _Element
10
11
12def get_element_text(element: _Element) -> str:
13    """Return the full text content of an XML element.
14
15    ``Element.text`` only returns the text up to the first child node, so a value
16    containing an XML comment (for example ``admin<!--x-->_user``) is silently
17    truncated to ``admin``. Comment-excluding signature canonicalization
18    (``xml-exc-c14n``) strips those comments before the digest is computed, so a
19    signed value can be truncated after signing while keeping the signature valid.
20    Reading the text via ``itertext()`` keeps the value equal to what was signed.
21    """
22    return "".join(cast(Iterator[str], element.itertext()))
23
24
25def x509_certificate_b64(cert: Certificate) -> str:
26    """Encode a single certificate as base64 DER, the format used by ds:X509Certificate.
27
28    Encodes the parsed certificate, so a keypair holding a chain only yields the leaf."""
29    return base64.b64encode(cert.public_bytes(Encoding.DER)).decode()
def get_element_text(element: lxml.etree._Element) -> str:
13def get_element_text(element: _Element) -> str:
14    """Return the full text content of an XML element.
15
16    ``Element.text`` only returns the text up to the first child node, so a value
17    containing an XML comment (for example ``admin<!--x-->_user``) is silently
18    truncated to ``admin``. Comment-excluding signature canonicalization
19    (``xml-exc-c14n``) strips those comments before the digest is computed, so a
20    signed value can be truncated after signing while keeping the signature valid.
21    Reading the text via ``itertext()`` keeps the value equal to what was signed.
22    """
23    return "".join(cast(Iterator[str], element.itertext()))

Return the full text content of an XML element.

Element.text only returns the text up to the first child node, so a value containing an XML comment (for example admin<!--x-->_user) is silently truncated to admin. Comment-excluding signature canonicalization (xml-exc-c14n) strips those comments before the digest is computed, so a signed value can be truncated after signing while keeping the signature valid. Reading the text via itertext() keeps the value equal to what was signed.

def x509_certificate_b64(cert: cryptography.hazmat.bindings._rust.x509.Certificate) -> str:
26def x509_certificate_b64(cert: Certificate) -> str:
27    """Encode a single certificate as base64 DER, the format used by ds:X509Certificate.
28
29    Encodes the parsed certificate, so a keypair holding a chain only yields the leaf."""
30    return base64.b64encode(cert.public_bytes(Encoding.DER)).decode()

Encode a single certificate as base64 DER, the format used by ds:X509Certificate.

Encodes the parsed certificate, so a keypair holding a chain only yields the leaf.