mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-02-02 04:20:50 +08:00
51 lines
1.5 KiB
Go
51 lines
1.5 KiB
Go
// Package gemini provides authentication and token management functionality
|
|
// for Google's Gemini AI services. It handles OAuth2 token storage, serialization,
|
|
// and retrieval for maintaining authenticated sessions with the Gemini API.
|
|
package gemini
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"time"
|
|
|
|
"github.com/router-for-me/CLIProxyAPI/v6/internal/misc"
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
// GeminiWebTokenStorage stores cookie information for Google Gemini Web authentication.
|
|
type GeminiWebTokenStorage struct {
|
|
Secure1PSID string `json:"secure_1psid"`
|
|
Secure1PSIDTS string `json:"secure_1psidts"`
|
|
Type string `json:"type"`
|
|
LastRefresh string `json:"last_refresh,omitempty"`
|
|
}
|
|
|
|
// SaveTokenToFile serializes the Gemini Web token storage to a JSON file.
|
|
func (ts *GeminiWebTokenStorage) SaveTokenToFile(authFilePath string) error {
|
|
misc.LogSavingCredentials(authFilePath)
|
|
ts.Type = "gemini-web"
|
|
if ts.LastRefresh == "" {
|
|
ts.LastRefresh = time.Now().Format(time.RFC3339)
|
|
}
|
|
if err := os.MkdirAll(filepath.Dir(authFilePath), 0700); err != nil {
|
|
return fmt.Errorf("failed to create directory: %v", err)
|
|
}
|
|
|
|
f, err := os.Create(authFilePath)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to create token file: %w", err)
|
|
}
|
|
defer func() {
|
|
if errClose := f.Close(); errClose != nil {
|
|
log.Errorf("failed to close file: %v", errClose)
|
|
}
|
|
}()
|
|
|
|
if err = json.NewEncoder(f).Encode(ts); err != nil {
|
|
return fmt.Errorf("failed to write token to file: %w", err)
|
|
}
|
|
return nil
|
|
}
|