Help us shape the future of Go! Take the 2026 Go Developer Survey

githubapp

package
v0.99.9 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2026 License: MIT Imports: 16 Imported by: 1

Documentation

Overview

Package githubapp 提供 GitHub App 凭证能力:用 App 的 RSA 私钥签发 RS256 JWT(第一层凭证),以及用该 JWT 换取 installation access token(第二层 凭证,实际调 GitHub API 用)。独立成包是为了让 git-sync-core、git-ferry 等下游共享同一实现,删掉各自手写的重复代码。

Index

Constants

View Source
const DefaultAPIBase = "https://api.github.com"

DefaultAPIBase 是 GitHub 公有 API 地址;GitHub Enterprise Server 传各自 的域名(如 https://ghe.example.com/api/v3)。

View Source
const DefaultRefreshMargin = time.Minute

DefaultRefreshMargin is how long before ExpiresAt a replacement token is fetched. GitHub rejects tokens the instant they expire, so a token used at 59:59.9 fails; refreshing a minute early keeps in-flight requests that obtained the token moments ago inside its validity window.

View Source
const DefaultTokenTTL = time.Hour

DefaultTokenTTL is the assumed validity of an installation token when the response omits expires_at. GitHub issues one-hour tokens; assuming the documented default is strictly safer than caching forever.

Variables

This section is empty.

Functions

func FetchInstallationToken

func FetchInstallationToken(ctx context.Context, apiBase string, appID, installationID int64, privateKeyPEM string) (string, error)

FetchInstallationToken 用 JWT 换 installation access token(有效期约 1h)。 apiBase 为空时取 DefaultAPIBase,尾部斜杠会被去掉。

请求走 transport.Client 而不是裸 http.Client,以复用平台统一的重试/日志/ 钩子链路;access_tokens 是不幂等的写操作,默认重试配置只允许"请求根本 没发出去"(DNS/dial 失败)级别的安全重试,不会重放已到达服务端的 POST。

func MintJWT

func MintJWT(appID int64, privateKeyPEM string) (string, error)

MintJWT 用 App 私钥签发 RS256 JWT,iss=app_id,iat=now-30(容忍调用方与 GitHub 之间的时钟偏移),exp=now+9 分钟(GitHub 限制 JWT 有效期 ≤10 分钟, 留 1 分钟余量)。支持 PKCS#1 与 PKCS#8 两种 PEM RSA 私钥格式——GitHub App 下载的 .pem 是 PKCS#8,openssl genrsa 产出的是 PKCS#1,两者都常见。

Types

type InstallationTokenSource added in v0.75.0

type InstallationTokenSource struct {
	APIBase        string
	AppID          int64
	InstallationID int64
	PrivateKeyPEM  string
	// RefreshMargin, when positive, overrides DefaultRefreshMargin. Tests
	// use small values to exercise the expiry paths without sleeping.
	RefreshMargin time.Duration
	// FallbackTTL, when positive, overrides DefaultTokenTTL for responses
	// without expires_at.
	FallbackTTL time.Duration
	// contains filtered or unexported fields
}

InstallationTokenSource is a caching TokenSource over FetchInstallationToken: it fetches an installation access token once, serves it until shortly before its expires_at, and only then mints a fresh JWT and fetches a replacement. Concurrent callers collapse onto a single fetch (the first one wins, the rest reuse its result), so wiring this straight into provider.Config.TokenSource costs one JWT signature plus one POST per hour — not per request.

src := githubapp.NewInstallationTokenSource("", appID, installationID, pem)
p, err := provider.NewProvider(provider.Config{
    Platform:    provider.PlatformGitHub,
    TokenSource: src, // satisfies provider.TokenSource structurally
})

Refresh failures return the error and leave the cache untouched, so the next call retries; a token already cached is never served past its expiry.

func NewInstallationTokenSource added in v0.75.0

func NewInstallationTokenSource(apiBase string, appID, installationID int64, privateKeyPEM string) *InstallationTokenSource

NewInstallationTokenSource builds a source for one app installation. apiBase follows FetchInstallationToken's rules (empty = GitHub public API; GHES passes its own /api/v3 root).

func (*InstallationTokenSource) ExpiresAt added in v0.75.0

func (s *InstallationTokenSource) ExpiresAt() time.Time

ExpiresAt reports when the cached token expires (zero value when nothing has been fetched yet) — for dashboards and tests.

func (*InstallationTokenSource) Token added in v0.75.0

Token returns a currently-valid installation access token, fetching or refreshing as needed. It is safe for concurrent use.

Jump to

Keyboard shortcuts

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