From 5e23b117641a9c6b9ac0fd24375b727dac56f484 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Sun, 7 Nov 2021 21:59:50 +0100 Subject: [PATCH] stages/prompt: default prompts to the current value of the context Signed-off-by: Jens Langhammer --- authentik/stages/prompt/models.py | 13 ++++++++----- authentik/stages/prompt/stage.py | 3 ++- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/authentik/stages/prompt/models.py b/authentik/stages/prompt/models.py index 67524873a..32495c447 100644 --- a/authentik/stages/prompt/models.py +++ b/authentik/stages/prompt/models.py @@ -1,5 +1,5 @@ """prompt models""" -from typing import Type +from typing import Any, Optional, Type from uuid import uuid4 from django.db import models @@ -74,13 +74,17 @@ class Prompt(SerializerModel): return PromptSerializer - @property - def field(self) -> CharField: + def field(self, default: Optional[Any]) -> CharField: """Get field type for Challenge and response""" field_class = CharField + _default = self.placeholder + if default: + _default = default kwargs = { "required": self.required, + "default": _default, } + if self.type == FieldTypes.EMAIL: field_class = EmailField if self.type == FieldTypes.NUMBER: @@ -88,7 +92,6 @@ class Prompt(SerializerModel): if self.type == FieldTypes.HIDDEN: field_class = HiddenField kwargs["required"] = False - kwargs["default"] = self.placeholder if self.type == FieldTypes.CHECKBOX: field_class = BooleanField kwargs["required"] = False @@ -97,7 +100,7 @@ class Prompt(SerializerModel): if self.type == FieldTypes.DATE_TIME: field_class = DateTimeField if self.type == FieldTypes.STATIC: - kwargs["initial"] = self.placeholder + kwargs["initial"] = _default kwargs["required"] = False kwargs["label"] = "" if self.type == FieldTypes.SEPARATOR: diff --git a/authentik/stages/prompt/stage.py b/authentik/stages/prompt/stage.py index f26bc2872..8365b0245 100644 --- a/authentik/stages/prompt/stage.py +++ b/authentik/stages/prompt/stage.py @@ -65,7 +65,8 @@ class PromptChallengeResponse(ChallengeResponse): fields = list(self.stage.fields.all()) for field in fields: field: Prompt - self.fields[field.field_key] = field.field + current = plan.context.get(PLAN_CONTEXT_PROMPT, {}).get(field.field_key) + self.fields[field.field_key] = field.field(current) # Special handling for fields with username type # these check for existing users with the same username if field.type == FieldTypes.USERNAME: