auth

package
v0.14.14 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package auth implements GitHub OAuth device code flow authentication and resolves GitHub credentials into Copilot API bearers.

Index

Constants

This section is empty.

Variables

View Source
var (

	// ErrNotAuthenticated indicates that no reusable GitHub access token was
	// available for a token refresh attempt.
	ErrNotAuthenticated = errors.New("not authenticated")

	// ErrInvalidAccessToken indicates that a stored GitHub access token exists
	// but can no longer be used for Copilot authentication.
	ErrInvalidAccessToken = errors.New("invalid access token")
)

Functions

func IsInteractiveLoginRequired added in v0.9.0

func IsInteractiveLoginRequired(err error) bool

IsInteractiveLoginRequired reports whether resolving the error should fall back to an interactive device-code login flow.

Types

type AccessTokenResponse

type AccessTokenResponse struct {
	AccessToken      string `json:"access_token"`
	TokenType        string `json:"token_type"`
	Scope            string `json:"scope"`
	Error            string `json:"error,omitempty"`
	ErrorDescription string `json:"error_description,omitempty"`
}

AccessTokenResponse is the response from GitHub's OAuth access token endpoint.

type AuthPreferences added in v0.10.0

type AuthPreferences struct {
	GitHubCLIAutoSignIn bool `json:"github_cli_auto_sign_in,omitempty"`
}

AuthPreferences stores explicit authentication preferences shared by the CLI and menubar app.

type AuthSource added in v0.10.0

type AuthSource string

AuthSource identifies the source of the currently usable authentication state.

const (
	AuthSourceNone      AuthSource = "none"
	AuthSourceEnv       AuthSource = "env"
	AuthSourceVekil     AuthSource = "vekil"
	AuthSourceGitHubCLI AuthSource = "github-cli"
)

type AuthStatus added in v0.10.0

type AuthStatus struct {
	SignedIn             bool
	Source               AuthSource
	GitHubCLIAutoSignIn  bool
	SignedOut            bool
	HasValidCopilotCache bool
	HasVekilAccessToken  bool
}

AuthStatus is a fast snapshot of local authentication state. It never shells out to external tools such as gh.

type Authenticator

type Authenticator struct {

	// DisableAutoDeviceFlow prevents refreshToken from falling through to the
	// interactive device-code flow. When true, callers (e.g. the menubar app)
	// are expected to drive the flow themselves via RequestDeviceCode /
	// PollForAuthorization.
	DisableAutoDeviceFlow bool
	// contains filtered or unexported fields
}

Authenticator manages GitHub OAuth credentials and Copilot API bearers. It handles the device code flow, legacy token-cache migration, and shared refreshes whose waiters can stop independently when their contexts expire.

func NewAuthenticator

func NewAuthenticator(tokenDir string) (*Authenticator, error)

NewAuthenticator creates an Authenticator that stores tokens in tokenDir. If tokenDir is empty, it defaults to ~/.config/vekil.

func NewTestAuthenticator

func NewTestAuthenticator(token string) *Authenticator

NewTestAuthenticator creates an Authenticator pre-loaded with a token for testing.

func NewTestAuthenticatorWithResponsesToken added in v0.14.2

func NewTestAuthenticatorWithResponsesToken(token, responsesToken string) *Authenticator

NewTestAuthenticatorWithResponsesToken creates an Authenticator with a direct token plus a preloaded endpoint-compatible Responses token.

func (*Authenticator) GetCredential added in v0.14.3

func (a *Authenticator) GetCredential(ctx context.Context) (Credential, error)

GetCredential returns a usable bearer and its source identity together.

func (*Authenticator) GetResponsesCredential added in v0.14.3

func (a *Authenticator) GetResponsesCredential(ctx context.Context) (Credential, error)

GetResponsesCredential returns a Responses-compatible bearer with the source identity captured before any endpoint-specific exchange.

func (*Authenticator) GetResponsesToken added in v0.14.2

func (a *Authenticator) GetResponsesToken(ctx context.Context) (string, error)

GetResponsesToken returns the credential used for Copilot /responses requests. GitHub App user credentials are accepted directly by the catalog and Chat endpoints but require the short-lived Copilot token exchange for Responses. That fallback is cached in memory only and keyed to the active GitHub credential. Other supported credentials remain direct bearers.

func (*Authenticator) GetToken

func (a *Authenticator) GetToken(ctx context.Context) (string, error)

GetToken returns a usable Copilot API bearer, refreshing it if necessary. It is safe for concurrent use.

func (*Authenticator) GetTokenNonInteractive

func (a *Authenticator) GetTokenNonInteractive(ctx context.Context) (string, error)

GetTokenNonInteractive returns a valid Copilot API token without falling back to the interactive device-code flow.

func (*Authenticator) GitHubCLIAutoSignInEnabled added in v0.10.0

func (a *Authenticator) GitHubCLIAutoSignInEnabled() bool

GitHubCLIAutoSignInEnabled reports whether the user has explicitly opted in to automatic GitHub CLI sign-in. Malformed preferences are treated as false.

func (*Authenticator) IsSignedIn

func (a *Authenticator) IsSignedIn() bool

IsSignedIn reports whether the authenticator has a usable or explicitly configured local authentication source.

func (*Authenticator) PollForAuthorization

func (a *Authenticator) PollForAuthorization(ctx context.Context, dcResp *DeviceCodeResponse) error

PollForAuthorization polls GitHub until the user authorizes the device code, then saves the access token and exchanges it for a Copilot API token.

func (*Authenticator) RefreshTokenNonInteractive added in v0.9.0

func (a *Authenticator) RefreshTokenNonInteractive(ctx context.Context) (string, error)

RefreshTokenNonInteractive reloads existing GitHub authentication without falling back to the interactive device-code flow. Unlike GetTokenNonInteractive, it bypasses any cached Copilot bearer.

func (*Authenticator) RequestDeviceCode

func (a *Authenticator) RequestDeviceCode(ctx context.Context) (*DeviceCodeResponse, error)

RequestDeviceCode initiates the GitHub device-code flow by requesting a device code and user code from GitHub. The caller should present the UserCode and VerificationURI to the user, then call PollForAuthorization. No lock is required — only immutable fields (client) are accessed.

func (*Authenticator) SignInWithGitHubCLI added in v0.10.0

func (a *Authenticator) SignInWithGitHubCLI(ctx context.Context) error

SignInWithGitHubCLI explicitly signs in using the currently authenticated GitHub CLI account. The GitHub CLI token is kept only in memory as a short-lived Copilot bearer token and is never persisted by Vekil.

func (*Authenticator) SignOut

func (a *Authenticator) SignOut() error

SignOut clears all authentication state from memory and removes persisted token files from disk. It is safe for concurrent use.

func (*Authenticator) Status added in v0.10.0

func (a *Authenticator) Status() AuthStatus

Status returns a fast snapshot of local authentication state without making network requests or invoking external commands.

type CopilotTokenResponse

type CopilotTokenResponse struct {
	Token        string `json:"token"`
	ExpiresAt    int64  `json:"expires_at"`
	ErrorDetails string `json:"error_details,omitempty"`
}

CopilotTokenResponse is the response from the legacy Copilot token exchange endpoint and the on-disk format of the legacy bearer cache.

type Credential added in v0.14.3

type Credential struct {
	Token             string
	SourceFingerprint [32]byte
}

Credential pairs a bearer with its issuing credential's identity. A refresh cannot change the source fingerprint of a credential already returned.

type DeviceCodeResponse

type DeviceCodeResponse struct {
	DeviceCode      string `json:"device_code"`
	UserCode        string `json:"user_code"`
	VerificationURI string `json:"verification_uri"`
	ExpiresIn       int    `json:"expires_in"`
	Interval        int    `json:"interval"`
}

DeviceCodeResponse is the response from GitHub's device code endpoint.

Jump to

Keyboard shortcuts

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