feat: add /oauth/callback and /api/oauth/poll relay endpoints
Build and Push Image / build (push) Failing after 17s
Build and Push Image / build (push) Failing after 17s
This commit is contained in:
1 parent
10650eee47
commit
ce459f69b1
3 files changed
+248
No files matched your search
@@ -0,0 +1,113 @@
|
|||||||
|
package server
|
||||||
|
|
||||||
|
import (
|
||||||
|
"html"
|
||||||
|
"net/http"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
const oauthRelayTTL = 5 * time.Minute
|
||||||
|
|
||||||
|
type oauthRelayEntry struct {
|
||||||
|
code string
|
||||||
|
expiresAt time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
type oauthRelayStore struct {
|
||||||
|
mu sync.Mutex
|
||||||
|
entries map[string]oauthRelayEntry
|
||||||
|
ttl time.Duration
|
||||||
|
now func() time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
func newOAuthRelayStore() *oauthRelayStore {
|
||||||
|
return &oauthRelayStore{
|
||||||
|
entries: make(map[string]oauthRelayEntry),
|
||||||
|
ttl: oauthRelayTTL,
|
||||||
|
now: time.Now,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *oauthRelayStore) Set(state, code string) {
|
||||||
|
o.mu.Lock()
|
||||||
|
defer o.mu.Unlock()
|
||||||
|
now := o.now()
|
||||||
|
o.pruneLocked(now)
|
||||||
|
o.entries[state] = oauthRelayEntry{
|
||||||
|
code: code,
|
||||||
|
expiresAt: now.Add(o.ttl),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *oauthRelayStore) Take(state string) (string, bool) {
|
||||||
|
o.mu.Lock()
|
||||||
|
defer o.mu.Unlock()
|
||||||
|
now := o.now()
|
||||||
|
o.pruneLocked(now)
|
||||||
|
entry, ok := o.entries[state]
|
||||||
|
if !ok {
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
delete(o.entries, state)
|
||||||
|
if now.After(entry.expiresAt) {
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
return entry.code, true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *oauthRelayStore) pruneLocked(now time.Time) {
|
||||||
|
for k, v := range o.entries {
|
||||||
|
if now.After(v.expiresAt) {
|
||||||
|
delete(o.entries, k)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) handleOAuthCallback(w http.ResponseWriter, r *http.Request) {
|
||||||
|
q := r.URL.Query()
|
||||||
|
if errParam := q.Get("error"); errParam != "" {
|
||||||
|
desc := q.Get("error_description")
|
||||||
|
if desc == "" {
|
||||||
|
desc = errParam
|
||||||
|
}
|
||||||
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||||
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
|
_, _ = w.Write([]byte(`<!doctype html><html><head><meta charset="utf-8"><title>Authorization Failed</title></head><body style="font-family:system-ui,sans-serif;max-width:480px;margin:4rem auto;padding:0 1rem;text-align:center;"><h1>Authorization Failed</h1><p>` + html.EscapeString(desc) + `</p></body></html>`))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
state := q.Get("state")
|
||||||
|
code := q.Get("code")
|
||||||
|
if state == "" || code == "" {
|
||||||
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||||
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
|
_, _ = w.Write([]byte(`<!doctype html><html><head><meta charset="utf-8"><title>Invalid OAuth Callback</title></head><body style="font-family:system-ui,sans-serif;max-width:480px;margin:4rem auto;padding:0 1rem;text-align:center;"><h1>Invalid OAuth Callback</h1><p>Missing required <code>code</code> or <code>state</code> query parameter.</p></body></html>`))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
s.oauth.Set(state, code)
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
_, _ = w.Write([]byte(`<!doctype html><html><head><meta charset="utf-8"><title>Authorized</title></head><body style="font-family:system-ui,sans-serif;max-width:480px;margin:4rem auto;padding:0 1rem;text-align:center;"><h1>Authorized!</h1><p>Authorized! You can close this tab.</p></body></html>`))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) handleOAuthPoll(w http.ResponseWriter, r *http.Request) {
|
||||||
|
state := r.URL.Query().Get("state")
|
||||||
|
if state == "" {
|
||||||
|
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "state is required"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
code, ok := s.oauth.Take(state)
|
||||||
|
if !ok {
|
||||||
|
writeJSON(w, http.StatusAccepted, map[string]string{"status": "pending"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
writeJSON(w, http.StatusOK, map[string]string{
|
||||||
|
"status": "ready",
|
||||||
|
"code": code,
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -0,0 +1,130 @@
|
|||||||
|
package server
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.site.latha.org/experiments/gitea-k3s-deployer/internal/config"
|
||||||
|
"git.site.latha.org/experiments/gitea-k3s-deployer/internal/deployer"
|
||||||
|
"git.site.latha.org/experiments/gitea-k3s-deployer/internal/store"
|
||||||
|
)
|
||||||
|
|
||||||
|
func newTestServer(t *testing.T) *Server {
|
||||||
|
t.Helper()
|
||||||
|
cfg := config.Config{
|
||||||
|
GiteaURL: "https://git.site.latha.org",
|
||||||
|
}
|
||||||
|
st, err := store.New(filepath.Join(t.TempDir(), "state.json"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("store.New: %v", err)
|
||||||
|
}
|
||||||
|
fg := &fakeGitea{}
|
||||||
|
fa := &fakeApplier{}
|
||||||
|
svc := deployer.NewService(cfg, st, fg, fa)
|
||||||
|
return New(cfg, st, svc, fa)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestOAuthRelayFlow(t *testing.T) {
|
||||||
|
srv := newTestServer(t)
|
||||||
|
ts := httptest.NewServer(srv.Handler())
|
||||||
|
defer ts.Close()
|
||||||
|
|
||||||
|
// 1. Missing state on poll -> 400
|
||||||
|
resp, err := http.Get(ts.URL + "/api/oauth/poll")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("poll missing state: %v", err)
|
||||||
|
}
|
||||||
|
resp.Body.Close()
|
||||||
|
if resp.StatusCode != http.StatusBadRequest {
|
||||||
|
t.Fatalf("expected 400 for missing state, got %d", resp.StatusCode)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. Poll before callback -> 202 pending
|
||||||
|
resp, err = http.Get(ts.URL + "/api/oauth/poll?state=state-123")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("poll pending: %v", err)
|
||||||
|
}
|
||||||
|
var pollOut map[string]string
|
||||||
|
_ = json.NewDecoder(resp.Body).Decode(&pollOut)
|
||||||
|
resp.Body.Close()
|
||||||
|
if resp.StatusCode != http.StatusAccepted || pollOut["status"] != "pending" {
|
||||||
|
t.Fatalf("expected 202 pending, got %d %v", resp.StatusCode, pollOut)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. Callback missing params -> 400
|
||||||
|
resp, err = http.Get(ts.URL + "/oauth/callback?state=state-123")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("callback missing code: %v", err)
|
||||||
|
}
|
||||||
|
resp.Body.Close()
|
||||||
|
if resp.StatusCode != http.StatusBadRequest {
|
||||||
|
t.Fatalf("expected 400 for missing code, got %d", resp.StatusCode)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4. Callback with error -> 400
|
||||||
|
resp, err = http.Get(ts.URL + "/oauth/callback?error=access_denied&state=state-123")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("callback error: %v", err)
|
||||||
|
}
|
||||||
|
resp.Body.Close()
|
||||||
|
if resp.StatusCode != http.StatusBadRequest {
|
||||||
|
t.Fatalf("expected 400 for oauth error, got %d", resp.StatusCode)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 5. Valid callback -> 200 HTML
|
||||||
|
resp, err = http.Get(ts.URL + "/oauth/callback?state=state-123&code=auth-code-xyz")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("callback valid: %v", err)
|
||||||
|
}
|
||||||
|
bodyBytes, _ := io.ReadAll(resp.Body)
|
||||||
|
resp.Body.Close()
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
t.Fatalf("expected 200 for callback, got %d", resp.StatusCode)
|
||||||
|
}
|
||||||
|
if !strings.Contains(string(bodyBytes), "Authorized! You can close this tab.") {
|
||||||
|
t.Fatalf("unexpected callback HTML: %s", string(bodyBytes))
|
||||||
|
}
|
||||||
|
|
||||||
|
// 6. Poll after callback -> 200 ready with code
|
||||||
|
resp, err = http.Get(ts.URL + "/api/oauth/poll?state=state-123")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("poll ready: %v", err)
|
||||||
|
}
|
||||||
|
pollOut = map[string]string{}
|
||||||
|
_ = json.NewDecoder(resp.Body).Decode(&pollOut)
|
||||||
|
resp.Body.Close()
|
||||||
|
if resp.StatusCode != http.StatusOK || pollOut["status"] != "ready" || pollOut["code"] != "auth-code-xyz" {
|
||||||
|
t.Fatalf("expected 200 ready with code, got %d %v", resp.StatusCode, pollOut)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 7. Single-use consumption: second poll -> 202 pending
|
||||||
|
resp, err = http.Get(ts.URL + "/api/oauth/poll?state=state-123")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("poll second time: %v", err)
|
||||||
|
}
|
||||||
|
pollOut = map[string]string{}
|
||||||
|
_ = json.NewDecoder(resp.Body).Decode(&pollOut)
|
||||||
|
resp.Body.Close()
|
||||||
|
if resp.StatusCode != http.StatusAccepted || pollOut["status"] != "pending" {
|
||||||
|
t.Fatalf("expected 202 pending after consumption, got %d %v", resp.StatusCode, pollOut)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestOAuthRelayTTLExpiration(t *testing.T) {
|
||||||
|
now := time.Date(2026, 11, 17, 21, 0, 0, 0, time.UTC)
|
||||||
|
st := newOAuthRelayStore()
|
||||||
|
st.now = func() time.Time { return now }
|
||||||
|
|
||||||
|
st.Set("s1", "c1")
|
||||||
|
now = now.Add(6 * time.Minute)
|
||||||
|
|
||||||
|
if code, ok := st.Take("s1"); ok || code != "" {
|
||||||
|
t.Fatalf("expected expired entry to be rejected, got (%q, %v)", code, ok)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -27,6 +27,7 @@ type Server struct {
|
|||||||
store *store.Store
|
store *store.Store
|
||||||
svc *deployer.Service
|
svc *deployer.Service
|
||||||
k8s k8s.Applier
|
k8s k8s.Applier
|
||||||
|
oauth *oauthRelayStore
|
||||||
mux *http.ServeMux
|
mux *http.ServeMux
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -36,6 +37,7 @@ func New(cfg config.Config, st *store.Store, svc *deployer.Service, ka k8s.Appli
|
|||||||
store: st,
|
store: st,
|
||||||
svc: svc,
|
svc: svc,
|
||||||
k8s: ka,
|
k8s: ka,
|
||||||
|
oauth: newOAuthRelayStore(),
|
||||||
mux: http.NewServeMux(),
|
mux: http.NewServeMux(),
|
||||||
}
|
}
|
||||||
s.routes()
|
s.routes()
|
||||||
@@ -52,6 +54,9 @@ func (s *Server) routes() {
|
|||||||
s.mux.HandleFunc("GET /readyz", s.handleReadyz)
|
s.mux.HandleFunc("GET /readyz", s.handleReadyz)
|
||||||
s.mux.HandleFunc("GET /metrics", s.handleMetrics)
|
s.mux.HandleFunc("GET /metrics", s.handleMetrics)
|
||||||
|
|
||||||
|
s.mux.HandleFunc("GET /oauth/callback", s.handleOAuthCallback)
|
||||||
|
s.mux.HandleFunc("GET /api/oauth/poll", s.handleOAuthPoll)
|
||||||
|
|
||||||
s.mux.HandleFunc("POST /webhook/gitea", s.handleGiteaWebhook)
|
s.mux.HandleFunc("POST /webhook/gitea", s.handleGiteaWebhook)
|
||||||
|
|
||||||
s.mux.HandleFunc("GET /api/apps", s.handleListApps)
|
s.mux.HandleFunc("GET /api/apps", s.handleListApps)
|
||||||
|
|||||||
Reference in new issue
Block a user