authentik.core.views.apps

app views

 1"""app views"""
 2
 3from django.http import Http404, HttpRequest, HttpResponse, HttpResponseRedirect
 4from django.shortcuts import get_object_or_404
 5from django.utils.translation import gettext_lazy as _
 6from django.views import View
 7
 8from authentik.core.models import Application
 9from authentik.flows.challenge import (
10    ChallengeResponse,
11    HttpChallengeResponse,
12    RedirectChallenge,
13)
14from authentik.flows.exceptions import FlowNonApplicableException
15from authentik.flows.models import FlowDesignation, in_memory_stage
16from authentik.flows.planner import PLAN_CONTEXT_APPLICATION, FlowPlanner
17from authentik.flows.stage import ChallengeStageView
18from authentik.flows.views.executor import (
19    ToDefaultFlow,
20)
21from authentik.lib.models import DomainlessFormattedURLValidator
22from authentik.stages.consent.stage import (
23    PLAN_CONTEXT_CONSENT_HEADER,
24    PLAN_CONTEXT_CONSENT_PERMISSIONS,
25)
26
27
28class LaunchURLRedirect(HttpResponseRedirect):
29    """Redirect to a launch URL, which can use any scheme the launch URL validator accepts,
30    such as `ssh://`. Django only allows http, https and ftp by default."""
31
32    allowed_schemes = DomainlessFormattedURLValidator().schemes
33
34
35class RedirectToAppLaunch(View):
36    """Application launch view, redirect to the launch URL"""
37
38    def dispatch(self, request: HttpRequest, application_slug: str) -> HttpResponse:
39        app = get_object_or_404(Application, slug=application_slug)
40        # Check here if the application has any launch URL set, if not 404
41        user = request.user if request.user.is_authenticated else None
42        launch = app.get_launch_url(user)
43        if not launch:
44            raise Http404
45        # Check if we're authenticated already, saves us the flow run
46        if user is not None:
47            return LaunchURLRedirect(launch)
48        # otherwise, do a custom flow plan that includes the application that's
49        # being accessed, to improve usability
50        if app and app.provider and app.provider.authentication_flow:
51            flow = app.provider.authentication_flow
52        else:
53            flow = ToDefaultFlow.get_flow(
54                request=request, designation=FlowDesignation.AUTHENTICATION
55            )
56        planner = FlowPlanner(flow)
57        planner.allow_empty_flows = True
58        try:
59            plan = planner.plan(
60                request,
61                {
62                    PLAN_CONTEXT_APPLICATION: app,
63                    PLAN_CONTEXT_CONSENT_HEADER: _("You're about to sign into %(application)s.")
64                    % {"application": app.name},
65                    PLAN_CONTEXT_CONSENT_PERMISSIONS: [],
66                },
67            )
68        except FlowNonApplicableException:
69            raise Http404 from None
70        # We redirect with an in_memory stage instead of `?next=...` as the launch URL
71        # might be formatted with the user, which hasn't logged in yet
72        plan.append_stage(in_memory_stage(RedirectToAppStage))
73        return plan.to_redirect(request, flow)
74
75
76class RedirectToAppStage(ChallengeStageView):
77    """Final stage to be inserted after the user logs in"""
78
79    def get_challenge(self, *args, **kwargs) -> RedirectChallenge:
80        app = self.executor.plan.context[PLAN_CONTEXT_APPLICATION]
81        launch = app.get_launch_url(self.get_pending_user())
82        # sanity check to ensure launch is still set
83        if not launch:
84            raise Http404
85        return RedirectChallenge(
86            instance={
87                "to": launch,
88                "final_redirect": True,
89            }
90        )
91
92    def challenge_valid(self, response: ChallengeResponse) -> HttpResponse:
93        return HttpChallengeResponse(self.get_challenge())
class LaunchURLRedirect(django.http.response.HttpResponseRedirect):
29class LaunchURLRedirect(HttpResponseRedirect):
30    """Redirect to a launch URL, which can use any scheme the launch URL validator accepts,
31    such as `ssh://`. Django only allows http, https and ftp by default."""
32
33    allowed_schemes = DomainlessFormattedURLValidator().schemes

Redirect to a launch URL, which can use any scheme the launch URL validator accepts, such as ssh://. Django only allows http, https and ftp by default.

allowed_schemes = ['http', 'https', 'blank', 'ssh', 'sftp', 'http', 'https', 'blank', 'http', 'https', 'ftp', 'ftps']
class RedirectToAppLaunch(django.views.generic.base.View):
36class RedirectToAppLaunch(View):
37    """Application launch view, redirect to the launch URL"""
38
39    def dispatch(self, request: HttpRequest, application_slug: str) -> HttpResponse:
40        app = get_object_or_404(Application, slug=application_slug)
41        # Check here if the application has any launch URL set, if not 404
42        user = request.user if request.user.is_authenticated else None
43        launch = app.get_launch_url(user)
44        if not launch:
45            raise Http404
46        # Check if we're authenticated already, saves us the flow run
47        if user is not None:
48            return LaunchURLRedirect(launch)
49        # otherwise, do a custom flow plan that includes the application that's
50        # being accessed, to improve usability
51        if app and app.provider and app.provider.authentication_flow:
52            flow = app.provider.authentication_flow
53        else:
54            flow = ToDefaultFlow.get_flow(
55                request=request, designation=FlowDesignation.AUTHENTICATION
56            )
57        planner = FlowPlanner(flow)
58        planner.allow_empty_flows = True
59        try:
60            plan = planner.plan(
61                request,
62                {
63                    PLAN_CONTEXT_APPLICATION: app,
64                    PLAN_CONTEXT_CONSENT_HEADER: _("You're about to sign into %(application)s.")
65                    % {"application": app.name},
66                    PLAN_CONTEXT_CONSENT_PERMISSIONS: [],
67                },
68            )
69        except FlowNonApplicableException:
70            raise Http404 from None
71        # We redirect with an in_memory stage instead of `?next=...` as the launch URL
72        # might be formatted with the user, which hasn't logged in yet
73        plan.append_stage(in_memory_stage(RedirectToAppStage))
74        return plan.to_redirect(request, flow)

Application launch view, redirect to the launch URL

def dispatch( self, request: django.http.request.HttpRequest, application_slug: str) -> django.http.response.HttpResponse:
39    def dispatch(self, request: HttpRequest, application_slug: str) -> HttpResponse:
40        app = get_object_or_404(Application, slug=application_slug)
41        # Check here if the application has any launch URL set, if not 404
42        user = request.user if request.user.is_authenticated else None
43        launch = app.get_launch_url(user)
44        if not launch:
45            raise Http404
46        # Check if we're authenticated already, saves us the flow run
47        if user is not None:
48            return LaunchURLRedirect(launch)
49        # otherwise, do a custom flow plan that includes the application that's
50        # being accessed, to improve usability
51        if app and app.provider and app.provider.authentication_flow:
52            flow = app.provider.authentication_flow
53        else:
54            flow = ToDefaultFlow.get_flow(
55                request=request, designation=FlowDesignation.AUTHENTICATION
56            )
57        planner = FlowPlanner(flow)
58        planner.allow_empty_flows = True
59        try:
60            plan = planner.plan(
61                request,
62                {
63                    PLAN_CONTEXT_APPLICATION: app,
64                    PLAN_CONTEXT_CONSENT_HEADER: _("You're about to sign into %(application)s.")
65                    % {"application": app.name},
66                    PLAN_CONTEXT_CONSENT_PERMISSIONS: [],
67                },
68            )
69        except FlowNonApplicableException:
70            raise Http404 from None
71        # We redirect with an in_memory stage instead of `?next=...` as the launch URL
72        # might be formatted with the user, which hasn't logged in yet
73        plan.append_stage(in_memory_stage(RedirectToAppStage))
74        return plan.to_redirect(request, flow)
class RedirectToAppStage(authentik.flows.stage.ChallengeStageView):
77class RedirectToAppStage(ChallengeStageView):
78    """Final stage to be inserted after the user logs in"""
79
80    def get_challenge(self, *args, **kwargs) -> RedirectChallenge:
81        app = self.executor.plan.context[PLAN_CONTEXT_APPLICATION]
82        launch = app.get_launch_url(self.get_pending_user())
83        # sanity check to ensure launch is still set
84        if not launch:
85            raise Http404
86        return RedirectChallenge(
87            instance={
88                "to": launch,
89                "final_redirect": True,
90            }
91        )
92
93    def challenge_valid(self, response: ChallengeResponse) -> HttpResponse:
94        return HttpChallengeResponse(self.get_challenge())

Final stage to be inserted after the user logs in

def get_challenge(self, *args, **kwargs) -> authentik.flows.challenge.RedirectChallenge:
80    def get_challenge(self, *args, **kwargs) -> RedirectChallenge:
81        app = self.executor.plan.context[PLAN_CONTEXT_APPLICATION]
82        launch = app.get_launch_url(self.get_pending_user())
83        # sanity check to ensure launch is still set
84        if not launch:
85            raise Http404
86        return RedirectChallenge(
87            instance={
88                "to": launch,
89                "final_redirect": True,
90            }
91        )

Return the challenge that the client should solve

def challenge_valid( self, response: authentik.flows.challenge.ChallengeResponse) -> django.http.response.HttpResponse:
93    def challenge_valid(self, response: ChallengeResponse) -> HttpResponse:
94        return HttpChallengeResponse(self.get_challenge())

Callback when the challenge has the correct format