From 0cb4d64b576f90b2020460f22566bcc96f59bc9a Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Fri, 30 Jul 2021 09:39:24 +0200 Subject: [PATCH] stages/email: fix error when re-requesting email after token has expired Signed-off-by: Jens Langhammer --- authentik/stages/email/stage.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/authentik/stages/email/stage.py b/authentik/stages/email/stage.py index f706502ad..6c94e8ed7 100644 --- a/authentik/stages/email/stage.py +++ b/authentik/stages/email/stage.py @@ -67,10 +67,15 @@ class EmailStageView(ChallengeStageView): "user": pending_user, "identifier": f"ak-email-stage-{current_stage.name}-{pending_user}", } - tokens = Token.filter_not_expired(**token_filters) + # Don't check for validity here, we only care if the token exists + tokens = Token.objects.filter(**token_filters) if not tokens.exists(): return Token.objects.create(expires=now() + valid_delta, **token_filters) - return tokens.first() + token = tokens.first() + # Check if token is expired and rotate key if so + if token.is_expired: + token.expire_action() + return token def send_email(self): """Helper function that sends the actual email. Implies that you've