Description
Description
Summary
I propose the addition of new functionality to go-github that will enable users to create and manage GitHub App tokens and installation tokens. This feature will enhance the library's capabilities, making it easier for developers to authenticate and interact with GitHub Apps.
Motivation
GitHub Apps are a powerful way to integrate with GitHub, allowing developers to automate workflows and enhance their repositories' functionality. However, generating and managing tokens for GitHub Apps its done outside go-github pkg, implemented by every project that need it. This proposal aims to simplify this process by introducing a new set of functionalities for creating and handling GitHub App tokens and installation tokens.
Design
The main idea is implementing different token sources to authenticate requests TokenSource
type TokenSource interface {
// Token returns a token or an error.
// Token must be safe for concurrent use by multiple goroutines.
// The returned Token must not be modified.
Token() (Token, error)
}
Proposed code scaffolding
// applicationTokenSource represents a GitHub App token.
// https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/generating-a-json-web-token-jwt-for-a-github-app
type applicationTokenSource struct {}
// ApplicationTokenOpt is a functional option for ApplicationTokenSource.
type ApplicationTokenOpt func(*applicationTokenSource)
// NewApplicationTokenSource creates a new GitHub App token source.
// An application token is used to authenticate as a GitHub App.
func NewApplicationTokenSource(id string, privateKey []byte, opts ...ApplicationTokenOpt) (oauth2.TokenSource, error) {
return &applicationTokenSource{}, nil
}
// Token creates a new GitHub App token.
// The token is used to authenticate as a GitHub App.
// Each token is valid for 10 minutes.
func (t *applicationTokenSource) Token() (*oauth2.Token, error) {
return &oauth2.Token{}, nil
}
// InstallationTokenSourceOpt is a functional option for InstallationTokenSource.
type InstallationTokenSourceOpt func(*installationTokenSource)
// InstallationTokenSource represents a GitHub App installation token source.
type installationTokenSource struct {
}
// NewInstallationTokenSource creates a new GitHub App installation token source.
// It could use an application token sources to authenticate requests to retrieve
// the installation token using github API.
func NewInstallationTokenSource(id int64, src oauth2.TokenSource, opts ...InstallationTokenSourceOpt) oauth2.TokenSource {
return &installationTokenSource{}
}
// Token creates a new GitHub App installation token.
// The token is used to authenticate as a GitHub App installation.
func (t *installationTokenSource) Token() (*oauth2.Token, error) {
return &oauth2.Token{}, nil
}
Usage
import (
"context"
"github.com/google/go-github/v62/github"
"golang.org/x/oauth2"
)
func main () {
ctx := context.Background()
appts, _ := github.NewApplicationTokenSource("app_id", []byte("private_key"))
its := github.NewInstallationTokenSource("installation_id", appts)
githubClient := github.NewClient(oauth2.NewClient(ctx, its))
}