mwares

package
v0.0.0-...-20da4e3 Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2021 License: BSD-3-Clause Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrUserNotFound = errors.New("user not found")

ErrUserNotFound should be returned from Get when the record is not found.

Functions

func AuthCheckMiddleware

func AuthCheckMiddleware(um UserManagement, log *zerolog.Logger) func(http.Handler) http.Handler

AuthCheckMiddleware prevents someone from accessing a route that should be only allowed for users who are logged in. This middleware is designed to be used in client-side apps, because it does not handle redirecting.

It allows the user through if the UserManagement.IsAuthed returns true. Usually your implementation of this will check the users cookie.

UserManagement is an interface that implements context management to pass along the authed user state, whether that be a username or a simple bool to indicate that the requester is authed, or alternatively the implementation could just return nil, nil if there is no additional state to pass along. It also implements a function to check whether the supplied request holds details of an authed user. Usually this means your implementation would involve checking the request variable for an active and valid cookie.

log is a zerolog logger that is used if a logger cannot be found in the request context. If you're using request ID's, the request ID middlewares will handle this for you.

func BasicPanicRecoverHandler

func BasicPanicRecoverHandler(w http.ResponseWriter, r *http.Request)

BasicPanicRecoverHandler is a basic panic recovery handler that will add the X-Request-ID header if present in the context, and return a http.StatusInternalServerError.

This can be used as the errorHandler argument for ZerologRecover.

func ValidationConverter

func ValidationConverter(errs support.Errors) error

Types

type MW

type MW interface {
	Wrap(http.Handler) http.Handler
}

MW is an interface defining middleware wrapping

func Zerolog

func Zerolog(logger *zerolog.Logger) MW

Zerolog returns a logging middleware that outputs details about a request

func ZerologRecover

func ZerologRecover(fallback *zerolog.Logger, errorHandler http.HandlerFunc) MW

ZerologRecover will attempt to log a panic, as well as produce a reasonable error for the client by calling the passed in errorHandler function. If your app is using request-id logging, don't forget to write the request-id header in your errorHandler function as X-Request-ID. Or use the BasicPanicRecoverHandler if you want to have that done for you, and simply want to return a StatusInternalServerError on panics.

It uses the zerolog logger and attempts to look up a request-scoped logger created with this package before using the passed in logger.

The zerolog logger that's used here needs to have stacktrace logging enabled by setting zerolog.ErrorStackMarshaler, if using this middleware.

func ZerologRequestIDLogger

func ZerologRequestIDLogger(logger *zerolog.Logger) MW

ZerologRequestIDLogger returns a request id logger middleware. This only works if chi has inserted a request id into the stack first.

type UnexpectedErrorHandler

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

func NewUnexpectedErrorHandler

func NewUnexpectedErrorHandler(log *zerolog.Logger) UnexpectedErrorHandler

NewUnexpectedErrorHandler is a validation error handler and http 500 error handler, primarily for use with oa3 generated APIs.

oa3 API's take a validation error converter and a error handler on initialization. The returned object here can be used as the error handler argument, and the ValidationConverter function in this package can be used the validation converter argument.

func (UnexpectedErrorHandler) Wrap

type UserManagement

type UserManagement interface {
	IsAuthed(r *http.Request) (isAuthed bool)
	SetSessionOnContext(r **http.Request) (interface{}, error)
}

UserManagement is an interface that allows control over the AuthCheckMiddleware.

Jump to

Keyboard shortcuts

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