ghlink

package
v0.16.0 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: MIT Imports: 12 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 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

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

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

func CookieName(integration string) string

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

type Integration struct {
	Name     string
	BaseURL  string
	ClientID string
}

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 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 New

func New(cfg Config) *Linker

New builds a Linker; nil when cfg has no key or no Users client (the feature is off).

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.

func (*Linker) Get

func (l *Linker) Get(r *http.Request, id auth.Identity, integration string) (*Link, bool)

Get reads the user's link for one Integration. A link made for another username, a tampered cookie, or one past its lifetime reads as absent.

func (*Linker) Register

func (l *Linker) Register(mux *http.ServeMux)

Register mounts the flow. Disconnect is POST-only so a cross-site GET cannot drop a link.

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.

Jump to

Keyboard shortcuts

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