import json import re import urllib.parse as up PASSWORD = "NixOS-Test-Pass-99!" def curl(machine, args): return machine.succeed("curl -sS -c /tmp/jar -b /tmp/jar " + args) def post_json(machine, url, payload, bearer=None): body = json.dumps(payload) auth = f" -H 'Authorization: Bearer {bearer}'" if bearer else "" return json.loads( machine.succeed( "curl -sSf -X POST -H 'Content-Type: application/json' -H 'Accept: application/json'" f"{auth} -d '{body}' '{url}'" ) ) def restart_provider(): ratlogin.succeed("systemctl restart ratlogin.service") ratlogin.wait_for_unit("ratlogin.service") ratlogin.wait_for_open_port(9411) def marked(page, name): match = re.search(rf'id="{name}">([^<]+)<', page) assert match, page return match.group(1) def register_url_from(log): client.execute(f"tailscale up --login-server https://headscale.test > {log} 2>&1 &") return client.wait_until_succeeds( f"grep -o 'https://headscale.test/register/[A-Za-z0-9_:.~-]*' {log} | head -1", timeout=120, ).strip() def confirm_registration(headscale_url): page = curl(client, f"-o - -w '\\n%{{http_code}}' '{headscale_url}'") body, status = page.rsplit("\n", 1) assert status.strip() == "200", page action = re.search(r'action="(/register/confirm/[^"]+)"', body) assert action, body field = re.search(r']*name="headscale_register_confirm"[^>]*>', body) assert field, body csrf = re.search(r'value="([^"]+)"', field.group(0)) assert csrf, field.group(0) confirmed = curl( client, f"-o /dev/null -w '%{{http_code}}' -X POST" f" --data-urlencode 'headscale_register_confirm={csrf.group(1)}'" f" 'https://headscale.test{action.group(1)}'", ).strip() assert confirmed == "200", confirmed def start_login(handle, register_url, remember=False): form_url = curl( client, f"-L -o /tmp/form.html -w '%{{url_effective}}' '{register_url}'" ).strip() assert "ratlogin.test/authorize" in form_url, form_url client.succeed("grep -q 'name=\"login_hint\"' /tmp/form.html") extra = "&remember=1" if remember else "" pds_url = curl( client, f"-o /dev/null -w '%{{redirect_url}}' '{form_url}&login_hint={handle}{extra}'", ).strip() assert pds_url.startswith("https://pds.test/oauth/authorize"), pds_url return up.parse_qs(up.urlparse(pds_url).query)["request_uri"][0] def sign_in(handle, request_uri, email=False, username=None): result = post_json( client, "https://pds.test/oauth/authorize", { "request_uri": request_uri, "username": username or handle, "password": PASSWORD, "remember_device": False, }, ) location = result["redirect_uri"] if "/oauth/consent" in location: result = post_json( client, "https://pds.test/oauth/authorize/consent", { "request_uri": request_uri, "approved_scopes": ["atproto", "transition:email"] if email else ["atproto"], "remember": False, }, ) location = result["redirect_uri"] if location.startswith("https://pds.test/"): location = client.succeed( f"curl -sS -o /dev/null -w '%{{redirect_url}}' '{location}'" ).strip() assert location.startswith("https://ratlogin.test/atproto/callback"), location return location def provider_callback(location, prefix="https://headscale.test/oidc/callback"): headscale_url = curl(client, f"-o /dev/null -w '%{{redirect_url}}' '{location}'").strip() assert headscale_url.startswith(prefix), headscale_url return headscale_url start_all() dns.wait_for_unit("dnsmasq.service") pds.wait_for_unit("tranquil-pds.service") pds.wait_for_open_port(3000) pds.wait_for_unit("caddy.service") ratlogin.wait_for_unit("ratlogin.service") ratlogin.wait_for_open_port(9411) ratlogin.wait_for_unit("caddy.service") headscale.wait_for_unit("headscale.service") headscale.wait_for_open_port(8080) headscale.wait_for_unit("caddy.service") grafana.wait_for_unit("grafana.service") grafana.wait_for_open_port(3000) grafana.wait_for_unit("caddy.service") client.wait_for_unit("tailscaled.service")