-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathadmin_codex.go
More file actions
391 lines (331 loc) · 10.4 KB
/
Copy pathadmin_codex.go
File metadata and controls
391 lines (331 loc) · 10.4 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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
package main
import (
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
"path/filepath"
"regexp"
"strings"
"sync"
"time"
)
// Codex OAuth constants (from codex-rs/login/src/server.rs)
const (
CodexOAuthClientID = "app_EMoamEEZ73f0CkXaXp7hrann"
CodexOAuthRedirectURI = "http://localhost:1455/auth/callback"
CodexOAuthTokenURL = "https://auth.openai.com/oauth/token"
CodexOAuthAuthorizeURL = "https://auth.openai.com/oauth/authorize"
)
// CodexOAuthSession stores pending OAuth state
type CodexOAuthSession struct {
AccountID string
Verifier string
Challenge string
State string
CreatedAt time.Time
}
// In-memory store for pending Codex OAuth sessions
var codexOAuthSessions = struct {
sync.RWMutex
sessions map[string]*CodexOAuthSession
}{sessions: make(map[string]*CodexOAuthSession)}
// CodexTokenResponse is the response from the token endpoint
type CodexTokenResponse struct {
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
IDToken string `json:"id_token"`
TokenType string `json:"token_type"`
ExpiresIn int `json:"expires_in"`
Scope string `json:"scope"`
}
// serveCodexAdmin routes Codex admin requests
func (h *proxyHandler) serveCodexAdmin(w http.ResponseWriter, r *http.Request) {
path := strings.TrimPrefix(r.URL.Path, "/admin/codex")
if path == "" {
path = "/"
}
switch {
case path == "/" && r.Method == http.MethodGet:
h.handleCodexList(w, r)
case path == "/add" && r.Method == http.MethodPost:
h.handleCodexAdd(w, r)
case path == "/exchange" && r.Method == http.MethodPost:
h.handleCodexExchange(w, r)
default:
http.NotFound(w, r)
}
}
// GET /admin/codex - list all Codex accounts
func (h *proxyHandler) handleCodexList(w http.ResponseWriter, r *http.Request) {
accounts := h.pool.allAccounts()
type accountInfo struct {
ID string `json:"id"`
PlanType string `json:"plan_type"`
Dead bool `json:"dead"`
Disabled bool `json:"disabled"`
CyberAccess bool `json:"cyber_access,omitempty"`
ExpiresAt time.Time `json:"expires_at,omitempty"`
LastRefresh time.Time `json:"last_refresh,omitempty"`
}
var result []accountInfo
for _, acc := range accounts {
if acc.Type == AccountTypeCodex {
result = append(result, accountInfo{
ID: acc.ID,
PlanType: acc.PlanType,
Dead: acc.Dead,
Disabled: acc.Disabled,
CyberAccess: acc.CyberAccess,
ExpiresAt: acc.ExpiresAt,
LastRefresh: acc.LastRefresh,
})
}
}
respondJSON(w, map[string]any{
"accounts": result,
"count": len(result),
})
}
// POST /admin/codex/add - start OAuth flow
func (h *proxyHandler) handleCodexAdd(w http.ResponseWriter, r *http.Request) {
// Generate PKCE verifier and challenge
verifierBytes := make([]byte, 32)
if _, err := rand.Read(verifierBytes); err != nil {
respondJSONError(w, http.StatusInternalServerError, "failed to generate verifier")
return
}
verifier := base64.RawURLEncoding.EncodeToString(verifierBytes)
challengeHash := sha256.Sum256([]byte(verifier))
challenge := base64.RawURLEncoding.EncodeToString(challengeHash[:])
// Generate state
stateBytes := make([]byte, 32)
if _, err := rand.Read(stateBytes); err != nil {
respondJSONError(w, http.StatusInternalServerError, "failed to generate state")
return
}
state := base64.RawURLEncoding.EncodeToString(stateBytes)
// Build OAuth URL
u, _ := url.Parse(CodexOAuthAuthorizeURL)
q := u.Query()
q.Set("response_type", "code")
q.Set("client_id", CodexOAuthClientID)
q.Set("redirect_uri", CodexOAuthRedirectURI)
q.Set("scope", "openid profile email offline_access")
q.Set("code_challenge", challenge)
q.Set("code_challenge_method", "S256")
q.Set("id_token_add_organizations", "true")
q.Set("codex_cli_simplified_flow", "true")
q.Set("state", state)
q.Set("originator", "codex_cli_rs")
u.RawQuery = q.Encode()
// Store session
session := &CodexOAuthSession{
Verifier: verifier,
Challenge: challenge,
State: state,
CreatedAt: time.Now(),
}
codexOAuthSessions.Lock()
codexOAuthSessions.sessions[verifier] = session
codexOAuthSessions.Unlock()
// Clean up old sessions
go cleanupOldCodexSessions()
respondJSON(w, map[string]any{
"oauth_url": u.String(),
"verifier": verifier,
"state": state,
})
}
// POST /admin/codex/exchange - exchange OAuth code for tokens
func (h *proxyHandler) handleCodexExchange(w http.ResponseWriter, r *http.Request) {
var req struct {
Code string `json:"code"`
Verifier string `json:"verifier"`
}
if r.Header.Get("Content-Type") == "application/json" {
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
respondJSONError(w, http.StatusBadRequest, "invalid json: "+err.Error())
return
}
} else {
req.Code = r.FormValue("code")
req.Verifier = r.FormValue("verifier")
}
code := strings.TrimSpace(req.Code)
verifier := strings.TrimSpace(req.Verifier)
if code == "" || verifier == "" {
respondJSONError(w, http.StatusBadRequest, "code and verifier are required")
return
}
// Look up session
codexOAuthSessions.RLock()
_, ok := codexOAuthSessions.sessions[verifier]
codexOAuthSessions.RUnlock()
if !ok {
respondJSONError(w, http.StatusBadRequest, "invalid or expired session")
return
}
// Exchange code for tokens
tokens, err := codexExchangeCode(code, verifier)
if err != nil {
log.Printf("Codex token exchange failed: %v", err)
respondJSONError(w, http.StatusInternalServerError, "token exchange failed: "+err.Error())
return
}
// Generate account ID from email in id_token
accountID := generateCodexAccountID(tokens.IDToken)
// Save the account
poolDir := filepath.Join(h.cfg.poolDir, "codex")
if err := saveNewCodexAccount(poolDir, accountID, tokens); err != nil {
respondJSONError(w, http.StatusInternalServerError, "failed to save account: "+err.Error())
return
}
// Remove session
codexOAuthSessions.Lock()
delete(codexOAuthSessions.sessions, verifier)
codexOAuthSessions.Unlock()
// Reload accounts
h.reloadAccounts()
respondJSON(w, map[string]any{
"success": true,
"account_id": accountID,
})
}
// codexExchangeCode exchanges an authorization code for tokens
func codexExchangeCode(code, verifier string) (*CodexTokenResponse, error) {
data := url.Values{}
data.Set("grant_type", "authorization_code")
data.Set("client_id", CodexOAuthClientID)
data.Set("code", code)
data.Set("redirect_uri", CodexOAuthRedirectURI)
data.Set("code_verifier", verifier)
req, err := http.NewRequest(http.MethodPost, CodexOAuthTokenURL, strings.NewReader(data.Encode()))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Accept", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("token exchange failed: %s: %s", resp.Status, string(body))
}
var tokens CodexTokenResponse
if err := json.Unmarshal(body, &tokens); err != nil {
return nil, fmt.Errorf("failed to parse token response: %w", err)
}
if tokens.AccessToken == "" {
return nil, fmt.Errorf("empty access token in response")
}
return &tokens, nil
}
// generateCodexAccountID generates an account ID from the id_token email
func generateCodexAccountID(idToken string) string {
// Parse JWT to get email
parts := strings.Split(idToken, ".")
if len(parts) < 2 {
return fmt.Sprintf("codex_%d", time.Now().Unix())
}
payloadBytes, err := base64.RawURLEncoding.DecodeString(parts[1])
if err != nil {
return fmt.Sprintf("codex_%d", time.Now().Unix())
}
var payload map[string]any
if err := json.Unmarshal(payloadBytes, &payload); err != nil {
return fmt.Sprintf("codex_%d", time.Now().Unix())
}
// Try to get email from profile claim
email := ""
if profile, ok := payload["https://api.openai.com/profile"].(map[string]any); ok {
if e, ok := profile["email"].(string); ok {
email = e
}
}
if email == "" {
if e, ok := payload["email"].(string); ok {
email = e
}
}
if email == "" {
return fmt.Sprintf("codex_%d", time.Now().Unix())
}
// Extract meaningful part from email
// e.g., "dlssnetsec+1@gmail.com" -> "dlss_1"
// e.g., "foo@bar.com" -> "foo"
localPart := strings.Split(email, "@")[0]
// Handle plus aliases: user+alias -> user_alias
localPart = strings.ReplaceAll(localPart, "+", "_")
// Truncate long prefixes, keep suffix
// e.g., "dlssnetsec_1" -> "dlss_1"
re := regexp.MustCompile(`^([a-zA-Z]{1,4})[a-zA-Z]*(_\d+)?$`)
if matches := re.FindStringSubmatch(localPart); len(matches) > 0 {
result := matches[1]
if len(matches) > 2 && matches[2] != "" {
result += matches[2]
}
return result
}
// Fallback: just use first 8 chars of local part
if len(localPart) > 8 {
localPart = localPart[:8]
}
return localPart
}
// saveNewCodexAccount saves a new Codex account to the pool directory
func saveNewCodexAccount(poolDir, accountID string, tokens *CodexTokenResponse) error {
// Ensure pool directory exists
if err := os.MkdirAll(poolDir, 0755); err != nil {
return fmt.Errorf("create pool dir: %w", err)
}
filePath := filepath.Join(poolDir, accountID+".json")
// Check if file already exists
if _, err := os.Stat(filePath); err == nil {
// File exists, append a number
for i := 2; i <= 99; i++ {
newPath := filepath.Join(poolDir, fmt.Sprintf("%s_%d.json", accountID, i))
if _, err := os.Stat(newPath); os.IsNotExist(err) {
filePath = newPath
accountID = fmt.Sprintf("%s_%d", accountID, i)
break
}
}
}
authJSON := map[string]any{
"added_at": time.Now().UTC().Format(time.RFC3339Nano),
"tokens": map[string]any{
"id_token": tokens.IDToken,
"access_token": tokens.AccessToken,
"refresh_token": tokens.RefreshToken,
},
}
data, err := json.MarshalIndent(authJSON, "", " ")
if err != nil {
return fmt.Errorf("marshal json: %w", err)
}
if err := os.WriteFile(filePath, data, 0600); err != nil {
return fmt.Errorf("write file: %w", err)
}
log.Printf("Saved new Codex account: %s -> %s", accountID, filePath)
return nil
}
func cleanupOldCodexSessions() {
codexOAuthSessions.Lock()
defer codexOAuthSessions.Unlock()
now := time.Now()
for verifier, session := range codexOAuthSessions.sessions {
if now.Sub(session.CreatedAt) > 10*time.Minute {
delete(codexOAuthSessions.sessions, verifier)
}
}
}