2020-05-08 18:46:39 +01:00
|
|
|
"""passbook multi-stage authentication engine"""
|
2020-05-23 23:57:25 +01:00
|
|
|
from typing import Any, Dict, Optional
|
2018-12-13 17:02:08 +00:00
|
|
|
|
2020-05-23 19:23:09 +01:00
|
|
|
from django.http import Http404, HttpRequest, HttpResponse
|
2020-05-23 23:57:25 +01:00
|
|
|
from django.shortcuts import get_object_or_404, redirect, reverse
|
|
|
|
from django.utils.decorators import method_decorator
|
|
|
|
from django.views.decorators.clickjacking import xframe_options_sameorigin
|
|
|
|
from django.views.generic import TemplateView, View
|
2019-10-01 09:24:10 +01:00
|
|
|
from structlog import get_logger
|
2018-12-13 17:02:08 +00:00
|
|
|
|
2020-05-08 15:50:50 +01:00
|
|
|
from passbook.core.views.utils import PermissionDeniedView
|
2020-05-09 19:54:56 +01:00
|
|
|
from passbook.flows.exceptions import EmptyFlowException, FlowNonApplicableException
|
|
|
|
from passbook.flows.models import Flow, FlowDesignation, Stage
|
2020-05-10 14:28:52 +01:00
|
|
|
from passbook.flows.planner import FlowPlan, FlowPlanner
|
2018-12-14 08:49:34 +00:00
|
|
|
from passbook.lib.utils.reflection import class_to_path, path_to_class
|
2020-05-11 14:01:14 +01:00
|
|
|
from passbook.lib.utils.urls import redirect_with_qs
|
2020-02-21 14:04:37 +00:00
|
|
|
from passbook.lib.views import bad_request_message
|
2018-12-13 17:02:08 +00:00
|
|
|
|
2019-10-04 09:08:53 +01:00
|
|
|
LOGGER = get_logger()
|
2019-12-31 11:45:29 +00:00
|
|
|
# Argument used to redirect user after login
|
2019-12-31 11:51:16 +00:00
|
|
|
NEXT_ARG_NAME = "next"
|
2020-05-08 15:10:27 +01:00
|
|
|
SESSION_KEY_PLAN = "passbook_flows_plan"
|
2019-12-31 11:45:29 +00:00
|
|
|
|
2018-12-13 17:02:08 +00:00
|
|
|
|
2020-05-23 23:57:25 +01:00
|
|
|
@method_decorator(xframe_options_sameorigin, name="dispatch")
|
2020-05-08 15:10:27 +01:00
|
|
|
class FlowExecutorView(View):
|
2020-05-08 18:46:39 +01:00
|
|
|
"""Stage 1 Flow executor, passing requests to Stage Views"""
|
2018-12-13 17:02:08 +00:00
|
|
|
|
2020-05-08 15:10:27 +01:00
|
|
|
flow: Flow
|
2019-02-25 11:29:40 +00:00
|
|
|
|
2020-05-09 22:19:36 +01:00
|
|
|
plan: Optional[FlowPlan] = None
|
2020-05-08 18:46:39 +01:00
|
|
|
current_stage: Stage
|
|
|
|
current_stage_view: View
|
2018-12-13 17:02:08 +00:00
|
|
|
|
2020-05-08 15:10:27 +01:00
|
|
|
def setup(self, request: HttpRequest, flow_slug: str):
|
|
|
|
super().setup(request, flow_slug=flow_slug)
|
2020-05-31 22:01:08 +01:00
|
|
|
self.flow = get_object_or_404(Flow.objects.select_related(), slug=flow_slug)
|
2018-12-18 14:34:26 +00:00
|
|
|
|
2020-05-09 19:54:56 +01:00
|
|
|
def handle_invalid_flow(self, exc: BaseException) -> HttpResponse:
|
2020-05-08 15:10:27 +01:00
|
|
|
"""When a flow is non-applicable check if user is on the correct domain"""
|
2019-10-07 15:33:48 +01:00
|
|
|
if NEXT_ARG_NAME in self.request.GET:
|
2020-05-10 00:01:58 +01:00
|
|
|
LOGGER.debug("f(exec): Redirecting to next on fail")
|
2019-10-07 15:33:48 +01:00
|
|
|
return redirect(self.request.GET.get(NEXT_ARG_NAME))
|
2020-05-11 14:01:14 +01:00
|
|
|
message = exc.__doc__ if exc.__doc__ else str(exc)
|
|
|
|
return bad_request_message(self.request, message)
|
2020-05-08 13:33:14 +01:00
|
|
|
|
|
|
|
def dispatch(self, request: HttpRequest, flow_slug: str) -> HttpResponse:
|
|
|
|
# Early check if theres an active Plan for the current session
|
2020-05-10 19:15:24 +01:00
|
|
|
if SESSION_KEY_PLAN in self.request.session:
|
|
|
|
self.plan = self.request.session[SESSION_KEY_PLAN]
|
|
|
|
if self.plan.flow_pk != self.flow.pk.hex:
|
|
|
|
LOGGER.warning(
|
|
|
|
"f(exec): Found existing plan for other flow, deleteing plan",
|
|
|
|
flow_slug=flow_slug,
|
|
|
|
)
|
|
|
|
# Existing plan is deleted from session and instance
|
|
|
|
self.plan = None
|
|
|
|
self.cancel()
|
|
|
|
LOGGER.debug("f(exec): Continuing existing plan", flow_slug=flow_slug)
|
|
|
|
|
|
|
|
# Don't check session again as we've either already loaded the plan or we need to plan
|
|
|
|
if not self.plan:
|
2020-05-08 13:33:14 +01:00
|
|
|
LOGGER.debug(
|
2020-05-10 00:01:58 +01:00
|
|
|
"f(exec): No active Plan found, initiating planner", flow_slug=flow_slug
|
2020-05-08 13:33:14 +01:00
|
|
|
)
|
|
|
|
try:
|
|
|
|
self.plan = self._initiate_plan()
|
2020-05-09 19:54:56 +01:00
|
|
|
except FlowNonApplicableException as exc:
|
2020-05-10 00:01:58 +01:00
|
|
|
LOGGER.warning("f(exec): Flow not applicable to current user", exc=exc)
|
2020-05-09 19:54:56 +01:00
|
|
|
return self.handle_invalid_flow(exc)
|
|
|
|
except EmptyFlowException as exc:
|
2020-05-10 00:01:58 +01:00
|
|
|
LOGGER.warning("f(exec): Flow is empty", exc=exc)
|
2020-05-09 19:54:56 +01:00
|
|
|
return self.handle_invalid_flow(exc)
|
2020-05-08 18:46:39 +01:00
|
|
|
# We don't save the Plan after getting the next stage
|
2020-05-08 13:33:14 +01:00
|
|
|
# as it hasn't been successfully passed yet
|
2020-05-08 18:46:39 +01:00
|
|
|
self.current_stage = self.plan.next()
|
2020-05-08 17:29:18 +01:00
|
|
|
LOGGER.debug(
|
2020-05-10 00:01:58 +01:00
|
|
|
"f(exec): Current stage",
|
|
|
|
current_stage=self.current_stage,
|
|
|
|
flow_slug=self.flow.slug,
|
2020-05-08 17:29:18 +01:00
|
|
|
)
|
2020-05-08 18:46:39 +01:00
|
|
|
stage_cls = path_to_class(self.current_stage.type)
|
|
|
|
self.current_stage_view = stage_cls(self)
|
|
|
|
self.current_stage_view.request = request
|
2020-05-08 13:33:14 +01:00
|
|
|
return super().dispatch(request)
|
|
|
|
|
|
|
|
def get(self, request: HttpRequest, *args, **kwargs) -> HttpResponse:
|
2020-05-08 18:46:39 +01:00
|
|
|
"""pass get request to current stage"""
|
2020-05-08 13:33:14 +01:00
|
|
|
LOGGER.debug(
|
2020-05-10 00:01:58 +01:00
|
|
|
"f(exec): Passing GET",
|
2020-05-08 18:46:39 +01:00
|
|
|
view_class=class_to_path(self.current_stage_view.__class__),
|
2020-05-08 15:51:12 +01:00
|
|
|
flow_slug=self.flow.slug,
|
2020-05-08 13:33:14 +01:00
|
|
|
)
|
2020-05-08 18:46:39 +01:00
|
|
|
return self.current_stage_view.get(request, *args, **kwargs)
|
2020-05-08 13:33:14 +01:00
|
|
|
|
|
|
|
def post(self, request: HttpRequest, *args, **kwargs) -> HttpResponse:
|
2020-05-08 18:46:39 +01:00
|
|
|
"""pass post request to current stage"""
|
2020-05-08 13:33:14 +01:00
|
|
|
LOGGER.debug(
|
2020-05-10 00:01:58 +01:00
|
|
|
"f(exec): Passing POST",
|
2020-05-08 18:46:39 +01:00
|
|
|
view_class=class_to_path(self.current_stage_view.__class__),
|
2020-05-08 15:51:12 +01:00
|
|
|
flow_slug=self.flow.slug,
|
2020-05-08 13:33:14 +01:00
|
|
|
)
|
2020-05-08 18:46:39 +01:00
|
|
|
return self.current_stage_view.post(request, *args, **kwargs)
|
2020-05-08 13:33:14 +01:00
|
|
|
|
|
|
|
def _initiate_plan(self) -> FlowPlan:
|
|
|
|
planner = FlowPlanner(self.flow)
|
|
|
|
plan = planner.plan(self.request)
|
|
|
|
self.request.session[SESSION_KEY_PLAN] = plan
|
|
|
|
return plan
|
2020-05-08 15:10:27 +01:00
|
|
|
|
|
|
|
def _flow_done(self) -> HttpResponse:
|
2020-05-08 18:46:39 +01:00
|
|
|
"""User Successfully passed all stages"""
|
2020-05-08 15:10:27 +01:00
|
|
|
self.cancel()
|
2020-05-11 14:01:14 +01:00
|
|
|
next_param = self.request.GET.get(NEXT_ARG_NAME, "passbook_core:overview")
|
|
|
|
return redirect_with_qs(next_param)
|
2020-05-08 15:10:27 +01:00
|
|
|
|
2020-05-08 18:46:39 +01:00
|
|
|
def stage_ok(self) -> HttpResponse:
|
|
|
|
"""Callback called by stages upon successful completion.
|
2020-05-08 15:10:27 +01:00
|
|
|
Persists updated plan and context to session."""
|
|
|
|
LOGGER.debug(
|
2020-05-10 00:01:58 +01:00
|
|
|
"f(exec): Stage ok",
|
2020-05-08 18:46:39 +01:00
|
|
|
stage_class=class_to_path(self.current_stage_view.__class__),
|
2020-05-08 15:51:12 +01:00
|
|
|
flow_slug=self.flow.slug,
|
2020-05-08 15:10:27 +01:00
|
|
|
)
|
2020-05-09 19:54:56 +01:00
|
|
|
self.plan.stages.pop(0)
|
2020-05-08 15:10:27 +01:00
|
|
|
self.request.session[SESSION_KEY_PLAN] = self.plan
|
2020-05-08 18:46:39 +01:00
|
|
|
if self.plan.stages:
|
2020-05-08 15:10:27 +01:00
|
|
|
LOGGER.debug(
|
2020-05-10 00:01:58 +01:00
|
|
|
"f(exec): Continuing with next stage",
|
2020-05-08 18:46:39 +01:00
|
|
|
reamining=len(self.plan.stages),
|
2020-05-08 15:51:12 +01:00
|
|
|
flow_slug=self.flow.slug,
|
2020-05-08 15:10:27 +01:00
|
|
|
)
|
|
|
|
return redirect_with_qs(
|
|
|
|
"passbook_flows:flow-executor", self.request.GET, **self.kwargs
|
|
|
|
)
|
2020-05-08 18:46:39 +01:00
|
|
|
# User passed all stages
|
2020-05-08 15:10:27 +01:00
|
|
|
LOGGER.debug(
|
2020-05-10 00:01:58 +01:00
|
|
|
"f(exec): User passed all stages",
|
2020-05-08 15:51:12 +01:00
|
|
|
flow_slug=self.flow.slug,
|
2020-05-10 14:28:52 +01:00
|
|
|
context=self.plan.context,
|
2020-05-08 15:10:27 +01:00
|
|
|
)
|
|
|
|
return self._flow_done()
|
|
|
|
|
2020-05-08 18:46:39 +01:00
|
|
|
def stage_invalid(self) -> HttpResponse:
|
|
|
|
"""Callback used stage when data is correct but a policy denies access
|
2020-05-08 15:10:27 +01:00
|
|
|
or the user account is disabled."""
|
2020-05-10 00:01:58 +01:00
|
|
|
LOGGER.debug("f(exec): Stage invalid", flow_slug=self.flow.slug)
|
2020-05-08 15:10:27 +01:00
|
|
|
self.cancel()
|
2020-05-08 15:50:50 +01:00
|
|
|
return redirect_with_qs("passbook_flows:denied", self.request.GET)
|
2020-05-08 15:10:27 +01:00
|
|
|
|
2020-05-09 22:19:36 +01:00
|
|
|
def cancel(self):
|
2020-05-08 15:10:27 +01:00
|
|
|
"""Cancel current execution and return a redirect"""
|
2020-05-10 14:28:52 +01:00
|
|
|
if SESSION_KEY_PLAN in self.request.session:
|
|
|
|
del self.request.session[SESSION_KEY_PLAN]
|
2020-05-08 15:50:50 +01:00
|
|
|
|
|
|
|
|
|
|
|
class FlowPermissionDeniedView(PermissionDeniedView):
|
|
|
|
"""User could not be authenticated"""
|
2020-05-09 19:54:56 +01:00
|
|
|
|
|
|
|
|
|
|
|
class ToDefaultFlow(View):
|
|
|
|
"""Redirect to default flow matching by designation"""
|
|
|
|
|
|
|
|
designation: Optional[FlowDesignation] = None
|
|
|
|
|
|
|
|
def dispatch(self, request: HttpRequest) -> HttpResponse:
|
2020-05-23 19:23:09 +01:00
|
|
|
flow = Flow.with_policy(request, designation=self.designation)
|
|
|
|
if not flow:
|
|
|
|
raise Http404
|
2020-05-11 14:01:14 +01:00
|
|
|
# If user already has a pending plan, clear it so we don't have to later.
|
|
|
|
if SESSION_KEY_PLAN in self.request.session:
|
|
|
|
plan: FlowPlan = self.request.session[SESSION_KEY_PLAN]
|
|
|
|
if plan.flow_pk != flow.pk.hex:
|
|
|
|
LOGGER.warning(
|
|
|
|
"f(def): Found existing plan for other flow, deleteing plan",
|
|
|
|
flow_slug=flow.slug,
|
|
|
|
)
|
|
|
|
del self.request.session[SESSION_KEY_PLAN]
|
2020-05-09 19:54:56 +01:00
|
|
|
return redirect_with_qs(
|
2020-05-23 23:57:25 +01:00
|
|
|
"passbook_flows:flow-executor-shell", request.GET, flow_slug=flow.slug
|
2020-05-09 19:54:56 +01:00
|
|
|
)
|
2020-05-23 23:57:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
class FlowExecutorShellView(TemplateView):
|
|
|
|
"""Executor Shell view, loads a dummy card with a spinner
|
|
|
|
that loads the next stage in the background."""
|
|
|
|
|
|
|
|
template_name = "flows/shell.html"
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs) -> Dict[str, Any]:
|
|
|
|
kwargs["exec_url"] = reverse("passbook_flows:flow-executor", kwargs=self.kwargs)
|
|
|
|
kwargs["msg_url"] = reverse("passbook_api:messages-list")
|
|
|
|
return kwargs
|