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
- Variables
- func AuthorizeURL(baseURL, clientID, redirectURI, state, challenge string) string
- func Host(baseURL string) string
- func IsCode(err error, code string) bool
- func Maintains(role string) bool
- func StatusFor(code string) int
- func WebURL(baseURL string) string
- func WriteError(w http.ResponseWriter, e *Error)
- type AuthorizeRepositoryRequest
- type AuthorizeRepositoryResponse
- type Client
- func (c *Client) AuthorizeRepository(ctx context.Context, req AuthorizeRepositoryRequest) (*AuthorizeRepositoryResponse, error)
- func (c *Client) Enabled() bool
- func (c *Client) Exchange(ctx context.Context, req ExchangeRequest) (*ExchangeResponse, error)
- func (c *Client) Refresh(ctx context.Context, req RefreshRequest) (*Tokens, error)
- func (c *Client) Repositories(ctx context.Context, req RepositoriesRequest) (*RepositoriesResponse, error)
- type Error
- type ExchangeRequest
- type ExchangeResponse
- type RefreshRequest
- type RepositoriesRequest
- type RepositoriesResponse
- type Repository
- type Tokens
Constants ¶
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.
const ( RoleAdmin = "admin" RoleMaintain = "maintain" RoleWrite = "write" RoleTriage = "triage" RoleRead = "read" RoleNone = "none" )
Repository roles, highest first, as GitHub reports a user's permission.
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 ¶
ErrUnavailable reports that the endpoints are not configured.
Functions ¶
func AuthorizeURL ¶
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 Maintains ¶
Maintains reports whether role is admin or maintain: the roles the maintainer grants act on.
func WebURL ¶
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) Exchange ¶
func (c *Client) Exchange(ctx context.Context, req ExchangeRequest) (*ExchangeResponse, error)
Exchange trades an authorization code for the linked identity.
func (*Client) Repositories ¶
func (c *Client) Repositories(ctx context.Context, req RepositoriesRequest) (*RepositoriesResponse, error)
Repositories lists the user's maintained repositories.
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 ¶
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.