Something went wrong. Try again.
An OIDC provider for AT Protocol OAuth. Rust port of apenwarr/atlogin.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220import html
with subtest("discovery documents are served over TLS"): discovery = json.loads( client.succeed("curl -sSf https://ratlogin.test/.well-known/openid-configuration") ) assert discovery["issuer"] == "https://ratlogin.test", discovery metadata = json.loads(client.succeed("curl -sSf https://ratlogin.test/client-metadata.json")) assert metadata["client_id"] == "https://ratlogin.test/client-metadata.json", metadata client.succeed("curl -sSf https://pds.test/.well-known/oauth-authorization-server")
with subtest("headscale answers the control key request through the proxy"): key = client.succeed("curl -sS -w '\\n%{http_code}' 'https://headscale.test/key?v=138'") assert key.rstrip().endswith("200"), key
with subtest("names resolve through the test DNS server"): answer = client.succeed("dig +short TXT _atproto.carol.test").strip() assert answer == '"did=did:web:carol.pds.test"', answer address = client.succeed("dig +short A carol.test").strip() assert address, "carol.test has no A record"
with subtest("alice, bob, and carol exist on the PDS as did:web identities"): dids = {} tokens = {} for name in ("alice", "bob", "carol"): account = post_json( pds, "https://pds.test/xrpc/com.atproto.server.createAccount", { "handle": name, "password": PASSWORD, "email": f"{name}@pds.test", "didType": "web", }, ) dids[name] = account["did"] tokens[name] = account["accessJwt"] assert dids[name] == f"did:web:{name}.pds.test", dids[name] resolved = client.succeed("curl -sSf https://alice.pds.test/.well-known/atproto-did").strip() assert resolved == dids["alice"], resolved pds.succeed( "sudo -u postgres psql -d tranquil-pds -c" " \"UPDATE users SET email_verified = TRUE WHERE did = 'did:web:alice.pds.test'\"" ) session = json.loads( pds.succeed( f"curl -sSf -H 'Authorization: Bearer {tokens['alice']}'" " https://pds.test/xrpc/com.atproto.server.getSession" ) ) assert session.get("preferredChannel") == "email", session assert session.get("preferredChannelVerified") is True, session
with subtest("carol moves her handle to the bare domain carol.test"): post_json( pds, "https://pds.test/xrpc/com.atproto.identity.updateHandle", {"handle": "carol.test"}, bearer=tokens["carol"], ) session = json.loads( pds.succeed( f"curl -sSf -H 'Authorization: Bearer {tokens['carol']}'" " https://pds.test/xrpc/com.atproto.server.getSession" ) ) assert session["handle"] == "carol.test", session
with subtest("tailscale asks headscale for an OIDC login"): register_url = register_url_from("/tmp/up.log")
with subtest("bob signs in at the PDS but headscale refuses his DID"): request_uri = start_login("bob.pds.test", register_url) location = sign_in("bob.pds.test", request_uri) headscale_url = provider_callback(location) query = up.parse_qs(up.urlparse(headscale_url).query) assert "code" in query, headscale_url page = curl(client, f"-o - -w '\n%{{http_code}}' '{headscale_url}'") body, status = page.rsplit("\n", 1) assert "/register/confirm/" not in body, body assert status.strip() == "401", page assert "Unauthorized" in body, body users = headscale.succeed("headscale users list -o json") assert "bob.pds.test" not in users, users
with subtest("bob is refused at a client whose allow list names only alice"): authorize = "https://ratlogin.test/authorize?" + up.urlencode( { "client_id": "caddy", "redirect_uri": "https://ratlogin.test/caddy/callback", "response_type": "code", "scope": "openid profile groups", "state": "caddy-state", "nonce": "caddy-nonce", "login_hint": "bob.pds.test", } ) pds_url = curl(client, f"-o /dev/null -w '%{{redirect_url}}' '{authorize}'").strip() assert pds_url.startswith("https://pds.test/oauth/authorize"), pds_url request_uri = up.parse_qs(up.urlparse(pds_url).query)["request_uri"][0] location = sign_in("bob.pds.test", request_uri) refused = provider_callback(location, prefix="https://ratlogin.test/caddy/callback") query = up.parse_qs(up.urlparse(refused).query) assert query["error"] == ["access_denied"], refused assert query["state"] == ["caddy-state"], refused assert query["iss"] == ["https://ratlogin.test"], refused assert "code" not in query, refused
with subtest("alice signs in and the provider returns a code to headscale"): request_uri = start_login("alice.pds.test", register_url, remember=True) restart_provider() location = sign_in("alice.pds.test", request_uri) headscale_url = provider_callback(location) assert "code=" in headscale_url, headscale_url restart_provider()
with subtest("headscale redeems the code and asks alice to confirm the node"): confirm_registration(headscale_url)
with subtest("the node is registered under alice's handle only"): client.wait_until_succeeds("tailscale status | grep -q '100.64'") users = headscale.succeed("headscale users list -o json") assert "alice.pds.test" in users, users assert "bob.pds.test" not in users, users alice_user = next(user for user in json.loads(users) if user.get("name") == "alice.pds.test") assert not alice_user.get("email"), alice_user nodes = headscale.succeed("headscale nodes list -o json") assert "client" in nodes, nodes
with subtest("alice re-registers from her remembered sign-in"): client.succeed("grep -q '__Host-ratlogin-session' /tmp/jar") client.succeed("tailscale logout") register_url = register_url_from("/tmp/up-remembered.log") page_url = curl( client, f"-L -o /tmp/continue.html -w '%{{url_effective}}' '{register_url}'" ).strip() assert "ratlogin.test/authorize" in page_url, page_url page = client.succeed("cat /tmp/continue.html") assert "Sign in to Headscale as alice.pds.test" in page, page assert "For <code>headscale.test</code>." in page, page fields = re.findall(r'<input type="hidden" name="([^"]+)" value="([^"]*)">', page) form = up.urlencode([(name, html.unescape(value)) for name, value in fields]) headscale_url = curl( client, f"-o /dev/null -w '%{{redirect_url}}' -X POST" f" -H 'Content-Type: application/x-www-form-urlencoded'" f" --data '{form}' 'https://ratlogin.test/authorize/continue'", ).strip() assert headscale_url.startswith("https://headscale.test/oidc/callback"), headscale_url assert "code=" in headscale_url, headscale_url confirm_registration(headscale_url) client.wait_until_succeeds("tailscale status | grep -q '100.64'") forgotten = curl( client, "-o /dev/null -w '%{http_code}' -X POST 'https://ratlogin.test/session/forget'" ).strip() assert forgotten == "200", forgotten
with subtest("a client without a secret file gets one generated on the host"): listing = ratlogin.succeed( "stat -c '%U:%G %a' /etc/ratlogin/clients/local /etc/ratlogin/clients /etc/ratlogin" ) assert listing.split("\n")[:3] == [ "root:relying-party 640", "root:root 711", "root:root 711", ], listing secret = ratlogin.succeed("runuser -u relying-party -- cat /etc/ratlogin/clients/local").strip() assert ratlogin.succeed("runuser -u caddy -- cat /etc/ratlogin/clients/caddy").strip() ratlogin.fail("runuser -u nobody -- cat /etc/ratlogin/clients/local") ratlogin.fail("runuser -u caddy -- cat /etc/ratlogin/clients/local") ratlogin.fail("runuser -u nobody -- ls /etc/ratlogin/clients") assert len(secret) == 64, secret answer = ratlogin.succeed( f"curl -sS -u local:{secret} -d grant_type=authorization_code -d code=nope" " -d redirect_uri=https://ratlogin.test/local/callback https://ratlogin.test/token" ) assert '"error":"invalid_grant"' in answer, answer order = ratlogin.succeed( "systemctl show -p ActiveEnterTimestampMonotonic --value" " ratlogin-client-secrets.service caddy.service" ).split() assert int(order[0]) < int(order[1]), order
with subtest("the signing key rotates and the old key stays published"): before = json.loads(client.succeed("curl -sSf https://ratlogin.test/.well-known/jwks.json")) assert [key["kty"] for key in before["keys"]] == ["RSA", "EC"], before assert before["keys"][1]["crv"] == "P-256", before client_before = json.loads(client.succeed("curl -sSf https://ratlogin.test/client-jwks.json")) assert len(client_before["keys"]) == 1, client_before metadata = json.loads(client.succeed("curl -sSf https://ratlogin.test/client-metadata.json")) assert metadata["token_endpoint_auth_method"] == "private_key_jwt", metadata assert metadata["jwks_uri"] == "https://ratlogin.test/client-jwks.json", metadata ratlogin.succeed("systemctl stop ratlogin.service") ratlogin.succeed("runuser -u ratlogin -- ratlogin rotate-key --state-dir /var/lib/ratlogin") ratlogin.succeed("systemctl start ratlogin.service") ratlogin.wait_for_open_port(9411) after = json.loads(client.succeed("curl -sSf https://ratlogin.test/.well-known/jwks.json")) kids = [key["kid"] for key in after["keys"]] assert len(kids) == 4, after assert before["keys"][0]["kid"] == kids[1], after assert before["keys"][1]["kid"] == kids[3], after client_after = json.loads(client.succeed("curl -sSf https://ratlogin.test/client-jwks.json")) client_kids = [key["kid"] for key in client_after["keys"]] assert len(client_kids) == 2, client_after assert client_before["keys"][0]["kid"] == client_kids[1], client_after
with subtest("carol signs in with a handle resolved only through DNS TXT"): client.succeed("tailscale logout") register_url = register_url_from("/tmp/up2.log") request_uri = start_login("carol.test", register_url) location = sign_in("carol.test", request_uri) headscale_url = provider_callback(location) assert "code=" in headscale_url, headscale_url confirm_registration(headscale_url) client.wait_until_succeeds("tailscale status | grep -q '100.64'") users = headscale.succeed("headscale users list -o json") assert "carol.test" in users, users nodes = headscale.succeed("headscale nodes list -o json") assert "carol.test" in nodes, nodes