Documentation
¶
Overview ¶
Package oidc implements the OpenID Connect authorization code flow with PKCE (S256) and nonce as an authentication.FederatedProvider.
Begin generates independent 256-bit state, nonce and PKCE verifier values and stores them through a caller-owned StateStore, keyed by the SHA-256 digest of the state so the raw state never reaches storage. Complete consumes that state atomically before exchanging the code, so a callback is accepted at most once, then verifies the ID token (signature through the configured go-oidc verifier, issuer, audience, authorized party and nonce) and builds a principal "<issuer>|<sub>". Only the ID token claims named in Options.TraitClaims are copied into Principal.Traits.
The package also defines the state types shared with the oauth2 package: Secret, Pending, StateStore and MemoryStateStore.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func StateDigest ¶
StateDigest returns the SHA-256 digest under which the raw callback state is stored.
Types ¶
type MemoryStateStore ¶
type MemoryStateStore struct {
// contains filtered or unexported fields
}
MemoryStateStore is a process-local StateStore for tests and single-process callers. Distributed deployments should provide their own durable store.
func NewMemoryStateStore ¶
func NewMemoryStateStore() *MemoryStateStore
NewMemoryStateStore returns an empty memory-backed state store.
type Options ¶
type Options struct {
// Name identifies the provider (lower-cased and trimmed). Required.
Name string
// Issuer is the https issuer URL. Defaults to the origin of
// Config.Endpoint.AuthURL.
Issuer string
// ClientID is the OAuth client ID; it may instead be set in Config and
// must match it when both are set.
ClientID string
// TraitClaims names the verified ID token claims (string or string
// array) copied into Principal.Traits. At most 128 unique names.
TraitClaims []string
// Config holds the client credentials and endpoints. Missing endpoints
// default to "<issuer>/authorize" and "<issuer>/token". Endpoints must
// use https, or http on a loopback host.
Config oauth2.Config
// Verifier verifies ID token signatures and standard claims. Complete
// fails closed when it is nil.
Verifier *coreosoidc.IDTokenVerifier
// RedirectURIs lists the exact redirect URIs Begin accepts (https, or
// http on a loopback host). Required, without duplicates.
RedirectURIs []string
// State persists pending callback state. Required.
State StateStore
// Clock returns the current time used for state expiry and principal
// validation; its result is converted to UTC. Defaults to time.Now. ID
// token expiry is checked by Verifier with its own clock
// (coreosoidc.Config.Now).
Clock func() time.Time
// Random supplies state, nonce and PKCE entropy. Defaults to
// crypto/rand.Reader; any replacement must be cryptographically secure.
Random io.Reader
// StateTTL bounds the time between Begin and Complete. Defaults to ten
// minutes.
StateTTL time.Duration
}
Options configures a Provider.
type Pending ¶
type Pending struct {
Digest [32]byte
Provider string
RedirectURI string
Nonce Secret
PKCEVerifier Secret
ExpiresAt time.Time
}
Pending is caller-owned callback state. It intentionally stores only a SHA-256 digest of the raw callback state.
type Provider ¶
type Provider struct {
// contains filtered or unexported fields
}
Provider is an OpenID Connect authorization code flow provider. It is safe for concurrent use when its StateStore is.
func New ¶
New validates options and returns a Provider. It returns authentication.ErrInvalidCredential or authentication.ErrInvalidCallback for invalid options.
func (*Provider) Begin ¶
func (p *Provider) Begin(ctx context.Context, request authentication.BeginRequest) (authentication.Redirect, error)
Begin starts a login for a registered redirect URI and returns the authorization URL. "openid" is always requested.
func (*Provider) Complete ¶
func (p *Provider) Complete(ctx context.Context, callback authentication.Callback) (authentication.Principal, error)
Complete consumes the callback state, exchanges the code and verifies the ID token. It returns authentication.ErrReplay for unknown or reused state, authentication.ErrExpired after Options.StateTTL, and authentication.ErrUnauthenticated for every other verification failure.
type Secret ¶
type Secret struct {
// contains filtered or unexported fields
}
Secret holds sensitive callback material and redacts it when formatted. Bytes and binary marshaling provide explicit access for provider use and caller-owned durable stores.
func (Secret) MarshalBinary ¶
MarshalBinary returns the secret value for caller-owned durable stores.
func (*Secret) UnmarshalBinary ¶
UnmarshalBinary restores a secret produced by MarshalBinary.
type StateStore ¶
type StateStore interface {
Put(context.Context, Pending) error
Consume(context.Context, [32]byte) (Pending, error)
}
StateStore persists callback state. Put must fail with authentication.ErrReplay when the digest exists. Consume must atomically remove and return the state for a digest and return authentication.ErrReplay when none exists, so that concurrent callbacks cannot both consume it.