auth

package
v1.13.0 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2026 License: MIT Imports: 31 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

View Source
const MCPScope = "read write send"

MCPScope is what `hey auth login --mcp` asks for. A scoped row that names no scope is refused (haystack#9085), so the sign-in always sends it.

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.

View Source
var ErrGrantChanged = errors.New("hey mcp was signed in again, or signed out, since it started; restart it")

ErrGrantChanged is a stored credential under a different sign-in than the pinned one.

View Source
var ErrKeyringRefused = errors.New("system keyring refused access")

ErrKeyringRefused means the system keyring would not hand a credential to this process. Signing in again cannot fix that: the refusal is about where the process runs, not about the login.

View Source
var ErrMCPClientUnconfigured = errors.New("no HEY MCP (local) client id is configured for this HEY")

ErrMCPClientUnconfigured means this HEY has no local MCP client id configured yet.

Functions

func CredentialStorageError added in v1.10.1

func CredentialStorageError(context string, err error) error

CredentialStorageError reports a credential store that could not be read. It is not an authentication failure, so it keeps its own exit status; a keyring refusal gets the hint that stops a caller from asking for another login.

func HasScope added in v1.11.0

func HasScope(granted string, want ...string) bool

HasScope reports whether a granted scope string holds every scope in want, by the expanded set rather than the literal string.

func NotLoggedInHint added in v1.10.1

func NotLoggedInHint() string

NotLoggedInHint is the remedy for a missing login. Codex's sandbox hides the system keyring, so a login that is saved reads as missing there.

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"`

	// Scope is what HEY granted these tokens, space-separated, e.g. "read write send".
	Scope string `json:"scope,omitempty"`
	// ClientID is the OAuth client the tokens were issued to, which a refresh names.
	ClientID string `json:"client_id,omitempty"`
	// GrantKey names this sign-in locally. A running hey mcp stays on the sign-in it
	// started with, and binds each send confirmation to it.
	GrantKey string `json:"grant_key,omitempty"`
	// GrantedAt is when the sign-in completed, in Unix seconds, for hey auth status.
	GrantedAt int64 `json:"granted_at,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 NewMCPManager added in v1.11.0

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

NewMCPManager is the manager for hey mcp's credential, signing in and refreshing as the local MCP client clientID.

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, the CLI's manager uses it 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) ClientID added in v1.11.0

func (m *Manager) ClientID() string

ClientID is the OAuth client this manager signs in as.

func (*Manager) CredentialKey

func (m *Manager) CredentialKey() string

CredentialKey returns the credential's storage key: the base URL for the CLI's own credential, prefixed for hey mcp's (mcp.go).

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) LoginDevice added in v1.11.0

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

LoginDevice signs in with the device authorization grant, asking for the manager's scope. It returns errDeviceFlowUnavailable, wrapped with HEY's answer, when this HEY can't serve the request, so the caller can fall back to Login.

func (*Manager) LoginMCP added in v1.11.0

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

LoginMCP signs in the local MCP client. It uses the device flow where HEY serves scoped device requests, and otherwise loopback authorization with the same scope, returning which one it used ("device" or "oauth").

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) PinGrant added in v1.11.0

func (m *Manager) PinGrant(grantKey string)

PinGrant makes grantKey the only sign-in this manager authenticates with from now on (pinnedGrant). hey mcp pins the sign-in it starts on.

func (*Manager) Refresh

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

Refresh forces a token refresh.

func (*Manager) Revoke added in v1.11.0

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

Revoke asks HEY to revoke the stored grant (DELETE /oauth/tokens). It sends the refresh token when there is one, since revoking it ends the whole grant.

func (*Manager) StoredCredentials added in v1.11.0

func (m *Manager) StoredCredentials() (*Credentials, error)

StoredCredentials reads this manager's stored credential.

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"`
	// Scope is what HEY granted, space-separated. RFC 6749 §5.1 lets a server leave it
	// out when it granted exactly what was asked for.
	Scope     string    `json:"scope"`
	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