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
12// CredentialFileName returns the filename used to persist Codex OAuth credentials.12// CredentialFileName returns the filename used to persist Codex OAuth credentials.
13// When planType is available (e.g. "plus", "team"), it is appended after the email13// When planType is available (e.g. "plus", "team"), it is appended after the email
14// as a suffix to disambiguate subscriptions.14// as a suffix to disambiguate subscriptions.
15func CredentialFileName(email, planType string, includeProviderPrefix bool) string {15func CredentialFileName(email, planType, hashAccountID string, includeProviderPrefix bool) string {
16 email = strings.TrimSpace(email)16 email = strings.TrimSpace(email)
17 plan := normalizePlanTypeForFilename(planType)17 plan := normalizePlanTypeForFilename(planType)
18 18
19 prefix := ""19 prefix := ""
20 if includeProviderPrefix {20 if includeProviderPrefix {
21 prefix = "codex-"21 prefix = "codex"
22 }22 }
23 23
24 if plan == "" {24 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)
26 }28 }
27 return fmt.Sprintf("%s%s-%s.json", prefix, email, plan)29 return fmt.Sprintf("%s-%s-%s.json", prefix, email, plan)
28}30}
29 31
30func normalizePlanTypeForFilename(planType string) string {32func normalizePlanTypeForFilename(planType string) string {
sdk/auth/codex.go+9−1
@@ -2,6 +2,8 @@ package auth · 1 unmodified line
2 2
3import (3import (
4 "context"4 "context"
5 "crypto/sha256"
6 "encoding/hex"
5 "fmt"7 "fmt"
6 "net/http"8 "net/http"
7 "strings"9 "strings"
@@ -192,12 +194,18 @@ waitForCallback: · 184 unmodified lines
192 }194 }
193 195
194 planType := ""196 planType := ""
197 hashAccountID := ""
195 if tokenStorage.IDToken != "" {198 if tokenStorage.IDToken != "" {
196 if claims, errParse := codex.ParseJWTToken(tokenStorage.IDToken); errParse == nil && claims != nil {199 if claims, errParse := codex.ParseJWTToken(tokenStorage.IDToken); errParse == nil && claims != nil {
197 planType = strings.TrimSpace(claims.CodexAuthInfo.ChatgptPlanType)200 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 }
198 }206 }
199 }207 }
200 fileName := codex.CredentialFileName(tokenStorage.Email, planType, true)208 fileName := codex.CredentialFileName(tokenStorage.Email, planType, hashAccountID, true)
201 metadata := map[string]any{209 metadata := map[string]any{
202 "email": tokenStorage.Email,210 "email": tokenStorage.Email,
203 }211 }