From d9b2d6dc485751a2d3dae836a7636fdb6401d64d Mon Sep 17 00:00:00 2001 From: Raymond Penners Date: Sat, 22 Feb 2025 10:48:53 +0100 Subject: [PATCH] refactor(code_verification): abort_if_invalid --- allauth/account/internal/flows/code_verification.py | 6 ++++++ .../account/internal/flows/email_verification_by_code.py | 4 +--- allauth/account/internal/flows/login_by_code.py | 4 +--- allauth/account/internal/flows/password_reset_by_code.py | 4 +--- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/allauth/account/internal/flows/code_verification.py b/allauth/account/internal/flows/code_verification.py index 14cf7bbfd5..9db74b9771 100644 --- a/allauth/account/internal/flows/code_verification.py +++ b/allauth/account/internal/flows/code_verification.py @@ -53,6 +53,12 @@ def record_invalid_attempt(self) -> bool: self.persist() return True + def abort_if_invalid(self): + if not self.is_valid(): + self.abort() + return None + return self + def is_valid(self) -> bool: return time.time() - self.state["at"] <= self.timeout diff --git a/allauth/account/internal/flows/email_verification_by_code.py b/allauth/account/internal/flows/email_verification_by_code.py index 864a58c259..0b92465ced 100644 --- a/allauth/account/internal/flows/email_verification_by_code.py +++ b/allauth/account/internal/flows/email_verification_by_code.py @@ -90,6 +90,4 @@ def resume(cls, request): if not state: return None process = EmailVerificationProcess(request=request, state=state) - if not process.is_valid(): - return None - return process + return process.abort_if_invalid() diff --git a/allauth/account/internal/flows/login_by_code.py b/allauth/account/internal/flows/login_by_code.py index 185cd5d502..6c6fc6f32d 100644 --- a/allauth/account/internal/flows/login_by_code.py +++ b/allauth/account/internal/flows/login_by_code.py @@ -97,6 +97,4 @@ def initiate(cls, *, request, user, email: str, stage=None): @classmethod def resume(cls, stage): process = LoginCodeVerificationProcess(stage=stage) - if not process.is_valid(): - return None - return process + return process.abort_if_invalid() diff --git a/allauth/account/internal/flows/password_reset_by_code.py b/allauth/account/internal/flows/password_reset_by_code.py index 7d9ded4c40..e5c4830165 100644 --- a/allauth/account/internal/flows/password_reset_by_code.py +++ b/allauth/account/internal/flows/password_reset_by_code.py @@ -76,6 +76,4 @@ def resume( if not state: return None process = PasswordResetVerificationProcess(request, state=state) - if not process.is_valid(): - return None - return process + return process.abort_if_invalid()