authentik icon indicating copy to clipboard operation
authentik copied to clipboard

How to abort flow when a policy in a stage fails?

Open katrinm86 opened this issue 1 year ago • 4 comments

Describe your question/ Is it possible to completely abort a flow when a single policy in one of the stages fails?

Relevant info I have flows with multiple stages and i'm already using deny stages with policies to have some kind of control. each stage is responsible for retrieving or evaluating a specific thing e.g. an invitation flow i'm currently working on:

  • invitation accept stage
  • prompt stage for pre-enroll: get or create existing user via policy and then prompt enrollment type HOWTO: abort flow on error (e.g. necessary attributes missing or user creation problem)
  • deny stage: deny if the user is already active
  • redirect stage: check set enroll type & redirect to next flow according to enroll type
  • deny stage: deny if no enroll type matched

I know that i can add a check in the deny stage after the HOWTO question to deny the user but that means i need to basically add deny stages everywhere and execute similar checks everywhere (e.g. the get or create user code that i need in the prompt stage already did make sure that i have a user or error out).

Is it somehow possible (e.g. programatically; setting next stage or something) to abort the flow execution?

Note: Every stage and flow binding is already set to 'failure result' 'dont pass' and 'policy engine mode' to 'all'; i also don't want to retry or restart the flow. In a nutshell I think I would like to have a dynamically inserted deny stage?

Version and Deployment (please complete the following information):

  • authentik version: 2025.8.4
  • Deployment: docker-compose

katrinm86 avatar Oct 09 '25 08:10 katrinm86

you can access the flow plan with plan = request.context.get("flow_plan")

with that you could do .insert_stage() and use in_memory_stage to add a stage

BeryJu avatar Oct 09 '25 11:10 BeryJu

ok yeah ... i guess that could work but that also means that i need to include the lines in every policy i want it to use.

my feeling is that all of this would be so much simpler if I could just code the whole flow and the policies in python myself 🥲

katrinm86 avatar Oct 10 '25 07:10 katrinm86

You can have those things in a single policy and call that with https://docs.goauthentik.io/customize/policies/expression/#ak_call_policyname-str-kwargs---policyresult

BeryJu avatar Oct 10 '25 12:10 BeryJu

Answered above: insert a deny stage from the policy with request.context["flow_plan"].insert_stage(in_memory_stage(...)), and keep the logic in one shared policy that the others run with ak_call_policy.

dominic-r avatar Nov 04 '26 04:11 dominic-r