From 6f3cb6777bfc3a6d44bad4dd792cfd777497f6c8 Mon Sep 17 00:00:00 2001 From: nandi <78769380+codegod100@users.noreply.github.com> Date: Wed, 18 Nov 2026 06:20:41 +0000 Subject: [PATCH] feat: serve latha-auth atproto OAuth client metadata Add GET /oauth/atproto-client-metadata.json next to the existing OAuth relay routes so AT Protocol authorization servers can resolve the latha-auth public client (client_id is this URL). --- internal/server/oauth_relay.go | 28 ++++++++++++++++++++ internal/server/oauth_relay_test.go | 41 +++++++++++++++++++++++++++++ internal/server/server.go | 1 + 3 files changed, 70 insertions(+) diff --git a/internal/server/oauth_relay.go b/internal/server/oauth_relay.go index e46d9a5..6c873c1 100644 --- a/internal/server/oauth_relay.go +++ b/internal/server/oauth_relay.go @@ -64,6 +64,34 @@ func (o *oauthRelayStore) pruneLocked(now time.Time) { } } +// atprotoClientMetadata is the OAuth Client ID Metadata Document for +// the latha-auth AT Protocol public client. It must stay available at +// exactly the client_id URL for the life of any session issued against +// it. Source of truth: `latha-auth atproto --print-client-metadata`. +var atprotoClientMetadata = struct { + ClientID string `json:"client_id"` + ClientName string `json:"client_name"` + GrantTypes []string `json:"grant_types"` + ResponseTypes []string `json:"response_types"` + RedirectURIs []string `json:"redirect_uris"` + Scope string `json:"scope"` + TokenEndpointAuthMethod string `json:"token_endpoint_auth_method"` + DPoPBoundAccessTokens bool `json:"dpop_bound_access_tokens"` +}{ + ClientID: "https://deploy.site.latha.org/oauth/atproto-client-metadata.json", + ClientName: "latha-auth", + GrantTypes: []string{"authorization_code", "refresh_token"}, + ResponseTypes: []string{"code"}, + RedirectURIs: []string{"https://deploy.site.latha.org/oauth/callback"}, + Scope: "atproto transition:generic", + TokenEndpointAuthMethod: "none", + DPoPBoundAccessTokens: true, +} + +func (s *Server) handleAtprotoClientMetadata(w http.ResponseWriter, r *http.Request) { + writeJSON(w, http.StatusOK, atprotoClientMetadata) +} + func (s *Server) handleOAuthCallback(w http.ResponseWriter, r *http.Request) { q := r.URL.Query() if errParam := q.Get("error"); errParam != "" { diff --git a/internal/server/oauth_relay_test.go b/internal/server/oauth_relay_test.go index 755bab8..a97299b 100644 --- a/internal/server/oauth_relay_test.go +++ b/internal/server/oauth_relay_test.go @@ -128,3 +128,44 @@ func TestOAuthRelayTTLExpiration(t *testing.T) { t.Fatalf("expected expired entry to be rejected, got (%q, %v)", code, ok) } } + +func TestAtprotoClientMetadata(t *testing.T) { + srv := newTestServer(t) + ts := httptest.NewServer(srv.Handler()) + defer ts.Close() + + resp, err := http.Get(ts.URL + "/oauth/atproto-client-metadata.json") + if err != nil { + t.Fatalf("get client metadata: %v", err) + } + defer resp.Body.Close() + if resp.StatusCode != http.StatusOK { + t.Fatalf("expected 200, got %d", resp.StatusCode) + } + if ct := resp.Header.Get("Content-Type"); !strings.HasPrefix(ct, "application/json") { + t.Fatalf("expected application/json content type, got %q", ct) + } + body, err := io.ReadAll(resp.Body) + if err != nil { + t.Fatalf("read body: %v", err) + } + var out struct { + ClientID string `json:"client_id"` + RedirectURIs []string `json:"redirect_uris"` + Scope string `json:"scope"` + DPoPBoundAccessTokens bool `json:"dpop_bound_access_tokens"` + } + if err := json.Unmarshal(body, &out); err != nil { + t.Fatalf("unmarshal client metadata: %v", err) + } + // client_id must exactly match the public URL this is served from. + if out.ClientID != "https://deploy.site.latha.org/oauth/atproto-client-metadata.json" { + t.Fatalf("client_id = %q", out.ClientID) + } + if len(out.RedirectURIs) != 1 || out.RedirectURIs[0] != "https://deploy.site.latha.org/oauth/callback" { + t.Fatalf("redirect_uris = %v", out.RedirectURIs) + } + if !out.DPoPBoundAccessTokens { + t.Fatal("dpop_bound_access_tokens must be true") + } +} diff --git a/internal/server/server.go b/internal/server/server.go index 724b6a4..0c4ed62 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -55,6 +55,7 @@ func (s *Server) routes() { s.mux.HandleFunc("GET /metrics", s.handleMetrics) s.mux.HandleFunc("GET /oauth/callback", s.handleOAuthCallback) + s.mux.HandleFunc("GET /oauth/atproto-client-metadata.json", s.handleAtprotoClientMetadata) s.mux.HandleFunc("GET /api/oauth/poll", s.handleOAuthPoll) s.mux.HandleFunc("POST /webhook/gitea", s.handleGiteaWebhook)