auth

package
v1.8.1 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2026 License: MIT Imports: 28 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 auth provides OAuth authentication for HEY.

Index

Constants

This section is empty.

Variables

View Source
var ErrCredentialsNotFound = errors.New("credentials not found")

ErrCredentialsNotFound means the store has no credential for an origin. It does not cover a credential that could not be read from otherwise available storage.

Functions

This section is empty.

Types

type Credentials

type Credentials struct {
	AccessToken   string `json:"access_token"`  //nolint:gosec // G117: legitimate credential field
	RefreshToken  string `json:"refresh_token"` //nolint:gosec // G117: legitimate credential field
	ExpiresAt     int64  `json:"expires_at"`
	OAuthType     string `json:"oauth_type"`
	TokenEndpoint string `json:"token_endpoint"`
	SessionCookie string `json:"session_cookie,omitempty"`
	// InstallID is the install these tokens were issued to. HEY binds a refresh token's
	// lineage to it and revokes the session when another install presents the token, so it
	// travels with the tokens: every config directory that shares this keychain entry
	// refreshes as the same install.
	InstallID string `json:"install_id,omitempty"`
}

Credentials holds OAuth tokens and metadata.

type LoginOptions

type LoginOptions struct {
	NoBrowser bool

	// Logger receives login progress messages (browser hand-off, the
	// authorization URL, waiting notice). Nil keeps the default os.Stderr
	// output unchanged. Messages may span multiple lines.
	Logger func(msg string)
}

LoginOptions configures the login flow.

type Manager

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

Manager handles OAuth authentication.

func NewManager

func NewManager(baseURL string, httpClient *http.Client, configDir string) *Manager

NewManager creates a new auth manager.

func (*Manager) AccessToken

func (m *Manager) AccessToken(ctx context.Context) (string, error)

AccessToken returns a valid access token, refreshing if needed. If HEY_TOKEN env var is set, it's used directly.

func (*Manager) AuthenticateRequest

func (m *Manager) AuthenticateRequest(ctx context.Context, req *http.Request) error

AuthenticateRequest sets the appropriate auth header on an HTTP request. Uses Bearer token if available, otherwise falls back to session cookie.

func (*Manager) AuthenticateRequestFromStore added in v1.6.0

func (m *Manager) AuthenticateRequestFromStore(ctx context.Context, req *http.Request) error

AuthenticateRequestFromStore rereads persistent credentials before setting the auth header. Long-lived connections use it when they dial again, since they do not have the SDK's 401 path to invalidate this manager's process-local cache.

func (*Manager) AuthenticationStatus added in v1.7.0

func (m *Manager) AuthenticationStatus() (bool, error)

AuthenticationStatus reports whether usable credentials exist. A missing credential is a known signed-out state; a storage failure leaves the state unknown and is returned to the caller.

func (*Manager) CredentialKey

func (m *Manager) CredentialKey() string

CredentialKey returns the base URL used as the credential storage key.

func (*Manager) GetStore

func (m *Manager) GetStore() *Store

GetStore returns the credential store.

func (*Manager) IsAuthenticated

func (m *Manager) IsAuthenticated() bool

IsAuthenticated reports whether usable credentials exist. Callers that need to distinguish signed out from unavailable storage use AuthenticationStatus.

func (*Manager) Login

func (m *Manager) Login(ctx context.Context, opts LoginOptions) error

Login initiates the browser-based OAuth login flow with PKCE.

func (*Manager) LoginWithCookie

func (m *Manager) LoginWithCookie(cookie string) error

LoginWithCookie stores a session cookie.

func (*Manager) LoginWithToken

func (m *Manager) LoginWithToken(token string) error

LoginWithToken stores a pre-provided bearer token.

func (*Manager) Logout

func (m *Manager) Logout() error

Logout removes stored credentials.

func (*Manager) OnCredentialCleared added in v1.5.0

func (m *Manager) OnCredentialCleared(fn func())

OnCredentialCleared registers what to run when the manager clears a credential on its own — today, when the server has refused the refresh token. Replacing a credential with a signed-out record after deletion fails counts as clearing it. Logout does not: its callers clear the cache themselves.

func (*Manager) Refresh

func (m *Manager) Refresh(ctx context.Context) error

Refresh forces a token refresh.

type OAuthToken

type OAuthToken struct {
	AccessToken  string    `json:"access_token"`  //nolint:gosec // G117: legitimate OAuth field
	RefreshToken string    `json:"refresh_token"` //nolint:gosec // G117: legitimate OAuth field
	TokenType    string    `json:"token_type"`
	ExpiresIn    int64     `json:"expires_in"`
	ExpiresAt    time.Time `json:"-"`
}

OAuthToken represents the token response from the HEY OAuth server.

type Store

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

Store handles credential storage, preferring system keychain.

func NewStore

func NewStore(fallbackDir string) *Store

NewStore creates a credential store. Keyring availability is probed lazily on first credential operation, not at construction time.

func (*Store) Delete

func (s *Store) Delete(origin string) error

Delete removes credentials for the given origin.

func (*Store) InstallID added in v1.4.0

func (s *Store) InstallID() (string, error)

InstallID identifies this install to HEY as a device, minting the identifier on first use. It lives beside the credentials rather than in them: a device outlasts a logout, and HEY alerts on a sign-in from a device it hasn't seen.

func (*Store) Load

func (s *Store) Load(origin string) (*Credentials, error)

Load retrieves credentials for the given origin.

func (*Store) MigrateToKeyring

func (s *Store) MigrateToKeyring() error

MigrateToKeyring migrates credentials from file to keyring.

func (*Store) Save

func (s *Store) Save(origin string, creds *Credentials) error

Save stores credentials for the given origin.

func (*Store) UsingKeyring

func (s *Store) UsingKeyring() bool

UsingKeyring returns true if the store is using the system keyring.

Jump to

Keyboard shortcuts

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