githubauth

package
v0.15.5 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2026 License: AGPL-3.0 Imports: 13 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 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

View Source
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

func Status(ctx context.Context, c *Client, store credential.Store) (*Record, *Identity, error)

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

func StoredToken() (string, error)

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

func WriteRecord(path string, rec *Record) error

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 New

func New(clientID string) *Client

New returns a client pointed at github.com.

func (*Client) FetchIdentity

func (c *Client) FetchIdentity(ctx context.Context, accessToken string) (*Identity, error)

FetchIdentity verifies the token and returns the GitHub account.

func (*Client) StartDeviceFlow

func (c *Client) StartDeviceFlow(ctx context.Context, scopes string) (*DeviceCode, error)

StartDeviceFlow requests a user code and returns the instructions.

func (*Client) WaitForToken

func (c *Client) WaitForToken(ctx context.Context, dc *DeviceCode) (*Token, error)

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 LoadRecord

func LoadRecord() (*Record, error)

LoadRecord reads the metadata file.

func LoadRecordFrom

func LoadRecordFrom(path string) (*Record, error)

LoadRecordFrom reads a metadata file at path.

func Save

func Save(store credential.Store, token *Token, identity *Identity) (*Record, error)

Save stores the token and writes non-secret metadata.

func SaveAt

func SaveAt(store credential.Store, recordPath string, token *Token, identity *Identity) (*Record, error)

SaveAt is Save with an explicit metadata path (tests).

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.

Jump to

Keyboard shortcuts

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