From 7ccec3d5a49dc02fc4de269baff6b0a85c5fb6d4 Mon Sep 17 00:00:00 2001 From: Ali Maktabi Date: Mon, 26 Aug 2024 20:22:41 +0330 Subject: [PATCH 1/2] fixed passing context argument to is_observed --- core/constraints/abstract.py | 3 +-- core/constraints/captcha.py | 8 +++++--- prizetap/validators.py | 4 ++-- tokenTap/validators.py | 2 +- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/core/constraints/abstract.py b/core/constraints/abstract.py index e0f70f9..5292c94 100644 --- a/core/constraints/abstract.py +++ b/core/constraints/abstract.py @@ -56,10 +56,9 @@ class ConstraintVerification(ABC): app_name = ConstraintApp.GENERAL.value __response_text = "" - def __init__(self, user_profile, context=None) -> None: + def __init__(self, user_profile) -> None: self.user_profile = user_profile self._param_values = {} - self.context = context def get_info(self, *args, **kwargs): pass diff --git a/core/constraints/captcha.py b/core/constraints/captcha.py index feac1eb..d75cc76 100644 --- a/core/constraints/captcha.py +++ b/core/constraints/captcha.py @@ -16,17 +16,19 @@ class HasVerifiedCloudflareCaptcha(ConstraintVerification): def is_observed(self, *args, **kwargs) -> bool: - if self.context is None or self.context.get("requset") is None: + context = kwargs.get("context") + + if context is None or context.get("requset") is None: return False cloudflare = CloudflareUtil() request_context: RequestContextExtractor = RequestContextExtractor( - self.context["requset"] + context["requset"] ) turnstile_token = request_context.data.get("cf-turnstile-response") - return turnstile_token is not None and cloudflare.is_verified( + return request_context.ip is not None and turnstile_token is not None and cloudflare.is_verified( turnstile_token, request_context.ip ) diff --git a/prizetap/validators.py b/prizetap/validators.py index 3d8e37f..d636658 100644 --- a/prizetap/validators.py +++ b/prizetap/validators.py @@ -30,7 +30,7 @@ def check_user_constraints(self, raise_exception=True): result = dict() for c in self.raffle.constraints.all(): constraint: ConstraintVerification = get_constraint(c.name)( - self.user_profile, context={"request": self.request} + self.user_profile ) constraint.response = c.response try: @@ -56,7 +56,7 @@ def check_user_constraints(self, raise_exception=True): ) else: is_verified = constraint.is_observed( - **cdata, from_time=int(self.raffle.start_at.timestamp()) + **cdata, from_time=int(self.raffle.start_at.timestamp()), context={"request": self.request} ) caching_time = 60 * 60 if is_verified else 60 expiration_time = time.time() + caching_time diff --git a/tokenTap/validators.py b/tokenTap/validators.py index 315e464..3ee9ae3 100644 --- a/tokenTap/validators.py +++ b/tokenTap/validators.py @@ -61,7 +61,6 @@ def check_user_permissions(self, raise_exception=True): for c in self.td.constraints.all(): constraint: ConstraintVerification = get_constraint(c.name)( self.user_profile, - context={"request": self.request} ) constraint.response = c.response try: @@ -85,6 +84,7 @@ def check_user_permissions(self, raise_exception=True): is_verified = constraint.is_observed( **cdata, token_distribution=self.td, + context={"request": self.request} ) caching_time = 60 * 60 if is_verified else 60 expiration_time = time.time() + caching_time From cf505af825ce31a02f2ac50bc4950b3502a4d098 Mon Sep 17 00:00:00 2001 From: Ali Maktabi Date: Mon, 26 Aug 2024 20:31:00 +0330 Subject: [PATCH 2/2] fixed typo error --- core/constraints/captcha.py | 4 ++-- prizetap/validators.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/core/constraints/captcha.py b/core/constraints/captcha.py index d75cc76..458ce4c 100644 --- a/core/constraints/captcha.py +++ b/core/constraints/captcha.py @@ -18,13 +18,13 @@ def is_observed(self, *args, **kwargs) -> bool: context = kwargs.get("context") - if context is None or context.get("requset") is None: + if context is None or context.get("request") is None: return False cloudflare = CloudflareUtil() request_context: RequestContextExtractor = RequestContextExtractor( - context["requset"] + context["request"] ) turnstile_token = request_context.data.get("cf-turnstile-response") diff --git a/prizetap/validators.py b/prizetap/validators.py index d636658..e0ac8f9 100644 --- a/prizetap/validators.py +++ b/prizetap/validators.py @@ -15,7 +15,7 @@ def __init__(self, *args, **kwargs): self.user_profile: UserProfile = kwargs["user_profile"] self.raffle: Raffle = kwargs["raffle"] self.raffle_data: dict = kwargs.get("raffle_data", dict()) - self.request = kwargs.get("requset") + self.request = kwargs.get("request") def can_enroll_in_raffle(self): if not self.raffle.is_claimable: