Documentation
¶
Overview ¶
Package auth provides OAuth authentication for HEY.
Index ¶
- Constants
- Variables
- func CredentialStorageError(context string, err error) error
- func HasScope(granted string, want ...string) bool
- func MCPProfileCredentialKey(profile string) string
- func NotLoggedInHint() string
- func ProfileCredentialKey(profile string) string
- func RefreshFailed(err error) (status int, failed bool)
- type Credentials
- type LoginOptions
- type Manager
- func (m *Manager) AccessToken(ctx context.Context) (string, error)
- func (m *Manager) AuthenticateRequest(ctx context.Context, req *http.Request) error
- func (m *Manager) AuthenticateRequestFromStore(ctx context.Context, req *http.Request) error
- func (m *Manager) AuthenticationStatus() (bool, error)
- func (m *Manager) ClientID() string
- func (m *Manager) CredentialKey() string
- func (m *Manager) GetStore() *Store
- func (m *Manager) IsAuthenticated() bool
- func (m *Manager) Login(ctx context.Context, opts LoginOptions) error
- func (m *Manager) LoginCommand() string
- func (m *Manager) LoginDevice(ctx context.Context, opts LoginOptions) error
- func (m *Manager) LoginMCP(ctx context.Context, opts LoginOptions) (string, error)
- func (m *Manager) LoginWithToken(token string) error
- func (m *Manager) Logout() error
- func (m *Manager) OnCredentialCleared(fn func())
- func (m *Manager) PinGrant(grantKey string)
- func (m *Manager) Refresh(ctx context.Context) error
- func (m *Manager) Revoke(ctx context.Context) error
- func (m *Manager) StoredAccessToken(ctx context.Context) (string, error)
- func (m *Manager) StoredCredentials() (*Credentials, error)
- func (m *Manager) UseOAuthServer(issuerURL, tokenURL string)
- func (m *Manager) UseProfile(profile string)
- type OAuthToken
- type Store
Constants ¶
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 ¶
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.
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.
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.
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
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
HasScope reports whether a granted scope string holds every scope in want, by the expanded set rather than the literal string.
func MCPProfileCredentialKey ¶ added in v1.14.0
MCPProfileCredentialKey is the key of hey mcp's credential under a profile, kept apart from the profile's own as "mcp::<origin>" is from the origin's.
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.
func ProfileCredentialKey ¶ added in v1.14.0
ProfileCredentialKey is the key a named profile's credential is stored under, basecamp-cli's "profile:<name>" (internal/auth/auth.go at 48b01aaf).
func RefreshFailed ¶ added in v1.14.0
RefreshFailed reports whether err is a token refresh that failed without a verdict on the grant, with the token endpoint's status (0 when it didn't answer). The request that needed the refresh was never sent.
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"`
// ResourceURL is the base URL the sign-in was made for, which its access token is
// sent to. It differs from TokenEndpoint's server under a profile's oauth_token_url.
ResourceURL string `json:"resource_url,omitempty"`
// SessionCookie is inert legacy data, read only to explain why sign-in is required.
// New logins replace the record; it must never authenticate or become a bearer.
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
NewMCPManager is the manager for hey mcp's credential, signing in and refreshing as the local MCP client clientID.
func NewManager ¶
NewManager creates a new auth manager.
func (*Manager) AccessToken ¶
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 ¶
AuthenticateRequest sets the appropriate auth header on an HTTP request. Uses only bearer tokens; obsolete browser cookies are never sent.
func (*Manager) AuthenticateRequestFromStore ¶ added in v1.6.0
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
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 ¶
CredentialKey returns the credential's storage key: the base URL for the CLI's own credential, prefixed for hey mcp's (mcp.go), or the profile's (UseProfile).
func (*Manager) IsAuthenticated ¶
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) LoginCommand ¶ added in v1.14.0
LoginCommand is the command that signs this manager's credential in again: addressed to its profile when there is one, since a bare login would store the new credential under the top-level key instead. It is meant to be pasted, so the name is shell-quoted.
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
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) LoginWithToken ¶
LoginWithToken stores a pre-provided bearer token.
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
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) Revoke ¶ added in v1.11.0
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) StoredAccessToken ¶ added in v1.14.0
StoredAccessToken returns only the stored bearer token, ignoring HEY_TOKEN.
func (*Manager) StoredCredentials ¶ added in v1.11.0
func (m *Manager) StoredCredentials() (*Credentials, error)
StoredCredentials reads this manager's stored credential.
func (*Manager) UseOAuthServer ¶ added in v1.14.0
UseOAuthServer moves sign-in off the base URL: issuerURL authorizes it, tokenURL exchanges, refreshes and revokes it. An empty one stays the base URL.
func (*Manager) UseProfile ¶ added in v1.14.0
UseProfile keys this manager's credential by profile rather than by origin, so two profiles on one server hold two sign-ins and neither reads the other's.
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 ¶
NewStore creates a credential store. Keyring availability is probed lazily on first credential operation, not at construction time.
func (*Store) InstallID ¶ added in v1.4.0
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 ¶
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 ¶
UsingKeyring returns true if the store is using the system keyring.