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 ¶
- Variables
- func GetJSON(ctx context.Context, client *http.Client, url, accept string, out any) error
- func IdentityID(v any) string
- type AppleSecret
- type AuthRequest
- type Endpoint
- type ExchangeRequest
- type Identity
- type 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 ( ErrProviderNonHTTPSURL = errors.New("provider_non_https_url") ErrProviderInvalid = errors.New("provider_invalid") )
Functions ¶
func GetJSON ¶ added in v0.98.0
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
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
Endpoint is a plain OAuth2 authorization server. Client credentials are sent in the token request body.
type ExchangeRequest ¶ added in v0.98.0
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
WithAuthParams adds fixed authorization-request parameters (for example response_mode=form_post).
func WithDisplayName ¶ added in v0.98.0
WithDisplayName sets the human-readable name reported by DisplayName.
func WithHTTPClient ¶ added in v0.98.0
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 WithScopes ¶ added in v0.98.0
WithScopes replaces the requested scopes.
func WithSecret ¶ added in v0.98.0
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
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
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 ¶ 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.
type SecretFunc ¶ added in v0.98.0
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)