Added shared parameter

This commit is contained in:
Ivan Nikolskiy 2025-05-29 01:14:16 +02:00
parent fcfc724db5
commit 86ef9dc947
2 changed files with 7 additions and 0 deletions

View File

@ -202,6 +202,7 @@ hoptchaPost('/endpoint', payload, onSuccess, onError, function renderCustom(url)
| `response` | Optional custom response function on CAPTCHA failure | `None` | | `response` | Optional custom response function on CAPTCHA failure | `None` |
| `exempt_if` | Skip protection for trusted users | For staff | | `exempt_if` | Skip protection for trusted users | For staff |
| `methods` | HTTP methods to track (POST, GET, etc.) | POST | | `methods` | HTTP methods to track (POST, GET, etc.) | POST |
| `shared` | Share same attempts counter among all endpoints | `False` |
--- ---

View File

@ -56,6 +56,7 @@ def hoptcha_protected(
response=None, response=None,
exempt_if=lambda request: request.user.is_staff or request.user.is_superuser, exempt_if=lambda request: request.user.is_staff or request.user.is_superuser,
backoff=False, backoff=False,
shared=False
): ):
""" """
Enforces CAPTCHA if request exceeds `threshold`. Enforces CAPTCHA if request exceeds `threshold`.
@ -67,7 +68,9 @@ def hoptcha_protected(
- response: optional custom response function on CAPTCHA failure. - response: optional custom response function on CAPTCHA failure.
- exempt_if: skip protection for trusted users. - exempt_if: skip protection for trusted users.
- methods: HTTP methods to track (default: POST). - methods: HTTP methods to track (default: POST).
- shared: Share same attempts counter among all endpoints.
""" """
if isinstance(key, str): if isinstance(key, str):
key_func = BUILTIN_KEYS.get(key) key_func = BUILTIN_KEYS.get(key)
if not key_func: if not key_func:
@ -87,6 +90,9 @@ def hoptcha_protected(
return view_func(request, *args, **kwargs) return view_func(request, *args, **kwargs)
user_key = key_func(request) user_key = key_func(request)
if not shared:
user_key = f"{user_key}:{request.path}"
cache_key = f"hoptcha-attempts:{user_key}" cache_key = f"hoptcha-attempts:{user_key}"
attempts = cache.get(cache_key, 0) attempts = cache.get(cache_key, 0)