authentik.providers.saml.views.unified
Unified SAML endpoint - handles SSO and SLO based on message type
1"""Unified SAML endpoint - handles SSO and SLO based on message type""" 2 3from base64 import b64decode 4 5from django.http import HttpRequest, HttpResponse 6from django.utils.decorators import method_decorator 7from django.utils.translation import gettext as _ 8from django.views import View 9from django.views.decorators.clickjacking import xframe_options_sameorigin 10from django.views.decorators.csrf import csrf_exempt 11from structlog.stdlib import get_logger 12 13from authentik.common.saml.constants import NS_MAP 14from authentik.flows.views.executor import SESSION_KEY_POST 15from authentik.lib.views import bad_request_message 16from authentik.lib.xml import lxml_from_string 17from authentik.providers.saml.utils.encoding import decode_base64_and_inflate 18from authentik.providers.saml.views.flows import ( 19 REQUEST_KEY_SAML_REQUEST, 20 REQUEST_KEY_SAML_RESPONSE, 21) 22from authentik.providers.saml.views.sp_slo import ( 23 SPInitiatedSLOBindingPOSTView, 24 SPInitiatedSLOBindingRedirectView, 25) 26from authentik.providers.saml.views.sso import ( 27 SAMLSSOBindingPOSTView, 28 SAMLSSOBindingRedirectView, 29) 30 31LOGGER = get_logger() 32 33# SAML message type constants 34SAML_MESSAGE_TYPE_AUTHN_REQUEST = "AuthnRequest" 35SAML_MESSAGE_TYPE_LOGOUT_REQUEST = "LogoutRequest" 36 37 38def detect_saml_message_type(saml_request: str, is_post_binding: bool) -> str | None: 39 """Parse SAML request to determine if AuthnRequest or LogoutRequest.""" 40 try: 41 if is_post_binding: 42 decoded_xml = b64decode(saml_request.encode()) 43 else: 44 decoded_xml = decode_base64_and_inflate(saml_request) 45 46 if isinstance(decoded_xml, str): 47 decoded_xml = decoded_xml.encode() 48 49 root = lxml_from_string(decoded_xml) 50 if len(root.xpath("//samlp:AuthnRequest", namespaces=NS_MAP)): 51 return SAML_MESSAGE_TYPE_AUTHN_REQUEST 52 if len(root.xpath("//samlp:LogoutRequest", namespaces=NS_MAP)): 53 return SAML_MESSAGE_TYPE_LOGOUT_REQUEST 54 return None 55 except Exception: # noqa: BLE001 56 return None 57 58 59@method_decorator(xframe_options_sameorigin, name="dispatch") 60@method_decorator(csrf_exempt, name="dispatch") 61class SAMLUnifiedView(View): 62 """Unified SAML endpoint - handles SSO and SLO based on message type. 63 64 The operation type is determined by parsing 65 the incoming SAML message: 66 - AuthnRequest -> SSO flow (delegates to SAMLSSOBindingRedirectView/POSTView) 67 - LogoutRequest -> SLO flow (delegates to SPInitiatedSLOBindingRedirectView/POSTView) 68 - LogoutResponse -> SLO completion (delegates to SPInitiatedSLOBindingRedirectView/POSTView) 69 """ 70 71 def dispatch(self, request: HttpRequest, application_slug: str) -> HttpResponse: 72 """Route the request based on SAML message type.""" 73 # ak user was not logged in, redirected to login, and is back w POST payload in session 74 if SESSION_KEY_POST in request.session: 75 return self._delegate_to_sso(request, application_slug, is_post_binding=True) 76 77 # Determine binding from HTTP method 78 is_post_binding = request.method == "POST" 79 data = request.POST if is_post_binding else request.GET 80 81 # LogoutResponse - delegate to SLO view (handles it in dispatch) 82 if REQUEST_KEY_SAML_RESPONSE in data: 83 return self._delegate_to_slo(request, application_slug, is_post_binding) 84 85 # Check for SAML request 86 if REQUEST_KEY_SAML_REQUEST not in data: 87 LOGGER.info("SAML payload missing") 88 return bad_request_message(request, _("The SAML request payload is missing.")) 89 90 # Detect message type and delegate 91 saml_request = data[REQUEST_KEY_SAML_REQUEST] 92 message_type = detect_saml_message_type(saml_request, is_post_binding) 93 94 if message_type == SAML_MESSAGE_TYPE_AUTHN_REQUEST: 95 return self._delegate_to_sso(request, application_slug, is_post_binding) 96 elif message_type == SAML_MESSAGE_TYPE_LOGOUT_REQUEST: 97 return self._delegate_to_slo(request, application_slug, is_post_binding) 98 else: 99 LOGGER.warning("Unknown SAML message type", message_type=message_type) 100 return bad_request_message( 101 request, 102 _("Unsupported SAML message type: {message_type}").format( 103 message_type=message_type or "unknown" 104 ), 105 ) 106 107 def _delegate_to_sso( 108 self, request: HttpRequest, application_slug: str, is_post_binding: bool 109 ) -> HttpResponse: 110 """Delegate to the appropriate SSO view.""" 111 if is_post_binding: 112 view = SAMLSSOBindingPOSTView.as_view() 113 else: 114 view = SAMLSSOBindingRedirectView.as_view() 115 return view(request, application_slug=application_slug) 116 117 def _delegate_to_slo( 118 self, request: HttpRequest, application_slug: str, is_post_binding: bool 119 ) -> HttpResponse: 120 """Delegate to the appropriate SLO view.""" 121 if is_post_binding: 122 view = SPInitiatedSLOBindingPOSTView.as_view() 123 else: 124 view = SPInitiatedSLOBindingRedirectView.as_view() 125 return view(request, application_slug=application_slug)
LOGGER =
<BoundLoggerLazyProxy(logger=None, wrapper_class=None, processors=None, context_class=None, initial_values={}, logger_factory_args=())>
SAML_MESSAGE_TYPE_AUTHN_REQUEST =
'AuthnRequest'
SAML_MESSAGE_TYPE_LOGOUT_REQUEST =
'LogoutRequest'
def
detect_saml_message_type(saml_request: str, is_post_binding: bool) -> str | None:
39def detect_saml_message_type(saml_request: str, is_post_binding: bool) -> str | None: 40 """Parse SAML request to determine if AuthnRequest or LogoutRequest.""" 41 try: 42 if is_post_binding: 43 decoded_xml = b64decode(saml_request.encode()) 44 else: 45 decoded_xml = decode_base64_and_inflate(saml_request) 46 47 if isinstance(decoded_xml, str): 48 decoded_xml = decoded_xml.encode() 49 50 root = lxml_from_string(decoded_xml) 51 if len(root.xpath("//samlp:AuthnRequest", namespaces=NS_MAP)): 52 return SAML_MESSAGE_TYPE_AUTHN_REQUEST 53 if len(root.xpath("//samlp:LogoutRequest", namespaces=NS_MAP)): 54 return SAML_MESSAGE_TYPE_LOGOUT_REQUEST 55 return None 56 except Exception: # noqa: BLE001 57 return None
Parse SAML request to determine if AuthnRequest or LogoutRequest.
@method_decorator(xframe_options_sameorigin, name='dispatch')
@method_decorator(csrf_exempt, name='dispatch')
class
SAMLUnifiedView60@method_decorator(xframe_options_sameorigin, name="dispatch") 61@method_decorator(csrf_exempt, name="dispatch") 62class SAMLUnifiedView(View): 63 """Unified SAML endpoint - handles SSO and SLO based on message type. 64 65 The operation type is determined by parsing 66 the incoming SAML message: 67 - AuthnRequest -> SSO flow (delegates to SAMLSSOBindingRedirectView/POSTView) 68 - LogoutRequest -> SLO flow (delegates to SPInitiatedSLOBindingRedirectView/POSTView) 69 - LogoutResponse -> SLO completion (delegates to SPInitiatedSLOBindingRedirectView/POSTView) 70 """ 71 72 def dispatch(self, request: HttpRequest, application_slug: str) -> HttpResponse: 73 """Route the request based on SAML message type.""" 74 # ak user was not logged in, redirected to login, and is back w POST payload in session 75 if SESSION_KEY_POST in request.session: 76 return self._delegate_to_sso(request, application_slug, is_post_binding=True) 77 78 # Determine binding from HTTP method 79 is_post_binding = request.method == "POST" 80 data = request.POST if is_post_binding else request.GET 81 82 # LogoutResponse - delegate to SLO view (handles it in dispatch) 83 if REQUEST_KEY_SAML_RESPONSE in data: 84 return self._delegate_to_slo(request, application_slug, is_post_binding) 85 86 # Check for SAML request 87 if REQUEST_KEY_SAML_REQUEST not in data: 88 LOGGER.info("SAML payload missing") 89 return bad_request_message(request, _("The SAML request payload is missing.")) 90 91 # Detect message type and delegate 92 saml_request = data[REQUEST_KEY_SAML_REQUEST] 93 message_type = detect_saml_message_type(saml_request, is_post_binding) 94 95 if message_type == SAML_MESSAGE_TYPE_AUTHN_REQUEST: 96 return self._delegate_to_sso(request, application_slug, is_post_binding) 97 elif message_type == SAML_MESSAGE_TYPE_LOGOUT_REQUEST: 98 return self._delegate_to_slo(request, application_slug, is_post_binding) 99 else: 100 LOGGER.warning("Unknown SAML message type", message_type=message_type) 101 return bad_request_message( 102 request, 103 _("Unsupported SAML message type: {message_type}").format( 104 message_type=message_type or "unknown" 105 ), 106 ) 107 108 def _delegate_to_sso( 109 self, request: HttpRequest, application_slug: str, is_post_binding: bool 110 ) -> HttpResponse: 111 """Delegate to the appropriate SSO view.""" 112 if is_post_binding: 113 view = SAMLSSOBindingPOSTView.as_view() 114 else: 115 view = SAMLSSOBindingRedirectView.as_view() 116 return view(request, application_slug=application_slug) 117 118 def _delegate_to_slo( 119 self, request: HttpRequest, application_slug: str, is_post_binding: bool 120 ) -> HttpResponse: 121 """Delegate to the appropriate SLO view.""" 122 if is_post_binding: 123 view = SPInitiatedSLOBindingPOSTView.as_view() 124 else: 125 view = SPInitiatedSLOBindingRedirectView.as_view() 126 return view(request, application_slug=application_slug)
Unified SAML endpoint - handles SSO and SLO based on message type.
The operation type is determined by parsing the incoming SAML message:
- AuthnRequest -> SSO flow (delegates to SAMLSSOBindingRedirectView/POSTView)
- LogoutRequest -> SLO flow (delegates to SPInitiatedSLOBindingRedirectView/POSTView)
- LogoutResponse -> SLO completion (delegates to SPInitiatedSLOBindingRedirectView/POSTView)
def
dispatch( self, request: django.http.request.HttpRequest, application_slug: str) -> django.http.response.HttpResponse:
72 def dispatch(self, request: HttpRequest, application_slug: str) -> HttpResponse: 73 """Route the request based on SAML message type.""" 74 # ak user was not logged in, redirected to login, and is back w POST payload in session 75 if SESSION_KEY_POST in request.session: 76 return self._delegate_to_sso(request, application_slug, is_post_binding=True) 77 78 # Determine binding from HTTP method 79 is_post_binding = request.method == "POST" 80 data = request.POST if is_post_binding else request.GET 81 82 # LogoutResponse - delegate to SLO view (handles it in dispatch) 83 if REQUEST_KEY_SAML_RESPONSE in data: 84 return self._delegate_to_slo(request, application_slug, is_post_binding) 85 86 # Check for SAML request 87 if REQUEST_KEY_SAML_REQUEST not in data: 88 LOGGER.info("SAML payload missing") 89 return bad_request_message(request, _("The SAML request payload is missing.")) 90 91 # Detect message type and delegate 92 saml_request = data[REQUEST_KEY_SAML_REQUEST] 93 message_type = detect_saml_message_type(saml_request, is_post_binding) 94 95 if message_type == SAML_MESSAGE_TYPE_AUTHN_REQUEST: 96 return self._delegate_to_sso(request, application_slug, is_post_binding) 97 elif message_type == SAML_MESSAGE_TYPE_LOGOUT_REQUEST: 98 return self._delegate_to_slo(request, application_slug, is_post_binding) 99 else: 100 LOGGER.warning("Unknown SAML message type", message_type=message_type) 101 return bad_request_message( 102 request, 103 _("Unsupported SAML message type: {message_type}").format( 104 message_type=message_type or "unknown" 105 ), 106 )
Route the request based on SAML message type.