middleware

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2026 License: GPL-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	SessionContextKey contextKey = "session_data"
	AuthTypeKey       contextKey = "auth_type"
	UserIDKey         contextKey = "user_id"
)

Variables

View Source
var (
	ErrTokenMissing  = errors.New("CSRF token missing")
	ErrTokenMismatch = errors.New("CSRF token mismatch")
)
View Source
var CacheDurations = struct {
	// Default fallback duration
	Default time.Duration

	// System-level cache durations
	HealthCheck time.Duration
	Statistics  time.Duration

	// Service-specific durations for frequently updated data
	PlexSessions      time.Duration
	OverseerrRequests time.Duration

	// Service-specific durations for less frequently updated data
	AutobrrStatus     time.Duration
	AutobrrIRC        time.Duration
	AutobrrReleases   time.Duration
	MaintainerrStatus time.Duration
	SonarrStatus      time.Duration
	RadarrStatus      time.Duration
	LidarrStatus      time.Duration
	ReadarrStatus     time.Duration
	ProwlarrStatus    time.Duration
	TraefikStatus     time.Duration
	BazarrStatus      time.Duration
	SabnzbdStatus     time.Duration
	NzbgetStatus      time.Duration
	JellyfinStatus    time.Duration
	UptimeKumaStatus  time.Duration
}{
	Default:           30 * time.Second,
	HealthCheck:       10 * time.Minute,
	Statistics:        5 * time.Minute,
	PlexSessions:      5 * time.Second,
	OverseerrRequests: 30 * time.Second,
	AutobrrStatus:     1 * time.Minute,
	AutobrrIRC:        5 * time.Minute,
	AutobrrReleases:   1 * time.Minute,
	MaintainerrStatus: 10 * time.Minute,
	SonarrStatus:      1 * time.Minute,
	RadarrStatus:      1 * time.Minute,
	LidarrStatus:      1 * time.Minute,
	ReadarrStatus:     1 * time.Minute,
	ProwlarrStatus:    1 * time.Minute,
	TraefikStatus:     30 * time.Second,
	BazarrStatus:      1 * time.Minute,
	SabnzbdStatus:     1 * time.Minute,
	NzbgetStatus:      1 * time.Minute,
	JellyfinStatus:    15 * time.Second,
	UptimeKumaStatus:  30 * time.Second,
}

CacheDurations defines all cache TTLs in one place for consistency

Functions

func CSRF

func CSRF(config *CSRFConfig) gin.HandlerFunc

CSRF returns a middleware that provides CSRF protection

func IsAuthBypassEnabled added in v0.3.0

func IsAuthBypassEnabled() bool

IsAuthBypassEnabled returns true when API auth should be bypassed for local troubleshooting.

Env:

  • DASHBRR_AUTH_BYPASS=true

func Logger

func Logger() gin.HandlerFunc

Logger returns a gin middleware for logging HTTP requests with zerolog

func Secure

func Secure(config *SecureConfig) gin.HandlerFunc

Secure returns a middleware that adds security headers

func SetupCORS

func SetupCORS(allowedOrigins, allowedHeaders, allowedMethods []string, maxAge time.Duration, allowCredentials *bool) gin.HandlerFunc

SetupCORS returns the CORS middleware configuration

Types

type AuthMiddleware

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

func NewAuthMiddleware

func NewAuthMiddleware(cache cache.Store) *AuthMiddleware

func (*AuthMiddleware) OptionalAuth

func (m *AuthMiddleware) OptionalAuth() gin.HandlerFunc

OptionalAuth middleware checks for authentication but doesn't require it

func (*AuthMiddleware) RequireAuth

func (m *AuthMiddleware) RequireAuth() gin.HandlerFunc

RequireAuth middleware checks for valid authentication

type CSRFConfig

type CSRFConfig struct {
	// Secure indicates if the cookie should be sent only over HTTPS
	Secure bool
	// Cookie path
	Path string
	// Cookie domain
	Domain string
	// Cookie max age in seconds
	MaxAge int
	// If true, cookie is not accessible via JavaScript
	HttpOnly bool
	// Methods that don't require CSRF validation
	ExemptMethods []string
	// Paths that don't require CSRF validation
	ExemptPaths []string
}

CSRFConfig holds configuration for CSRF protection

func DefaultCSRFConfig

func DefaultCSRFConfig() *CSRFConfig

DefaultCSRFConfig returns the default CSRF configuration

type CacheMiddleware

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

func NewCacheMiddleware

func NewCacheMiddleware(store cache.Store) *CacheMiddleware

func (*CacheMiddleware) Cache

func (m *CacheMiddleware) Cache() gin.HandlerFunc

type CachedResponse

type CachedResponse struct {
	Status      int               `json:"status"`
	Body        []byte            `json:"body"`
	ContentType string            `json:"content_type"`
	Headers     map[string]string `json:"headers"`
}

type RateLimiter

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

func NewRateLimiter

func NewRateLimiter(store cache.Store, window time.Duration, limit int, keyPrefix string) *RateLimiter

NewRateLimiter creates a new rate limiter with the specified configuration

func (*RateLimiter) RateLimit

func (rl *RateLimiter) RateLimit() gin.HandlerFunc

RateLimit returns a Gin middleware function that implements rate limiting

type SecureConfig

type SecureConfig struct {
	CSPEnabled            bool
	CSPDefaultSrc         []string
	CSPScriptSrc          []string
	CSPStyleSrc           []string
	CSPImgSrc             []string
	CSPConnectSrc         []string
	CSPFontSrc            []string
	CSPObjectSrc          []string
	CSPMediaSrc           []string
	CSPFrameSrc           []string
	CSPWorkerSrc          []string
	CSPManifestSrc        []string
	HSTSEnabled           bool
	HSTSMaxAge            int
	HSTSIncludeSubdomains bool
	HSTSPreload           bool
	FrameGuardEnabled     bool
	FrameGuardAction      string // DENY, SAMEORIGIN
	ContentTypeNosniff    bool
	XSSProtection         bool
	XSSProtectionMode     string // "0", "1", "1; mode=block"
	ReferrerPolicy        string
}

SecureConfig holds configuration for secure headers

func DefaultSecureConfig

func DefaultSecureConfig() *SecureConfig

DefaultSecureConfig returns the default secure configuration

Jump to

Keyboard shortcuts

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