oidc

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2026 License: Apache-2.0 Imports: 18 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

func StateDigest(state string) [32]byte

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.

func (*MemoryStateStore) Consume

func (s *MemoryStateStore) Consume(ctx context.Context, digest [32]byte) (Pending, error)

Consume atomically removes and returns the state for digest.

func (*MemoryStateStore) Put

func (s *MemoryStateStore) Put(ctx context.Context, pending Pending) error

Put stores pending unless its digest is already present.

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

func New(options Options) (*Provider, error)

New validates options and returns a Provider. It returns authentication.ErrInvalidCredential or authentication.ErrInvalidCallback for invalid options.

func (*Provider) Begin

Begin starts a login for a registered redirect URI and returns the authorization URL. "openid" is always requested.

func (*Provider) Complete

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.

func (*Provider) Name

func (p *Provider) Name() string

Name returns the normalized provider name.

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 NewSecret

func NewSecret(value string) Secret

NewSecret wraps value.

func (Secret) Bytes

func (s Secret) Bytes() []byte

Bytes returns a copy of the secret value.

func (Secret) GoString

func (Secret) GoString() string

GoString returns a redaction marker, never the secret.

func (Secret) MarshalBinary

func (s Secret) MarshalBinary() ([]byte, error)

MarshalBinary returns the secret value for caller-owned durable stores.

func (Secret) String

func (Secret) String() string

String returns a redaction marker, never the secret.

func (*Secret) UnmarshalBinary

func (s *Secret) UnmarshalBinary(value []byte) error

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.

Jump to

Keyboard shortcuts

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