diff --git a/deploy/interop/docker-compose.interop.yml b/deploy/interop/docker-compose.interop.yml new file mode 100644 index 0000000..6e17017 --- /dev/null +++ b/deploy/interop/docker-compose.interop.yml @@ -0,0 +1,82 @@ +# Federation interop test: Scrobbler and a real GoToSocial server on one private network. +# +# docker compose -f docker-compose.yml -f deploy/interop/docker-compose.interop.yml up -d --build +# ...create the GoToSocial user (see scripts/interop/run.sh), then run the driver. +# +# TEST ONLY: GoToSocial runs over plain HTTP and skips TLS verification; Scrobbler is +# served over HTTPS by a Caddy proxy with a throwaway certificate, and its "insecure +# testing" switch lets it reach GoToSocial over HTTP on this private network. All on the +# reserved .test domain. Never use these settings anywhere else. +name: scrobbler-interop + +x-federation: &federation + DATABASE_URL: postgresql+psycopg://scrobbler:scrobbler@db:5432/scrobbler + FEDERATION_ENABLED: "1" + FEDERATION_DOMAIN: scrobble.test + FEDERATION_BASE_URL: https://scrobble.test + FEDERATION_KEY_SECRET: interop-test-only-secret-0123456789abcdef + FEDERATION_INSECURE_TESTING: "1" + UI_BASE_URL: https://scrobble.test + +networks: + fedi: + ipam: + config: + - subnet: 172.30.0.0/24 + +services: + db: + ports: !reset [] + networks: [fedi] + + api: + environment: *federation + ports: !reset [] + networks: [fedi] + + worker: + environment: *federation + networks: [fedi] + + ui: + environment: + API_BASE_URL: https://scrobble.test + API_INTERNAL_URL: http://api:8000 + ports: !reset [] + networks: [fedi] + + # GoToSocial looks up remote servers over HTTPS: serve scrobble.test with TLS + # (Caddy's internal CA) in front of the UI, which forwards fediverse paths to the API. + tls: + image: caddy:2.10 + command: ["caddy", "reverse-proxy", "--from", "https://scrobble.test", "--to", "http://ui:80", "--internal-certs"] + networks: + fedi: + aliases: [scrobble.test] + + gotosocial: + image: superseriousbusiness/gotosocial:0.22.1 + environment: + GTS_HOST: gts.test + GTS_PROTOCOL: http # local testing only + GTS_PORT: "80" + GTS_DB_TYPE: sqlite + GTS_DB_ADDRESS: /gotosocial/storage/sqlite.db + GTS_HTTP_CLIENT_ALLOW_IPS: 172.30.0.0/24 # let it reach Scrobbler on this network + GTS_HTTP_CLIENT_TLS_INSECURE_SKIP_VERIFY: "true" # Caddy's throwaway certificate + GTS_TRUSTED_PROXIES: 172.30.0.0/24 + GTS_ACCOUNTS_REGISTRATION_OPEN: "false" + GTS_LOG_LEVEL: ${GTS_LOG_LEVEL:-info} + sysctls: + net.ipv4.ip_unprivileged_port_start: 0 # listen on :80 as its non-root user + networks: + fedi: + aliases: [gts.test] + + driver: + image: python:3.13-slim + profiles: [driver] + volumes: + - ./scripts/interop:/interop:ro # relative to the project (first compose file) + command: ["python", "/interop/gotosocial_interop.py"] + networks: [fedi] diff --git a/docs/openapi.json b/docs/openapi.json index e053765..1ecff2a 100644 --- a/docs/openapi.json +++ b/docs/openapi.json @@ -2361,7 +2361,7 @@ } }, "summary": "Unblock an account", - "description": "They can follow you again (they aren't re-added).", + "description": "Their server is told (an Undo of the Block). They can follow you again; they aren't\nre-added.", "tags": [ "Federation" ], @@ -2675,6 +2675,52 @@ } ] }, + "/users/{username}/collections/featured": { + "get": { + "tags": [ + "ActivityPub" + ], + "summary": "Featured", + "description": "Pinned posts (always empty for now).", + "operationId": "federation_featured", + "responses": { + "200": { + "description": "OrderedCollection", + "content": { + "application/activity+json": { + "schema": { + "type": "object" + } + } + } + }, + "404": { + "description": "Unknown, or not sharing (the two are indistinguishable)" + } + }, + "parameters": [ + { + "name": "username", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + } + ] + }, + "parameters": [ + { + "in": "path", + "name": "username", + "required": true, + "schema": { + "type": "string", + "minLength": 1 + } + } + ] + }, "/actor": { "get": { "tags": [ diff --git a/scripts/interop/compose.sh b/scripts/interop/compose.sh new file mode 100755 index 0000000..9674de8 --- /dev/null +++ b/scripts/interop/compose.sh @@ -0,0 +1,5 @@ +#!/usr/bin/env bash +# docker compose for the federation interop stack (Scrobbler + GoToSocial). +set -euo pipefail +root="$(cd "$(dirname "$0")/../.." && pwd)" +exec docker compose -f "$root/docker-compose.yml" -f "$root/deploy/interop/docker-compose.interop.yml" "$@" diff --git a/scripts/interop/gotosocial_interop.py b/scripts/interop/gotosocial_interop.py new file mode 100644 index 0000000..2434d79 --- /dev/null +++ b/scripts/interop/gotosocial_interop.py @@ -0,0 +1,354 @@ +"""Federation interop test: a real GoToSocial account follows a Scrobbler user. + +Runs inside the interop compose network (see deploy/interop/). Standard library only. +Exits non-zero, with a description, on the first thing that doesn't work. +""" + +import html +import http.cookiejar +import json +import os +import re +import sys +import time +import urllib.error +import urllib.parse +import urllib.request + +GTS = "http://gts.test" +SCROBBLER_API = "http://api:8000/api/v1" +GTS_EMAIL = os.environ.get("GTS_EMAIL", "gtsuser@gts.test") +GTS_PASSWORD = os.environ.get("GTS_PASSWORD", "Interop-Test-Password-1") +OOB = "urn:ietf:wg:oauth:2.0:oob" + + +def step(message: str) -> None: + print(f"==> {message}", flush=True) + + +class NoRedirect(urllib.request.HTTPRedirectHandler): + def redirect_request(self, *args, **kwargs): + return None + + +class Browser: + """Just enough of a browser to sign in and authorize an OAuth app.""" + + def __init__(self): + self.jar = http.cookiejar.CookieJar() + self.opener = urllib.request.build_opener( + urllib.request.HTTPCookieProcessor(self.jar), NoRedirect() + ) + + def request(self, method, url, form=None): + data = urllib.parse.urlencode(form).encode() if form is not None else None + req = urllib.request.Request(url, data=data, method=method) + if data is not None: + req.add_header("Content-Type", "application/x-www-form-urlencoded") + try: + with self.opener.open(req, timeout=20) as response: + return response.status, dict(response.headers), response.read().decode() + except urllib.error.HTTPError as err: # includes 3xx, since we don't follow them + return err.code, dict(err.headers), err.read().decode() + + +def api(method, url, token=None, body=None, form=None): + headers = {"Accept": "application/json"} + data = None + if body is not None: + data, headers["Content-Type"] = json.dumps(body).encode(), "application/json" + elif form is not None: + data, headers["Content-Type"] = ( + urllib.parse.urlencode(form).encode(), + "application/x-www-form-urlencoded", + ) + if token: + headers["Authorization"] = f"Bearer {token}" + req = urllib.request.Request(url, data=data, method=method, headers=headers) + try: + with urllib.request.urlopen(req, timeout=20) as response: + raw = response.read() + return response.status, json.loads(raw) if raw else None + except urllib.error.HTTPError as err: + raw = err.read() + try: + return err.code, json.loads(raw) + except json.JSONDecodeError: + return err.code, raw.decode(errors="replace")[:500] + + +def fail(message: str): + print(f"FAILED: {message}", flush=True) + sys.exit(1) + + +def gotosocial_token() -> str: + """Sign in to GoToSocial's web UI and authorize an app (it has no password grant).""" + status, app = api( + "POST", + f"{GTS}/api/v1/apps", + form={ + "client_name": "scrobbler-interop", + "redirect_uris": OOB, + "scopes": "read write follow", + }, + ) + if status != 200: + fail(f"creating a GoToSocial app: {status} {app}") + browser = Browser() + authorize = f"{GTS}/oauth/authorize?" + urllib.parse.urlencode( + { + "response_type": "code", + "client_id": app["client_id"], + "redirect_uri": OOB, + "scope": "read write follow", + } + ) + status, headers, body = browser.request("GET", authorize) + print(f" authorize -> {status} {headers.get('Location', '')}") + status, headers, body = browser.request( + "POST", f"{GTS}/auth/sign_in", {"username": GTS_EMAIL, "password": GTS_PASSWORD} + ) + print(f" sign in -> {status} {headers.get('Location', '')}") + if status not in (302, 303): + fail(f"sign-in didn't redirect: {status} {body[:300]}") + location = urllib.parse.urljoin(GTS, headers.get("Location", "/oauth/authorize")) + status, headers, body = browser.request("GET", location) + print(f" authorize page -> {status} {headers.get('Location', '')}") + if status == 200: + # The consent page: submit its form (with any hidden fields it has) + fields = dict(re.findall(r']*name="([^"]+)"[^>]*value="([^"]*)"', body)) + action = re.search(r'