blob: faf79077c95485c07335f05776fdf95cab1086cf (
plain) (
blame)
1
2
3
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
|
"""reCAPTCHA v2 server-side verification."""
import logging
import httpx
log = logging.getLogger(__name__)
VERIFY_URL = "https://www.google.com/recaptcha/api/siteverify"
async def verify_captcha(
secret_key: str, token: str, remote_ip: str | None = None,
) -> bool:
payload: dict[str, str] = {"secret": secret_key, "response": token}
if remote_ip:
payload["remoteip"] = remote_ip
try:
async with httpx.AsyncClient(timeout=5) as client:
resp = await client.post(VERIFY_URL, data=payload)
resp.raise_for_status()
result = resp.json()
if not result.get("success"):
log.info("captcha rejected: %s", result.get("error-codes", []))
return result.get("success", False)
except Exception:
log.exception("captcha verification request failed")
return False
|