githubauth

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package githubauth provides a recipe that prepares an HTTP mock server to stand in for "Sign in with GitHub": the OAuth web flow of an OAuth app, and the profile of the user who signs in. Point the github.com and api.github.com base URLs of the app under test at the mock server.

err := m.Prep(ctx, githubauth.New(githubauth.ClientID("my-app"), githubauth.Token("gho_test")))

The recipe covers the OAuth web flow only: not the device flow, nor GitHub Apps.

Index

Constants

View Source
const (
	DefaultClientID     = "vlatte-client-id"
	DefaultClientSecret = "vlatte-client-secret"
	DefaultCode         = "vlatte-code"
	DefaultToken        = "gho_vlatte"
	DefaultScope        = "read:user,user:email"
)

Defaults of the recipe, for a test to send what the recipe expects without setting it.

View Source
const (
	AuthorizePath   = "/login/oauth/authorize"
	AccessTokenPath = "/login/oauth/access_token"
	UserPath        = "/user"
	UserEmailsPath  = "/user/emails"
)

The paths the recipe mocks, the first two of github.com, the others of api.github.com.

Variables

This section is empty.

Functions

func New

func New(opts ...Option) mockhttp.Recipe

New returns a recipe that mocks the OAuth web flow of GitHub and the profile of the user who signs in:

  • GET /login/oauth/authorize, with the client ID and a redirect_uri, redirects to redirect_uri with the code and the request's state, as GitHub does once the user consents. Unlike GitHub, which falls back to the callback URL the app registered, it needs redirect_uri: without it, no mock matches.
  • POST /login/oauth/access_token, with Accept: application/json and the client ID and secret and the code in its form, answers the token as JSON. A wrong code or wrong credentials get GitHub's error answer: a 200 whose JSON has error set to bad_verification_code or incorrect_client_credentials. GitHub answers a form-encoded body without that Accept; the recipe matches no such request.
  • GET /user and GET /user/emails, with the token in an Authorization header, as "Bearer <token>" or "token <token>", answer the profile and the addresses. Any other GET to them gets a 401, Bad credentials.

Types

type Email

type Email struct {
	Email      string `json:"email"`
	Primary    bool   `json:"primary"`
	Verified   bool   `json:"verified"`
	Visibility string `json:"visibility"`
}

Email is an address GET /user/emails answers.

type Option

type Option func(c *config)

Option configures the recipe New returns.

func ClientID

func ClientID(id string) Option

ClientID sets the client ID of the OAuth app: the authorize and access token endpoints answer only requests that send it. The default is DefaultClientID.

func ClientSecret

func ClientSecret(secret string) Option

ClientSecret sets the client secret of the OAuth app, which the access token endpoint asks for. The default is DefaultClientSecret.

func Code

func Code(code string) Option

Code sets the code the authorize endpoint gives the app, and the only one the access token endpoint exchanges. Unlike GitHub's, it can be exchanged any number of times. The default is DefaultCode.

func Emails

func Emails(emails ...Email) Option

Emails sets the addresses GET /user/emails answers. The default is the email of the profile, primary and verified, or none when the profile has no email.

func Scope

func Scope(scope string) Option

Scope sets the scope the access token endpoint says the token has. The default is DefaultScope.

func Token

func Token(token string) Option

Token sets the access token the access token endpoint issues, and the only one GET /user and GET /user/emails accept. The default is DefaultToken.

func WithUser

func WithUser(user User) Option

WithUser sets the profile GET /user answers. The default is the user octocat. It is not named User, the type of the profile.

type User

type User struct {
	ID        int64  `json:"id"`
	Login     string `json:"login"`
	Name      string `json:"name"`
	Email     string `json:"email"`
	AvatarURL string `json:"avatar_url"`
	HTMLURL   string `json:"html_url"`
	Type      string `json:"type"`
}

User is the profile GET /user answers, with the fields an app signing users in usually reads.

Jump to

Keyboard shortcuts

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