Some checks failed
Code scanning / CodeQL (push) Failing after 15s
Zulip production suite / Ubuntu 22.04 production build (push) Failing after 26s
Zulip production suite / Debian 12 production install with custom db name and user (push) Has been skipped
Zulip production suite / Ubuntu 22.04 production install and PostgreSQL upgrade with pgroonga (push) Has been skipped
Zulip production suite / Ubuntu 24.04 production install (push) Has been skipped
Zulip production suite / Ubuntu 26.04 production install (push) Has been skipped
Zulip production suite / Debian 13 production install (push) Has been skipped
Zulip production suite / 7.0 Version Upgrade (push) Has been skipped
Zulip production suite / 8.0 Version Upgrade (push) Has been skipped
Zulip production suite / 6.0 Version Upgrade (push) Has been skipped
Zulip production suite / 10.0 Version Upgrade (push) Has been skipped
Zulip production suite / 9.0 Version Upgrade (push) Has been skipped
Zulip production suite / 12.0 Version Upgrade (push) Has been skipped
Zulip production suite / 11.0 Version Upgrade (push) Has been skipped
GitHub Actions Security Analysis with zizmor / zizmor (push) Failing after 3s
Zulip CI / Debian 12 (Python 3.11, backend + documentation) (push) Failing after 26s
Zulip CI / Ubuntu 22.04 (Python 3.10, backend + frontend) (push) Failing after 3s
Zulip CI / Ubuntu 24.04 (Python 3.12, backend) (push) Failing after 25s
Zulip CI / Ubuntu 26.04 (Python 3.14, backend) (push) Failing after 25s
Zulip CI / Debian 13 (Python 3.13, backend) (push) Failing after 28s
Zulip production suite / Required jobs (push) Has been cancelled
Zulip CI / Required jobs (push) Has been cancelled
API Documentation Update Check / check-feature-level-updated (push) Has been cancelled
API Documentation Update Check / notify-if-api-docs-changed (push) Has been cancelled
46 lines
1.7 KiB
Python
46 lines
1.7 KiB
Python
import logging
|
|
from datetime import timedelta
|
|
|
|
from altcha.v1 import ChallengeOptions, create_challenge
|
|
from django.conf import settings
|
|
from django.http import HttpRequest, HttpResponseBase
|
|
from django.utils.timezone import now as timezone_now
|
|
from django.utils.translation import gettext as _
|
|
|
|
from zerver.lib.captcha import captcha_enabled
|
|
from zerver.lib.exceptions import JsonableError
|
|
from zerver.lib.response import json_success
|
|
from zerver.lib.typed_endpoint import typed_endpoint_without_parameters
|
|
|
|
|
|
@typed_endpoint_without_parameters
|
|
def get_challenge(
|
|
request: HttpRequest,
|
|
) -> HttpResponseBase:
|
|
if not captcha_enabled(): # nocoverage
|
|
raise JsonableError(_("Challenges are not enabled."))
|
|
|
|
now = timezone_now()
|
|
expires = now + timedelta(minutes=1)
|
|
try:
|
|
challenge = create_challenge(
|
|
ChallengeOptions(
|
|
hmac_key=settings.ALTCHA_HMAC_KEY,
|
|
max_number=500000,
|
|
expires=expires,
|
|
)
|
|
)
|
|
session_challenges = request.session.get("altcha_challenges", [])
|
|
# We prune out expired challenges not for correctness (the
|
|
# expiration is validated separately) but to prevent this from
|
|
# growing without bound
|
|
session_challenges = [(c, e) for (c, e) in session_challenges if e > now.timestamp()]
|
|
request.session["altcha_challenges"] = [
|
|
*session_challenges,
|
|
(challenge.challenge, expires.timestamp()),
|
|
]
|
|
return json_success(request, data=challenge.to_dict())
|
|
except Exception: # nocoverage
|
|
logging.exception("Error while generating challenge")
|
|
raise JsonableError(_("Failed to generate challenge"))
|