mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-11-16 22:57:53 +08:00
92 lines
3.4 KiB
Go
92 lines
3.4 KiB
Go
package codex
|
|
|
|
// PKCECodes holds the verification codes for the OAuth2 PKCE (Proof Key for Code Exchange) flow.
|
|
// PKCE is an extension to the Authorization Code flow to prevent CSRF and authorization code injection attacks.
|
|
type PKCECodes struct {
|
|
// CodeVerifier is the cryptographically random string used to correlate
|
|
// the authorization request to the token request
|
|
CodeVerifier string `json:"code_verifier"`
|
|
// CodeChallenge is the SHA256 hash of the code verifier, base64url-encoded
|
|
CodeChallenge string `json:"code_challenge"`
|
|
}
|
|
|
|
// CodexTokenData holds the OAuth token information obtained from OpenAI.
|
|
// It includes the ID token, access token, refresh token, and associated user details.
|
|
type CodexTokenData struct {
|
|
// IDToken is the JWT ID token containing user claims
|
|
IDToken string `json:"id_token"`
|
|
// AccessToken is the OAuth2 access token for API access
|
|
AccessToken string `json:"access_token"`
|
|
// RefreshToken is used to obtain new access tokens
|
|
RefreshToken string `json:"refresh_token"`
|
|
// AccountID is the OpenAI account identifier
|
|
AccountID string `json:"account_id"`
|
|
// Email is the OpenAI account email
|
|
Email string `json:"email"`
|
|
// Expire is the timestamp of the token expire
|
|
Expire string `json:"expired"`
|
|
// PlanType is the subscription plan type (e.g. "free", "plus", "pro", "team")
|
|
PlanType string `json:"plan_type,omitempty"`
|
|
}
|
|
|
|
// CodexSIWCTokenData holds the OAuth token information obtained from OpenAI's Sign in with ChatGPT (SIWC) flow.
|
|
type CodexSIWCTokenData struct {
|
|
AccessToken string `json:"access_token"`
|
|
RefreshToken string `json:"refresh_token"`
|
|
IDToken string `json:"id_token,omitempty"`
|
|
ClientID string `json:"client_id"`
|
|
Scope string `json:"scope,omitempty"`
|
|
Scopes []string `json:"scopes,omitempty"`
|
|
Expire string `json:"expired"`
|
|
ExpiresAt string `json:"expires_at,omitempty"`
|
|
EarliestRefreshAt any `json:"earliest_refresh_at,omitempty"`
|
|
LastRefresh string `json:"last_refresh"`
|
|
DeviceID string `json:"device_id,omitempty"`
|
|
}
|
|
|
|
// ToMap converts CodexSIWCTokenData into a map suitable for credential metadata persistence.
|
|
func (d *CodexSIWCTokenData) ToMap() map[string]any {
|
|
if d == nil {
|
|
return nil
|
|
}
|
|
out := map[string]any{
|
|
"access_token": d.AccessToken,
|
|
"refresh_token": d.RefreshToken,
|
|
"client_id": d.ClientID,
|
|
"expired": d.Expire,
|
|
"last_refresh": d.LastRefresh,
|
|
}
|
|
if d.ExpiresAt != "" {
|
|
out["expires_at"] = d.ExpiresAt
|
|
}
|
|
if d.EarliestRefreshAt != nil {
|
|
out["earliest_refresh_at"] = d.EarliestRefreshAt
|
|
}
|
|
if d.IDToken != "" {
|
|
out["id_token"] = d.IDToken
|
|
}
|
|
if d.Scope != "" {
|
|
out["scope"] = d.Scope
|
|
}
|
|
if len(d.Scopes) > 0 {
|
|
scopesCopy := make([]string, len(d.Scopes))
|
|
copy(scopesCopy, d.Scopes)
|
|
out["scopes"] = scopesCopy
|
|
}
|
|
if d.DeviceID != "" {
|
|
out["device_id"] = d.DeviceID
|
|
}
|
|
return out
|
|
}
|
|
|
|
// CodexAuthBundle aggregates all authentication-related data after the OAuth flow is complete.
|
|
// This includes the API key, token data, and the timestamp of the last refresh.
|
|
type CodexAuthBundle struct {
|
|
// APIKey is the OpenAI API key obtained from token exchange
|
|
APIKey string `json:"api_key"`
|
|
// TokenData contains the OAuth tokens from the authentication flow
|
|
TokenData CodexTokenData `json:"token_data"`
|
|
// LastRefresh is the timestamp of the last token refresh
|
|
LastRefresh string `json:"last_refresh"`
|
|
}
|