23 // chatgptIssuer is the OpenAI login service.
24 chatgptIssuer = "https://auth.openai.com"
26 // chatgptClientID is the OAuth client ID of the Codex CLI. The ChatGPT
27 // backend takes the tokens of this client only.
28 chatgptClientID = "app_EMoamEEZ73f0CkXaXp7hrann"
30 // chatgptAuthClaim is the JWT claim that holds the ChatGPT account ID.
31 chatgptAuthClaim = "https://api.openai.com/auth"
33 // chatgptRefreshMargin is how long before its expiry an access token is
35 chatgptRefreshMargin = 60 * time.Second
37 // chatgptLoginTimeout is how long a device code login waits for the
39 chatgptLoginTimeout = 15 * time.Minute
41 // chatgptTokenFileName is the name of the token file in the data
43 chatgptTokenFileName = "auth.json"
45 // chatgptDefaultPollInterval is the poll interval of a device code
46 // login when the login service gives none.
47 chatgptDefaultPollInterval = 5 * time.Second
50 // errChatGPTLoginNeeded tells that no valid token is available, so the user
51 // must run "onechat login".
52 var errChatGPTLoginNeeded = errors.New(`login needed: run "onechat login"`)
54 // ChatGPTAuth authorizes model calls to the ChatGPT backend with the tokens in
55 // the token file. The tokens belong to OneChat only. The Codex CLI rotates the
56 // refresh token on each refresh, so two programs with one token file log each
59 // Before each call it reads the expiry from the access token. Within
60 // chatgptRefreshMargin of the expiry it gets new tokens with the refresh token
61 // and replaces the file atomically. A mutex lets one refresh run at a time, and
62 // a call that waits for it uses the new tokens.
63 type ChatGPTAuth struct {
70 // ChatGPTDeviceCode is the answer of the user code endpoint. The interval is a
71 // number or a string of seconds.
72 type ChatGPTDeviceCode struct {
73 DeviceAuthID string `json:"device_auth_id"`
74 UserCode string `json:"user_code"`
75 UserCodeAlt string `json:"usercode"`
76 Interval json.RawMessage `json:"interval"`
79 // ChatGPTTokenFile is the content of the token file.
80 type ChatGPTTokenFile struct {
81 AccessToken string `json:"access_token"`
82 RefreshToken string `json:"refresh_token"`
83 IDToken string `json:"id_token"`
84 AccountID string `json:"account_id"`
87 // ChatGPTTokenResponse is the answer of the token endpoint.
88 type ChatGPTTokenResponse struct {
89 AccessToken string `json:"access_token"`
90 RefreshToken string `json:"refresh_token"`
91 IDToken string `json:"id_token"`
94 // ChatGPTDeviceGrant is the answer of the device token endpoint after the user
96 type ChatGPTDeviceGrant struct {
97 AuthorizationCode string `json:"authorization_code"`
98 CodeVerifier string `json:"code_verifier"`
101 // newChatGPTAuth returns the authorizer for the token file at path, with the
102 // login service at issuer.
103 func newChatGPTAuth(path, issuer string) *ChatGPTAuth {
107 http: &http.Client{},
111 // Authorize sets the access token and the account ID, after a refresh when the
112 // token expires within chatgptRefreshMargin.
113 func (a *ChatGPTAuth) Authorize(ctx context.Context, header http.Header) error {
115 defer a.mutex.Unlock()
117 tokens, err := a.read()
119 return fmt.Errorf("read login tokens: %w", err)
122 expiry, found := chatgptTokenExpiry(tokens.AccessToken)
123 if !found || !time.Now().Before(expiry.Add(-chatgptRefreshMargin)) {
124 tokens, err = a.refresh(ctx, tokens)
126 return fmt.Errorf("refresh login tokens: %w", err)
130 header.Set("Authorization", "Bearer "+tokens.AccessToken)
131 header.Set("ChatGPT-Account-Id", tokens.AccountID)
136 // read reads the token file.
137 func (a *ChatGPTAuth) read() (ChatGPTTokenFile, error) {
138 data, err := os.ReadFile(a.path)
139 if errors.Is(err, os.ErrNotExist) {
140 return ChatGPTTokenFile{}, errChatGPTLoginNeeded
144 return ChatGPTTokenFile{}, fmt.Errorf("read token file: %w", err)
147 var tokens ChatGPTTokenFile
149 err = json.Unmarshal(data, &tokens)
150 if err == nil && (tokens.AccessToken == "" || tokens.RefreshToken == "" || tokens.AccountID == "") {
151 err = errors.New("a token or the account ID is missing")
155 return ChatGPTTokenFile{}, fmt.Errorf("token file: %w: %w", err, errChatGPTLoginNeeded)
161 // refresh gets new tokens with the refresh token and replaces the token file.
162 // It keeps the old refresh token and ID token when the answer has none.
163 func (a *ChatGPTAuth) refresh(ctx context.Context, tokens ChatGPTTokenFile) (ChatGPTTokenFile, error) {
164 var response ChatGPTTokenResponse
166 err := a.postJSON(ctx, "/oauth/token", map[string]string{
167 "client_id": chatgptClientID,
168 "grant_type": "refresh_token",
169 "refresh_token": tokens.RefreshToken,
170 "scope": "openid profile email",
172 if err == nil && response.AccessToken == "" {
173 err = errors.New("refresh response has no access token")
177 return ChatGPTTokenFile{}, fmt.Errorf("refresh tokens: %w: %w", err, errChatGPTLoginNeeded)
180 tokens.AccessToken = response.AccessToken
181 tokens.RefreshToken = cmp.Or(response.RefreshToken, tokens.RefreshToken)
182 tokens.IDToken = cmp.Or(response.IDToken, tokens.IDToken)
183 tokens.AccountID = cmp.Or(chatgptAccountID(tokens.AccessToken), chatgptAccountID(tokens.IDToken), tokens.AccountID)
185 err = a.write(tokens)
187 return ChatGPTTokenFile{}, fmt.Errorf("save refreshed tokens: %w", err)
193 // postJSON posts body as JSON to the path of the login service and decodes the
194 // answer into result.
195 func (a *ChatGPTAuth) postJSON(ctx context.Context, path string, body, result any) error {
196 data, err := json.Marshal(body)
198 return fmt.Errorf("encode request: %w", err)
201 _, err = a.post(ctx, path, "application/json", data, result)
203 return fmt.Errorf("post JSON request: %w", err)
209 // write replaces the token file atomically. The file has mode 0600.
210 func (a *ChatGPTAuth) write(tokens ChatGPTTokenFile) error {
211 data, err := json.Marshal(tokens)
213 return fmt.Errorf("encode tokens: %w", err)
216 err = writeFileAtomic(a.path, append(data, '\n'))
218 return fmt.Errorf("write token file: %w", err)
224 // post posts body to the path of the login service and decodes a 200 answer
225 // into result. It returns the HTTP status, or 0 when no answer came. The error
226 // holds the status, but not the body, which can hold a token.
227 func (a *ChatGPTAuth) post(ctx context.Context, path, contentType string, body []byte, result any) (status int, err error) {
228 request, err := http.NewRequestWithContext(ctx, http.MethodPost, a.issuer+path, bytes.NewReader(body))
230 return 0, fmt.Errorf("create request: %w", err)
233 request.Header.Set("Content-Type", contentType)
235 response, err := a.http.Do(request)
237 return 0, fmt.Errorf("send request: %w", err)
241 err = errors.Join(err, response.Body.Close())
244 if response.StatusCode != http.StatusOK {
245 return response.StatusCode, fmt.Errorf("HTTP %d", response.StatusCode)
248 err = json.NewDecoder(response.Body).Decode(result)
250 return response.StatusCode, fmt.Errorf("decode answer: %w", err)
253 return response.StatusCode, nil
256 // Login runs the device code login. It writes the link and the code for the
257 // user to out, waits until the user approves the code, and writes the tokens to
259 func (a *ChatGPTAuth) Login(ctx context.Context, out io.Writer) error {
261 defer a.mutex.Unlock()
263 var code ChatGPTDeviceCode
265 err := a.postJSON(ctx, "/api/accounts/deviceauth/usercode", map[string]string{
266 "client_id": chatgptClientID,
269 return fmt.Errorf("request device code (device code login must be allowed in the ChatGPT security settings): %w", err)
272 userCode := cmp.Or(code.UserCode, code.UserCodeAlt)
273 if code.DeviceAuthID == "" || userCode == "" {
274 return errors.New("device code response has no device ID or user code")
277 _, err = fmt.Fprintf(out, "Open %s/codex/device on any device and enter this code:\n\n %s\n\nWaiting for the approval...\n", a.issuer, userCode)
279 return fmt.Errorf("write device code: %w", err)
282 grant, err := a.poll(ctx, code.DeviceAuthID, userCode, chatgptPollInterval(code.Interval))
284 return fmt.Errorf("wait for device grant: %w", err)
287 tokens, err := a.exchange(ctx, grant)
289 return fmt.Errorf("exchange device grant: %w", err)
292 err = a.write(tokens)
294 return fmt.Errorf("save login tokens: %w", err)
297 _, err = fmt.Fprintln(out, "Logged in.")
299 return fmt.Errorf("write login result: %w", err)
305 // poll asks for the grant of the device code until the user approves it or the
307 func (a *ChatGPTAuth) poll(ctx context.Context, deviceID, userCode string, interval time.Duration) (ChatGPTDeviceGrant, error) {
308 ctx, cancel := context.WithTimeout(ctx, chatgptLoginTimeout)
311 body, err := json.Marshal(map[string]string{
312 "device_auth_id": deviceID,
313 "user_code": userCode,
316 return ChatGPTDeviceGrant{}, fmt.Errorf("encode poll request: %w", err)
320 var grant ChatGPTDeviceGrant
322 status, err := a.post(ctx, "/api/accounts/deviceauth/token", "application/json", body, &grant)
323 if err == nil && (grant.AuthorizationCode == "" || grant.CodeVerifier == "") {
324 err = errors.New("device grant has no authorization code or code verifier")
331 if status != http.StatusForbidden && status != http.StatusNotFound {
332 return ChatGPTDeviceGrant{}, fmt.Errorf("poll device code: %w", err)
335 timer := time.NewTimer(interval)
340 return ChatGPTDeviceGrant{}, fmt.Errorf("wait for approval: %w", ctx.Err())
346 // exchange trades the grant of the device code for tokens.
347 func (a *ChatGPTAuth) exchange(ctx context.Context, grant ChatGPTDeviceGrant) (ChatGPTTokenFile, error) {
349 "grant_type": {"authorization_code"},
350 "code": {grant.AuthorizationCode},
351 "redirect_uri": {a.issuer + "/deviceauth/callback"},
352 "client_id": {chatgptClientID},
353 "code_verifier": {grant.CodeVerifier},
356 var response ChatGPTTokenResponse
358 _, err := a.post(ctx, "/oauth/token", "application/x-www-form-urlencoded", []byte(form.Encode()), &response)
360 return ChatGPTTokenFile{}, fmt.Errorf("exchange code for tokens: %w", err)
363 if response.AccessToken == "" || response.RefreshToken == "" || response.IDToken == "" {
364 return ChatGPTTokenFile{}, errors.New("token response has no access, refresh, or ID token")
367 tokens := ChatGPTTokenFile{
368 AccessToken: response.AccessToken,
369 RefreshToken: response.RefreshToken,
370 IDToken: response.IDToken,
373 tokens.AccountID = cmp.Or(chatgptAccountID(tokens.AccessToken), chatgptAccountID(tokens.IDToken))
374 if tokens.AccountID == "" {
375 return ChatGPTTokenFile{}, errors.New("tokens have no ChatGPT account ID")
381 // chatgptTokenExpiry returns the expiry of a JWT from its exp claim.
382 func chatgptTokenExpiry(token string) (time.Time, bool) {
384 Exp *float64 `json:"exp"`
387 if !chatgptDecodeClaims(token, &claims) || claims.Exp == nil {
388 return time.Time{}, false
391 return time.Unix(int64(*claims.Exp), 0), true
394 // chatgptAccountID returns the ChatGPT account ID of a JWT, or "" when it has
396 func chatgptAccountID(token string) string {
397 var claims map[string]json.RawMessage
399 if !chatgptDecodeClaims(token, &claims) {
404 AccountID string `json:"chatgpt_account_id"`
407 err := json.Unmarshal(claims[chatgptAuthClaim], &auth)
412 return auth.AccountID
415 // chatgptPollInterval returns the poll interval of a device code, given in
416 // seconds as a number or a string.
417 func chatgptPollInterval(raw json.RawMessage) time.Duration {
418 seconds, err := strconv.ParseFloat(strings.Trim(string(raw), `"`), 64)
419 if err != nil || seconds < 0 {
420 return chatgptDefaultPollInterval
423 return time.Duration(seconds * float64(time.Second))
426 // chatgptDecodeClaims decodes the payload of a JWT into claims, without a check
427 // of the signature. The token comes from the login service over HTTPS, and the
428 // client reads only the expiry and the account ID from it.
429 func chatgptDecodeClaims(token string, claims any) bool {
430 parts := strings.Split(token, ".")
435 payload, err := base64.RawURLEncoding.DecodeString(strings.TrimRight(parts[1], "="))
440 return json.Unmarshal(payload, claims) == nil
443 // chatgptLogin runs "onechat login": the device code login into the token file
444 // of the data directory. It holds the lock of the token file, so it does not
445 // run while the daemon runs.
446 func chatgptLogin(ctx context.Context, out io.Writer) (err error) {
447 dir, err := dataDir()
449 return fmt.Errorf("find data directory: %w", err)
452 tokens, err := lockTokens(dir)
454 return fmt.Errorf("lock login tokens: %w", err)
458 err = errors.Join(err, tokens.Close())
461 err = newChatGPTAuth(filepath.Join(dir, chatgptTokenFileName), chatgptIssuer).Login(ctx, out)
463 return fmt.Errorf("complete device login: %w", err)