Spaces:
Paused
Paused
Update services/captcha_service.py
Browse files- services/captcha_service.py +86 -58
services/captcha_service.py
CHANGED
|
@@ -1,58 +1,86 @@
|
|
| 1 |
-
"""
|
| 2 |
-
Captcha verification service.
|
| 3 |
-
Supports Google reCAPTCHA.
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Captcha verification service.
|
| 3 |
+
Supports Google reCAPTCHA.
|
| 4 |
+
Automatically skips verification for localhost/development.
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
import requests
|
| 8 |
+
from config import Config
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
class CaptchaService:
|
| 12 |
+
"""Captcha verification service."""
|
| 13 |
+
|
| 14 |
+
# IPs that are considered localhost
|
| 15 |
+
LOCALHOST_IPS = {'127.0.0.1', '::1', 'localhost', '0.0.0.0', '10.0.0.0'}
|
| 16 |
+
|
| 17 |
+
@staticmethod
|
| 18 |
+
def is_localhost(remote_ip: str = None) -> bool:
|
| 19 |
+
"""
|
| 20 |
+
Check if the request comes from localhost.
|
| 21 |
+
|
| 22 |
+
Args:
|
| 23 |
+
remote_ip: Client IP address
|
| 24 |
+
|
| 25 |
+
Returns:
|
| 26 |
+
True if localhost
|
| 27 |
+
"""
|
| 28 |
+
if not remote_ip:
|
| 29 |
+
return False
|
| 30 |
+
return remote_ip in CaptchaService.LOCALHOST_IPS or remote_ip.startswith('127.') or remote_ip.startswith('192.168.') or remote_ip.startswith('10.')
|
| 31 |
+
|
| 32 |
+
@staticmethod
|
| 33 |
+
def verify(token: str, remote_ip: str = None) -> tuple[bool, str]:
|
| 34 |
+
"""
|
| 35 |
+
Verify a captcha token.
|
| 36 |
+
Automatically skips verification for localhost requests.
|
| 37 |
+
|
| 38 |
+
Args:
|
| 39 |
+
token: Captcha token from frontend
|
| 40 |
+
remote_ip: Client IP address (optional)
|
| 41 |
+
|
| 42 |
+
Returns:
|
| 43 |
+
Tuple of (success, error_message)
|
| 44 |
+
"""
|
| 45 |
+
# Skip captcha for localhost / development
|
| 46 |
+
if CaptchaService.is_localhost(remote_ip):
|
| 47 |
+
print(f"[Captcha] Skipping verification for localhost ({remote_ip})")
|
| 48 |
+
return True, None
|
| 49 |
+
|
| 50 |
+
if not Config.CAPTCHA_SECRET_KEY:
|
| 51 |
+
# Captcha not configured - allow in development
|
| 52 |
+
print("[Captcha] No CAPTCHA_SECRET_KEY configured, skipping verification")
|
| 53 |
+
return True, None
|
| 54 |
+
|
| 55 |
+
if not token:
|
| 56 |
+
return False, "Captcha token is required"
|
| 57 |
+
|
| 58 |
+
try:
|
| 59 |
+
payload = {
|
| 60 |
+
'secret': Config.CAPTCHA_SECRET_KEY,
|
| 61 |
+
'response': token,
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
if remote_ip:
|
| 65 |
+
payload['remoteip'] = remote_ip
|
| 66 |
+
|
| 67 |
+
response = requests.post(Config.CAPTCHA_VERIFY_URL, data=payload, timeout=10)
|
| 68 |
+
result = response.json()
|
| 69 |
+
|
| 70 |
+
if result.get('success'):
|
| 71 |
+
return True, None
|
| 72 |
+
|
| 73 |
+
# Get error codes
|
| 74 |
+
error_codes = result.get('error-codes', [])
|
| 75 |
+
if 'timeout-or-duplicate' in error_codes:
|
| 76 |
+
return False, "Captcha expired. Please try again."
|
| 77 |
+
elif 'invalid-input-response' in error_codes:
|
| 78 |
+
return False, "Invalid captcha. Please try again."
|
| 79 |
+
else:
|
| 80 |
+
return False, "Captcha verification failed. Please try again."
|
| 81 |
+
|
| 82 |
+
except requests.RequestException as e:
|
| 83 |
+
print(f"Captcha verification error: {e}")
|
| 84 |
+
# On captcha service failure, allow the request through
|
| 85 |
+
# rather than blocking legitimate users
|
| 86 |
+
return True, None
|