""" WebSub hub endpoint (v2.10.0, ADR-071) POST /websub with hub.mode subscribe/unsubscribe, hub.topic, hub.callback, optional hub.secret and hub.lease_seconds. Answers 202 and verifies later (spec 5.1); errors are 4xx with a plain-text reason. """ from flask import Blueprint, current_app, request from starpunk.database import get_db from starpunk.rate_limiter import SharedRateLimiter from starpunk import websub bp = Blueprint("websub", __name__) # Subscription requests per client IP per minute # Shared by all server processes (v2.11.0). Needs TRUSTED_PROXY=true behind a # reverse proxy, or every client shares the proxy's address. _limiter = SharedRateLimiter(limit=30, window_seconds=60, prefix="websub") def _text(message: str, status: int): return message + "\n", status, {"Content-Type": "text/plain; charset=utf-8"} @bp.route("/websub", methods=["POST"]) def hub(): if not websub.enabled(): return _text("WebSub is not enabled on this site", 404) from starpunk.routes.webmention import get_client_ip if not _limiter.is_allowed(get_client_ip()): return _text("Too many requests", 429) # Media types are case-insensitive (v2.10.1) if request.mimetype != "application/x-www-form-urlencoded": return _text("Send an application/x-www-form-urlencoded body", 400) try: websub.accept_subscription_request(request.form, get_db(current_app)) except websub.SubscriptionError as e: return _text(str(e), e.status) return _text("Accepted; the subscription will be verified", 202) @bp.route("/websub", methods=["GET"]) def hub_info(): return _text( "WebSub hub for this site's own feeds. Subscribe with a POST " "(https://www.w3.org/TR/websub/).", 200)