Documentation
¶
Overview ¶
Package ghlink is the status page's "Connect GitHub" link: an optional GitHub identity a signed-in user attaches, per github Integration, through that Integration's GitHub App user-to-server OAuth flow (PKCE).
Sign-in stays with the configured provider (dex, say), whose groups drive the cluster RBAC. Dex issues its own tokens and never hands the upstream GitHub token to the client, so acting as the user on GitHub needs this separate link. The status server builds the authorize redirect from the Integration's spec (the client ID is not secret); the code exchange and every token refresh go through the integration-controller's internal endpoints (internal/ghuser), which hold the App's client secret.
The link lives in one sealed, HttpOnly cookie per Integration, bound to the patchy username it was made for: a link made by another user of a shared browser is ignored. Tokens refresh lazily, only on requests that call GitHub. GitHub refresh tokens are single-use, so concurrent refreshes of one token share a single call, and a refresh GitHub rejects drops the link.
Index ¶
Constants ¶
const ( // CookieError carries a human-readable link failure to the SPA. It is // not HttpOnly and carries no secret. CookieError = "patchy-github-error" )
Cookie names and bounds.
Variables ¶
var ErrNotLinked = errors.New("github account not connected")
ErrNotLinked reports that the user has no usable link for the Integration: none was made, it belongs to someone else, or GitHub rejected its tokens (the UI offers "Reconnect").
Functions ¶
func CookieName ¶
CookieName is the link cookie of one Integration.
Types ¶
type Config ¶
type Config struct {
// Key seals the state and link cookies (auth.LinkKey).
Key []byte
// Secure sets the Secure cookie attribute.
Secure bool
// MaxAge caps a link cookie's lifetime: the session duration.
MaxAge time.Duration
// BaseURL is the status page's public origin, for the callback URL.
BaseURL func(r *http.Request) string
// Lookup resolves a connectable Integration by name.
Lookup func(ctx context.Context, name string) (Integration, bool)
// Identify resolves the signed-in user.
Identify func(w http.ResponseWriter, r *http.Request) (*auth.Identity, error)
// Users calls the integration-controller.
Users Users
Log *slog.Logger
// Now tells time; nil means time.Now.
Now func() time.Time
}
Config configures a Linker.
type Integration ¶
Integration is a connectable github Integration: one with spec.github.userAuthorization and the UserAuthorizationReady condition.
func (Integration) Host ¶
func (i Integration) Host() string
Host is where the Integration's repositories live.
type Link ¶
type Link struct {
Integration string `json:"integration"`
Login string `json:"login"`
UserID int64 `json:"userID"`
AccessToken string `json:"accessToken"`
RefreshToken string `json:"refreshToken,omitempty"`
Expiry time.Time `json:"expiry,omitzero"`
// LinkedTo is the patchy username the link was made for.
LinkedTo string `json:"linkedTo"`
// LinkedAt anchors the cookie lifetime: refreshing never extends a link
// past LinkedAt + MaxAge.
LinkedAt time.Time `json:"linkedAt"`
}
Link is the sealed payload of one link cookie.
type Linker ¶
type Linker struct {
// contains filtered or unexported fields
}
Linker runs the link flow and reads the link cookies.
func (*Linker) Call ¶
func (l *Linker) Call( ctx context.Context, w http.ResponseWriter, link *Link, fn func(token string) error, ) error
Call runs fn with a live access token for the link, refreshing it first when it is about to expire, and once more if GitHub rejects it. A refresh GitHub rejects drops the link and returns ErrNotLinked. The link is updated in place, and its cookie rewritten, on every refresh.
type Users ¶
type Users interface {
Exchange(ctx context.Context, req ghuser.ExchangeRequest) (*ghuser.ExchangeResponse, error)
Refresh(ctx context.Context, req ghuser.RefreshRequest) (*ghuser.Tokens, error)
}
Users is the slice of the integration-controller client the flow needs; ghuser.Client implements it.