2020-06-07 14:35:08 +00:00
|
|
|
# Generated by Django 3.0.6 on 2020-05-23 15:47
|
|
|
|
|
|
|
|
from django.apps.registry import Apps
|
|
|
|
from django.db import migrations
|
|
|
|
from django.db.backends.base.schema import BaseDatabaseSchemaEditor
|
|
|
|
|
|
|
|
from passbook.flows.models import FlowDesignation
|
|
|
|
from passbook.stages.prompt.models import FieldTypes
|
|
|
|
|
2020-06-25 08:24:53 +00:00
|
|
|
FLOW_POLICY_EXPRESSION = """# This policy ensures that this flow can only be used when the user
|
|
|
|
# is in a SSO Flow (meaning they come from an external IdP)
|
|
|
|
return pb_is_sso_flow"""
|
2020-07-08 22:19:12 +00:00
|
|
|
PROMPT_POLICY_EXPRESSION = """# Check if we've not been given a username by the external IdP
|
2020-06-25 08:24:53 +00:00
|
|
|
# and trigger the enrollment flow
|
2020-09-26 11:58:32 +00:00
|
|
|
return 'username' not in context.get('prompt_data', {})"""
|
2020-06-07 14:35:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
def create_default_source_enrollment_flow(
|
|
|
|
apps: Apps, schema_editor: BaseDatabaseSchemaEditor
|
|
|
|
):
|
|
|
|
Flow = apps.get_model("passbook_flows", "Flow")
|
|
|
|
FlowStageBinding = apps.get_model("passbook_flows", "FlowStageBinding")
|
|
|
|
PolicyBinding = apps.get_model("passbook_policies", "PolicyBinding")
|
|
|
|
|
|
|
|
ExpressionPolicy = apps.get_model(
|
|
|
|
"passbook_policies_expression", "ExpressionPolicy"
|
|
|
|
)
|
|
|
|
|
|
|
|
PromptStage = apps.get_model("passbook_stages_prompt", "PromptStage")
|
|
|
|
Prompt = apps.get_model("passbook_stages_prompt", "Prompt")
|
|
|
|
UserWriteStage = apps.get_model("passbook_stages_user_write", "UserWriteStage")
|
|
|
|
UserLoginStage = apps.get_model("passbook_stages_user_login", "UserLoginStage")
|
|
|
|
|
|
|
|
db_alias = schema_editor.connection.alias
|
|
|
|
|
|
|
|
# Create a policy that only allows this flow when doing an SSO Request
|
2020-06-29 14:19:39 +00:00
|
|
|
flow_policy, _ = ExpressionPolicy.objects.using(db_alias).update_or_create(
|
|
|
|
name="default-source-enrollment-if-sso",
|
|
|
|
defaults={"expression": FLOW_POLICY_EXPRESSION},
|
2020-06-07 14:35:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# This creates a Flow used by sources to enroll users
|
|
|
|
# It makes sure that a username is set, and if not, prompts the user for a Username
|
2020-06-29 14:19:39 +00:00
|
|
|
flow, _ = Flow.objects.using(db_alias).update_or_create(
|
2020-06-07 14:35:08 +00:00
|
|
|
slug="default-source-enrollment",
|
|
|
|
designation=FlowDesignation.ENROLLMENT,
|
2020-09-30 17:34:22 +00:00
|
|
|
defaults={
|
|
|
|
"name": "Welcome to passbook!",
|
|
|
|
},
|
2020-06-07 14:35:08 +00:00
|
|
|
)
|
2020-06-29 14:19:39 +00:00
|
|
|
PolicyBinding.objects.using(db_alias).update_or_create(
|
|
|
|
policy=flow_policy, target=flow, defaults={"order": 0}
|
2020-06-19 18:35:38 +00:00
|
|
|
)
|
2020-06-07 14:35:08 +00:00
|
|
|
|
|
|
|
# PromptStage to ask user for their username
|
2020-06-29 14:19:39 +00:00
|
|
|
prompt_stage, _ = PromptStage.objects.using(db_alias).update_or_create(
|
2020-09-11 21:21:11 +00:00
|
|
|
name="Welcome to passbook! Please select a username.",
|
2020-06-07 14:35:08 +00:00
|
|
|
)
|
2020-06-29 14:19:39 +00:00
|
|
|
prompt, _ = Prompt.objects.using(db_alias).update_or_create(
|
|
|
|
field_key="username",
|
|
|
|
defaults={
|
|
|
|
"label": "Username",
|
|
|
|
"type": FieldTypes.TEXT,
|
|
|
|
"required": True,
|
|
|
|
"placeholder": "Username",
|
|
|
|
},
|
2020-06-07 14:35:08 +00:00
|
|
|
)
|
2020-06-29 14:19:39 +00:00
|
|
|
prompt_stage.fields.add(prompt)
|
|
|
|
|
2020-06-07 14:35:08 +00:00
|
|
|
# Policy to only trigger prompt when no username is given
|
2020-06-29 14:19:39 +00:00
|
|
|
prompt_policy, _ = ExpressionPolicy.objects.using(db_alias).update_or_create(
|
2020-06-07 14:35:08 +00:00
|
|
|
name="default-source-enrollment-if-username",
|
2020-06-29 14:19:39 +00:00
|
|
|
defaults={"expression": PROMPT_POLICY_EXPRESSION},
|
2020-06-07 14:35:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# UserWrite stage to create the user, and login stage to log user in
|
2020-06-29 14:19:39 +00:00
|
|
|
user_write, _ = UserWriteStage.objects.using(db_alias).update_or_create(
|
2020-06-19 18:35:38 +00:00
|
|
|
name="default-source-enrollment-write"
|
|
|
|
)
|
2020-06-29 14:19:39 +00:00
|
|
|
user_login, _ = UserLoginStage.objects.using(db_alias).update_or_create(
|
2020-06-19 18:35:38 +00:00
|
|
|
name="default-source-enrollment-login"
|
|
|
|
)
|
2020-06-07 14:35:08 +00:00
|
|
|
|
2020-06-29 14:19:39 +00:00
|
|
|
binding, _ = FlowStageBinding.objects.using(db_alias).update_or_create(
|
2020-09-25 23:37:06 +00:00
|
|
|
target=flow,
|
|
|
|
stage=prompt_stage,
|
|
|
|
defaults={"order": 0, "re_evaluate_policies": True},
|
2020-06-19 18:35:38 +00:00
|
|
|
)
|
2020-06-29 14:19:39 +00:00
|
|
|
PolicyBinding.objects.using(db_alias).update_or_create(
|
|
|
|
policy=prompt_policy, target=binding, defaults={"order": 0}
|
2020-06-24 21:24:45 +00:00
|
|
|
)
|
2020-06-07 14:35:08 +00:00
|
|
|
|
2020-06-29 14:19:39 +00:00
|
|
|
FlowStageBinding.objects.using(db_alias).update_or_create(
|
2020-07-03 21:34:44 +00:00
|
|
|
target=flow, stage=user_write, defaults={"order": 1}
|
2020-06-19 18:35:38 +00:00
|
|
|
)
|
2020-06-29 14:19:39 +00:00
|
|
|
FlowStageBinding.objects.using(db_alias).update_or_create(
|
2020-07-03 21:34:44 +00:00
|
|
|
target=flow, stage=user_login, defaults={"order": 2}
|
2020-06-19 18:35:38 +00:00
|
|
|
)
|
2020-06-07 14:35:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
def create_default_source_authentication_flow(
|
|
|
|
apps: Apps, schema_editor: BaseDatabaseSchemaEditor
|
|
|
|
):
|
|
|
|
Flow = apps.get_model("passbook_flows", "Flow")
|
|
|
|
FlowStageBinding = apps.get_model("passbook_flows", "FlowStageBinding")
|
|
|
|
PolicyBinding = apps.get_model("passbook_policies", "PolicyBinding")
|
|
|
|
|
|
|
|
ExpressionPolicy = apps.get_model(
|
|
|
|
"passbook_policies_expression", "ExpressionPolicy"
|
|
|
|
)
|
|
|
|
|
|
|
|
UserLoginStage = apps.get_model("passbook_stages_user_login", "UserLoginStage")
|
|
|
|
|
|
|
|
db_alias = schema_editor.connection.alias
|
|
|
|
|
|
|
|
# Create a policy that only allows this flow when doing an SSO Request
|
2020-06-29 14:19:39 +00:00
|
|
|
flow_policy, _ = ExpressionPolicy.objects.using(db_alias).update_or_create(
|
|
|
|
name="default-source-authentication-if-sso",
|
2020-09-30 17:34:22 +00:00
|
|
|
defaults={
|
|
|
|
"expression": FLOW_POLICY_EXPRESSION,
|
|
|
|
},
|
2020-06-07 14:35:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# This creates a Flow used by sources to authenticate users
|
2020-06-29 14:19:39 +00:00
|
|
|
flow, _ = Flow.objects.using(db_alias).update_or_create(
|
2020-06-07 14:35:08 +00:00
|
|
|
slug="default-source-authentication",
|
|
|
|
designation=FlowDesignation.AUTHENTICATION,
|
2020-09-30 17:34:22 +00:00
|
|
|
defaults={
|
|
|
|
"name": "Welcome to passbook!",
|
|
|
|
},
|
2020-06-07 14:35:08 +00:00
|
|
|
)
|
2020-06-29 14:19:39 +00:00
|
|
|
PolicyBinding.objects.using(db_alias).update_or_create(
|
|
|
|
policy=flow_policy, target=flow, defaults={"order": 0}
|
2020-06-19 18:35:38 +00:00
|
|
|
)
|
2020-06-07 14:35:08 +00:00
|
|
|
|
2020-06-29 14:19:39 +00:00
|
|
|
user_login, _ = UserLoginStage.objects.using(db_alias).update_or_create(
|
2020-06-07 14:35:08 +00:00
|
|
|
name="default-source-authentication-login"
|
|
|
|
)
|
2020-06-29 14:19:39 +00:00
|
|
|
FlowStageBinding.objects.using(db_alias).update_or_create(
|
2020-07-03 21:34:44 +00:00
|
|
|
target=flow, stage=user_login, defaults={"order": 0}
|
2020-06-19 18:35:38 +00:00
|
|
|
)
|
2020-06-07 14:35:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Migration(migrations.Migration):
|
|
|
|
|
|
|
|
dependencies = [
|
2020-07-03 21:34:44 +00:00
|
|
|
("passbook_flows", "0008_default_flows"),
|
2020-06-07 14:35:08 +00:00
|
|
|
("passbook_policies", "0001_initial"),
|
|
|
|
("passbook_policies_expression", "0001_initial"),
|
|
|
|
("passbook_stages_prompt", "0001_initial"),
|
|
|
|
("passbook_stages_user_write", "0001_initial"),
|
|
|
|
("passbook_stages_user_login", "0001_initial"),
|
|
|
|
]
|
|
|
|
|
|
|
|
operations = [
|
|
|
|
migrations.RunPython(create_default_source_enrollment_flow),
|
|
|
|
migrations.RunPython(create_default_source_authentication_flow),
|
|
|
|
]
|