authentik.core.views.interface
Interface views
1"""Interface views""" 2 3from json import dumps 4from typing import Any 5 6from django.http import HttpRequest 7from django.http.response import HttpResponse 8from django.shortcuts import redirect 9from django.utils.translation import gettext as _ 10from django.views.generic.base import RedirectView, TemplateView 11 12from authentik import authentik_build_hash 13from authentik.admin.tasks import LOCAL_VERSION 14from authentik.api.v3.config import ConfigView 15from authentik.brands.api import CurrentBrandSerializer 16from authentik.brands.models import Brand 17from authentik.core.models import UserTypes 18from authentik.lib.config import CONFIG 19from authentik.policies.denied import AccessDeniedResponse 20 21 22class RootRedirectView(RedirectView): 23 """Root redirect view, redirect to brand's default application if set""" 24 25 pattern_name = "authentik_core:if-user" 26 query_string = True 27 28 def redirect_to_app(self, request: HttpRequest): 29 if request.user.is_authenticated and request.user.type in ( 30 UserTypes.EXTERNAL, 31 UserTypes.SERVICE_ACCOUNT, 32 UserTypes.INTERNAL_SERVICE_ACCOUNT, 33 ): 34 brand: Brand = request.brand 35 if brand.default_application: 36 return redirect( 37 "authentik_core:application-launch", 38 application_slug=brand.default_application.slug, 39 ) 40 return None 41 42 def dispatch(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse: 43 if redirect_response := RootRedirectView().redirect_to_app(request): 44 return redirect_response 45 return super().dispatch(request, *args, **kwargs) 46 47 48class InterfaceView(TemplateView): 49 """Base interface view""" 50 51 def get_context_data(self, **kwargs: Any) -> dict[str, Any]: 52 brand = CurrentBrandSerializer(self.request.brand) 53 kwargs["config_json"] = dumps(ConfigView.get_config(self.request).data) 54 kwargs["ui_theme"] = brand.data["ui_theme"] 55 kwargs["brand_json"] = dumps(brand.data) 56 kwargs["version_family"] = f"{LOCAL_VERSION.major}.{LOCAL_VERSION.minor}" 57 kwargs["version_subdomain"] = f"version-{LOCAL_VERSION.major}-{LOCAL_VERSION.minor}" 58 kwargs["build"] = authentik_build_hash() 59 kwargs["url_kwargs"] = self.kwargs 60 kwargs["base_url"] = self.request.build_absolute_uri(CONFIG.get("web.path", "/")) 61 kwargs["base_url_rel"] = CONFIG.get("web.path", "/") 62 return super().get_context_data(**kwargs) 63 64 65class BrandDefaultRedirectView(InterfaceView): 66 """By default redirect to default app""" 67 68 def dispatch(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse: 69 if request.user.is_authenticated and request.user.type in ( 70 UserTypes.EXTERNAL, 71 UserTypes.SERVICE_ACCOUNT, 72 UserTypes.INTERNAL_SERVICE_ACCOUNT, 73 ): 74 brand: Brand = request.brand 75 if brand.default_application: 76 return redirect( 77 "authentik_core:application-launch", 78 application_slug=brand.default_application.slug, 79 ) 80 response = AccessDeniedResponse(self.request) 81 response.error_message = _("Interface can only be accessed by internal users.") 82 return response 83 return super().dispatch(request, *args, **kwargs)
class
RootRedirectView(django.views.generic.base.RedirectView):
23class RootRedirectView(RedirectView): 24 """Root redirect view, redirect to brand's default application if set""" 25 26 pattern_name = "authentik_core:if-user" 27 query_string = True 28 29 def redirect_to_app(self, request: HttpRequest): 30 if request.user.is_authenticated and request.user.type in ( 31 UserTypes.EXTERNAL, 32 UserTypes.SERVICE_ACCOUNT, 33 UserTypes.INTERNAL_SERVICE_ACCOUNT, 34 ): 35 brand: Brand = request.brand 36 if brand.default_application: 37 return redirect( 38 "authentik_core:application-launch", 39 application_slug=brand.default_application.slug, 40 ) 41 return None 42 43 def dispatch(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse: 44 if redirect_response := RootRedirectView().redirect_to_app(request): 45 return redirect_response 46 return super().dispatch(request, *args, **kwargs)
Root redirect view, redirect to brand's default application if set
def
redirect_to_app(self, request: django.http.request.HttpRequest):
29 def redirect_to_app(self, request: HttpRequest): 30 if request.user.is_authenticated and request.user.type in ( 31 UserTypes.EXTERNAL, 32 UserTypes.SERVICE_ACCOUNT, 33 UserTypes.INTERNAL_SERVICE_ACCOUNT, 34 ): 35 brand: Brand = request.brand 36 if brand.default_application: 37 return redirect( 38 "authentik_core:application-launch", 39 application_slug=brand.default_application.slug, 40 ) 41 return None
class
InterfaceView(django.views.generic.base.TemplateView):
49class InterfaceView(TemplateView): 50 """Base interface view""" 51 52 def get_context_data(self, **kwargs: Any) -> dict[str, Any]: 53 brand = CurrentBrandSerializer(self.request.brand) 54 kwargs["config_json"] = dumps(ConfigView.get_config(self.request).data) 55 kwargs["ui_theme"] = brand.data["ui_theme"] 56 kwargs["brand_json"] = dumps(brand.data) 57 kwargs["version_family"] = f"{LOCAL_VERSION.major}.{LOCAL_VERSION.minor}" 58 kwargs["version_subdomain"] = f"version-{LOCAL_VERSION.major}-{LOCAL_VERSION.minor}" 59 kwargs["build"] = authentik_build_hash() 60 kwargs["url_kwargs"] = self.kwargs 61 kwargs["base_url"] = self.request.build_absolute_uri(CONFIG.get("web.path", "/")) 62 kwargs["base_url_rel"] = CONFIG.get("web.path", "/") 63 return super().get_context_data(**kwargs)
Base interface view
def
get_context_data(self, **kwargs: Any) -> dict[str, typing.Any]:
52 def get_context_data(self, **kwargs: Any) -> dict[str, Any]: 53 brand = CurrentBrandSerializer(self.request.brand) 54 kwargs["config_json"] = dumps(ConfigView.get_config(self.request).data) 55 kwargs["ui_theme"] = brand.data["ui_theme"] 56 kwargs["brand_json"] = dumps(brand.data) 57 kwargs["version_family"] = f"{LOCAL_VERSION.major}.{LOCAL_VERSION.minor}" 58 kwargs["version_subdomain"] = f"version-{LOCAL_VERSION.major}-{LOCAL_VERSION.minor}" 59 kwargs["build"] = authentik_build_hash() 60 kwargs["url_kwargs"] = self.kwargs 61 kwargs["base_url"] = self.request.build_absolute_uri(CONFIG.get("web.path", "/")) 62 kwargs["base_url_rel"] = CONFIG.get("web.path", "/") 63 return super().get_context_data(**kwargs)
66class BrandDefaultRedirectView(InterfaceView): 67 """By default redirect to default app""" 68 69 def dispatch(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse: 70 if request.user.is_authenticated and request.user.type in ( 71 UserTypes.EXTERNAL, 72 UserTypes.SERVICE_ACCOUNT, 73 UserTypes.INTERNAL_SERVICE_ACCOUNT, 74 ): 75 brand: Brand = request.brand 76 if brand.default_application: 77 return redirect( 78 "authentik_core:application-launch", 79 application_slug=brand.default_application.slug, 80 ) 81 response = AccessDeniedResponse(self.request) 82 response.error_message = _("Interface can only be accessed by internal users.") 83 return response 84 return super().dispatch(request, *args, **kwargs)
By default redirect to default app
def
dispatch( self, request: django.http.request.HttpRequest, *args: Any, **kwargs: Any) -> django.http.response.HttpResponse:
69 def dispatch(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse: 70 if request.user.is_authenticated and request.user.type in ( 71 UserTypes.EXTERNAL, 72 UserTypes.SERVICE_ACCOUNT, 73 UserTypes.INTERNAL_SERVICE_ACCOUNT, 74 ): 75 brand: Brand = request.brand 76 if brand.default_application: 77 return redirect( 78 "authentik_core:application-launch", 79 application_slug=brand.default_application.slug, 80 ) 81 response = AccessDeniedResponse(self.request) 82 response.error_message = _("Interface can only be accessed by internal users.") 83 return response 84 return super().dispatch(request, *args, **kwargs)