Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions authentik/providers/saml/processors/assertion.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,14 +320,6 @@ def get_assertion(self) -> Element:
ns=xmlsec.constants.DSigNs,
)
assertion.append(signature)
if self.provider.encryption_kp:
encryption = xmlsec.template.encrypted_data_create(
assertion,
xmlsec.constants.TransformAes128Cbc,
self._assertion_id,
ns=xmlsec.constants.DSigNs,
)
assertion.append(encryption)

assertion.append(self.get_assertion_subject())
assertion.append(self.get_assertion_conditions())
Expand Down Expand Up @@ -403,6 +395,13 @@ def _sign(self, element: Element):

def _encrypt(self, element: Element, parent: Element):
"""Encrypt SAMLResponse EncryptedAssertion Element"""
# Create a standalone copy so namespace declarations are included in the encrypted content
element_xml = etree.tostring(element)
standalone_element = etree.fromstring(element_xml)

# Remove the original element from the tree since we're replacing it with encrypted version
parent.remove(element)

manager = xmlsec.KeysManager()
key = xmlsec.Key.from_memory(
self.provider.encryption_kp.key_data,
Expand All @@ -429,11 +428,10 @@ def _encrypt(self, element: Element, parent: Element):
xmlsec.template.encrypted_data_ensure_cipher_value(enc_key)

try:
enc_data = encryption_context.encrypt_xml(enc_data, element)
enc_data = encryption_context.encrypt_xml(enc_data, standalone_element)
except xmlsec.Error as exc:
raise InvalidEncryption() from exc

parent.remove(enc_data)
container.append(enc_data)

def build_response(self) -> str:
Expand Down
Loading