middleware

package
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2026 License: MIT Imports: 14 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

internal/api/middleware/auth.go

internal/api/middleware/authorization.go

internal/api/middleware/metrics.go

Index

Constants

View Source
const APITokenIDKey = "api_token_id"

APITokenIDKey is set in the context when a request authenticates with an API token rather than a user session.

View Source
const AnonymousKey = "anonymous"

AnonymousKey is set in the context of requests without credentials that act with the public role.

View Source
const MaxAnonymousLevel = 2

MaxAnonymousLevel is the deepest relation population anonymous requests get: each level multiplies the work.

Variables

This section is empty.

Functions

func AdminOnly

func AdminOnly() gin.HandlerFunc

func AuthMiddleware

func AuthMiddleware() gin.HandlerFunc

func AuthorizeRole

func AuthorizeRole(allowedRoles ...string) gin.HandlerFunc

func CORSMiddleware

func CORSMiddleware(cfg config.Config) gin.HandlerFunc

CORSMiddleware applies dynamic CORS settings based on config

func IsAnonymous added in v0.6.0

func IsAnonymous(c *gin.Context) bool

IsAnonymous reports whether the request acts with the public role.

func MetricsMiddleware

func MetricsMiddleware() gin.HandlerFunc

func PublicOrAuth added in v0.6.0

func PublicOrAuth(limit Limiter) gin.HandlerFunc

PublicOrAuth authenticates like AuthMiddleware, except that a request without an Authorization header acts with the public role when that role grants anything. Anonymous requests go through limit (per client IP), and a 403 they get becomes a 401 so clients know signing in may help.

func RateLimit

func RateLimit(counter ratelimit.Counter, name string, limit int, window time.Duration) gin.HandlerFunc

RateLimit allows at most limit requests per client IP and window for the routes it is attached to; name separates independent budgets. A limit of 0 disables it. Counters are shared across replicas when backed by Redis.

The client IP comes from gin's ClientIP, which only trusts forwarding headers from configured trusted proxies (see trusted_proxies).

func ResponseWrapper

func ResponseWrapper() gin.HandlerFunc

func SignedInOnly added in v0.6.0

func SignedInOnly() gin.HandlerFunc

SignedInOnly refuses anonymous requests (public role) with 401, for routes only accounts and tokens may use whatever the public role grants.

func TooManyRequests

func TooManyRequests(c *gin.Context, retryAfter time.Duration, message string)

TooManyRequests aborts with 429, a Retry-After header and the standard error envelope.

func UserSessionOnly added in v0.5.0

func UserSessionOnly() gin.HandlerFunc

UserSessionOnly refuses requests made with an API token. It protects routes that would let a leaked token create more credentials (tokens, users).

Types

type Limiter added in v0.6.1

type Limiter func(c *gin.Context) bool

Limiter counts a request and reports whether it may go on; when it may not, it has answered 429. Unlike a gin middleware it does not run the rest of the chain, so another middleware can call it inline.

func RateLimitAllow added in v0.6.1

func RateLimitAllow(counter ratelimit.Counter, name string, limit int, window time.Duration) Limiter

RateLimitAllow is RateLimit as a Limiter.

Jump to

Keyboard shortcuts

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