Documentation
¶
Overview ¶
internal/api/middleware/auth.go
internal/api/middleware/authorization.go
internal/api/middleware/metrics.go
Index ¶
- Constants
- func AdminOnly() gin.HandlerFunc
- func AuthMiddleware() gin.HandlerFunc
- func AuthorizeRole(allowedRoles ...string) gin.HandlerFunc
- func CORSMiddleware(cfg config.Config) gin.HandlerFunc
- func IsAnonymous(c *gin.Context) bool
- func MetricsMiddleware() gin.HandlerFunc
- func PublicOrAuth(limit gin.HandlerFunc) gin.HandlerFunc
- func RateLimit(counter ratelimit.Counter, name string, limit int, window time.Duration) gin.HandlerFunc
- func ResponseWrapper() gin.HandlerFunc
- func SignedInOnly() gin.HandlerFunc
- func TooManyRequests(c *gin.Context, retryAfter time.Duration, message string)
- func UserSessionOnly() gin.HandlerFunc
Constants ¶
const APITokenIDKey = "api_token_id"
APITokenIDKey is set in the context when a request authenticates with an API token rather than a user session.
const AnonymousKey = "anonymous"
AnonymousKey is set in the context of requests without credentials that act with the public role.
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
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 gin.HandlerFunc) 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 ¶
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 ¶
This section is empty.