Help us shape the future of Go! Take the 2026 Go Developer Survey

strategy

package
v1.9.0 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package strategy defines the authentication strategy system. Strategies implement different authentication mechanisms (password, social OAuth, magic link, passkey, etc.) and are registered with priorities for ordered evaluation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ErrStrategyNotApplicable deprecated

type ErrStrategyNotApplicable = NotApplicableError //nolint:errname // backward-compatible alias

ErrStrategyNotApplicable is an alias kept for backward compatibility.

Deprecated: Use NotApplicableError instead.

type NotApplicableError

type NotApplicableError struct{}

NotApplicableError is returned when a strategy does not apply to the request.

func (NotApplicableError) Error

func (e NotApplicableError) Error() string

type Registry

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

Registry holds registered auth strategies ordered by priority. Strategies are evaluated in registration order during authentication.

func NewRegistry

func NewRegistry(logger log.Logger) *Registry

NewRegistry creates a strategy registry with the given logger.

func (*Registry) Authenticate

func (r *Registry) Authenticate(ctx context.Context, req *http.Request) (*Result, error)

Authenticate evaluates all registered strategies in priority order. Returns the result from the first applicable strategy that succeeds.

func (*Registry) Get

func (r *Registry) Get(name string) (Strategy, bool)

Get returns a strategy by name.

func (*Registry) Register

func (r *Registry) Register(s Strategy, priority int)

Register adds a strategy with a given priority. Lower priority values are evaluated first.

func (*Registry) Strategies

func (r *Registry) Strategies() []string

Strategies returns the names of all registered strategies in priority order.

type Result

type Result struct {
	User    *user.User       `json:"user"`
	Session *session.Session `json:"session"`
	New     bool             `json:"new"` // True if user was created (signup)
}

Result represents the outcome of a successful authentication.

type Strategy

type Strategy interface {
	// Name returns the unique identifier for this strategy (e.g., "password", "google", "magic-link").
	Name() string

	// Authenticate attempts to authenticate a request using this strategy.
	// Returns the authenticated user and a new session, or an error if authentication fails.
	// If this strategy does not apply to the request, return NotApplicableError.
	Authenticate(ctx context.Context, r *http.Request) (*Result, error)
}

Strategy is the interface that authentication strategies must implement.

Jump to

Keyboard shortcuts

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