-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathadmin_codex_loopback.go
More file actions
253 lines (229 loc) · 6.7 KB
/
Copy pathadmin_codex_loopback.go
File metadata and controls
253 lines (229 loc) · 6.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package main
import (
"fmt"
"html"
"log"
"net"
"net/http"
"strings"
"sync"
"time"
)
var codexOAuthLoopbackCallback = struct {
sync.Mutex
servers []*http.Server
listeners []net.Listener
}{}
var ensureCodexLoopbackCallbackServersForOperator = ensureCodexLoopbackCallbackServers
func hasPendingAutoCodexSessions() bool {
codexOAuthSessions.RLock()
defer codexOAuthSessions.RUnlock()
for _, session := range codexOAuthSessions.sessions {
if session != nil && session.AutoComplete {
return true
}
}
return false
}
func ensureCodexLoopbackCallbackServers(h *proxyHandler) error {
codexOAuthLoopbackCallback.Lock()
if len(codexOAuthLoopbackCallback.listeners) > 0 {
codexOAuthLoopbackCallback.Unlock()
return nil
}
mux := http.NewServeMux()
mux.HandleFunc("/auth/callback", h.handleCodexLoopbackCallback)
var (
listeners []net.Listener
servers []*http.Server
failures []string
)
for _, addr := range []string{"127.0.0.1:1455", "[::1]:1455"} {
ln, err := net.Listen("tcp", addr)
if err != nil {
failures = append(failures, fmt.Sprintf("%s: %v", addr, err))
continue
}
srv := &http.Server{
Handler: mux,
ReadHeaderTimeout: 5 * time.Second,
IdleTimeout: time.Minute,
}
listeners = append(listeners, ln)
servers = append(servers, srv)
}
if len(listeners) == 0 {
codexOAuthLoopbackCallback.Unlock()
return fmt.Errorf("unable to bind localhost:1455 for Codex OAuth callback (%s)", strings.Join(failures, "; "))
}
codexOAuthLoopbackCallback.listeners = listeners
codexOAuthLoopbackCallback.servers = servers
codexOAuthLoopbackCallback.Unlock()
for idx, ln := range listeners {
srv := servers[idx]
go func(listener net.Listener, server *http.Server) {
log.Printf("codex oauth loopback callback listening on http://%s/auth/callback", listener.Addr().String())
if err := server.Serve(listener); err != nil && err != http.ErrServerClosed {
log.Printf("warning: codex oauth loopback callback server stopped: %v", err)
}
}(ln, srv)
}
return nil
}
func stopCodexLoopbackCallbackServersIfIdle() {
if hasPendingAutoCodexSessions() {
return
}
codexOAuthLoopbackCallback.Lock()
servers := codexOAuthLoopbackCallback.servers
listeners := codexOAuthLoopbackCallback.listeners
codexOAuthLoopbackCallback.servers = nil
codexOAuthLoopbackCallback.listeners = nil
codexOAuthLoopbackCallback.Unlock()
for _, srv := range servers {
_ = srv.Close()
}
for _, ln := range listeners {
_ = ln.Close()
}
}
func scheduleStopCodexLoopbackCallbackServersIfIdle() {
go func() {
time.Sleep(250 * time.Millisecond)
stopCodexLoopbackCallbackServersIfIdle()
}()
}
func (h *proxyHandler) handleCodexLoopbackCallback(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
if !isLoopbackRemoteAddr(r.RemoteAddr) {
http.Error(w, "loopback access required", http.StatusForbidden)
return
}
code := strings.TrimSpace(r.URL.Query().Get("code"))
state := strings.TrimSpace(r.URL.Query().Get("state"))
oauthErr := strings.TrimSpace(r.URL.Query().Get("error"))
stateMatch := false
if state != "" {
_, _, stateMatch = findCodexSessionByState(state)
}
log.Printf(
"codex oauth loopback callback received: has_code=%t has_state=%t has_error=%t state_match=%t",
code != "",
state != "",
oauthErr != "",
stateMatch,
)
if oauthErr != "" {
if state != "" {
if verifier, _, ok := findCodexSessionByState(state); ok {
finalizeCodexSession(verifier)
}
}
renderCodexLoopbackResult(w, false, "", false, "Codex OAuth was cancelled", oauthErr)
return
}
callbackURL := CodexOAuthRedirectURI
if strings.TrimSpace(r.URL.RawQuery) != "" {
callbackURL += "?" + r.URL.RawQuery
}
result, err := h.completeCodexExchange(r.Context(), codexExchangeRequest{
State: state,
CallbackURL: callbackURL,
Lane: "loopback",
})
if err != nil {
log.Printf("codex oauth loopback callback failed: %v", err)
renderCodexLoopbackResult(w, false, "", false, "Failed to add Codex account", err.Error())
return
}
title := "Codex account added"
detail := "You can close this tab and return to codex-pool."
if result.RefreshedExisting {
title = "Codex account refreshed"
detail = "This OAuth flow refreshed an existing Codex seat in place. The total account count may stay the same."
}
log.Printf(
"codex oauth loopback callback completed: account_id=%s refreshed_existing=%t",
result.AccountID,
result.RefreshedExisting,
)
renderCodexLoopbackResult(w, true, result.AccountID, result.RefreshedExisting, title, detail)
}
func renderCodexLoopbackResult(w http.ResponseWriter, success bool, accountID string, refreshedExisting bool, title, detail string) {
statusCode := http.StatusOK
accent := "#3fb950"
closeScript := `<script>setTimeout(function(){ window.close(); }, 1600);</script>`
if !success {
statusCode = http.StatusBadRequest
accent = "#f85149"
closeScript = ""
}
accountLine := ""
if strings.TrimSpace(accountID) != "" {
accountLine = fmt.Sprintf(`<p style="margin: 0 0 10px; color: #c9d1d9;">Account: <code style="background: #21262d; padding: 2px 6px; border-radius: 4px;">%s</code></p>`, html.EscapeString(accountID))
}
notifyScript := fmt.Sprintf(`<script>
(function() {
try {
if (window.opener && !window.opener.closed) {
window.opener.postMessage({
type: 'codex-oauth-result',
success: %t,
account_id: %q,
refreshed_existing: %t,
title: %q,
detail: %q
}, '*');
}
} catch (error) {
// Ignore opener messaging failures.
}
})();
</script>`,
success,
accountID,
refreshedExisting,
title,
detail,
)
page := fmt.Sprintf(`<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>%s</title>
<style>
body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; background: #0d1117; color: #c9d1d9; margin: 0; padding: 24px; }
.card { max-width: 720px; margin: 48px auto; background: #161b22; border: 1px solid #30363d; border-radius: 10px; padding: 24px; box-shadow: 0 16px 40px rgba(0,0,0,0.28); }
h1 { margin: 0 0 12px; color: %s; }
p { line-height: 1.6; color: #8b949e; }
a { color: #58a6ff; }
code { color: #e6edf3; }
</style>
</head>
<body>
<div class="card">
<h1>%s</h1>
%s
<p>%s</p>
<p><a href="/status">Return to pool status</a></p>
</div>
%s
%s
</body>
</html>`,
html.EscapeString(title),
accent,
html.EscapeString(title),
accountLine,
html.EscapeString(detail),
notifyScript,
closeScript,
)
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Header().Set("Cache-Control", "no-store")
w.WriteHeader(statusCode)
_, _ = w.Write([]byte(page))
}