Documentation
¶
Overview ¶
Package gemini provides authentication and token management functionality for Google's Gemini AI services. It handles OAuth2 authentication flows, including obtaining tokens via web-based authorization, storing tokens, and refreshing them when they expire.
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.
Index ¶
- Constants
- Variables
- func CheckCloudAPIIsEnabled(ctx context.Context, httpClient *http.Client, projectID string) (bool, error)
- func CredentialFileName(email, projectID string, includeProviderPrefix bool) string
- func FetchGCPProjects(ctx context.Context, httpClient *http.Client) ([]interfaces.GCPProjectProjects, error)
- func PerformCLISetup(ctx context.Context, httpClient *http.Client, storage *TokenStorage, ...) error
- func TokenAndConfigFromMetadata(metadata map[string]any) (map[string]any, *oauth2.Token, *oauth2.Config)
- type Auth
- type CLISetupOptions
- type ProjectSelectionRequiredError
- type TokenStorage
- type WebLoginOptions
Constants ¶
const ( ClientID = "681255809395-oo8ft2oprdrnp9e3aqf6av3hmdib135j.apps.googleusercontent.com" ClientSecret = "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" DefaultCallbackPort = 8085 )
OAuth configuration constants for Gemini
Variables ¶
var Scopes = []string{
"https://www.googleapis.com/auth/cloud-platform",
"https://www.googleapis.com/auth/userinfo.email",
"https://www.googleapis.com/auth/userinfo.profile",
}
Scopes defines the OAuth scopes required for Gemini authentication.
Functions ¶
func CheckCloudAPIIsEnabled ¶
func CheckCloudAPIIsEnabled(ctx context.Context, httpClient *http.Client, projectID string) (bool, error)
CheckCloudAPIIsEnabled verifies that the required Cloud AI API is enabled for the given project, attempting to enable it if necessary.
func CredentialFileName ¶
CredentialFileName returns the filename used to persist Gemini CLI credentials. When projectID represents multiple projects (comma-separated or literal ALL), the suffix is normalized to "all" and a "gemini-" prefix is enforced to keep web and CLI generated files consistent.
func FetchGCPProjects ¶
func FetchGCPProjects(ctx context.Context, httpClient *http.Client) ([]interfaces.GCPProjectProjects, error)
FetchGCPProjects retrieves the list of Google Cloud projects for the authenticated user.
func PerformCLISetup ¶
func PerformCLISetup( ctx context.Context, httpClient *http.Client, storage *TokenStorage, requestedProject string, opts *CLISetupOptions, ) error
PerformCLISetup runs the Gemini CLI onboarding flow for the given project.
Types ¶
type Auth ¶
type Auth struct {
}
Auth provides methods for handling the Gemini OAuth2 authentication flow. It encapsulates the logic for obtaining, storing, and refreshing authentication tokens for Google's Gemini AI services.
func (*Auth) GetAuthenticatedClient ¶
func (g *Auth) GetAuthenticatedClient( ctx context.Context, ts *TokenStorage, cfg *config.Config, opts *WebLoginOptions, ) (*http.Client, error)
GetAuthenticatedClient configures and returns an HTTP client ready for making authenticated API calls. It manages the entire OAuth2 flow, including handling proxies, loading existing tokens, initiating a new web-based OAuth flow if necessary, and refreshing tokens.
Parameters:
- ctx: The context for the HTTP client
- ts: The Gemini token storage containing authentication tokens
- cfg: The configuration containing proxy settings
- opts: Optional parameters to customize browser and prompt behavior
Returns:
- *http.Client: An HTTP client configured with authentication
- error: An error if the client configuration fails, nil otherwise
type CLISetupOptions ¶
type CLISetupOptions struct {
// OnFreeUserProjectConflict is called when a free-tier user's requested project
// differs from the backend-assigned project. It receives the requested and returned
// project IDs and should return the chosen project ID.
// If nil, the backend project is used automatically.
OnFreeUserProjectConflict func(requestedProject, returnedProject string) string
}
CLISetupOptions controls the behavior of PerformCLISetup.
type ProjectSelectionRequiredError ¶
type ProjectSelectionRequiredError struct{}
ProjectSelectionRequiredError indicates that a Gemini project must be explicitly selected.
func (*ProjectSelectionRequiredError) Error ¶
func (e *ProjectSelectionRequiredError) Error() string
type TokenStorage ¶
type TokenStorage 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 authentication provider type, always "gemini" for this storage.
Type string `json:"type"`
// Metadata holds arbitrary key-value pairs injected via hooks.
// It is not exported to JSON directly to allow flattening during serialization.
Metadata map[string]any `json:"-"`
}
TokenStorage stores OAuth2 token information for Google Gemini API authentication. It maintains compatibility with the existing auth system while adding Gemini-specific fields for managing access tokens, refresh tokens, and user account information.
func (*TokenStorage) SaveTokenToFile ¶
func (ts *TokenStorage) SaveTokenToFile(authFilePath string) error
SaveTokenToFile serializes the Gemini 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 for persistent storage. It merges any injected metadata into the top-level JSON object.
Parameters:
- authFilePath: The full path where the token file should be saved
Returns:
- error: An error if the operation fails, nil otherwise
func (*TokenStorage) SetMetadata ¶
func (ts *TokenStorage) SetMetadata(meta map[string]any)
SetMetadata allows external callers to inject metadata into the storage before saving.