History

feat(auth): add hashed account ID to credential filenames for team plans

Luis Pater committed 9 months ago

Commit 059bfee1 parent 7beaf0e2 files · +15 −5Browse files

Unified · Split

2 files
  1. internal/auth/codex/filename.go +6 −4
  2. sdk/auth/codex.go +9 −1
internal/auth/codex/filename.go+6−4
@@ -12,19 +12,21 @@ import ( · 11 unmodified lines
1212 // CredentialFileName returns the filename used to persist Codex OAuth credentials.
1313 // When planType is available (e.g. "plus", "team"), it is appended after the email
1414 // as a suffix to disambiguate subscriptions.
15−func CredentialFileName(email, planType string, includeProviderPrefix bool) string {
15+func CredentialFileName(email, planType, hashAccountID string, includeProviderPrefix bool) string {
1616 email = strings.TrimSpace(email)
1717 plan := normalizePlanTypeForFilename(planType)
1818
1919 prefix := ""
2020 if includeProviderPrefix {
21− prefix = "codex-"
21+ prefix = "codex"
2222 }
2323
2424 if plan == "" {
25− return fmt.Sprintf("%s%s.json", prefix, email)
25+ return fmt.Sprintf("%s-%s.json", prefix, email)
26+ } else if plan == "team" {
27+ return fmt.Sprintf("%s-%s-%s-%s.json", prefix, hashAccountID, email, plan)
2628 }
27− return fmt.Sprintf("%s%s-%s.json", prefix, email, plan)
29+ return fmt.Sprintf("%s-%s-%s.json", prefix, email, plan)
2830 }
2931
3032 func normalizePlanTypeForFilename(planType string) string {
sdk/auth/codex.go+9−1
@@ -2,6 +2,8 @@ package auth · 1 unmodified line
22
33 import (
44 "context"
5+ "crypto/sha256"
6+ "encoding/hex"
57 "fmt"
68 "net/http"
79 "strings"
@@ -192,12 +194,18 @@ waitForCallback: · 184 unmodified lines
192194 }
193195
194196 planType := ""
197+ hashAccountID := ""
195198 if tokenStorage.IDToken != "" {
196199 if claims, errParse := codex.ParseJWTToken(tokenStorage.IDToken); errParse == nil && claims != nil {
197200 planType = strings.TrimSpace(claims.CodexAuthInfo.ChatgptPlanType)
201+ accountID := strings.TrimSpace(claims.CodexAuthInfo.ChatgptAccountID)
202+ if accountID != "" {
203+ digest := sha256.Sum256([]byte(accountID))
204+ hashAccountID = hex.EncodeToString(digest[:])[:8]
205+ }
198206 }
199207 }
200− fileName := codex.CredentialFileName(tokenStorage.Email, planType, true)
208+ fileName := codex.CredentialFileName(tokenStorage.Email, planType, hashAccountID, true)
201209 metadata := map[string]any{
202210 "email": tokenStorage.Email,
203211 }