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 ¶
- Variables
- type AppleSecret
- type AuthRequest
- type Endpoint
- type ExchangeRequest
- type HealthChecker
- type Identity
- type Option
- func WithAuthParams(params map[string]string) Option
- func WithDisplayName(name string) Option
- func WithHTTPClient(c *http.Client) Option
- func WithPKCE(on bool) Option
- func WithScopes(scopes ...string) Option
- func WithSecret(secret Secret) Option
- func WithTrustedEmailVerification(trusted bool) Option
- type Provider
- func Apple(clientID string, secret AppleSecret, opts ...Option) Provider
- func Discord(clientID, clientSecret string, opts ...Option) Provider
- func GitHub(clientID, clientSecret string, opts ...Option) Provider
- func Google(clientID, clientSecret string, opts ...Option) Provider
- func OAuth2(name, issuer string, ep Endpoint, clientID, clientSecret string, ...) Provider
- func OIDC(name, issuer, clientID, clientSecret string, opts ...Option) Provider
- type Secret
- type SecretFunc
- type StaticSecret
- type UserInfoFunc
Constants ¶
This section is empty.
Variables ¶
var ( ErrNonHTTPSURL = errors.New("provider_non_https_url") ErrInvalid = errors.New("provider_invalid") )
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 ¶
Endpoint is a plain OAuth2 authorization server. Client credentials are sent in the token request body.
type ExchangeRequest ¶
ExchangeRequest carries the authorization response plus the flow values it must be checked against.
type HealthChecker ¶
HealthChecker is implemented by providers that cache remote state (OIDC discovery). CheckHealth retries a failing provider through its cache, at most once per failure backoff, so a host can register it as a dependency probe that turns healthy once the provider recovers.
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 ¶
WithAuthParams adds fixed authorization-request parameters (for example response_mode=form_post).
func WithDisplayName ¶
WithDisplayName sets the human-readable name reported by DisplayName.
func WithHTTPClient ¶
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 WithScopes ¶
WithScopes replaces the requested scopes.
func WithSecret ¶
WithSecret replaces the client secret source (dynamic secrets).
func WithTrustedEmailVerification ¶
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 ¶
Discord is Discord OAuth2 with PKCE. /users/@me reports whether Discord itself has verified the address (`verified`), which is what EmailVerified carries.
func GitHub ¶
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 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 ¶
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. Discovery is cached under cachepolicy.OIDCDiscovery: stale metadata keeps serving while it is reloaded, up to MaxStale after the last success; past that logins fail closed, so blocking discovery cannot keep revoked keys valid.
type SecretFunc ¶
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)