Documentation
¶
Overview ¶
Package githubauth implements GitHub's OAuth device flow and the small amount of state lezz keeps around it.
Device flow is used deliberately: a distributed CLI cannot keep an OAuth client secret, and device flow does not need one. Only the public client ID is embedded (or supplied at runtime for development).
Index ¶
- Constants
- func DefaultRecordPath() string
- func LoadToken(store credential.Store) (string, error)
- func Logout(store credential.Store) error
- func ReleaseConfig(config selfupdate.Config) (selfupdate.Config, error)
- func ReleaseUpdater(config selfupdate.Config) (*selfupdate.Updater, error)
- func Status(ctx context.Context, c *Client, store credential.Store) (*Record, *Identity, error)
- func StoredToken() (string, error)
- func WriteRecord(path string, rec *Record) error
- type Client
- type DeviceCode
- type Identity
- type Record
- type Token
Constants ¶
const ( // TokenService is the Keychain service / seed keychain ref name. TokenService = "LEZZ_GITHUB_TOKEN" //nolint:gosec // G101: a Keychain service name, not a credential. // TokenAccount is the Keychain account name. TokenAccount = "lezz" // DefaultScopes is the minimum scope: identity only, no repo access. DefaultScopes = "read:user" // AuthRef is the seed auth.ref for the stored token. AuthRef = "keychain:" + TokenService )
Variables ¶
This section is empty.
Functions ¶
func DefaultRecordPath ¶
func DefaultRecordPath() string
DefaultRecordPath is ~/.lezz/auth/github.json.
func LoadToken ¶
func LoadToken(store credential.Store) (string, error)
LoadToken returns the stored access token. Metadata is not required.
func Logout ¶
func Logout(store credential.Store) error
Logout deletes the token and metadata. Missing items are not an error.
func ReleaseConfig ¶ added in v0.15.0
func ReleaseConfig(config selfupdate.Config) (selfupdate.Config, error)
ReleaseConfig returns the go-selfupdate config used for GitHub release downloads.
The preferred credential is the token `lezz auth github` stored — the one the embedded OAuth App client ID minted. An ambient $GITHUB_TOKEN is deliberately not consulted: a stale or unrelated token turns every public download into a 401 (james-gibson/lezz.go#36), and inheriting a stranger's credential is a worse default than an anonymous request. With no stored token the config carries an anonymous source (60 requests/hour, public repos only).
The source is built here rather than left to NewUpdater because selfupdate.Config has no token field: NewUpdater would construct GitHubConfig{} and pick up $GITHUB_TOKEN from the environment.
A config that already carries a Source is returned unchanged, so tests can substitute their own.
func ReleaseUpdater ¶ added in v0.15.0
func ReleaseUpdater(config selfupdate.Config) (*selfupdate.Updater, error)
ReleaseUpdater builds the updater used by `lezz install`, `lezz update` and the tool upgrade path.
func Status ¶
Status returns the stored token and, when reachable, the current identity. A stale token still returns the record; the caller decides whether the verification error is fatal.
func StoredToken ¶ added in v0.15.0
StoredToken returns the access token saved by `lezz auth github`, or credential.ErrNotFound when nobody has signed in.
The token is the one the embedded OAuth App client ID minted, so it is this CLI's own identity rather than an unrelated personal access token.
func WriteRecord ¶
WriteRecord writes metadata atomically with 0600.
Types ¶
type Client ¶
type Client struct {
HTTP *http.Client
ClientID string
DeviceCodeURL string
TokenURL string
UserURL string
UserAgent string
// Sleep is injectable for tests. It must respect ctx.
Sleep func(context.Context, time.Duration) error
}
Client talks to GitHub's device-flow and user endpoints.
func (*Client) FetchIdentity ¶
FetchIdentity verifies the token and returns the GitHub account.
func (*Client) StartDeviceFlow ¶
StartDeviceFlow requests a user code and returns the instructions.
func (*Client) WaitForToken ¶
WaitForToken polls until the user authorizes, denies, or the code expires.
type DeviceCode ¶
type DeviceCode struct {
DeviceCode string `json:"device_code"`
UserCode string `json:"user_code"`
VerificationURI string `json:"verification_uri"`
VerificationURIComplete string `json:"verification_uri_complete"`
ExpiresIn int `json:"expires_in"`
Interval int `json:"interval"`
Error string `json:"error"`
ErrorDescription string `json:"error_description"`
}
DeviceCode is the first device-flow response.
type Identity ¶
type Identity struct {
ID int64 `json:"id"`
Login string `json:"login"`
Name string `json:"name"`
HTMLURL string `json:"html_url"`
}
Identity is the subset of GET /user that lezz records.
type Record ¶
type Record struct {
Login string `json:"login"`
ID int64 `json:"id"`
Name string `json:"name,omitempty"`
HTMLURL string `json:"html_url,omitempty"`
Scopes []string `json:"scopes,omitempty"`
TokenService string `json:"token_service"`
TokenAccount string `json:"token_account"`
AuthRef string `json:"auth_ref"`
StoreKind string `json:"store_kind"`
CreatedAt time.Time `json:"created_at"`
VerifiedAt *time.Time `json:"verified_at,omitempty"`
}
Record is non-secret metadata stored next to the token.
func LoadRecordFrom ¶
LoadRecordFrom reads a metadata file at path.
type Token ¶
type Token struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
Scope string `json:"scope"`
Error string `json:"error"`
ErrorDescription string `json:"error_description"`
Interval int `json:"interval"`
}
Token is a device-flow token response or an OAuth error.