provider

package
v0.147.0 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2026 License: MIT Imports: 21 Imported by: 0

Documentation

Overview

Package provider defines the external identity providers AuthKit's browser login flows delegate to. A Provider owns everything specific to one IdP — endpoints, scopes, PKCE, the shape of its authorization response, how its client secret is produced, and how its identity is read — so the HTTP layer keeps only the browser state machine. Use Google/Apple/Discord/GitHub for the built-ins and OIDC/OAuth2 for anything else.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNonHTTPSURL = errors.New("provider_non_https_url")
	ErrInvalid     = errors.New("provider_invalid")
)
View Source
var ErrUnavailable = errors.New("provider_unavailable")

ErrUnavailable marks a failure caused by the provider being unreachable or answering 5xx/429, as opposed to rejecting the request.

Functions

This section is empty.

Types

type AppleSecret

type AppleSecret struct {
	Static        string
	TeamID        string
	KeyID         string
	PrivateKeyPEM []byte
	TTL           time.Duration
}

AppleSecret is how Sign in with Apple authenticates the client: either a pre-minted client secret JWT (Static) or the developer key that mints a fresh ES256 JWT per exchange (TeamID, KeyID, PrivateKeyPEM; TTL defaults to five minutes).

type AuthRequest

type AuthRequest struct {
	State         string
	Nonce         string
	CodeChallenge string
	RedirectURI   string
	// Params are extra authorization parameters for this request (for example
	// max_age=0 on a step-up).
	Params map[string]string
}

AuthRequest carries the per-flow values the browser state machine generated. CodeChallenge is set only when the provider reports PKCE.

type Endpoint

type Endpoint struct {
	AuthorizeURL string
	TokenURL     string
}

Endpoint is a plain OAuth2 authorization server. Client credentials are sent in the token request body.

type ExchangeRequest

type ExchangeRequest struct {
	Code         string
	CodeVerifier string
	Nonce        string
	RedirectURI  string
}

ExchangeRequest carries the authorization response plus the flow values it must be checked against.

type HealthChecker

type HealthChecker interface {
	CheckHealth(ctx context.Context) error
}

HealthChecker is implemented by providers that cache remote state (OIDC discovery). CheckHealth performs no I/O, so a host can register it as an optional dependency probe.

type Identity

type Identity struct {
	Subject           string
	Email             string
	EmailVerified     bool
	PreferredUsername string
	DisplayName       string
	// AuthTime is when the user last authenticated interactively at the IdP;
	// zero when the provider does not assert one.
	AuthTime time.Time
}

Identity is what a provider asserts about the signed-in user after a successful code exchange. EmailVerified is true only when the provider itself vouches for the address.

type Option

type Option func(*base)

Option adjusts a provider at construction.

func WithAuthParams

func WithAuthParams(params map[string]string) Option

WithAuthParams adds fixed authorization-request parameters (for example response_mode=form_post).

func WithDisplayName

func WithDisplayName(name string) Option

WithDisplayName sets the human-readable name reported by DisplayName.

func WithHTTPClient

func WithHTTPClient(c *http.Client) Option

WithHTTPClient sets the outbound client for discovery, token and userinfo calls. The default is timeout-bounded and may reach private addresses, since IdP endpoints are host configuration.

func WithPKCE

func WithPKCE(on bool) Option

WithPKCE turns the S256 code challenge on or off.

func WithScopes

func WithScopes(scopes ...string) Option

WithScopes replaces the requested scopes.

func WithSecret

func WithSecret(secret Secret) Option

WithSecret replaces the client secret source (dynamic secrets).

func WithTrustedEmailVerification

func WithTrustedEmailVerification(trusted bool) Option

WithTrustedEmailVerification declares whether the provider's email_verified claim proves address ownership. Google, Apple, GitHub and Discord default to trusted; OIDC and OAuth2 default to untrusted.

type Provider

type Provider interface {
	// Name is the slug used in routes (/oidc/{name}/…) and stored on links.
	Name() string
	DisplayName() string
	// Issuer identifies the identity source; provider links are keyed by it.
	Issuer() string
	// PKCE reports whether the authorization request carries an S256 challenge.
	PKCE() bool
	// ResponseModeFormPost reports whether the IdP posts the authorization
	// response cross-site (response_mode=form_post) instead of redirecting.
	ResponseModeFormPost() bool
	// SupportsStepUp reports whether a completed login proves a fresh
	// interactive authentication (OIDC max_age=0 checked against auth_time).
	// OAuth2 IdPs silently re-authorize an approved app, so they never do.
	SupportsStepUp() bool
	// TrustsEmailVerification reports whether this provider's email_verified
	// assertion is proof that the user controls the address. Only then may
	// AuthKit create an account with that address verified; otherwise the
	// address is ignored at registration (ak#393). Generic OIDC/OAuth2 IdPs
	// often let a tenant or user choose the claimed email, so they are
	// untrusted unless the host opts in with WithTrustedEmailVerification.
	TrustsEmailVerification() bool
	// AuthCodeURL and Exchange wrap ErrUnavailable when the provider
	// could not be reached or answered 5xx/429.
	AuthCodeURL(ctx context.Context, req AuthRequest) (string, error)
	Exchange(ctx context.Context, req ExchangeRequest) (Identity, error)
	// Validate checks the static configuration; it performs no network calls.
	Validate() error
	// contains filtered or unexported methods
}

Provider is one external identity provider. It is sealed: build one with Google, Apple, Discord, GitHub, OIDC or OAuth2, so a capability can be added to every provider without breaking hosts.

func Apple

func Apple(clientID string, secret AppleSecret, opts ...Option) Provider

Apple is Sign in with Apple over OIDC. Apple returns the authorization response as a cross-site POST (response_mode=form_post) whenever name or email is requested, does not support PKCE for web flows, and asserts email_verified on its ID token.

func Discord

func Discord(clientID, clientSecret string, opts ...Option) Provider

Discord is Discord OAuth2 with PKCE. /users/@me reports whether Discord itself has verified the address (`verified`), which is what EmailVerified carries.

func GitHub

func GitHub(clientID, clientSecret string, opts ...Option) Provider

GitHub is GitHub OAuth2 with PKCE. /user.email is the public profile address and carries no verification guarantee, so the verified address is taken from /user/emails (the primary+verified entry); the public address is only a fallback and is never reported verified.

func Google

func Google(clientID, clientSecret string, opts ...Option) Provider

Google is Google Sign-In over OIDC with PKCE.

func OAuth2

func OAuth2(name, issuer string, ep Endpoint, clientID, clientSecret string, userInfo UserInfoFunc, opts ...Option) Provider

OAuth2 returns a provider for an authorization server without OpenID Connect: the code is exchanged for an access token and userInfo reads the identity with it. issuer keys the stored provider links.

func OIDC

func OIDC(name, issuer, clientID, clientSecret string, opts ...Option) Provider

OIDC returns an OpenID Connect provider: endpoints and keys come from the issuer's discovery document, the ID token is verified (signature, audience, nonce) and identity is read from its standard claims. "openid" is always requested.

type Secret

type Secret interface {
	ClientSecret(ctx context.Context) (string, error)
}

Secret produces the client secret sent on a code exchange.

type SecretFunc

type SecretFunc func(ctx context.Context) (string, error)

SecretFunc mints a client secret per exchange.

func (SecretFunc) ClientSecret

func (f SecretFunc) ClientSecret(ctx context.Context) (string, error)

type StaticSecret

type StaticSecret string

StaticSecret is a fixed client secret.

func (StaticSecret) ClientSecret

func (s StaticSecret) ClientSecret(context.Context) (string, error)

type UserInfoFunc

type UserInfoFunc func(ctx context.Context, client *http.Client) (Identity, error)

UserInfoFunc reads the signed-in user's identity. client already sends the access token as a bearer credential.

Jump to

Keyboard shortcuts

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