middleware

package
v0.0.18 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2022 License: GPL-3.0 Imports: 18 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultSQLXConfig = SQLXConfig{
		Skipper:        em.DefaultSkipper,
		DB:             nil,
		Driver:         "",
		DataSourceName: "",
		AutoMigrate:    false,
		MigrationPath:  "",
	}
)
View Source
var ErrDBXMissing = echo.NewHTTPError(http.StatusInternalServerError, "unable to obtain the dbx in context. Please, initiate the sqlx middleware first")
View Source
var ErrNoIDTokenFound = fmt.Errorf("IDToken not found in context key %s", userContextField)

Functions

func BodyDumpOnHeader

func BodyDumpOnHeader() echo.MiddlewareFunc

func DefaultLogger

func DefaultLogger(level zerolog.Level) echo.MiddlewareFunc

Logger will inject in context a default zerolog logger with all available contextual info. Default logger is a pretty logger with info level and timestamp functionality.

func DefaultRequestZeroLoggerConfig

func DefaultRequestZeroLoggerConfig() echo.MiddlewareFunc

func GetDBX added in v0.0.14

func GetDBX(c echo.Context) (*sqlx.DB, error)

func GetIDToken added in v0.0.10

func GetIDToken(c echo.Context) (*auth.Token, error)

GetIDToken extract the token from the context field in userContextField and return it. If no token is found, it returns a ErrNoIDTokenFound.

func GetLogger

func GetLogger(c echo.Context) zerolog.Logger

GetLogger returns the contextual logger from context

func LoggedUserIs added in v0.0.12

func LoggedUserIs(c echo.Context, rol string) bool

LoggedUserIs returns true if a idToken exists in the context and it have the desired rol.

func LoggedUserIsAny added in v0.0.12

func LoggedUserIsAny(c echo.Context, roles []string) bool

LoggedUserIsAny returns true if a idToken exists in the context and it have, at least, one of the expecified roles.

func RequestID

func RequestID(c echo.Context) string

func SetLogger

func SetLogger(c echo.Context, logger zerolog.Logger)

SetLogger overrides the contextual logger in the echo context.

Types

type AuthClient added in v0.0.12

type AuthClient interface {
	VerifyIDToken(ctx context.Context, idToken string) (*auth.Token, error)
}

type AuthMiddleware added in v0.0.10

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

func DefaultAuthMiddleware added in v0.0.12

func DefaultAuthMiddleware() *AuthMiddleware

DefaultAuthMiddleware returns the auth middleware with a firebase auth client initialized from env vars.

func NewAuthMiddleware added in v0.0.10

func NewAuthMiddleware(ctx context.Context, authClient AuthClient) *AuthMiddleware

NewAuthMiddleware return a new auth middleware with the desired authClient attached.

func (*AuthMiddleware) AllowAnonymous added in v0.0.10

func (a *AuthMiddleware) AllowAnonymous() echo.MiddlewareFunc

AllowAnonymous will let pass all petitions trying to find a JWT in headers and loging in the user if the JWT is found.

func (*AuthMiddleware) LoggedUser added in v0.0.10

func (a *AuthMiddleware) LoggedUser() echo.MiddlewareFunc

LoggedUsers searchs for a valid JWT and logs in the founded user. If no JWT is found, it returns a 401 unauthorized standar error, stopping the request.

func (*AuthMiddleware) WithAny added in v0.0.12

func (a *AuthMiddleware) WithAny(roles []string) echo.MiddlewareFunc

WhitAny searchs for a valid JWT with at least one of the desired roles as a boolean true key in the token Claims and logs in the founded user. If no JWT with one desired rol is found, it returns a 401 or 403 standard error, stopping the request.

func (*AuthMiddleware) WithRol added in v0.0.10

func (a *AuthMiddleware) WithRol(rol string) echo.MiddlewareFunc

WithRol searchs for a valid JWT with the desired rol as a boolean true key in the token Claims and logs in the founded user. If no JWT with the rol is found, it returns a 401 or 403 standard error, stopping the request.

type SQLX added in v0.0.14

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

func NewSQLX added in v0.0.16

func NewSQLX() *SQLX

func (*SQLX) Default added in v0.0.16

func (m *SQLX) Default() echo.MiddlewareFunc

func (*SQLX) WithConfig added in v0.0.16

func (m *SQLX) WithConfig(config SQLXConfig) echo.MiddlewareFunc

type SQLXConfig added in v0.0.14

type SQLXConfig struct {
	// Skipper defines a function to skip middleware.
	Skipper em.Skipper

	// BeforeFunc defines a function which is executed just before the middleware.
	BeforeFunc em.BeforeFunc

	// DB is the inner db to be used. If is not provided, a new db will be open based either
	// in the Driver and DataSourceName attribute or in the default env vars.
	DB *sql.DB

	// Driver defines the driver to be used when a new database is tried to be opened.
	// If DB is provided, the middleware will never tried to stablish this connection.
	Driver string

	// DataSorceName is used in conjuntion with the Driver attribute when a new database is tried to be opened.
	// If DB is provided, the middleware will never tried to stablish this connection.
	DataSourceName string

	// AutoMigrate defines if migrations will apply on middleware initialization. If true,
	// you must provide a MigrationPath folder with the migrations files.
	// pressly/goose (https://github.com/pressly/goose) is used to execute the migrations.
	// The default SQLX middleware set this attribute to true
	AutoMigrate bool

	// MigrationPath defines the path to migrations files. Is used on middleware initialization
	// If AutoMigrate is set to true. Also used in calls to SQLXApplyMigrations.
	// pressly/goose (https://github.com/pressly/goose) is used to execute the migrations.
	// The default SQLX middleware set this attribute to ./migration
	MigrationPath string
}

Jump to

Keyboard shortcuts

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