Debian Codegraph @

captcha/validators.py

raw symbols SCIP
1 from rest_framework import exceptions
2
3 from django.utils import timezone
4 from django.utils.translation import gettext_lazy as gettext_lazy
5
6 from captcha.conf import settings
7 from captcha.models import CaptchaStore
8
9
10 def captcha_validate(hashkey, response):
11 hashkey, response = hashkey.lower(), response.lower()
12
13 if not settings.CAPTCHA_GET_FROM_POOL:
14 CaptchaStore.remove_expired()
15 if settings.CAPTCHA_TEST_MODE and response.lower() == "passed":
16 try:
17 CaptchaStore.objects.get(hashkey=hashkey).delete()
18 except CaptchaStore.DoesNotExist:
19 pass
20 else:
21 store = CaptchaStore.objects.filter(
22 hashkey=hashkey, expiration__gt=timezone.now()
23 ).first()
24 # A captcha can only be attempted once: invalidate the store
25 # even when the response is wrong, to prevent brute-force attacks.
26 if store is not None:
27 store.delete()
28 if store is None or store.response != response:
29 raise exceptions.ValidationError({"error": gettext_lazy("Invalid CAPTCHA")})