gmail

package
v3.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2026 License: MIT Imports: 19 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package gmail provides a bounded Gmail metadata and label transport.

Index

Constants

View Source
const ModifyScope = "https://www.googleapis.com/auth/gmail.modify"

Variables

View Source
var ErrCredentialsInvalid = errors.New("Gmail credentials are invalid")

ErrCredentialsInvalid marks stored or imported credentials that cannot be used. Errors that match it keep their own message text.

View Source
var ErrCredentialsNotFound = errors.New("Gmail credentials were not found in macOS Keychain; connect or explicitly import credentials")
View Source
var ErrKeychainUnavailable = errors.New("Gmail credential storage requires macOS with cgo and the Security framework")
View Source
var ErrScopeInsufficient = errors.New("Gmail credentials require gmail.modify or full-mail OAuth scope")

ErrScopeInsufficient means the credentials do not grant gmail.modify or full-mail scope.

Functions

func HasModifyScope

func HasModifyScope(scope string) bool

func KeychainAvailable

func KeychainAvailable() bool

func StoreCredentials

func StoreCredentials(email string, credentials Credentials) error

Types

type API

type API interface {
	Profile(context.Context) (Profile, error)
	ListLabels(context.Context) ([]Label, error)
	FindMessages(context.Context, string) ([]string, error)
	GetMessage(context.Context, string) (Message, error)
	ModifyLabels(context.Context, string, []string, []string) error
}

type AddressStatus added in v3.2.2

type AddressStatus struct {
	Email         string     `json:"email"`
	Status        AuthStatus `json:"status"`
	Detail        string     `json:"detail,omitempty"`
	Hint          string     `json:"hint,omitempty"`
	ProviderEmail string     `json:"providerEmail,omitempty"`
}

AddressStatus is the result of probing the stored credentials for one address.

func ProbeStoredCredentials added in v3.2.2

func ProbeStoredCredentials(ctx context.Context, email string, client *http.Client) AddressStatus

ProbeStoredCredentials loads the Keychain credentials for email, exchanges the refresh token for a new access token and reads the Gmail profile. Rotated credentials are saved as in normal use. It never returns secrets.

type AuthStatus added in v3.2.2

type AuthStatus string

AuthStatus is the Gmail authorization state of one address.

const (
	AuthConnected           AuthStatus = "connected"
	AuthCredentialsMissing  AuthStatus = "credentials_missing"
	AuthKeychainUnavailable AuthStatus = "keychain_unavailable"
	AuthKeychainAccess      AuthStatus = "keychain_access"
	AuthCredentialsInvalid  AuthStatus = "credentials_invalid"
	AuthScopeInsufficient   AuthStatus = "scope_insufficient"
	AuthTokenRejected       AuthStatus = "token_rejected"
	AuthClientRejected      AuthStatus = "client_rejected"
	AuthAccessDenied        AuthStatus = "access_denied"
	AuthNetwork             AuthStatus = "network"
	AuthProviderError       AuthStatus = "provider_error"
	AuthAccountMismatch     AuthStatus = "account_mismatch"
	AuthUnknown             AuthStatus = "unknown"
)

func AuthStatuses added in v3.2.2

func AuthStatuses() []AuthStatus

AuthStatuses lists every status in documentation order.

func ClassifyAuthError added in v3.2.2

func ClassifyAuthError(err error) AuthStatus

ClassifyAuthError maps an error from credential loading, token exchange or a Gmail API call to a status. A nil error is connected.

func (AuthStatus) Hint added in v3.2.2

func (s AuthStatus) Hint() string

Hint returns one recovery step for the status. Connected has none.

func (AuthStatus) StopsSelection added in v3.2.2

func (s AuthStatus) StopsSelection() bool

StopsSelection reports a failure to read or accept stored credentials. Operational commands fail the whole account on it, even when another address has working credentials.

func (AuthStatus) Stored added in v3.2.2

func (s AuthStatus) Stored() bool

Stored reports that the address has a credential entry in Keychain, working or not. Operational credential selection counts these addresses: more than one makes the account ambiguous.

type Client

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

func NewClient

func NewClient(client *http.Client, tokens TokenSource, options ...ClientOption) *Client

func NewStoredClient

func NewStoredClient(email string, client *http.Client) (*Client, error)

func (*Client) FindMessages

func (c *Client) FindMessages(ctx context.Context, messageID string) ([]string, error)

FindMessages retrieves every page or returns no candidates and an error. A result is only a candidate: callers must verify the Message-ID header.

func (*Client) GetMessage

func (c *Client) GetMessage(ctx context.Context, id string) (Message, error)

func (*Client) ListLabels

func (c *Client) ListLabels(ctx context.Context) ([]Label, error)

func (*Client) ModifyLabels

func (c *Client) ModifyLabels(ctx context.Context, id string, add, remove []string) error

ModifyLabels reports HTTP acceptance only. Read metadata to verify final labels.

func (*Client) Profile

func (c *Client) Profile(ctx context.Context) (Profile, error)

type ClientOption

type ClientOption func(*Client)

func WithBaseURL

func WithBaseURL(baseURL string) ClientOption

type CredentialTokenSource

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

func NewCredentialTokenSource

func NewCredentialTokenSource(credentials Credentials, client *http.Client) *CredentialTokenSource

func (*CredentialTokenSource) InvalidateAccessToken added in v3.2.2

func (s *CredentialTokenSource) InvalidateAccessToken()

InvalidateAccessToken drops any cached access token so the next Token call exchanges the refresh token. Pending persistence is kept.

func (*CredentialTokenSource) Token

type Credentials

type Credentials struct {
	ClientID     string    `json:"client_id"`
	ClientSecret string    `json:"client_secret,omitempty"`
	RefreshToken string    `json:"refresh_token"`
	AccessToken  string    `json:"access_token,omitempty"`
	Expiry       time.Time `json:"expiry,omitempty"`
	Scope        string    `json:"scope"`
}

Credentials is the explicit JSON import format. Expiry is RFC3339. Secret fields are persisted only in macOS Keychain, never a plaintext cache.

func LoadCredentials

func LoadCredentials(email string) (Credentials, error)

func (Credentials) GoString

func (c Credentials) GoString() string

func (Credentials) String

func (c Credentials) String() string
type Header struct {
	Name  string `json:"name"`
	Value string `json:"value"`
}

type KeychainError added in v3.2.2

type KeychainError struct {
	Operation string
	Status    int
}

KeychainError is a macOS Security framework failure for one Keychain operation.

func (*KeychainError) Error added in v3.2.2

func (e *KeychainError) Error() string

func (*KeychainError) InteractionRequired added in v3.2.2

func (e *KeychainError) InteractionRequired() bool

InteractionRequired reports a status that needs a user prompt or an unlocked Keychain.

type Label

type Label struct {
	ID   string `json:"id"`
	Name string `json:"name"`
	Type string `json:"type"`
}

type Message

type Message struct {
	ID           string   `json:"id"`
	ThreadID     string   `json:"threadId"`
	LabelIDs     []string `json:"labelIds"`
	Payload      Payload  `json:"payload"`
	SizeEstimate int64    `json:"sizeEstimate"`
}

type Payload

type Payload struct {
	Headers []Header `json:"headers"`
}

type Profile

type Profile struct {
	EmailAddress string `json:"emailAddress"`
}

func Connect

func Connect(ctx context.Context, clientJSONPath, expectedEmail string, openURL func(string) error, client *http.Client) (Profile, error)

Connect runs installed-client OAuth using a loopback listener, state and PKCE. openURL is supplied by the CLI and receives the consent URL, without secrets.

func ImportCredentials

func ImportCredentials(ctx context.Context, path, expectedEmail string, client *http.Client) (Profile, error)

type RequestError

type RequestError struct {
	Operation  string
	StatusCode int
	Uncertain  bool
	Transport  bool
}

RequestError never includes response bodies, bearer tokens, or request URLs. Uncertain means a mutation was dispatched and its final outcome needs a read. Transport means no HTTP response was received.

func (*RequestError) Error

func (e *RequestError) Error() string

type TokenError added in v3.2.2

type TokenError struct {
	StatusCode int
	Code       string
	Transport  bool
}

TokenError is a failed OAuth token exchange. Code is Google's OAuth error code when it is a short lowercase identifier. The response description, body, tokens and client secrets are never kept.

func (*TokenError) Error added in v3.2.2

func (e *TokenError) Error() string

type TokenSource

type TokenSource interface {
	Token(context.Context) (string, error)
}

type TokenSourceFunc

type TokenSourceFunc func(context.Context) (string, error)

func (TokenSourceFunc) Token

func (f TokenSourceFunc) Token(ctx context.Context) (string, error)

Jump to

Keyboard shortcuts

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