middleware

package
v2.1.2 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2026 License: MIT Imports: 7 Imported by: 2

Documentation

Overview

Package middleware provides login middlewares: - Auth: adds auth from session and populates user info - Trace: populates user info if token presented - AdminOnly: restrict access to admin users only

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Authenticator

type Authenticator struct {
	logger.L
	JWTService       TokenService
	Providers        []provider.Service
	Validator        token.Validator
	AdminPasswd      string
	BasicAuthChecker BasicAuthFunc
	RefreshCache     RefreshCache
	ErrorHandler     ErrorHandlerFunc // custom error handler for auth failures
}

Authenticator is top level auth object providing middlewares

func (*Authenticator) AdminOnly

func (a *Authenticator) AdminOnly(next http.Handler) http.Handler

AdminOnly middleware allows access for admins only this handler internally wrapped with auth(true) to avoid situation if AdminOnly defined without prior Auth

func (*Authenticator) Auth

func (a *Authenticator) Auth(next http.Handler) http.Handler

Auth middleware adds auth from session and populates user info

func (*Authenticator) RBAC

func (a *Authenticator) RBAC(roles ...string) func(http.Handler) http.Handler

RBAC middleware allows role based control for routes this handler internally wrapped with auth(true) to avoid situation if RBAC defined without prior Auth

func (*Authenticator) Trace

func (a *Authenticator) Trace(next http.Handler) http.Handler

Trace middleware doesn't require valid user but if user info presented populates info

func (*Authenticator) UpdateUser

func (a *Authenticator) UpdateUser(upd UserUpdater) func(http.Handler) http.Handler

UpdateUser update user info with UserUpdater if it exists in request's context. Otherwise do nothing. should be placed after either Auth, Trace. AdminOnly or RBAC middleware.

type BasicAuthFunc

type BasicAuthFunc func(user, passwd string) (ok bool, userInfo token.User, err error)

BasicAuthFunc type is an adapter to allow the use of ordinary functions as BasicAuth. The second return parameter `User` need for add user claims into context of request.

type ErrorHandlerFunc added in v2.1.2

type ErrorHandlerFunc func(w http.ResponseWriter, r *http.Request, statusCode int, err error)

ErrorHandlerFunc type is an adapter to allow custom error handling for auth failures. It receives the suggested HTTP status code and the error that caused the auth failure. The handler can respond with custom status codes, HTML pages, redirects, or JSON responses. Status codes are typically http.StatusUnauthorized (401) for auth failures or http.StatusForbidden (403) for permission denied.

type RefreshCache

type RefreshCache interface {
	Get(key string) (value token.Claims, ok bool)
	Set(key string, value token.Claims)
}

RefreshCache defines interface storing and retrieving refreshed tokens

type TokenService

type TokenService interface {
	Parse(tokenString string) (claims token.Claims, err error)
	Set(w http.ResponseWriter, claims token.Claims) (token.Claims, error)
	Get(r *http.Request) (claims token.Claims, token string, err error)
	IsExpired(claims token.Claims) bool
	Reset(w http.ResponseWriter)
}

TokenService defines interface accessing tokens

type UserUpdFunc

type UserUpdFunc func(user token.User) token.User

UserUpdFunc type is an adapter to allow the use of ordinary functions as UserUpdater. If f is a function with the appropriate signature, UserUpdFunc(f) is a Handler that calls f.

func (UserUpdFunc) Update

func (f UserUpdFunc) Update(user token.User) token.User

Update calls f(user)

type UserUpdater

type UserUpdater interface {
	Update(claims token.User) token.User
}

UserUpdater defines interface adding extras or modifying UserInfo in request context

Jump to

Keyboard shortcuts

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