middleware

package
v0.9.0 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var AuthNExcludedCtxKey = context.CtxKey("authn_excluded")
View Source
var ErrInvalidToken = errors.Newf("AUTHN_TOKEN_NOT_VALID", "token is not valid")
View Source
var PrincipalCtxKey = context.CtxKey("_fw_authn_principal")

Functions

func ProvideAsMiddleware

func ProvideAsMiddleware(provider any, anns ...fx.Annotation) fx.Option

func SetPrincipal

func SetPrincipal(ctx context.Context, principal Principal) context.Context

Types

type AuthNMiddleware

type AuthNMiddleware struct {
	BaseMiddleware
	// contains filtered or unexported fields
}

AuthNMiddleware The exclusions are implemented as a map from path prefix to list of methods. If the list of methods is empty, then all methods are excluded for the path prefix acting as map key

func NewAuthN

func NewAuthN(
	conf config.Config,
	lf log.LoggerFactory,
	provider AuthNProvider,
) *AuthNMiddleware

func (*AuthNMiddleware) ExcludePrefix

func (m *AuthNMiddleware) ExcludePrefix(pathPrefix string)

ExcludePrefix excludes the given path prefix from authentication for all methods. Internally, this method attempts to add the empty list to the exclusion map. If the path prefix was already excluded for specific methods, then the function will make the list nil to exclude all methods.

func (*AuthNMiddleware) ExcludePrefixAndMethods

func (m *AuthNMiddleware) ExcludePrefixAndMethods(pathPrefix string, methods ...string)

ExcludePrefixAndMethods excludes the given path prefix from authentication for the given methods. There must be at least one given method. Internally, this method attempts to append the given methods to the methods list already present in the exclusion map for the given path prefix. If the already present method list is of length zero, it means the path prefix was already excluded for all methods, and the function doesn't do anything further.

func (*AuthNMiddleware) Handle added in v0.7.0

func (m *AuthNMiddleware) Handle(next http.Handler) http.Handler

func (*AuthNMiddleware) Priority

func (m *AuthNMiddleware) Priority() MiddlewarePriority

type AuthNProvider

type AuthNProvider interface {
	Authenticate(ctx context.Context, w http.ResponseWriter, r *http.Request) (Principal, error)
}

type BaseMiddleware

type BaseMiddleware struct {
	Conf   config.Config
	Logger log.Logger
}

type DatabaseBunTrxMiddleware added in v0.7.0

type DatabaseBunTrxMiddleware struct {
	BaseMiddleware
	// contains filtered or unexported fields
}

func NewDatabaseBunTrxMiddleware added in v0.7.0

func NewDatabaseBunTrxMiddleware(
	conf config.Config,
	lf log.LoggerFactory,
	db *bun.DB,
) *DatabaseBunTrxMiddleware

func (*DatabaseBunTrxMiddleware) Handle added in v0.7.0

func (*DatabaseBunTrxMiddleware) Priority added in v0.7.0

type DatabaseGORMTrxMiddleware added in v0.7.0

type DatabaseGORMTrxMiddleware struct {
	BaseMiddleware
	// contains filtered or unexported fields
}

func NewDatabaseGORMTrx added in v0.7.0

func NewDatabaseGORMTrx(
	conf config.Config,
	lf log.LoggerFactory,
	db database.DB,
) *DatabaseGORMTrxMiddleware

func (*DatabaseGORMTrxMiddleware) Handle added in v0.7.0

func (*DatabaseGORMTrxMiddleware) Priority added in v0.7.0

type KeyValueContextMiddleware added in v0.7.0

type KeyValueContextMiddleware struct {
	BaseMiddleware
}

func NewKeyValueContextMiddleware added in v0.7.0

func NewKeyValueContextMiddleware(
	conf config.Config,
	lf log.LoggerFactory,
) *KeyValueContextMiddleware

func (*KeyValueContextMiddleware) Handle added in v0.7.0

func (*KeyValueContextMiddleware) Priority added in v0.7.0

type Middleware

type Middleware interface {
	Priority() MiddlewarePriority
	Handle(http.Handler) http.Handler
}

type MiddlewarePriority

type MiddlewarePriority int

generate the stringer

const (
	MiddlewarePriorityNotSet  MiddlewarePriority = iota
	MiddlewarePriorityHighest MiddlewarePriority = iota * 1_000
	MiddlewarePriorityBeforeMux
	MiddlewarePriorityBeforeMuxInclusive MiddlewarePriority = MiddlewarePriorityBeforeMux + 1
	MiddlewarePriorityAfterMux           MiddlewarePriority = iota * 1_000
	MiddlewarePriorityAuthN
	MiddlewarePriorityAuthZ
	MiddlewarePriorityDefault

	// Max int value
	MiddlewarePriorityLowest MiddlewarePriority = MiddlewarePriority(int(^uint(0) >> 1))
)

func (MiddlewarePriority) String added in v0.7.0

func (i MiddlewarePriority) String() string

type Middlewares

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

func NewMiddlewares

func NewMiddlewares(deps struct {
	fx.In

	LF          log.LoggerFactory
	Middlewares []Middleware `group:"rest_middlewares"`
}) Middlewares

func (Middlewares) Append added in v0.7.0

func (m Middlewares) Append(mw Middleware) Middlewares

func (Middlewares) Apply added in v0.7.0

Apply applies the middlewares in the given priority range [from, to) to the given handler, returning the wrapped handler

type PanicRecoveryMiddleware

type PanicRecoveryMiddleware struct {
	BaseMiddleware
}

func NewPanicRecovery

func NewPanicRecovery(
	conf config.Config,
	lf log.LoggerFactory,
) *PanicRecoveryMiddleware

func (*PanicRecoveryMiddleware) Handle added in v0.7.0

func (*PanicRecoveryMiddleware) Priority

type Principal

type Principal interface {
	GetID() any
	GetName() string
	GetEmail() string
	GetType() PrincipalType
}

func GetPrincipal

func GetPrincipal(ctx context.Context) (principal Principal, present bool)

func MustGetPrincipal

func MustGetPrincipal(ctx context.Context) Principal

type PrincipalType

type PrincipalType string

type RequestLoggerMiddleware

type RequestLoggerMiddleware struct {
	BaseMiddleware
	// contains filtered or unexported fields
}

func NewRequestLogger

func NewRequestLogger(conf config.Config, lf log.LoggerFactory) *RequestLoggerMiddleware

func (*RequestLoggerMiddleware) Handle added in v0.7.0

func (*RequestLoggerMiddleware) Priority

Jump to

Keyboard shortcuts

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