authprovider

package
v0.98.1 Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Overview

Package authprovider 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 (
	ErrProviderNonHTTPSURL = errors.New("provider_non_https_url")
	ErrProviderInvalid     = errors.New("provider_invalid")
)

Functions

func GetJSON added in v0.98.0

func GetJSON(ctx context.Context, client *http.Client, url, accept string, out any) error

GetJSON fetches url with client (Accept: accept when non-empty) and decodes the JSON body into out. A non-200 status is an error. UserInfoFunc implementations use it to read userinfo endpoints.

func IdentityID added in v0.98.0

func IdentityID(v any) string

IdentityID renders a JSON id (string or number) as the subject string; numbers render without a fractional part.

Types

type AppleSecret added in v0.98.0

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 added in v0.98.0

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 added in v0.98.0

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 added in v0.98.0

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 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 added in v0.98.0

type Option func(*base)

Option adjusts a provider at construction.

func WithAuthParams added in v0.98.0

func WithAuthParams(params map[string]string) Option

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

func WithDisplayName added in v0.98.0

func WithDisplayName(name string) Option

WithDisplayName sets the human-readable name reported by DisplayName.

func WithHTTPClient added in v0.98.0

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 operator configuration.

func WithPKCE added in v0.98.0

func WithPKCE(on bool) Option

WithPKCE turns the S256 code challenge on or off.

func WithScopes added in v0.98.0

func WithScopes(scopes ...string) Option

WithScopes replaces the requested scopes.

func WithSecret added in v0.98.0

func WithSecret(secret Secret) Option

WithSecret replaces the client secret source (dynamic secrets).

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
	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
}

Provider is one external identity provider.

func Apple added in v0.72.0

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 added in v0.72.0

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

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

func GitHub added in v0.72.0

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 added in v0.72.0

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

Google is Google Sign-In over OIDC with PKCE.

func OAuth2 added in v0.98.0

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 added in v0.98.0

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 added in v0.98.0

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

Secret produces the client secret sent on a code exchange.

type SecretFunc added in v0.98.0

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

SecretFunc mints a client secret per exchange.

func (SecretFunc) ClientSecret added in v0.98.0

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

type StaticSecret added in v0.98.0

type StaticSecret string

StaticSecret is a fixed client secret.

func (StaticSecret) ClientSecret added in v0.98.0

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

type UserInfoFunc added in v0.98.0

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