authprovider

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2026 License: MIT Imports: 8 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package authprovider abstracts the human login providers behind one interface (SPEC-0012): a provider knows how to start its login (produce the redirect to its authorization endpoint) and how to finish one (turn the callback's code into a verified identity). Everything flow-shaped — routes, the shared state cookie, session establishment, CSRF — lives in the httpapi dispatcher and is deliberately NOT part of this interface, so Pocket ID (OIDC) and GitHub (plain OAuth 2.0 + REST profile fetch) can differ in verification while feeding the same session establishment.

Governing: SPEC-0012, ADR-0017.

Index

Constants

View Source
const GitHubIssuer = "https://github.com"

GitHubIssuer is the iss recorded on sessions a GitHub login established.

Variables

This section is empty.

Functions

func ValidateNext

func ValidateNext(next string) bool

ValidateNext reports whether next is an acceptable post-login destination: empty (the default) or a same-origin absolute path. Anything with a scheme or netloc — the open-redirect shapes — is rejected, so the state cookie's stashed destination is the only post-login navigation.

Types

type GitHubProvider

type GitHubProvider struct {
	OAuth   *oauth2.Config // Scopes must be "read:user user:email".
	APIBase string         // default https://api.github.com
}

GitHubProvider implements Provider against GitHub's OAuth 2.0 endpoints and REST API. OAuth carries the client credentials and the endpoint (a field so tests can point it at an httptest fake); APIBase likewise defaults to github.com and is overridable in tests. The access token lives only inside FinishLogin and is dropped when it returns — never persisted or logged.

func NewGitHubProvider

func NewGitHubProvider(clientID, clientSecret, baseURL string) *GitHubProvider

NewGitHubProvider builds the GitHub provider from client credentials and the instance's public origin (the redirect URI is always BaseURL + /auth/callback, derived — never separately configured).

func (*GitHubProvider) DisplayName

func (g *GitHubProvider) DisplayName() string

DisplayName implements Provider.

func (*GitHubProvider) FinishLogin

func (g *GitHubProvider) FinishLogin(ctx context.Context, st State, r *http.Request) (Identity, error)

FinishLogin implements Provider. Checks the stashed state against the callback's state parameter BEFORE any token exchange, exchanges the code for an access token (dropped at return), fetches the profile and emails, and requires a primary VERIFIED email — an unverified or missing one is a rejection, not an actor id.

func (*GitHubProvider) ID

func (g *GitHubProvider) ID() string

ID implements Provider.

func (*GitHubProvider) StartLogin

func (g *GitHubProvider) StartLogin(state, _ string) (string, error)

StartLogin implements Provider.

type Identity

type Identity struct {
	Issuer        string
	Subject       string
	Email         string
	EmailVerified bool
	Handle        string
}

Identity is the verified outcome of a finished login: who issued it, the subject that issuer asserts, the email it asserts and whether that email is verified, and a display-handle hint. The caller resolves it to a user by (Issuer, Subject); the email only links a first sign-in to an existing user when it is verified (SPEC-0023 REQ "Users and Identities").

type Provider

type Provider interface {
	// ID is the registry key the ?provider= parameter selects ("github").
	ID() string
	// DisplayName is the human-facing name the login page renders.
	DisplayName() string
	// StartLogin mints the authorization redirect URL for this login attempt.
	// state is the single-use anti-CSRF value the caller also stashes in the
	// state cookie and requires back at the callback.
	StartLogin(state, next string) (redirectURL string, err error)
	// FinishLogin exchanges the callback's code and verifies the provider's
	// answer, returning the verified Identity. st carries the state that was
	// stashed at StartLogin; implementations must reject when st.State does
	// not match the callback's state parameter (checked here so no caller can
	// forget it).
	FinishLogin(ctx context.Context, st State, r *http.Request) (Identity, error)
}

Provider is one human login provider. StartLogin returns the authorization redirect URL for a fresh login (the caller stashes its own per-login state — including the provider id — in the shared state cookie before redirecting). FinishLogin validates the callback against the same stashed state and exchanges the code for a verified Identity. Implementations must treat every rejection as terminal: no partial identity, nothing persisted.

type State

type State struct {
	State    string `json:"s"`
	Provider string `json:"p,omitempty"`
	Next     string `json:"x,omitempty"`
}

State is the per-login state the dispatcher stashes in the shared state cookie and hands back at the callback. It reuses the OIDC state cookie's TTL and single-use clearing.

Jump to

Keyboard shortcuts

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