middleware

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2026 License: AGPL-3.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CookieSessionName  = "beebuzz_session"
	CookieLoggedInName = "beebuzz_logged_in"
)
View Source
const CtxKeyUser contextKey = "user"

Variables

This section is empty.

Functions

func APISecurity

func APISecurity(next http.Handler) http.Handler

APISecurity adds API-specific security headers to all /v1 responses. Cache-Control: no-store prevents sensitive data from being cached by browsers, proxies, or CDNs.

func BaseSecurity

func BaseSecurity(next http.Handler) http.Handler

BaseSecurity adds base security headers to all responses. CSP is enforced at Caddy layer, not here.

func BearerTokenFromContext

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

BearerTokenFromContext returns the raw Bearer token extracted by the ExtractBearerToken middleware. Returns ("", false) when the Authorization header was missing or malformed.

func CORS

func CORS(allowedOrigins []string) func(http.Handler) http.Handler

CORS adds Cross-Origin Resource Sharing headers. It checks the request Origin against allowedOrigins and, on match, reflects the origin with credentials support. OPTIONS preflight requests are answered immediately with 204.

func ClientIPFromContext

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

ClientIPFromContext returns the canonical client IP for the request, resolved by RealIP from RemoteAddr or trusted X-Forwarded-For.

func ExtractBearerToken

func ExtractBearerToken(next http.Handler) http.Handler

ExtractBearerToken reads the Authorization header, strips the "Bearer " prefix, and stores the raw token in the request context. If the header is absent or malformed the request proceeds without a context value, letting the handler return 401.

func HashedIPFromContext

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

HashedIPFromContext reads the hashed client IP stored by the IPHasher middleware. Returns the hashed IP and true if present.

func HostRewrite

func HostRewrite(pushHost, hookHost string) func(http.Handler) http.Handler

HostRewrite returns middleware that rewrites the request path based on the Host header. Requests to pushHost get /v1/push prepended; requests to hookHost get /v1/webhooks prepended. When pushHost or hookHost is empty, that rewrite is inactive. The host values must be bare host (with optional port), not full URLs.

func RateKeyByBearerToken

func RateKeyByBearerToken(r *http.Request) (string, error)

RateKeyByBearerToken is an httprate.KeyFunc that reads the raw Bearer token from context (set by ExtractBearerToken middleware) and uses its SHA-256 hash as key. Falls back to hashed IP when the token is absent so the request still reaches the handler for a proper 401.

func RateKeyByHashedIP

func RateKeyByHashedIP(r *http.Request) (string, error)

RateKeyByHashedIP is an httprate.KeyFunc that uses the hashed client IP from the request context as the rate-limit key. Requires RealIP and IPHasher middleware to run first.

func RateKeyByURLParam

func RateKeyByURLParam(param string) func(*http.Request) (string, error)

RateKeyByURLParam returns an httprate.KeyFunc that reads the named URL parameter and uses its SHA-256 hash as rate-limit key. Falls back to hashed IP when the parameter is empty.

func RequestIDFromContext

func RequestIDFromContext(ctx context.Context) string

RequestIDFromContext returns the request ID stored by the RequestID middleware. Returns empty string if not present.

func RequireAdmin

func RequireAdmin() func(http.Handler) http.Handler

RequireAdmin creates a middleware that requires admin privileges.

func RequireSession

func RequireSession(validator SessionValidator) func(http.Handler) http.Handler

RequireSession creates a middleware that requires a valid session.

func SentryTags

func SentryTags(next http.Handler) http.Handler

SentryTags sets Sentry scope tags for the request. Must be placed AFTER sentryhttp handler and RequestID middleware. Sets: request_id, route, user_id (if authenticated).

Types

type CtxUser

type CtxUser struct {
	ID      string
	IsAdmin bool
}

CtxUser represents the authenticated user in the request context.

func UserFromContext

func UserFromContext(ctx context.Context) (*CtxUser, bool)

UserFromContext retrieves the authenticated user from the request context.

type IPHasher

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

IPHasher hashes client IPs with a keyed BLAKE2b so they can be used as rate-limit keys and logged without storing plaintext addresses.

func NewIPHasher

func NewIPHasher(secret string) *IPHasher

NewIPHasher creates an IPHasher. The secret is used as a BLAKE2b key to prevent rainbow-table reversal of hashed IPs.

func (*IPHasher) Hash

func (h *IPHasher) Hash(ip string) string

Hash returns a 32 hex-character keyed BLAKE2b hash of the IP.

func (*IPHasher) Middleware

func (h *IPHasher) Middleware(next http.Handler) http.Handler

Middleware reads the plain client IP from context, hashes it, and stores the result. Requires the RealIP middleware to run first.

type RealIP

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

RealIP resolves the real client IP from RemoteAddr or X-Forwarded-For based on a trusted proxy subnet.

func NewRealIP

func NewRealIP(proxySubnet netip.Prefix) *RealIP

NewRealIP creates a RealIP resolver. proxySubnet is the CIDR of the trusted reverse proxy (e.g. "172.20.0.0/16"). Pass netip.Prefix{} (zero value) for direct connections with no proxy.

Use the narrowest possible CIDR (ideally /32 for a single proxy IP). A wide subnet trusts any peer within it, which in shared networks could allow other hosts to spoof X-Forwarded-For.

func (*RealIP) Middleware

func (rip *RealIP) Middleware(next http.Handler) http.Handler

Middleware stores the resolved client IP in the request context.

func (*RealIP) Resolve

func (rip *RealIP) Resolve(r *http.Request) string

Resolve extracts the real client IP from the request. If proxySubnet is configured and RemoteAddr falls within it, the rightmost X-Forwarded-For entry is used. Otherwise RemoteAddr is returned directly.

type RequestID

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

RequestID reads or generates a request ID, sets it in the response header and request context. The header name is configurable to match the reverse proxy convention (e.g. Traefik uses "X-Request-Id" by default). Pass an empty string to use the default "X-Request-ID".

func NewRequestID

func NewRequestID(header string) *RequestID

NewRequestID creates a RequestID middleware. header is the HTTP header name used to read and write the request ID (e.g. "X-Request-Id"). Pass "" to use the default "X-Request-ID".

func (*RequestID) Middleware

func (rid *RequestID) Middleware(next http.Handler) http.Handler

Middleware returns an http middleware that propagates or generates request IDs.

type SessionUser

type SessionUser struct {
	ID      string
	IsAdmin bool
}

SessionUser is the result of session validation returned by the auth provider.

type SessionValidator

type SessionValidator interface {
	ValidateSession(ctx context.Context, sessionToken string) (*SessionUser, error)
}

SessionValidator validates a session token and returns the user.

Jump to

Keyboard shortcuts

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