middleware

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: May 24, 2024 License: MIT Imports: 29 Imported by: 0

Documentation

Index

Constants

View Source
const AuthNExcludedCtxKey = "authn_excluded"
View Source
const ErrCodeHealthCheckFailed = "HEALTH_CHECK_FAILED"
View Source
const ErrCodeReadyCheckFailed = "READY_CHECK_FAILED"
View Source
const PrincipalCtxKey = "authn_principal"

Variables

View Source
var ErrInvalidToken = errors.Newf("AUTHN_TOKEN_NOT_VALID", "token is not valid")
View Source
var RequestLoggerModule = ProvideAsMiddleware(NewRequestLogger)

Functions

func AsErrorMapper

func AsErrorMapper(errMapper ErrorMapper) any

func ProvideAsMiddleware

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

func RegisterDefaultErrorMapper

func RegisterDefaultErrorMapper(builder ErrorResponseBuilder) fx.Option

func RegisterErrorAsMapper

func RegisterErrorAsMapper(errAs error, builder ErrorResponseBuilder) fx.Option

func RegisterErrorCodeMapper

func RegisterErrorCodeMapper(errCode string, builder ErrorResponseBuilder) fx.Option

func RegisterErrorIsMapper

func RegisterErrorIsMapper(errIs error, builder ErrorResponseBuilder) fx.Option

func SetPrincipal

func SetPrincipal(ctx *gin.Context, principal Principal)

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) Priority

func (m *AuthNMiddleware) Priority() MiddlewarePriority

func (*AuthNMiddleware) Require

func (m *AuthNMiddleware) Require(ctx *gin.Context)

func (*AuthNMiddleware) Setup

func (m *AuthNMiddleware) Setup(httpHandler rest.HTTPHandler)

type AuthNProvider

type AuthNProvider interface {
	Authenticate(ctx *gin.Context) (Principal, error)
}

type AuthZMiddleware

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

func NewAuthZ

func NewAuthZ(
	conf config.Config,
	lf *log.LoggerFactory,
	provider AuthZProvider,
) *AuthZMiddleware

func (*AuthZMiddleware) Priority

func (m *AuthZMiddleware) Priority() MiddlewarePriority

func (*AuthZMiddleware) Require

func (m *AuthZMiddleware) Require(args ...any) gin.HandlerFunc

func (*AuthZMiddleware) RequireCustom

func (m *AuthZMiddleware) RequireCustom(provider AuthZProvider, args ...any) gin.HandlerFunc

func (*AuthZMiddleware) Setup

func (m *AuthZMiddleware) Setup(rest.HTTPHandler)

type AuthZProvider

type AuthZProvider interface {
	Authorize(ctx *gin.Context, args ...any) (bool, error)
}

type BaseMiddleware

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

func (*BaseMiddleware) GetLogger

func (m *BaseMiddleware) GetLogger() log.Logger

type DatabaseTrxMiddleware

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

func NewDatabaseTrx

func NewDatabaseTrx(
	conf config.Config,
	lf *log.LoggerFactory,
	db database.DB,
) *DatabaseTrxMiddleware

func (*DatabaseTrxMiddleware) Priority

func (*DatabaseTrxMiddleware) Run

func (m *DatabaseTrxMiddleware) Run(ctx *gin.Context)

func (*DatabaseTrxMiddleware) Setup

func (m *DatabaseTrxMiddleware) Setup(httpHandler rest.HTTPHandler)

type ErrorHandlerMiddleware

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

func NewErrorHandler

func NewErrorHandler(
	conf config.Config,
	lf *log.LoggerFactory,
	errMappings []ErrorMapper,
	defaultErrorMapper ErrorResponseBuilder,
) *ErrorHandlerMiddleware

func (*ErrorHandlerMiddleware) Priority

func (*ErrorHandlerMiddleware) Run

func (m *ErrorHandlerMiddleware) Run(ctx *gin.Context)

func (*ErrorHandlerMiddleware) Setup

func (m *ErrorHandlerMiddleware) Setup(httpHandler rest.HTTPHandler)

type ErrorHandlerMiddlewareParams

type ErrorHandlerMiddlewareParams struct {
	di.BaseParams
	ErrorMappers       []ErrorMapper        `group:"error_mappers"`
	DefaultErrorMapper ErrorResponseBuilder `name:"default_error_mapper" optional:"true"`
}

type ErrorMapper

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

func NewErrorAsMapper

func NewErrorAsMapper[E error](mapErr E, builder ErrorResponseBuilder) ErrorMapper

func NewErrorCodeMapper

func NewErrorCodeMapper(errCode string, builder ErrorResponseBuilder) ErrorMapper

func NewErrorIsMapper

func NewErrorIsMapper(mapErr error, builder ErrorResponseBuilder) ErrorMapper

type ErrorResponseBuilder

type ErrorResponseBuilder = func(*gin.Error) (body any, status int, buildErr error)

type HealthCheckMiddleware

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

func NewHealthCheck

func NewHealthCheck(conf config.Config, lf *log.LoggerFactory, healthChecks []HealthCheckProvider) *HealthCheckMiddleware

func (*HealthCheckMiddleware) HealthCheck

func (m *HealthCheckMiddleware) HealthCheck(ctx *gin.Context)

func (*HealthCheckMiddleware) Priority

func (*HealthCheckMiddleware) Setup

func (m *HealthCheckMiddleware) Setup(httpHandler rest.HTTPHandler)

type HealthCheckMiddlewareParams

type HealthCheckMiddlewareParams struct {
	di.BaseParams

	HealthChecks []HealthCheckProvider `group:"health_checks"`
}

type HealthCheckProvider

type HealthCheckProvider interface {
	GetName() string
	HealthCheck() error
}

type Middleware

type Middleware interface {
	Setup(httpHandler rest.HTTPHandler)
	Priority() MiddlewarePriority
	GetLogger() log.Logger
}

type MiddlewarePriority

type MiddlewarePriority int
const (
	MiddlewarePriorityHighest MiddlewarePriority = iota * 1_000

	MiddlewarePriorityProbes
	MiddlewarePriorityAuthN
	MiddlewarePriorityAuthZ
	MiddlewarePriorityHeader
	MiddlewarePriorityBody

	MiddlewarePriorityDefault
	MiddlewarePriorityLowest
)

type Middlewares

type Middlewares []Middleware

func NewMiddlewares

func NewMiddlewares(deps struct {
	fx.In

	LF          *log.LoggerFactory
	Middlewares []Middleware `group:"middlewares"`
	HTTPHandler rest.HTTPHandler
}) Middlewares

func (Middlewares) Setup

func (ms Middlewares) Setup(httpHandler rest.HTTPHandler)

type PanicRecoveryMiddleware

type PanicRecoveryMiddleware struct {
	BaseMiddleware
}

func NewPanicRecovery

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

func (*PanicRecoveryMiddleware) Priority

func (*PanicRecoveryMiddleware) Run

func (m *PanicRecoveryMiddleware) Run(ctx *gin.Context)

func (*PanicRecoveryMiddleware) Setup

func (m *PanicRecoveryMiddleware) Setup(httpHandler rest.HTTPHandler)

type Principal

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

func GetPrincipal

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

func MustGetPrincipal

func MustGetPrincipal(ctx *gin.Context) Principal

type PrincipalType

type PrincipalType string

type ReadyCheckMiddleware

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

func NewReadyCheck

func NewReadyCheck(
	conf config.Config,
	lf *log.LoggerFactory,
	readyChecks []ReadyCheckProvider,
) *ReadyCheckMiddleware

func (*ReadyCheckMiddleware) Priority

func (*ReadyCheckMiddleware) ReadyCheck

func (m *ReadyCheckMiddleware) ReadyCheck(ctx *gin.Context)

func (*ReadyCheckMiddleware) Setup

func (m *ReadyCheckMiddleware) Setup(httpHandler rest.HTTPHandler)

type ReadyCheckMiddlewareParams

type ReadyCheckMiddlewareParams struct {
	di.BaseParams
	LF          *log.LoggerFactory
	ReadyChecks []ReadyCheckProvider `group:"ready_checks"`
}

type ReadyCheckProvider

type ReadyCheckProvider interface {
	GetName() string
	ReadyCheck() error
}

type RequestLoggerMiddleware

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

func NewRequestLogger

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

func (*RequestLoggerMiddleware) Priority

func (*RequestLoggerMiddleware) Run

func (m *RequestLoggerMiddleware) Run(ctx *gin.Context)

func (*RequestLoggerMiddleware) Setup

func (m *RequestLoggerMiddleware) Setup(httpHandler rest.HTTPHandler)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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