oauth

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package oauth runs the provider side of signing in with Google and GitHub: building the authorization URL with PKCE and state, and swapping the returned code for the user's identity.

It holds no state of its own. The caller stores state, the PKCE verifier, and the nonce between the two steps, and decides what the identity means.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Apple

type Apple struct {
	// contains filtered or unexported fields
}

Apple checks the identity tokens of native Sign in with Apple on iOS. There's no redirect or code exchange: the app gets the signed token from the system sheet and sends it to the API.

func NewApple

func NewApple(bundleIDs []string, e AppleEndpoints, now func() time.Time) *Apple

NewApple returns a checker for tokens issued to any of bundleIDs. now is the clock for the token's expiry check.

func (*Apple) Verify

func (a *Apple) Verify(ctx context.Context, rawToken, nonce string) (Identity, error)

Verify checks an identity token from the native sheet and returns the identity in it. The app gives Apple the hex SHA-256 of a random nonce, and sends the nonce itself here, so a token on its own can't be replayed by someone who doesn't have the nonce.

type AppleEndpoints

type AppleEndpoints struct {
	JWKSURL string
	Issuer  string
}

AppleEndpoints are where Apple's keys live, and the issuer its tokens name. Tests point them at a fake.

func DefaultAppleEndpoints

func DefaultAppleEndpoints() AppleEndpoints

DefaultAppleEndpoints returns Apple's real endpoints.

type GitHubEndpoints

type GitHubEndpoints struct {
	AuthURL  string
	TokenURL string
	APIURL   string
}

GitHubEndpoints are where GitHub's endpoints live. Tests point them at a fake.

func DefaultGitHubEndpoints

func DefaultGitHubEndpoints() GitHubEndpoints

DefaultGitHubEndpoints returns GitHub's real endpoints.

type GoogleEndpoints

type GoogleEndpoints struct {
	AuthURL  string
	TokenURL string
	JWKSURL  string
	// Issuers are the accepted values of the ID token's iss claim.
	Issuers []string
}

GoogleEndpoints are where Google's endpoints live. Tests point them at a fake.

func DefaultGoogleEndpoints

func DefaultGoogleEndpoints() GoogleEndpoints

DefaultGoogleEndpoints returns Google's real endpoints.

type Identity

type Identity struct {
	// Subject is the provider's ID for the user, which never changes.
	Subject string
	// Email is the user's address at the provider. It may be empty.
	Email string
	// EmailVerified reports whether the provider vouches for Email.
	EmailVerified bool
}

Identity is who a provider says the user is.

type Provider

type Provider interface {
	// AuthCodeURL returns the URL to send the user to. verifier is the PKCE
	// verifier; nonce is checked in the ID token, by providers that issue
	// one.
	AuthCodeURL(state, verifier, nonce, redirectURI string) string
	// Exchange swaps the code from the provider's redirect for the user's
	// identity.
	Exchange(ctx context.Context, code, verifier, nonce, redirectURI string) (Identity, error)
}

Provider is one sign-in provider.

func NewGitHub

func NewGitHub(clientID, clientSecret string, e GitHubEndpoints) Provider

NewGitHub returns GitHub sign-in. GitHub has no ID tokens, so the identity comes from its API: the user's ID, and their primary email if verified.

func NewGoogle

func NewGoogle(clientID, clientSecret string, e GoogleEndpoints, now func() time.Time) Provider

NewGoogle returns Google sign-in, using OpenID Connect: the identity comes from the signed ID token. now is the clock for the token's expiry check.

Directories

Path Synopsis
Package oauthtest provides fake Google and GitHub servers for tests.
Package oauthtest provides fake Google and GitHub servers for tests.

Jump to

Keyboard shortcuts

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