feat!: remove gitea command in favor of gt

Gitea login is handled by gt directly. The OAuth relay polling that atproto
shares moves to internal/relay.
This commit is contained in:
nandi committed 2026-11-21 16:31:42 -08:00
1 parent a1cfcc1de7
commit 8bc017ca72
6 files changed
+143 -541

No files matched your search

+90
View File
@@ -0,0 +1,90 @@
package relay
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"time"
)
// DefaultURL is the OAuth callback relay that collects authorization codes.
const DefaultURL = "https://deploy.site.latha.org"
type pollResponse struct {
Status string `json:"status"`
Code string `json:"code,omitempty"`
Error string `json:"error,omitempty"`
}
// RedirectURI is the redirect URI registered for the relay.
func RedirectURI(relayURL string) string {
return strings.TrimRight(relayURL, "/") + "/oauth/callback"
}
// Poll waits for the relay to receive the authorization code for state.
func Poll(ctx context.Context, client *http.Client, relayURL, state string, interval time.Duration) (string, error) {
if client == nil {
client = &http.Client{Timeout: 15 * time.Second}
}
if interval <= 0 {
interval = 2 * time.Second
}
pollURL := strings.TrimRight(relayURL, "/") + "/api/oauth/poll?state=" + url.QueryEscape(state)
ticker := time.NewTicker(interval)
defer ticker.Stop()
for {
code, ready, err := pollOnce(ctx, client, pollURL)
if err != nil {
return "", err
}
if ready {
return code, nil
}
select {
case <-ctx.Done():
return "", ctx.Err()
case <-ticker.C:
}
}
}
func pollOnce(ctx context.Context, client *http.Client, pollURL string) (string, bool, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, pollURL, nil)
if err != nil {
return "", false, err
}
resp, err := client.Do(req)
if err != nil {
return "", false, err
}
defer resp.Body.Close()
body, err := io.ReadAll(io.LimitReader(resp.Body, 1<<20))
if err != nil {
return "", false, err
}
var out pollResponse
if err := json.Unmarshal(body, &out); err != nil {
return "", false, fmt.Errorf("invalid poll response (%d): %w", resp.StatusCode, err)
}
switch resp.StatusCode {
case http.StatusAccepted:
return "", false, nil
case http.StatusOK:
if out.Status == "ready" && out.Code != "" {
return out.Code, true, nil
}
return "", false, fmt.Errorf("unexpected ready payload: %s", string(body))
default:
return "", false, fmt.Errorf("relay poll failed (%d): %s", resp.StatusCode, string(body))
}
}
+45
View File
@@ -0,0 +1,45 @@
package relay
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"sync/atomic"
"testing"
"time"
)
func TestPoll(t *testing.T) {
var polls atomic.Int32
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/oauth/poll" || r.URL.Query().Get("state") != "st-1" {
w.WriteHeader(http.StatusBadRequest)
return
}
w.Header().Set("Content-Type", "application/json")
if polls.Add(1) < 2 {
w.WriteHeader(http.StatusAccepted)
_ = json.NewEncoder(w).Encode(map[string]string{"status": "pending"})
return
}
_ = json.NewEncoder(w).Encode(map[string]string{"status": "ready", "code": "code-abc"})
}))
defer srv.Close()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
code, err := Poll(ctx, srv.Client(), srv.URL, "st-1", 10*time.Millisecond)
if err != nil {
t.Fatalf("Poll: %v", err)
}
if code != "code-abc" {
t.Fatalf("code = %q", code)
}
}
func TestRedirectURI(t *testing.T) {
if got := RedirectURI("https://x.example/"); got != "https://x.example/oauth/callback" {
t.Fatalf("got %q", got)
}
}