ghuser

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 ghuser is the wire contract of the integration-controller's internal GitHub-user endpoints, and the client the status server calls them with.

A status-page user may link their own GitHub identity through a github Integration's GitHub App (the user-to-server OAuth flow, with PKCE). The status server holds no forge Secret and makes no GitHub calls, so every call that needs the App's client secret or the user's token goes through the integration-controller, which holds the Integration's credential and proxy settings already: exchanging the code, refreshing the token pair, listing the repositories the user maintains, and checking the user's role on one repository together with the Integration's alert access to it.

The endpoints live on the same in-cluster listener as the repository-access check and accept the same audience-bound projected ServiceAccount token (internal/tokenauth). User tokens travel in request and response bodies only. The integration-controller never logs or stores them; the status server keeps them in a sealed cookie.

The package is stdlib-only (plus internal/repoaccess), so the status server does not link the GitHub client.

Index

Constants

View Source
const (
	PathExchange            = "/internal/v1/github-user/exchange"
	PathRefresh             = "/internal/v1/github-user/refresh"
	PathRepositories        = "/internal/v1/github-user/repositories"
	PathAuthorizeRepository = "/internal/v1/github-user/authorize-repository"
	// TokenHeader carries the caller's projected ServiceAccount token.
	TokenHeader = repoaccess.TokenHeader
	// MaxRepositories caps the maintained-repository list.
	MaxRepositories = 1000
)

The endpoints' fixed contract. They share the repository-access endpoint's listener, token header and audience.

View Source
const (
	RoleAdmin    = "admin"
	RoleMaintain = "maintain"
	RoleWrite    = "write"
	RoleTriage   = "triage"
	RoleRead     = "read"
	RoleNone     = "none"
)

Repository roles, highest first, as GitHub reports a user's permission.

View Source
const (
	// CodeCaller: the caller's ServiceAccount token was rejected.
	CodeCaller = "caller"
	// CodeBadRequest: the request body is malformed or out of bounds.
	CodeBadRequest = "bad_request"
	// CodeIntegration: the Integration is missing, suspended, not github, or
	// does not enable user authorization (or its credential cannot).
	CodeIntegration = "integration"
	// CodeUserToken: GitHub rejected the user's code, access token or
	// refresh token. An access token can be refreshed; a refresh token or
	// code means authorizing again.
	CodeUserToken = "user_token"
	// CodeUpstream: GitHub failed or could not be reached.
	CodeUpstream = "upstream"
)

Error codes the endpoints answer with.

Variables

View Source
var ErrUnavailable = errors.New("github user endpoints unavailable")

ErrUnavailable reports that the endpoints are not configured.

Functions

func AuthorizeURL

func AuthorizeURL(baseURL, clientID, redirectURI, state, challenge string) string

AuthorizeURL builds the GitHub authorize redirect for the user-to-server flow. A GitHub App takes no scopes: the App's permissions bound the token.

func Host

func Host(baseURL string) string

Host is the lower-case host an Integration's repositories live on.

func IsCode

func IsCode(err error, code string) bool

IsCode reports whether err is an endpoint Error with the code.

func Maintains

func Maintains(role string) bool

Maintains reports whether role is admin or maintain: the roles the maintainer grants act on.

func StatusFor

func StatusFor(code string) int

StatusFor maps an error code onto its HTTP status.

func WebURL

func WebURL(baseURL string) string

WebURL is the GitHub web root for an Integration's base URL: the OAuth endpoints live there, not under the API. "" is github.com; a GHES base URL keeps its scheme and host and drops the /api/v3 path.

func WriteError

func WriteError(w http.ResponseWriter, e *Error)

WriteError answers a request with an endpoint Error.

Types

type AuthorizeRepositoryRequest

type AuthorizeRepositoryRequest struct {
	Integration string `json:"integration"`
	AccessToken string `json:"accessToken"`
	Repo        string `json:"repo"`
}

AuthorizeRepositoryRequest asks for the user's role on one repository.

func (AuthorizeRepositoryRequest) Validate

func (r AuthorizeRepositoryRequest) Validate() error

Validate checks an authorize-repository request.

type AuthorizeRepositoryResponse

type AuthorizeRepositoryResponse struct {
	Role   string                       `json:"role"`
	Access repoaccess.IntegrationAccess `json:"access"`
}

AuthorizeRepositoryResponse is the user's role on the repository and the Integration's alert access to it: together they decide a maintained backfill in one round trip.

type Client

type Client struct {
	// URL is the integration-controller's internal base URL.
	URL string
	// TokenFile is the projected ServiceAccount token, read again on every
	// call because the kubelet rotates it.
	TokenFile string
	// HTTP defaults to a client with a 30-second timeout.
	HTTP *http.Client
}

Client calls the endpoints. The zero URL disables it.

func (*Client) AuthorizeRepository

func (c *Client) AuthorizeRepository(
	ctx context.Context, req AuthorizeRepositoryRequest,
) (*AuthorizeRepositoryResponse, error)

AuthorizeRepository returns the user's role on one repository and the Integration's alert access to it.

func (*Client) Enabled

func (c *Client) Enabled() bool

Enabled reports whether the client has an endpoint to call.

func (*Client) Exchange

func (c *Client) Exchange(ctx context.Context, req ExchangeRequest) (*ExchangeResponse, error)

Exchange trades an authorization code for the linked identity.

func (*Client) Refresh

func (c *Client) Refresh(ctx context.Context, req RefreshRequest) (*Tokens, error)

Refresh rotates a token pair.

func (*Client) Repositories

func (c *Client) Repositories(ctx context.Context, req RepositoriesRequest) (*RepositoriesResponse, error)

Repositories lists the user's maintained repositories.

type Error

type Error struct {
	Code    string `json:"code"`
	Message string `json:"message"`
}

Error is an endpoint's failure, as the body carries it.

func (*Error) Error

func (e *Error) Error() string

type ExchangeRequest

type ExchangeRequest struct {
	Integration string `json:"integration"`
	Code        string `json:"code"`
	Verifier    string `json:"verifier"`
	RedirectURI string `json:"redirectURI"`
}

ExchangeRequest trades an authorization code for a user token.

func (ExchangeRequest) Validate

func (r ExchangeRequest) Validate() error

Validate checks an exchange request.

type ExchangeResponse

type ExchangeResponse struct {
	Login  string `json:"login"`
	UserID int64  `json:"userID"`
	Tokens
}

ExchangeResponse is the linked identity and its tokens.

type RefreshRequest

type RefreshRequest struct {
	Integration  string `json:"integration"`
	RefreshToken string `json:"refreshToken"`
}

RefreshRequest rotates a token pair. GitHub accepts each refresh token once.

func (RefreshRequest) Validate

func (r RefreshRequest) Validate() error

Validate checks a refresh request.

type RepositoriesRequest

type RepositoriesRequest struct {
	Integration string `json:"integration"`
	AccessToken string `json:"accessToken"`
}

RepositoriesRequest lists the repositories the user maintains.

func (RepositoriesRequest) Validate

func (r RepositoriesRequest) Validate() error

Validate checks a repositories request.

type RepositoriesResponse

type RepositoriesResponse struct {
	Repositories []Repository `json:"repos"`
	// Truncated marks a list cut at MaxRepositories or the page budget.
	Truncated bool `json:"truncated,omitempty"`
}

RepositoriesResponse is the user's maintained repositories (admin or maintain), across the Integration App's installations the user can see.

type Repository

type Repository struct {
	// Name is "owner/name".
	Name string `json:"name"`
	Role string `json:"role"`
}

Repository is one repository the user maintains.

type Tokens

type Tokens struct {
	AccessToken  string    `json:"accessToken"`
	RefreshToken string    `json:"refreshToken,omitempty"`
	ExpiresAt    time.Time `json:"expiresAt,omitzero"`
}

Tokens is a user-to-server token pair. ExpiresAt is zero when the App does not expire user tokens.

Jump to

Keyboard shortcuts

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