stages/authenticator_validate: improve validation for not_configured_action

Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
Jens Langhammer 2022-11-28 10:52:51 +01:00
parent 3b8cb9e525
commit e22fce02f8
2 changed files with 13 additions and 9 deletions

View file

@ -11,16 +11,17 @@ from authentik.stages.authenticator_validate.models import AuthenticatorValidate
class AuthenticatorValidateStageSerializer(StageSerializer):
"""AuthenticatorValidateStage Serializer"""
def validate_configuration_stages(self, value):
def validate_not_configured_action(self, value):
"""Ensure that a configuration stage is set when not_configured_action is configure"""
not_configured_action = self.initial_data.get("not_configured_action", [])
if not_configured_action == NotConfiguredAction.CONFIGURE and len(value) < 1:
raise ValidationError(
(
'When "Not configured action" is set to "Configure", '
"you must set a configuration stage."
configuration_stages = self.initial_data.get("configuration_stages", None)
if value == NotConfiguredAction.CONFIGURE:
if not configuration_stages or len(configuration_stages) < 1:
raise ValidationError(
(
'When "Not configured action" is set to "Configure", '
"you must set a configuration stage."
)
)
)
return value
class Meta:

View file

@ -68,7 +68,10 @@ class AuthenticatorValidateStageTests(FlowTestCase):
"""Test serializer validation"""
self.client.force_login(self.user)
serializer = AuthenticatorValidateStageSerializer(
data={"name": generate_id(), "not_configured_action": NotConfiguredAction.CONFIGURE}
data={
"name": generate_id(),
"not_configured_action": NotConfiguredAction.CONFIGURE,
}
)
self.assertFalse(serializer.is_valid())
self.assertIn("not_configured_action", serializer.errors)