diff options
Diffstat (limited to 'packages/meshbay-hub/src/meshbay_hub/captcha.py')
| -rw-r--r-- | packages/meshbay-hub/src/meshbay_hub/captcha.py | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/packages/meshbay-hub/src/meshbay_hub/captcha.py b/packages/meshbay-hub/src/meshbay_hub/captcha.py new file mode 100644 index 0000000..faf7907 --- /dev/null +++ b/packages/meshbay-hub/src/meshbay_hub/captcha.py @@ -0,0 +1,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 |