middleware

package
v0.0.20 Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2023 License: GPL-3.0 Imports: 19 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ContextLoggerDefaultConfig = ContextLoggerConfig{
	Skipper: em.DefaultSkipper,
	Prefix:  "context",
}
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 ContextLogger added in v0.0.20

func ContextLogger(logger echo.Logger, level uint8) echo.MiddlewareFunc

func ContextLoggerWithConfig added in v0.0.20

func ContextLoggerWithConfig(config ContextLoggerConfig) 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 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 specified roles.

func RequestID

func RequestID(c echo.Context) string

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 log in the user if the JWT is found.

func (*AuthMiddleware) LoggedUser added in v0.0.10

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

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

func (*AuthMiddleware) ParseJWT added in v0.0.20

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

ParseJWT tries to find the JWT in auth header and parse it, letting the parsed jwt token in both in context and in the X-Logged-User-ID headers. Its a shortcut of AllowAnonymous method, with a better name to apply as a top level middleware. Useful to have user information in middlewares before the "per path" middlewares.

func (*AuthMiddleware) WithAny added in v0.0.12

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

WhitAny searches 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 searches 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 ContextLoggerConfig added in v0.0.20

type ContextLoggerConfig 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

	// Logger defines the interface of the logger to inject to the context
	Logger echo.Logger

	// Level defines the log level. It uses the github.com/labstack/gommon/log levels.
	// Let it empty to preserve the one sets in provided Logger.
	Level uint8

	// Output represent the output stream to write the log.
	// Let it nil to preserve the one sets in provided Logger.
	Output io.Writer

	// Header defines the template to use to print logs. See github.com/labstack/gommon/log.
	// Set it can cause lost the id attribute that logs the request ID.
	// Let it empty to preserve the one sets in provided Logger.
	Header string

	// Header defines the prefix to print in logs, if defined in Header. See github.com/labstack/gommon/log.
	// Defaults set to "context".
	// Let it empty to preserve the one sets in provided Logger.
	Prefix string
}

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