pkce

package
v0.233.0 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package pkce implements the in-memory store of per-code authorization state that links a /authorize redirect to its later /oauth/token exchange.

Every code issued by /authorize gets an Entry carrying the nonce and the authorization time, echoed into the id_token. When the client also sent a PKCE code_challenge + code_challenge_method (required by Auth0 and OAuth 2.1 for SPAs and native clients), the Entry carries the challenge too, and the matching code_verifier must be presented on /oauth/token: the verifier must hash back to the stored challenge.

Index

Constants

View Source
const DefaultTTL = 10 * time.Minute

DefaultTTL is how long a stored challenge is valid before the matching code becomes unredeemable.

Variables

This section is empty.

Functions

This section is empty.

Types

type Entry

type Entry struct {
	Challenge string
	Method    Method
	ClientID  string
	Redirect  string
	// Nonce is the /authorize nonce, echoed into the id_token. May be empty.
	Nonce string
	// LoginHint is the subject /authorize resolved for the login: the session
	// user, the login_hint, or DefaultSubject. It becomes the id_token subject.
	LoginHint string
	// Audience and Scope are the /authorize audience and scope. When set,
	// the code exchange uses them, as Auth0 does. May be empty.
	Audience string
	Scope    string
	// AuthTime is the login instant, set by /authorize (a reused session keeps
	// its original one). Store.Put stamps the store clock only when it is
	// zero. It becomes the id_token auth_time claim.
	AuthTime time.Time
	// contains filtered or unexported fields
}

Entry is the per-code authorization state stashed at /authorize and consulted at /oauth/token. Challenge and Method are set only when the client used PKCE; Nonce, LoginHint and AuthTime feed the id_token.

func (*Entry) Verify

func (e *Entry) Verify(verifier string) error

Verify reports whether the supplied verifier matches the entry's challenge per the entry's Method. Returns a descriptive error on mismatch.

type Method

type Method string

Method is the code_challenge_method value the client used at /authorize.

const (
	// MethodS256 hashes the verifier with SHA-256.
	MethodS256 Method = "S256"
	// MethodPlain accepts the verifier as-is. Discouraged; supported for parity.
	MethodPlain Method = "plain"
)

type Option added in v0.227.0

type Option func(*Store)

Option configures a Store at construction.

func WithNow added in v0.227.0

func WithNow(now func() time.Time) Option

WithNow overrides the time source used for entry expiry. Wire to a clock.Controlled.Now to give tests runtime control over PKCE-code expiry windows. Nil is silently ignored.

type Store

type Store struct {
	// contains filtered or unexported fields
}

Store maps the random code returned by /authorize to its authorization Entry. Entries expire after DefaultTTL. Safe for concurrent use.

func NewStore

func NewStore(opts ...Option) *Store

NewStore returns an empty Store with the default TTL.

func (*Store) Consume

func (s *Store) Consume(code string) (Entry, bool)

Consume returns the Entry registered against code and removes it. Returns false if the code is unknown or its entry has expired.

func (*Store) Put

func (s *Store) Put(code string, e Entry)

Put records a challenge for the given code. The entry is single-use and expires after DefaultTTL.

func (*Store) Reset

func (s *Store) Reset()

Reset drops every stored challenge.

Jump to

Keyboard shortcuts

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