auth

package
v0.8.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 16, 2026 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var RequiredScopes = map[string][]string{
	"calendar": {
		"https://www.googleapis.com/auth/calendar",
		"https://www.googleapis.com/auth/calendar.events",
	},
	"drive": {
		"https://www.googleapis.com/auth/drive",
		"https://www.googleapis.com/auth/drive.file",
	},
	"gmail": {
		"https://www.googleapis.com/auth/gmail.readonly",
		"https://www.googleapis.com/auth/gmail.modify",
	},
	"sheets": {
		"https://www.googleapis.com/auth/spreadsheets",
	},
	"docs": {
		"https://www.googleapis.com/auth/documents",
	},
	"slides": {
		"https://www.googleapis.com/auth/presentations",
	},
}

RequiredScopes defines the required OAuth scopes for each service

Functions

func DefaultScopes

func DefaultScopes() []string

DefaultScopes returns the default set of OAuth scopes

func HandleServiceError added in v0.2.0

func HandleServiceError(err error, service string, account string) error

HandleServiceError provides consistent error handling across all services

func IsAPIDisabledError added in v0.2.0

func IsAPIDisabledError(err error) bool

IsAPIDisabledError checks if an error indicates an API is disabled

func IsScopeError added in v0.2.0

func IsScopeError(err error) bool

IsScopeError checks if an error is a ScopeError

Types

type Account added in v0.2.0

type Account struct {
	Email   string `json:"email"`
	Name    string `json:"name"`
	Picture string `json:"picture"`
	// Token and LastUsed are updated concurrently by token refreshes and
	// account lookups. Read them through CurrentToken and LastUsedAt.
	Token       *oauth2.Token `json:"token"`
	LastUsed    time.Time     `json:"last_used"`
	TokenFile   string        `json:"token_file"`
	OAuthClient *OAuthClient  `json:"-"`
	// contains filtered or unexported fields
}

Account represents a single authenticated Google account

func (*Account) CurrentToken added in v0.8.1

func (a *Account) CurrentToken() *oauth2.Token

CurrentToken returns the account's current token.

func (*Account) LastUsedAt added in v0.8.1

func (a *Account) LastUsedAt() time.Time

LastUsedAt returns when the account was last used.

type AccountManager added in v0.2.0

type AccountManager struct {
	// contains filtered or unexported fields
}

AccountManager manages multiple Google accounts

func NewAccountManager added in v0.2.0

func NewAccountManager(ctx context.Context, oauthConfig OAuthConfig) (*AccountManager, error)

NewAccountManager creates a new account manager

func (*AccountManager) AddAccount added in v0.2.0

func (am *AccountManager) AddAccount(ctx context.Context, token *oauth2.Token) (*Account, error)

AddAccount adds a new account or updates existing one

func (*AccountManager) CheckScopes added in v0.2.0

func (am *AccountManager) CheckScopes(ctx context.Context, account *Account, service string) error

CheckScopes verifies if an account has the required scopes for a service

func (*AccountManager) GetAccount added in v0.2.0

func (am *AccountManager) GetAccount(email string) (*Account, error)

GetAccount returns an account by email

func (*AccountManager) GetAccountForContext added in v0.2.0

func (am *AccountManager) GetAccountForContext(ctx context.Context, hint string) (*Account, error)

GetAccountForContext attempts to determine the appropriate account based on context

func (*AccountManager) GetAllOAuthClients added in v0.2.0

func (am *AccountManager) GetAllOAuthClients() map[string]*OAuthClient

GetAllOAuthClients returns OAuth clients for all accounts (for cross-account operations)

func (*AccountManager) GetOAuthConfig added in v0.2.0

func (am *AccountManager) GetOAuthConfig() *oauth2.Config

GetOAuthConfig returns the OAuth configuration

func (*AccountManager) GetTokenScopes added in v0.2.0

func (am *AccountManager) GetTokenScopes(ctx context.Context, account *Account) ([]string, error)

GetTokenScopes retrieves the scopes associated with an account's token

func (*AccountManager) ListAccounts added in v0.2.0

func (am *AccountManager) ListAccounts() []*Account

ListAccounts returns all accounts

func (*AccountManager) RefreshAccountWithScopes added in v0.2.0

func (am *AccountManager) RefreshAccountWithScopes(ctx context.Context, email string, additionalScopes []string) error

RefreshAccountWithScopes refreshes an account's authentication with additional scopes

func (*AccountManager) RefreshToken added in v0.2.0

func (am *AccountManager) RefreshToken(ctx context.Context, email string) error

RefreshToken refreshes the token for an account

func (*AccountManager) RemoveAccount added in v0.2.0

func (am *AccountManager) RemoveAccount(email string) error

RemoveAccount removes an account

type OAuthCallbackServer added in v0.2.0

type OAuthCallbackServer struct {
	// contains filtered or unexported fields
}

OAuthCallbackServer handles OAuth callbacks

func NewOAuthCallbackServer added in v0.2.0

func NewOAuthCallbackServer(config *oauth2.Config) *OAuthCallbackServer

NewOAuthCallbackServer creates a new OAuth callback server

func (*OAuthCallbackServer) GetAuthURL added in v0.2.0

func (s *OAuthCallbackServer) GetAuthURL() string

GetAuthURL returns the OAuth authorization URL

func (*OAuthCallbackServer) GetCallbackURL added in v0.2.0

func (s *OAuthCallbackServer) GetCallbackURL() string

GetCallbackURL returns the callback URL

func (*OAuthCallbackServer) StartAndWaitForCallback added in v0.2.0

func (s *OAuthCallbackServer) StartAndWaitForCallback(ctx context.Context) (*oauth2.Token, error)

StartAndWaitForCallback starts the server and waits for OAuth callback

type OAuthClient

type OAuthClient struct {
	// contains filtered or unexported fields
}

OAuthClient manages OAuth2 authentication for Google APIs

func NewOAuthClient

func NewOAuthClient(ctx context.Context, config OAuthConfig) (*OAuthClient, error)

NewOAuthClient creates a new OAuth client, falling back to the interactive browser flow when no usable token is stored.

func NewOAuthClientNonInteractive added in v0.7.1

func NewOAuthClientNonInteractive(ctx context.Context, config OAuthConfig) (*OAuthClient, error)

NewOAuthClientNonInteractive creates a new OAuth client from a stored token only. When no usable token exists it returns an error instead of starting the browser flow, so callers that have another way to authenticate (multi-account tokens) can fall back without blocking a headless process.

func (*OAuthClient) GetClientOption

func (c *OAuthClient) GetClientOption() option.ClientOption

GetClientOption returns the Google API client option

func (*OAuthClient) GetHTTPClient

func (c *OAuthClient) GetHTTPClient() *http.Client

GetHTTPClient returns the authenticated HTTP client

func (*OAuthClient) Revoke

func (c *OAuthClient) Revoke(ctx context.Context) error

Revoke revokes the current OAuth token

type OAuthConfig

type OAuthConfig struct {
	ClientID     string   `json:"client_id"`
	ClientSecret string   `json:"client_secret"`
	RedirectURI  string   `json:"redirect_uri"`
	TokenFile    string   `json:"token_file"`
	Scopes       []string `json:"scopes"`
}

OAuthConfig holds OAuth configuration

type ScopeError added in v0.2.0

type ScopeError struct {
	Service        string
	RequiredScopes []string
	CurrentScopes  []string
	Account        string
}

ScopeError represents an error when required scopes are missing

func (*ScopeError) Error added in v0.2.0

func (e *ScopeError) Error() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址