2020-05-07 18:51:06 +00:00
|
|
|
"""Flow models"""
|
2020-07-09 21:37:13 +00:00
|
|
|
from typing import TYPE_CHECKING, Optional, Type
|
2020-05-20 07:17:06 +00:00
|
|
|
from uuid import uuid4
|
2020-05-07 18:51:06 +00:00
|
|
|
|
|
|
|
from django.db import models
|
2020-07-20 14:33:34 +00:00
|
|
|
from django.forms import ModelForm
|
2020-05-23 18:23:09 +00:00
|
|
|
from django.http import HttpRequest
|
2020-05-07 18:51:06 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2020-05-08 17:46:39 +00:00
|
|
|
from model_utils.managers import InheritanceManager
|
2020-08-21 22:42:15 +00:00
|
|
|
from rest_framework.serializers import BaseSerializer
|
2020-05-23 18:23:09 +00:00
|
|
|
from structlog import get_logger
|
2020-05-07 18:51:06 +00:00
|
|
|
|
2020-09-07 09:27:02 +00:00
|
|
|
from passbook.lib.models import InheritanceForeignKey, SerializerModel
|
2020-05-07 18:51:06 +00:00
|
|
|
from passbook.policies.models import PolicyBindingModel
|
|
|
|
|
2020-07-09 21:37:13 +00:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from passbook.flows.stage import StageView
|
|
|
|
|
2020-05-23 18:23:09 +00:00
|
|
|
LOGGER = get_logger()
|
|
|
|
|
2020-05-07 18:51:06 +00:00
|
|
|
|
2020-06-28 08:31:26 +00:00
|
|
|
class NotConfiguredAction(models.TextChoices):
|
|
|
|
"""Decides how the FlowExecutor should proceed when a stage isn't configured"""
|
|
|
|
|
|
|
|
SKIP = "skip"
|
|
|
|
# CONFIGURE = "configure"
|
|
|
|
|
|
|
|
|
2020-05-09 18:54:56 +00:00
|
|
|
class FlowDesignation(models.TextChoices):
|
2020-05-07 18:51:06 +00:00
|
|
|
"""Designation of what a Flow should be used for. At a later point, this
|
|
|
|
should be replaced by a database entry."""
|
|
|
|
|
|
|
|
AUTHENTICATION = "authentication"
|
2020-06-07 14:35:08 +00:00
|
|
|
AUTHORIZATION = "authorization"
|
2020-05-12 12:50:00 +00:00
|
|
|
INVALIDATION = "invalidation"
|
2020-05-07 18:51:06 +00:00
|
|
|
ENROLLMENT = "enrollment"
|
2020-05-12 12:50:00 +00:00
|
|
|
UNRENOLLMENT = "unenrollment"
|
2020-05-07 18:51:06 +00:00
|
|
|
RECOVERY = "recovery"
|
2020-09-24 18:05:49 +00:00
|
|
|
STAGE_CONFIGURATION = "stage_configuration"
|
2020-05-07 18:51:06 +00:00
|
|
|
|
|
|
|
|
2020-08-21 22:42:15 +00:00
|
|
|
class Stage(SerializerModel):
|
2020-05-08 17:46:39 +00:00
|
|
|
"""Stage is an instance of a component used in a flow. This can verify the user,
|
|
|
|
enroll the user or offer a way of recovery"""
|
|
|
|
|
2020-05-20 07:17:06 +00:00
|
|
|
stage_uuid = models.UUIDField(primary_key=True, editable=False, default=uuid4)
|
|
|
|
|
2020-09-05 23:07:06 +00:00
|
|
|
name = models.TextField(unique=True)
|
2020-05-08 17:46:39 +00:00
|
|
|
|
|
|
|
objects = InheritanceManager()
|
2020-07-20 14:33:34 +00:00
|
|
|
|
2020-09-29 08:32:41 +00:00
|
|
|
@property
|
2020-07-20 14:33:34 +00:00
|
|
|
def type(self) -> Type["StageView"]:
|
|
|
|
"""Return StageView class that implements logic for this stage"""
|
2020-07-20 14:55:55 +00:00
|
|
|
# This is a bit of a workaround, since we can't set class methods with setattr
|
|
|
|
if hasattr(self, "__in_memory_type"):
|
|
|
|
return getattr(self, "__in_memory_type")
|
2020-07-20 14:33:34 +00:00
|
|
|
raise NotImplementedError
|
|
|
|
|
2020-09-29 08:32:41 +00:00
|
|
|
@property
|
2020-07-20 14:33:34 +00:00
|
|
|
def form(self) -> Type[ModelForm]:
|
|
|
|
"""Return Form class used to edit this object"""
|
|
|
|
raise NotImplementedError
|
2020-05-08 17:46:39 +00:00
|
|
|
|
|
|
|
@property
|
2020-11-22 19:30:26 +00:00
|
|
|
def ui_user_settings(self) -> Optional[str]:
|
2020-05-08 17:46:39 +00:00
|
|
|
"""Entrypoint to integrate with User settings. Can either return None if no
|
2020-11-22 19:30:26 +00:00
|
|
|
user settings are available, or a string with the URL to fetch."""
|
2020-05-08 17:46:39 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
def __str__(self):
|
2020-08-19 08:32:44 +00:00
|
|
|
if hasattr(self, "__in_memory_type"):
|
|
|
|
return f"In-memory Stage {getattr(self, '__in_memory_type')}"
|
2020-05-08 17:46:39 +00:00
|
|
|
return f"Stage {self.name}"
|
|
|
|
|
|
|
|
|
2020-07-09 21:37:13 +00:00
|
|
|
def in_memory_stage(view: Type["StageView"]) -> Stage:
|
2020-06-07 14:35:08 +00:00
|
|
|
"""Creates an in-memory stage instance, based on a `_type` as view."""
|
|
|
|
stage = Stage()
|
2020-07-20 14:55:55 +00:00
|
|
|
# Because we can't pickle a locally generated function,
|
|
|
|
# we set the view as a separate property and reference a generic function
|
|
|
|
# that returns that member
|
|
|
|
setattr(stage, "__in_memory_type", view)
|
2020-06-07 14:35:08 +00:00
|
|
|
return stage
|
|
|
|
|
|
|
|
|
2020-08-21 22:42:15 +00:00
|
|
|
class Flow(SerializerModel, PolicyBindingModel):
|
2020-05-08 17:46:39 +00:00
|
|
|
"""Flow describes how a series of Stages should be executed to authenticate/enroll/recover
|
2020-05-07 18:51:06 +00:00
|
|
|
a user. Additionally, policies can be applied, to specify which users
|
|
|
|
have access to this flow."""
|
|
|
|
|
2020-05-20 07:17:06 +00:00
|
|
|
flow_uuid = models.UUIDField(primary_key=True, editable=False, default=uuid4)
|
|
|
|
|
2020-05-07 18:51:06 +00:00
|
|
|
name = models.TextField()
|
2020-12-02 13:24:24 +00:00
|
|
|
slug = models.SlugField(unique=True, help_text=_("Visible in the URL."))
|
|
|
|
|
|
|
|
title = models.TextField(help_text=_("Shown as the Title in Flow pages."))
|
|
|
|
|
|
|
|
designation = models.CharField(
|
|
|
|
max_length=100,
|
|
|
|
choices=FlowDesignation.choices,
|
|
|
|
help_text=_(
|
|
|
|
(
|
|
|
|
"Decides what this Flow is used for. For example, the Authentication flow "
|
|
|
|
"is redirect to when an un-authenticated user visits passbook."
|
|
|
|
)
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
background = models.FileField(
|
|
|
|
upload_to="flow-backgrounds/",
|
|
|
|
default="../static/dist/assets/images/flow_background.jpg",
|
|
|
|
blank=True,
|
|
|
|
help_text=_("Background shown during execution"),
|
|
|
|
)
|
2020-05-07 18:51:06 +00:00
|
|
|
|
2020-05-08 17:46:39 +00:00
|
|
|
stages = models.ManyToManyField(Stage, through="FlowStageBinding", blank=True)
|
2020-05-07 18:51:06 +00:00
|
|
|
|
2020-08-21 22:42:15 +00:00
|
|
|
@property
|
|
|
|
def serializer(self) -> BaseSerializer:
|
|
|
|
from passbook.flows.api import FlowSerializer
|
|
|
|
|
|
|
|
return FlowSerializer
|
|
|
|
|
2020-05-23 18:23:09 +00:00
|
|
|
@staticmethod
|
|
|
|
def with_policy(request: HttpRequest, **flow_filter) -> Optional["Flow"]:
|
|
|
|
"""Get a Flow by `**flow_filter` and check if the request from `request` can access it."""
|
|
|
|
from passbook.policies.engine import PolicyEngine
|
|
|
|
|
2020-08-28 13:23:03 +00:00
|
|
|
flows = Flow.objects.filter(**flow_filter).order_by("slug")
|
2020-05-23 18:23:09 +00:00
|
|
|
for flow in flows:
|
|
|
|
engine = PolicyEngine(flow, request.user, request)
|
|
|
|
engine.build()
|
|
|
|
result = engine.result
|
|
|
|
if result.passing:
|
|
|
|
LOGGER.debug("with_policy: flow passing", flow=flow)
|
|
|
|
return flow
|
|
|
|
LOGGER.warning(
|
|
|
|
"with_policy: flow not passing", flow=flow, messages=result.messages
|
|
|
|
)
|
|
|
|
LOGGER.debug("with_policy: no flow found", filters=flow_filter)
|
|
|
|
return None
|
|
|
|
|
|
|
|
def related_flow(self, designation: str, request: HttpRequest) -> Optional["Flow"]:
|
2020-05-10 16:14:10 +00:00
|
|
|
"""Get a related flow with `designation`. Currently this only queries
|
|
|
|
Flows by `designation`, but will eventually use `self` for related lookups."""
|
2020-05-23 18:23:09 +00:00
|
|
|
return Flow.with_policy(request, designation=designation)
|
2020-05-10 16:14:10 +00:00
|
|
|
|
2020-05-07 18:51:06 +00:00
|
|
|
def __str__(self) -> str:
|
|
|
|
return f"Flow {self.name} ({self.slug})"
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
|
|
|
verbose_name = _("Flow")
|
|
|
|
verbose_name_plural = _("Flows")
|
|
|
|
|
2020-08-28 13:06:25 +00:00
|
|
|
permissions = [
|
|
|
|
("export_flow", "Can export a Flow"),
|
|
|
|
]
|
|
|
|
|
2020-05-07 18:51:06 +00:00
|
|
|
|
2020-08-21 22:42:15 +00:00
|
|
|
class FlowStageBinding(SerializerModel, PolicyBindingModel):
|
2020-05-08 17:46:39 +00:00
|
|
|
"""Relationship between Flow and Stage. Order is required and unique for
|
|
|
|
each flow-stage Binding. Additionally, policies can be specified, which determine if
|
2020-05-07 18:51:06 +00:00
|
|
|
this Binding applies to the current user"""
|
|
|
|
|
2020-05-20 07:17:06 +00:00
|
|
|
fsb_uuid = models.UUIDField(primary_key=True, editable=False, default=uuid4)
|
|
|
|
|
2020-07-03 21:34:44 +00:00
|
|
|
target = models.ForeignKey("Flow", on_delete=models.CASCADE)
|
2020-09-07 09:27:02 +00:00
|
|
|
stage = InheritanceForeignKey(Stage, on_delete=models.CASCADE)
|
2020-05-07 18:51:06 +00:00
|
|
|
|
2020-10-20 13:06:36 +00:00
|
|
|
evaluate_on_plan = models.BooleanField(
|
|
|
|
default=True,
|
2020-05-07 19:30:52 +00:00
|
|
|
help_text=_(
|
2020-09-25 23:37:06 +00:00
|
|
|
(
|
2020-10-20 13:06:36 +00:00
|
|
|
"Evaluate policies during the Flow planning process. "
|
|
|
|
"Disable this for input-based policies."
|
2020-09-25 23:37:06 +00:00
|
|
|
)
|
2020-05-07 19:30:52 +00:00
|
|
|
),
|
|
|
|
)
|
2020-10-20 13:41:50 +00:00
|
|
|
re_evaluate_policies = models.BooleanField(
|
2020-10-20 13:06:36 +00:00
|
|
|
default=False,
|
|
|
|
help_text=_("Evaluate policies when the Stage is present to the user."),
|
|
|
|
)
|
2020-05-07 19:30:52 +00:00
|
|
|
|
2020-05-07 18:51:06 +00:00
|
|
|
order = models.IntegerField()
|
|
|
|
|
2020-05-16 17:55:59 +00:00
|
|
|
objects = InheritanceManager()
|
|
|
|
|
2020-08-21 22:42:15 +00:00
|
|
|
@property
|
|
|
|
def serializer(self) -> BaseSerializer:
|
|
|
|
from passbook.flows.api import FlowStageBindingSerializer
|
|
|
|
|
|
|
|
return FlowStageBindingSerializer
|
|
|
|
|
2020-05-07 18:51:06 +00:00
|
|
|
def __str__(self) -> str:
|
2020-09-25 23:37:06 +00:00
|
|
|
return f"{self.target} #{self.order} -> {self.stage}"
|
2020-05-07 18:51:06 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
2020-09-25 23:37:06 +00:00
|
|
|
ordering = ["target", "order"]
|
2020-05-08 12:33:14 +00:00
|
|
|
|
2020-05-08 17:46:39 +00:00
|
|
|
verbose_name = _("Flow Stage Binding")
|
|
|
|
verbose_name_plural = _("Flow Stage Bindings")
|
2020-07-03 21:34:44 +00:00
|
|
|
unique_together = (("target", "stage", "order"),)
|
2020-09-25 10:43:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ConfigurableStage(models.Model):
|
|
|
|
"""Abstract base class for a Stage that can be configured by the enduser.
|
|
|
|
The stage should create a default flow with the configure_stage designation during
|
|
|
|
migration."""
|
|
|
|
|
|
|
|
configure_flow = models.ForeignKey(
|
|
|
|
Flow,
|
|
|
|
on_delete=models.SET_NULL,
|
|
|
|
null=True,
|
|
|
|
blank=True,
|
|
|
|
help_text=_(
|
|
|
|
(
|
|
|
|
"Flow used by an authenticated user to configure this Stage. "
|
|
|
|
"If empty, user will not be able to configure this stage."
|
|
|
|
)
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
|
|
|
abstract = True
|