ghauth

package module
v0.0.0-...-0c0f46e Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2025 License: MIT Imports: 14 Imported by: 1

README

GitHub Authentication HTTP Transport Go Reference

A Golang http.RoundTripper for injecting GitHub authentication headers to GitHub's REST API.

Usage

Here's an example of using the Transport method with go-github:

package main

import (
	"context"
	"fmt"
	"log"
	"net/http"
	"os"
	"os/signal"

	ghauth "github.com/bored-engineer/github-auth-http-transport"
	"github.com/google/go-github/v71/github"
)

func main() {
	ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
	defer cancel()

	transport, err := ghauth.Transport(ctx, nil)
	if err != nil {
		log.Fatalf("ghauth.Transport failed: %v", err)
	}
	client := github.NewClient(&http.Client{
		Transport: transport,
	})

	user, _, err := client.Users.Get(ctx, "")
	if err != nil {
		log.Fatalf("(*github.UsersService).Get failed: %v", err)
	}

	fmt.Printf("authenticated as: %s\n", user.GetLogin())
}

The Transport method will try the following sources (in order):

  • Environment: A static GitHub API token (typically a PAT) found in the current environment variables:
    • GH_TOKEN
    • GITHUB_TOKEN
    • GH_ENTERPRISE_TOKEN
    • GITHUB_ENTERPRISE_TOKEN
  • App: Authentication as a GitHub App configured via the GH_APP_ID, GH_APP_INSTALLATION_ID, and GH_APP_PRIVATE_KEY environment variables.
  • Basic: HTTP Basic Authentication as configured via the GH_CLIENT_ID and GH_CLIENT_SECRET environment variables.
  • Netrc: A machine entry for github.com or api.github.com (or GH_HOST if populated) in the ~/.netrc file
  • CLI: A token obtained by executing gh auth token

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func App

func App(
	ctx context.Context,
	appID string,
	installationID string,
	privateKeyPath string,
) (oauth2.TokenSource, error)

App returns a oauth2.TokenSource for a given GitHub App installation. If appID is empty, it will look for the GH_APP_ID environment variable. If installationID is empty, it will look for the GH_APP_INSTALLATION_ID environment variable. If privateKeyPath is empty, it will look for the GH_APP_PRIVATE_KEY environment variable.

func Basic

func Basic(
	base http.RoundTripper,
	clientID string,
	clientSecret string,
) (*basicauth.Transport, error)

Basic returns a basicauth.Transport using a Github OAuth App's client ID and client secret. If clientID is empty, it will look for the GH_CLIENT_ID environment variable. If clientSecret is empty, it will look for the GH_CLIENT_SECRET environment variable.

func CLI

func CLI(path string, host string) (*oauth2.Token, error)

CLI retrieves an *oauth2.Token by running the 'gh auth token' command to fetch the token used by the GitHub CLI. If path is empty, it will try the GH_PATH environment variable, then to look for the 'gh' command in the system PATH. If host is empty, it will use the default GitHub host (github.com).

func Environment

func Environment() (*oauth2.Token, error)

Environment reads a static GitHub token from the environment It tries (in order): GH_TOKEN, GITHUB_TOKEN, GH_ENTERPRISE_TOKEN, GITHUB_ENTERPRISE_TOKEN

func Host

func Host() string

Host returns the host name of the GitHub server by reading the GH_HOST environment variable. If the variable is not set, it defaults to "github.com".

func Netrc

func Netrc(path string, host string) (*oauth2.Token, error)

Netrc attempts to extract a static API token from a .netrc file. If path is empty, ~/.netrc is used If host is empty, the default host is used

func Token

func Token(token string) *oauth2.Token

Token creates a new oauth2.Token with the given token string. The token type is set to "Bearer" by default.

func TokenSource

func TokenSource(token *oauth2.Token) oauth2.TokenSource

TokenSource creates a new oauth2.TokenSource using the provided token.

func TokenTransport

func TokenTransport(token *oauth2.Token) *oauth2.Transport

TokenTransport creates a new oauth2.Transport using the provided token.

func Transport

func Transport(ctx context.Context, base http.RoundTripper) (http.RoundTripper, error)

Transport returns an http.RoundTripper that adds GitHub authentication to each HTTP request. If base is nil, http.DefaultTransport is used. It will try (in order): Environment, App, OAuth, Netrc, CLI

Types

This section is empty.

Jump to

Keyboard shortcuts

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