oauth2githubapp

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2024 License: Apache-2.0 Imports: 16 Imported by: 1

README

oauth2-github-app go Go Reference

This is a Go package for authenticating with a GitHub App Installation. It is interoperable with golang.org/x/oauth2 package.

Getting Started

Prerequisite

Set up your GitHub App Installation.

  1. Create a GitHub App
  2. Download a private key of the GitHub App
  3. Install your GitHub App on your repository or organization

This package requires the following inputs:

  • Private Key file
  • App ID (number)
  • Installation ID (number)
Create a client

To create an OAuth2 client with GitHub App installation token,

func run(ctx context.Context, appID, installationID, privateKeyFilename string) error {
	privateKey, err := oauth2githubapp.LoadPrivateKey(privateKeyFilename)
	if err != nil {
		return fmt.Errorf("could not load the private key: %w", err)
	}

	// create a client
	cfg := oauth2githubapp.Config{
		PrivateKey:     privateKey,
		AppID:          appID,
		InstallationID: installationID,
	}
	client := oauth2.NewClient(ctx, cfg.TokenSource(ctx))
}

For github.com/google/go-github package,

	cfg := oauth2githubapp.Config{
		PrivateKey:     privateKey,
		AppID:          appID,
		InstallationID: installationID,
	}
	client := github.NewClient(oauth2.NewClient(ctx, cfg.TokenSource(ctx)))

For github.com/shurcooL/githubv4 package,

	cfg := oauth2githubapp.Config{
		PrivateKey:     privateKey,
		AppID:          appID,
		InstallationID: installationID,
	}
	client := githubv4.NewClient(oauth2.NewClient(ctx, cfg.TokenSource(ctx)))

See also example.

GitHub Enterprise

To set your GitHub Enterprise server,

	cfg := oauth2githubapp.Config{
		PrivateKey:     privateKey,
		AppID:          appID,
		InstallationID: installationID,
		BaseURL:        "https://api.github.example.com",
	}
	client := oauth2.NewClient(ctx, cfg.TokenSource(ctx))

Contribution

This is an open source software. Feel free to contribute to it.

Documentation

Overview

Package oauth2githubapp provides an OAuth 2.0 client for GitHub App.

This package implements the authentication method described in https://docs.github.com/en/developers/apps/authenticating-with-github-apps

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LoadPrivateKey

func LoadPrivateKey(name string) (*rsa.PrivateKey, error)

LoadPrivateKey reads the private key file of GitHub App

func ParsePrivateKey

func ParsePrivateKey(b []byte) (*rsa.PrivateKey, error)

ParsePrivateKey parses the private key of GitHub App

Types

type Config

type Config struct {
	PrivateKey     *rsa.PrivateKey
	AppID          string
	InstallationID string

	// BaseURL is an endpoint of GitHub API.
	// Any trailing slash is trimmed.
	// If not set, it defaults to https://api.github.com
	BaseURL string
}

Config represents a config of GitHub App Installation

func (Config) Token

func (c Config) Token(ctx context.Context) (*oauth2.Token, error)

Token requests an installation access token using a JWT of GitHub App.

See https://docs.github.com/en/developers/apps/authenticating-with-github-apps#authenticating-as-a-github-app

func (Config) TokenSource

func (c Config) TokenSource(ctx context.Context) oauth2.TokenSource

TokenSource returns an oauth2.TokenSource for GitHub App Installation

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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