Documentation
¶
Overview ¶
Package github supplies the GitHub client and authentication shared by forge and verification adapters.
It selects credentials, constructs go-github clients lazily, implements device authorization and identity checks, and applies redirect and rate-limit handling. The adapters own repository, pull-request, and Actions operations; callers own credential persistence and user interaction.
It also reads and writes GitHub's addresses: the repository a Git remote or a web page names, a repository's remote, and a pull request's pages.
Index ¶
- Variables
- func PageRepository(address string) (string, error)
- func PullRequestChecksURL(repository string, number int) string
- func PullRequestURL(repository string, number int) string
- func RateLimitError(err error) error
- func Remote(repository string, ssh bool) string
- func RemoteRepository(remote string) (string, error)
- func Requests() int64
- func RevocationPage(clientID string) string
- func SavedLogin(ctx context.Context, store credential.Store) (credential.Login, error)
- func Used() int64
- func ValidRepositoryName(value string) bool
- type CLI
- type Client
- func (c *Client) API(ctx context.Context) (*gh.Client, error)
- func (c *Client) Authenticate(ctx context.Context) error
- func (c *Client) AuthenticatedAPI(ctx context.Context) (*gh.Client, error)
- func (c *Client) AuthenticatedUser(ctx context.Context) (string, error)
- func (c *Client) CredentialSource() CredentialSource
- type Config
- type CredentialSource
- type DeviceFlow
- type Identity
- type IdentityWatcher
- type Renewer
- type SystemCredentials
- type Token
- type TokenSource
- type TokenSourceFunc
Constants ¶
This section is empty.
Variables ¶
var CredentialKey = credential.Key{Service: "github.com/herbygillot/dockhand", Account: "github.com"}
CredentialKey is where dockhand keeps its GitHub login in the system credential store.
var DefaultOAuthClientID = "Ov23lixRrrqO0uEzU2GA"
DefaultOAuthClientID is the public client ID of dockhand's GitHub OAuth application, which has the device flow enabled. No client secret exists. The Makefile's GITHUB_OAUTH_CLIENT_ID replaces it at link time for development and alternate registrations.
var ErrAuthentication = forge.ErrAuthentication
var ErrLoginEnded = errors.New("github: the saved login can't renew itself")
errOldLogin and errExpiredLogin are a saved login that can't renew itself: one an earlier dockhand kept, a bare token, or one unused six months. ErrLoginEnded is a saved login that can't renew itself any more, which only dockhand setup github answers: serve says it once, rather than a failure for each pull request.
var ErrNoCredentials = fmt.Errorf("%w: no credential available", ErrAuthentication)
ErrNoCredentials permits anonymous public reads; invalid or inaccessible credentials do not.
var ErrNotGitHub = errors.New("github: not a github.com address")
ErrNotGitHub is an address somewhere other than github.com.
var LoginLock = func() string { home, err := os.UserHomeDir() if err != nil { return filepath.Join(os.TempDir(), "dockhand-github-login.lock") } return filepath.Join(home, ".dockhand", "github-login.lock") }()
LoginLock is the file processes renewing dockhand's login take in turn, beside its configuration: ~/.dockhand/github-login.lock. The command line sets it where its configuration is elsewhere.
Functions ¶
func PageRepository ¶
PageRepository is the owner/name of the repository a github.com page belongs to, as a person copies its address from a browser: with or without its scheme, on www.github.com too, and perhaps a page beneath the repository's own, such as https://github.com/owner/name/releases.
func PullRequestChecksURL ¶
PullRequestChecksURL is the page of a pull request's checks on github.com.
func PullRequestURL ¶
PullRequestURL is a pull request's page on github.com.
func RateLimitError ¶
RateLimitError says a refusal of go-github's own, from GitHub's rate limit, as forge.RateLimitError, with when it lifts. The transport refuses with one already (rateLimited); what reaches here is a refusal it couldn't read from GitHub's headers, as a secondary limit's 403 that names no wait.
func Remote ¶
Remote is a repository's Git address on github.com: over SSH, git@github.com:owner/name.git, or else HTTPS, https://github.com/owner/name.git.
func RemoteRepository ¶
RemoteRepository is the owner/name of the github.com repository a Git remote's URL names, over HTTPS or SSH: https://github.com/owner/name, ssh://git@github.com/owner/name, or git@github.com:owner/name, each perhaps ending in .git or a slash. It names the repository exactly: nothing beneath it, no query, and no credentials in an HTTPS address.
func Requests ¶
func Requests() int64
Requests is how many requests this process has sent GitHub's API.
func RevocationPage ¶
RevocationPage is where the person revokes the login an OAuth app was given, at GitHub.
func SavedLogin ¶
func SavedLogin(ctx context.Context, store credential.Store) (credential.Login, error)
SavedLogin is the login the store keeps, as the person sees it: whose, and until when it renews itself; ErrLoginEnded, wrapped, where it's from an earlier dockhand or expired, and the store's ErrNotFound where it keeps none. It asks GitHub nothing.
func ValidRepositoryName ¶
ValidRepositoryName reports whether value is a repository's owner/name, each in the characters GitHub allows.
Types ¶
type CLI ¶
type CLI struct {
// Path is gh's; found on the PATH when empty.
Path string
}
CLI is the GitHub CLI, gh, which acts on GitHub as its own app with its own login, which dockhand doesn't read here. An organization that refuses dockhand's app may take it (D8). Only its documented commands are run.
type Client ¶
type Client struct {
HTTP *http.Client
Config Config
Credentials TokenSource
// contains filtered or unexported fields
}
Configure a Client before its first API operation; it is then safe to share.
func SystemClient ¶
func SystemClient(store credential.Store) *Client
SystemClient is GitHub as the person's login reaches it: GH_TOKEN or GITHUB_TOKEN where set, else the login dockhand keeps in the store. It is the one way dockhand makes that client.
func (*Client) API ¶
API uses available credentials for public reads, allowing anonymous reads only when none exist.
func (*Client) AuthenticatedAPI ¶
AuthenticatedAPI shares one initialized credential and SDK client between adapters.
func (*Client) AuthenticatedUser ¶
func (*Client) CredentialSource ¶
func (c *Client) CredentialSource() CredentialSource
CredentialSource is where the token the client last carried came from; empty before it has asked for one.
type Config ¶
type Config struct {
BaseURL string
Token string
// CloneURL is where repositories are read with plain git, when the API
// cannot answer; empty is https://github.com.
CloneURL string
}
type CredentialSource ¶
type CredentialSource string
const ( SourceExplicit CredentialSource = "explicit credential" SourceGHEnvironment CredentialSource = "GH_TOKEN" SourceGitHubEnvironment CredentialSource = "GITHUB_TOKEN" SourceKeychain CredentialSource = "Dockhand macOS Keychain" SourceGitHubCLI CredentialSource = "GitHub CLI" )
type DeviceFlow ¶
type DeviceFlow struct {
HTTP *http.Client
Endpoint oauth2.Endpoint
APIBaseURL string
// Now is the clock a refresh token's expiry is reckoned by; time.Now
// where nil.
Now func() time.Time
}
DeviceFlow signs in through GitHub's device flow. HTTP is fetch.Client when nil, whose wait for a response is bounded.
func (*DeviceFlow) Authorize ¶
func (f *DeviceFlow) Authorize(ctx context.Context, clientID string, present func(credential.DeviceAuthorization) error) (credential.Login, error)
type Identity ¶
type Identity struct {
Account string
Source CredentialSource
}
Identity is who the person's GitHub login acts as now, and where its token comes from.
type IdentityWatcher ¶
type IdentityWatcher struct {
Credentials TokenSource
HTTP *http.Client
BaseURL string
// Recheck is how long an account read stands for the same token; ten
// minutes when zero.
Recheck time.Duration
// contains filtered or unexported fields
}
IdentityWatcher says who the login acts as each time it's asked, reading the credential chain afresh, as a client holding a token doesn't: a long-running serve kept acting with a token after a logout, and could have gone on with the GitHub CLI's, another account's, once it expired (the rc6 full stage, D-C2). The account is asked of GitHub when the token changes, and again each Recheck, so a token GitHub stopped honouring, as a revoked login's, is found: its error is then ErrAuthentication's. With no credential at all it returns ErrNoCredentials.
func SystemIdentity ¶
func SystemIdentity(credentials SystemCredentials) *IdentityWatcher
SystemIdentity watches the login SystemClient uses.
type Renewer ¶
Renewer is a TokenSource that, told the token GitHub rejected, can find another: a login it can refresh. A source that isn't one is asked again as it was, which finds a login saved since.
type SystemCredentials ¶
type SystemCredentials struct {
Store credential.Store
Key credential.Key
// Lock is the file processes renewing the login take in turn;
// LoginLock where empty.
Lock string
// Flow renews the login; GitHub's device flow where nil.
Flow *DeviceFlow
// contains filtered or unexported fields
}
func (SystemCredentials) Renew ¶
Renew is a token after GitHub rejected one: the login renewed, where the rejected token was its own, else whatever Token gives now.
func (SystemCredentials) Token ¶
func (s SystemCredentials) Token(ctx context.Context) (Token, error)
func (SystemCredentials) WithRenewal ¶
func (s SystemCredentials) WithRenewal(flow *DeviceFlow, path string) SystemCredentials
WithRenewal is the credentials renewing their login through flow, under the lock at path, for tests and other GitHubs; flow's endpoint is GitHub's where it's empty.