// 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" ) // GeminiTokenStorage defines the structure for storing OAuth2 token information, // along with associated user and project details. This data is typically // serialized to a JSON file for persistence. type GeminiTokenStorage struct { // Token holds the raw OAuth2 token data, including access and refresh tokens. Token any `json:"token"` // ProjectID is the Google Cloud Project ID associated with this token. ProjectID string `json:"project_id"` // Email is the email address of the authenticated user. Email string `json:"email"` // Auto indicates if the project ID was automatically selected. Auto bool `json:"auto"` // Checked indicates if the associated Cloud AI API has been verified as enabled. Checked bool `json:"checked"` // Type indicates the type (gemini, chatgpt, claude) of token storage. Type string `json:"type"` } // SaveTokenToFile serializes the token storage to a JSON file. // This method creates the necessary directory structure and writes the token // data in JSON format to the specified file path. It ensures the file is // properly closed after writing. // // Parameters: // - authFilePath: The full path where the token file should be saved // // Returns: // - error: An error if the operation fails, nil otherwise func (ts *GeminiTokenStorage) SaveTokenToFile(authFilePath string) error { ts.Type = "gemini" if err := os.MkdirAll(path.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() { _ = f.Close() }() if err = json.NewEncoder(f).Encode(ts); err != nil { return fmt.Errorf("failed to write token to file: %w", err) } return nil }