middleware

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TenantIDKey contextKey = iota
	UserIDKey
	RoleKey
)

Variables

This section is empty.

Functions

func Auth

func Auth(parseToken TokenParser) func(http.Handler) http.Handler

Auth is the middleware that validates the JWT and adds the user_id and tenant_id to the context.

func Idempotency

func Idempotency(store domain.IdempotencyStore) func(http.Handler) http.Handler

Idempotency middleware ensures that POST requests are processed at most once by caching the response associated with a specific Idempotency-Key.

func RequestLogger

func RequestLogger() func(http.Handler) http.Handler

RequestLogger returns a middleware that logs each request as a structured slog entry.

func RequireAuth

func RequireAuth(parseToken TokenParser) func(http.Handler) http.Handler

RequireAuth is an alias for Auth to satisfy existing calls.

func RequireRole

func RequireRole(roles ...domain.Role) func(http.Handler) http.Handler

RequireRole is a middleware that ensures the user has one of the required roles.

func RoleFromCtx

func RoleFromCtx(ctx context.Context) (string, bool)

RoleFromCtx extracts the user role from the context.

func TenantIDFromCtx

func TenantIDFromCtx(ctx context.Context) (string, bool)

TenantIDFromCtx extracts the tenant ID from the context.

func UserIDFromCtx

func UserIDFromCtx(ctx context.Context) (string, bool)

UserIDFromCtx extracts the user ID from the context.

Types

type ErrorResponse

type ErrorResponse struct {
	Error struct {
		Code    string `json:"code"`
		Message string `json:"message"`
	} `json:"error"`
}

ErrorResponse represents the JSON body returned for authentication and authorization errors.

type RateLimiterStore

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

RateLimiterStore holds the state for the in-memory rate limiters.

func NewRateLimiterStore

func NewRateLimiterStore() *RateLimiterStore

NewRateLimiterStore creates a new store and starts the cleanup goroutine.

func NewRateLimiterStoreWithInterval

func NewRateLimiterStoreWithInterval(interval time.Duration) *RateLimiterStore

NewRateLimiterStoreWithInterval is for testing cleanup with custom intervals.

func (*RateLimiterStore) Close

func (s *RateLimiterStore) Close()

Close stops the cleanup goroutine.

func (*RateLimiterStore) OTPRateLimiter

func (s *RateLimiterStore) OTPRateLimiter() func(http.Handler) http.Handler

OTPRateLimiter returns a middleware that enforces per-email OTP rate limiting.

type TokenParser

type TokenParser func(token string) (*paseto.Claims, error)

TokenParser is a function type that parses a token string into Claims.

Jump to

Keyboard shortcuts

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