middleware

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2023 License: GPL-2.0 Imports: 10 Imported by: 12

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrDatabaseError         = errors.New("there was a problem updating the database")
	ErrCookieNotFound        = errors.New("cookie is missing")
	ErrSessionContextMissing = errors.New("session context is missing")
)

Functions

func InvalidateSessions

func InvalidateSessions(db *gorm.DB, authID uint, authKey string) error

InvalidateSessions Helper method to invalidate all sessions of a user

func Login

func Login(db *gorm.DB, model IdentifiedAuthModel, c echo.Context, isSessionCookie bool) error

Login This method is used to log a user in. auth.Authenticate has to be called before. A cookie is set if the user can be logged in. Parameter user: Can be retrieved by auth.Authenticate. Parameter c: Pointer to the current context. Must implement middleware.SessionContext Parameter config: Refer to SessionConfig. Parameter isSessionCookie: Sets a session cookie if true, else a persistent cookie will be set.

func LoginRequired

func LoginRequired(f func(echo.Context) error) echo.HandlerFunc

LoginRequired Helper function to mark endpoint as login only. Requires Session as a middleware. Returns ErrSessionMisconfigured if middleware.SessionContext is not present in Context.

func Logout

func Logout(db *gorm.DB, c echo.Context) error

Logout Helper method to logout and therefore invalidating a user's session. If the user isn't logged in, nil is returned

func Panic

func Panic() echo.MiddlewareFunc

func RegisterAuthProvider added in v0.3.0

func RegisterAuthProvider(getProviderInformation func() (string, func(uint) any))

RegisterAuthProvider is used to register a new auth provider besides the already existing ones. The authIdentifier must be unique and is used to retrieve the correct UserModel with getUserModel which should return its own user struct

func Security

func Security(config *SecurityConfig) echo.MiddlewareFunc

func Session

func Session(db *gorm.DB, config *SessionConfig) echo.MiddlewareFunc

Session Use as middleware. Requires CustomContext to be set with a corresponding struct that embeds SessionContext or has a field named SessionContext. If SessionContext is not found, the middleware is skipped.

Types

type AllowedHost

type AllowedHost struct {
	Host  string
	Https bool
}

type IdentifiedAuthModel added in v0.3.0

type IdentifiedAuthModel interface {
	GetAuthModelIdentifier() (string, uint)
	UpdateLastLogin(c echo.Context, db *gorm.DB, loginTime time.Time)
}

type SecurityConfig

type SecurityConfig struct {
	AllowedHosts            []AllowedHost
	UseForwardedProtoHeader bool
}

type SessionConfig

type SessionConfig struct {
	CookieName     string
	CookieAge      *time.Duration
	Secure         *bool
	CookiePath     string
	DisableLogging bool
}

SessionConfig Set the parameters for the Session. Parameter CookieName defaults to "session_id". Parameter CookieAge defaults to 30 * time.Minute. Parameter Secure defaults to true. If set, the cookie can only be sent through an HTTPS connection. Parameter CookiePath defaults to "". Can be used to restrict the path the cookie can be sent to. Parameter DisableLogging defaults to false. If set, no debug logs are sent. Error logs are still sent.

func (*SessionConfig) FixSessionConfig

func (config *SessionConfig) FixSessionConfig()

type SessionContext

type SessionContext interface {
	GetUser() any
	GetSessionID() *string
	IsAuthenticated() bool
	GetSessionConfig() *SessionConfig
	// contains filtered or unexported methods
}

func GetSessionContext

func GetSessionContext(c echo.Context) (SessionContext, error)

GetSessionContext returns a SessionContext from a Context

Jump to

Keyboard shortcuts

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