authentik.flows.views.interface
Interface views
1"""Interface views""" 2 3from collections import defaultdict 4from typing import Any 5 6from django.shortcuts import get_object_or_404 7from ua_parser.user_agent_parser import Parse 8 9from authentik.core.views.interface import InterfaceView 10from authentik.flows.models import Flow 11from authentik.flows.stage import format_flow_title 12from authentik.flows.views.executor import SESSION_KEY_PLAN 13 14# Oldest WebKit that can parse the default flow executor bundle. Earlier versions 15# fail to parse it and render an empty page, so they are sent to the simplified 16# flow executor instead. 17MIN_WEBKIT_VERSION = (16, 4) 18MIN_EDGE_VERSION = (19,) 19# Based off of https://web-platform-dx.github.io/supported-browsers/?target=widelyAvailable 20# We don't know the exact version of chrome which is too old to render the standard flow executor 21# instead we base it off of the baseline 22MIN_CHROME_VERSION = (123, 0) 23 24 25def version_below(version: dict[str, Any], minimum: tuple[int, int]) -> bool: 26 """Check whether a parsed major/minor version is below minimum.""" 27 try: 28 major = int(version["major"]) 29 minor = int(version["minor"] or 0) 30 except KeyError, TypeError, ValueError: 31 return False 32 return (major, minor) < minimum 33 34 35class FlowInterfaceView(InterfaceView): 36 """Flow interface""" 37 38 def get_formatted_flow_title(self, flow: Flow) -> str: 39 if "%(" not in flow.title: 40 return flow.title 41 if plan := self.request.session.get(SESSION_KEY_PLAN): 42 return format_flow_title(plan, flow, self.request) 43 # Fallback if we don't have a flow plan yet, format the title with an empty defaultdict 44 # Only format titles with named placeholders, as a bare "% s" would 45 # otherwise inline the dict's repr 46 try: 47 return (flow.title % defaultdict(str)).strip() 48 except TypeError, ValueError: 49 # If title formatting is invalid, keep the original title unchanged. 50 pass 51 return flow.title 52 53 def get_context_data(self, **kwargs: Any) -> dict[str, Any]: 54 flow = get_object_or_404(Flow, slug=self.kwargs.get("flow_slug")) 55 kwargs["flow"] = flow 56 kwargs["flow_title"] = self.get_formatted_flow_title(flow) 57 kwargs["flow_background_url"] = flow.background_url(self.request) 58 kwargs["flow_background_themed_urls"] = flow.background_themed_urls(self.request) 59 kwargs["inspector"] = "inspector" in self.request.GET 60 return super().get_context_data(**kwargs) 61 62 def compat_needs_sfe(self) -> bool: 63 """Check if we need to use the simplified flow executor for compatibility""" 64 ua = Parse(self.request.META.get("HTTP_USER_AGENT", "")) 65 if ua["user_agent"]["family"] == "IE": 66 return True 67 # Only use SFE for Edge 18 and older, after Edge 18 MS switched to chromium which supports 68 # the default flow executor 69 if ua["user_agent"]["family"] == "Edge" and version_below( 70 ua["user_agent"], MIN_EDGE_VERSION 71 ): 72 return True 73 # https://github.com/AzureAD/microsoft-authentication-library-for-objc 74 # Used by Microsoft Teams/Office on macOS, and also uses a very outdated browser engine 75 if "PKeyAuth" in ua["string"]: 76 return True 77 # Every browser on iOS renders through the system WebKit, so the iOS version 78 # decides whether the bundle can be parsed regardless of which browser the 79 # user opened. 80 if ua["os"]["family"] == "iOS" and version_below(ua["os"], MIN_WEBKIT_VERSION): 81 return True 82 # iPadOS in desktop mode reports itself as Safari on macOS, which the check 83 # above does not cover. 84 if ua["user_agent"]["family"] in ("Safari", "Mobile Safari") and version_below( 85 ua["user_agent"], MIN_WEBKIT_VERSION 86 ): 87 return True 88 # Chrome versions behind the baseline get the SFE too, at least one reported case 89 # of the default flow executor not working in chrome 83 90 if ua["user_agent"]["family"] in ("Chrome", "Chrome Mobile") and version_below( 91 ua["user_agent"], MIN_CHROME_VERSION 92 ): 93 return True 94 return False 95 96 def get_template_names(self) -> list[str]: 97 if self.compat_needs_sfe() or "sfe" in self.request.GET: 98 return ["if/flow-sfe.html"] 99 return ["if/flow.html"]
MIN_WEBKIT_VERSION =
(16, 4)
MIN_EDGE_VERSION =
(19,)
MIN_CHROME_VERSION =
(123, 0)
def
version_below(version: dict[str, typing.Any], minimum: tuple[int, int]) -> bool:
26def version_below(version: dict[str, Any], minimum: tuple[int, int]) -> bool: 27 """Check whether a parsed major/minor version is below minimum.""" 28 try: 29 major = int(version["major"]) 30 minor = int(version["minor"] or 0) 31 except KeyError, TypeError, ValueError: 32 return False 33 return (major, minor) < minimum
Check whether a parsed major/minor version is below minimum.
36class FlowInterfaceView(InterfaceView): 37 """Flow interface""" 38 39 def get_formatted_flow_title(self, flow: Flow) -> str: 40 if "%(" not in flow.title: 41 return flow.title 42 if plan := self.request.session.get(SESSION_KEY_PLAN): 43 return format_flow_title(plan, flow, self.request) 44 # Fallback if we don't have a flow plan yet, format the title with an empty defaultdict 45 # Only format titles with named placeholders, as a bare "% s" would 46 # otherwise inline the dict's repr 47 try: 48 return (flow.title % defaultdict(str)).strip() 49 except TypeError, ValueError: 50 # If title formatting is invalid, keep the original title unchanged. 51 pass 52 return flow.title 53 54 def get_context_data(self, **kwargs: Any) -> dict[str, Any]: 55 flow = get_object_or_404(Flow, slug=self.kwargs.get("flow_slug")) 56 kwargs["flow"] = flow 57 kwargs["flow_title"] = self.get_formatted_flow_title(flow) 58 kwargs["flow_background_url"] = flow.background_url(self.request) 59 kwargs["flow_background_themed_urls"] = flow.background_themed_urls(self.request) 60 kwargs["inspector"] = "inspector" in self.request.GET 61 return super().get_context_data(**kwargs) 62 63 def compat_needs_sfe(self) -> bool: 64 """Check if we need to use the simplified flow executor for compatibility""" 65 ua = Parse(self.request.META.get("HTTP_USER_AGENT", "")) 66 if ua["user_agent"]["family"] == "IE": 67 return True 68 # Only use SFE for Edge 18 and older, after Edge 18 MS switched to chromium which supports 69 # the default flow executor 70 if ua["user_agent"]["family"] == "Edge" and version_below( 71 ua["user_agent"], MIN_EDGE_VERSION 72 ): 73 return True 74 # https://github.com/AzureAD/microsoft-authentication-library-for-objc 75 # Used by Microsoft Teams/Office on macOS, and also uses a very outdated browser engine 76 if "PKeyAuth" in ua["string"]: 77 return True 78 # Every browser on iOS renders through the system WebKit, so the iOS version 79 # decides whether the bundle can be parsed regardless of which browser the 80 # user opened. 81 if ua["os"]["family"] == "iOS" and version_below(ua["os"], MIN_WEBKIT_VERSION): 82 return True 83 # iPadOS in desktop mode reports itself as Safari on macOS, which the check 84 # above does not cover. 85 if ua["user_agent"]["family"] in ("Safari", "Mobile Safari") and version_below( 86 ua["user_agent"], MIN_WEBKIT_VERSION 87 ): 88 return True 89 # Chrome versions behind the baseline get the SFE too, at least one reported case 90 # of the default flow executor not working in chrome 83 91 if ua["user_agent"]["family"] in ("Chrome", "Chrome Mobile") and version_below( 92 ua["user_agent"], MIN_CHROME_VERSION 93 ): 94 return True 95 return False 96 97 def get_template_names(self) -> list[str]: 98 if self.compat_needs_sfe() or "sfe" in self.request.GET: 99 return ["if/flow-sfe.html"] 100 return ["if/flow.html"]
Flow interface
39 def get_formatted_flow_title(self, flow: Flow) -> str: 40 if "%(" not in flow.title: 41 return flow.title 42 if plan := self.request.session.get(SESSION_KEY_PLAN): 43 return format_flow_title(plan, flow, self.request) 44 # Fallback if we don't have a flow plan yet, format the title with an empty defaultdict 45 # Only format titles with named placeholders, as a bare "% s" would 46 # otherwise inline the dict's repr 47 try: 48 return (flow.title % defaultdict(str)).strip() 49 except TypeError, ValueError: 50 # If title formatting is invalid, keep the original title unchanged. 51 pass 52 return flow.title
def
get_context_data(self, **kwargs: Any) -> dict[str, typing.Any]:
54 def get_context_data(self, **kwargs: Any) -> dict[str, Any]: 55 flow = get_object_or_404(Flow, slug=self.kwargs.get("flow_slug")) 56 kwargs["flow"] = flow 57 kwargs["flow_title"] = self.get_formatted_flow_title(flow) 58 kwargs["flow_background_url"] = flow.background_url(self.request) 59 kwargs["flow_background_themed_urls"] = flow.background_themed_urls(self.request) 60 kwargs["inspector"] = "inspector" in self.request.GET 61 return super().get_context_data(**kwargs)
def
compat_needs_sfe(self) -> bool:
63 def compat_needs_sfe(self) -> bool: 64 """Check if we need to use the simplified flow executor for compatibility""" 65 ua = Parse(self.request.META.get("HTTP_USER_AGENT", "")) 66 if ua["user_agent"]["family"] == "IE": 67 return True 68 # Only use SFE for Edge 18 and older, after Edge 18 MS switched to chromium which supports 69 # the default flow executor 70 if ua["user_agent"]["family"] == "Edge" and version_below( 71 ua["user_agent"], MIN_EDGE_VERSION 72 ): 73 return True 74 # https://github.com/AzureAD/microsoft-authentication-library-for-objc 75 # Used by Microsoft Teams/Office on macOS, and also uses a very outdated browser engine 76 if "PKeyAuth" in ua["string"]: 77 return True 78 # Every browser on iOS renders through the system WebKit, so the iOS version 79 # decides whether the bundle can be parsed regardless of which browser the 80 # user opened. 81 if ua["os"]["family"] == "iOS" and version_below(ua["os"], MIN_WEBKIT_VERSION): 82 return True 83 # iPadOS in desktop mode reports itself as Safari on macOS, which the check 84 # above does not cover. 85 if ua["user_agent"]["family"] in ("Safari", "Mobile Safari") and version_below( 86 ua["user_agent"], MIN_WEBKIT_VERSION 87 ): 88 return True 89 # Chrome versions behind the baseline get the SFE too, at least one reported case 90 # of the default flow executor not working in chrome 83 91 if ua["user_agent"]["family"] in ("Chrome", "Chrome Mobile") and version_below( 92 ua["user_agent"], MIN_CHROME_VERSION 93 ): 94 return True 95 return False
Check if we need to use the simplified flow executor for compatibility
def
get_template_names(self) -> list[str]:
97 def get_template_names(self) -> list[str]: 98 if self.compat_needs_sfe() or "sfe" in self.request.GET: 99 return ["if/flow-sfe.html"] 100 return ["if/flow.html"]
Return a list of template names to be used for the request. Must return a list. May not be called if render_to_response() is overridden.