authentik.common.saml.parsers.logout_response
LogoutResponse parser
1"""LogoutResponse parser""" 2 3import binascii 4from base64 import b64decode 5from dataclasses import dataclass 6from xml.etree.ElementTree import ParseError # nosec 7 8from defusedxml import ElementTree 9from django.utils.translation import gettext_lazy as _ 10 11from authentik.common.saml.constants import NS_SAML_ASSERTION, NS_SAML_PROTOCOL, SAML_STATUS_SUCCESS 12from authentik.common.saml.exceptions import CannotHandleAssertion 13from authentik.providers.saml.utils.encoding import decode_base64_and_inflate 14 15ERROR_CANNOT_DECODE_RESPONSE = _("Cannot decode SAML response.") 16 17 18@dataclass(slots=True) 19class LogoutResponse: 20 """Logout Response""" 21 22 id: str | None = None 23 24 in_response_to: str | None = None 25 26 issuer: str | None = None 27 28 status: str | None = None 29 30 relay_state: str | None = None 31 32 33class LogoutResponseParser: 34 """LogoutResponse Parser""" 35 36 def _parse_xml( 37 self, decoded_xml: str | bytes, relay_state: str | None = None 38 ) -> LogoutResponse: 39 try: 40 root = ElementTree.fromstring(decoded_xml) 41 except (ParseError, ValueError) as exc: 42 raise CannotHandleAssertion(ERROR_CANNOT_DECODE_RESPONSE, detail=exc) from exc 43 response = LogoutResponse( 44 id=root.attrib.get("ID"), 45 in_response_to=root.attrib.get("InResponseTo"), 46 ) 47 48 # Try both namespaces for Issuer 49 issuers = root.findall(f"{{{NS_SAML_ASSERTION}}}Issuer") 50 if not issuers: 51 issuers = root.findall(f"{{{NS_SAML_PROTOCOL}}}Issuer") 52 if len(issuers) > 0: 53 response.issuer = issuers[0].text 54 55 # Extract Status 56 status_elements = root.findall(f"{{{NS_SAML_PROTOCOL}}}Status") 57 if len(status_elements) > 0: 58 status_codes = status_elements[0].findall(f"{{{NS_SAML_PROTOCOL}}}StatusCode") 59 if len(status_codes) > 0: 60 response.status = status_codes[0].attrib.get("Value") 61 62 response.relay_state = relay_state 63 return response 64 65 def parse(self, saml_response: str, relay_state: str | None = None) -> LogoutResponse: 66 """Parse raw response with enveloped signature (POST binding)""" 67 try: 68 decoded_xml = b64decode(saml_response.encode()) 69 except (UnicodeDecodeError, binascii.Error) as exc: 70 raise CannotHandleAssertion(ERROR_CANNOT_DECODE_RESPONSE, detail=exc) from None 71 return self._parse_xml(decoded_xml, relay_state) 72 73 def parse_detached(self, saml_response: str, relay_state: str | None = None) -> LogoutResponse: 74 """Parse raw response with detached signature (Redirect binding)""" 75 try: 76 decoded_xml = decode_base64_and_inflate(saml_response) 77 except (UnicodeDecodeError, binascii.Error) as exc: 78 raise CannotHandleAssertion(ERROR_CANNOT_DECODE_RESPONSE, detail=exc) from None 79 return self._parse_xml(decoded_xml, relay_state) 80 81 def verify_status(self, response: LogoutResponse) -> None: 82 """Verify that the LogoutResponse has a successful status.""" 83 if response.status != SAML_STATUS_SUCCESS: 84 raise CannotHandleAssertion(f"LogoutResponse status is not success: {response.status}")
ERROR_CANNOT_DECODE_RESPONSE =
'Cannot decode SAML response.'
@dataclass(slots=True)
class
LogoutResponse:
19@dataclass(slots=True) 20class LogoutResponse: 21 """Logout Response""" 22 23 id: str | None = None 24 25 in_response_to: str | None = None 26 27 issuer: str | None = None 28 29 status: str | None = None 30 31 relay_state: str | None = None
Logout Response
class
LogoutResponseParser:
34class LogoutResponseParser: 35 """LogoutResponse Parser""" 36 37 def _parse_xml( 38 self, decoded_xml: str | bytes, relay_state: str | None = None 39 ) -> LogoutResponse: 40 try: 41 root = ElementTree.fromstring(decoded_xml) 42 except (ParseError, ValueError) as exc: 43 raise CannotHandleAssertion(ERROR_CANNOT_DECODE_RESPONSE, detail=exc) from exc 44 response = LogoutResponse( 45 id=root.attrib.get("ID"), 46 in_response_to=root.attrib.get("InResponseTo"), 47 ) 48 49 # Try both namespaces for Issuer 50 issuers = root.findall(f"{{{NS_SAML_ASSERTION}}}Issuer") 51 if not issuers: 52 issuers = root.findall(f"{{{NS_SAML_PROTOCOL}}}Issuer") 53 if len(issuers) > 0: 54 response.issuer = issuers[0].text 55 56 # Extract Status 57 status_elements = root.findall(f"{{{NS_SAML_PROTOCOL}}}Status") 58 if len(status_elements) > 0: 59 status_codes = status_elements[0].findall(f"{{{NS_SAML_PROTOCOL}}}StatusCode") 60 if len(status_codes) > 0: 61 response.status = status_codes[0].attrib.get("Value") 62 63 response.relay_state = relay_state 64 return response 65 66 def parse(self, saml_response: str, relay_state: str | None = None) -> LogoutResponse: 67 """Parse raw response with enveloped signature (POST binding)""" 68 try: 69 decoded_xml = b64decode(saml_response.encode()) 70 except (UnicodeDecodeError, binascii.Error) as exc: 71 raise CannotHandleAssertion(ERROR_CANNOT_DECODE_RESPONSE, detail=exc) from None 72 return self._parse_xml(decoded_xml, relay_state) 73 74 def parse_detached(self, saml_response: str, relay_state: str | None = None) -> LogoutResponse: 75 """Parse raw response with detached signature (Redirect binding)""" 76 try: 77 decoded_xml = decode_base64_and_inflate(saml_response) 78 except (UnicodeDecodeError, binascii.Error) as exc: 79 raise CannotHandleAssertion(ERROR_CANNOT_DECODE_RESPONSE, detail=exc) from None 80 return self._parse_xml(decoded_xml, relay_state) 81 82 def verify_status(self, response: LogoutResponse) -> None: 83 """Verify that the LogoutResponse has a successful status.""" 84 if response.status != SAML_STATUS_SUCCESS: 85 raise CannotHandleAssertion(f"LogoutResponse status is not success: {response.status}")
LogoutResponse Parser
66 def parse(self, saml_response: str, relay_state: str | None = None) -> LogoutResponse: 67 """Parse raw response with enveloped signature (POST binding)""" 68 try: 69 decoded_xml = b64decode(saml_response.encode()) 70 except (UnicodeDecodeError, binascii.Error) as exc: 71 raise CannotHandleAssertion(ERROR_CANNOT_DECODE_RESPONSE, detail=exc) from None 72 return self._parse_xml(decoded_xml, relay_state)
Parse raw response with enveloped signature (POST binding)
74 def parse_detached(self, saml_response: str, relay_state: str | None = None) -> LogoutResponse: 75 """Parse raw response with detached signature (Redirect binding)""" 76 try: 77 decoded_xml = decode_base64_and_inflate(saml_response) 78 except (UnicodeDecodeError, binascii.Error) as exc: 79 raise CannotHandleAssertion(ERROR_CANNOT_DECODE_RESPONSE, detail=exc) from None 80 return self._parse_xml(decoded_xml, relay_state)
Parse raw response with detached signature (Redirect binding)
82 def verify_status(self, response: LogoutResponse) -> None: 83 """Verify that the LogoutResponse has a successful status.""" 84 if response.status != SAML_STATUS_SUCCESS: 85 raise CannotHandleAssertion(f"LogoutResponse status is not success: {response.status}")
Verify that the LogoutResponse has a successful status.