Documentation
¶
Index ¶
- func APIKeyAuth(db *sqlx.DB, masterKey string, sessions SessionVerifier) gin.HandlerFunc
- func Actor(c *gin.Context) string
- func ActorName(c *gin.Context) string
- func ActorRateLimit(requestsPerMinute int) gin.HandlerFunc
- func NewRedisActorRateLimit(addr, password string, db, limit int) gin.HandlerFunc
- func NewRedisRateLimit(addr, password string, db, limit int) gin.HandlerFunc
- func RateLimit(requestsPerMinute int) gin.HandlerFunc
- func RequireMasterKey() gin.HandlerFunc
- type SessionVerifier
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func APIKeyAuth ¶
func APIKeyAuth(db *sqlx.DB, masterKey string, sessions SessionVerifier) gin.HandlerFunc
APIKeyAuth validates Bearer or X-API-Key tokens against the database. When sessions is non-nil, a valid SSO session cookie is also accepted. Master key comparison is length-safe via HMAC before ConstantTimeCompare.
func Actor ¶
Actor returns the canonical identity of the authenticated caller: the API key UUID string, or "master" for the master key, or "unknown" if unset. Use this for access-control checks and as the stored identity in the DB.
func ActorName ¶
ActorName returns the human-readable key name for the authenticated caller. Use this for audit log entries and display. Falls back to Actor(c) when the display name is not set (e.g. in tests that don't go through APIKeyAuth).
func ActorRateLimit ¶
func ActorRateLimit(requestsPerMinute int) gin.HandlerFunc
ActorRateLimit returns middleware that limits requests per authenticated actor (API-key name). Must run AFTER APIKeyAuth so that Actor(c) is populated. The master actor is always exempt — it should never be throttled.
func NewRedisActorRateLimit ¶
func NewRedisActorRateLimit(addr, password string, db, limit int) gin.HandlerFunc
NewRedisActorRateLimit is like NewRedisRateLimit but keys on the authenticated actor. Must run AFTER APIKeyAuth so that Actor(c) is populated. The master actor is always exempt — it should never be throttled. On Redis error the middleware falls back to an in-memory token-bucket limiter.
func NewRedisRateLimit ¶
func NewRedisRateLimit(addr, password string, db, limit int) gin.HandlerFunc
NewRedisRateLimit returns a per-minute rate-limit middleware backed by Redis. Uses a sliding-window approximation: each 1-minute bucket has its own key. On Redis error the middleware falls back to an in-memory token-bucket limiter rather than failing open. The fallback is less precise under concurrent instances but always enforces a limit — it never allows unlimited traffic.
func RateLimit ¶
func RateLimit(requestsPerMinute int) gin.HandlerFunc
RateLimit returns middleware that allows at most requestsPerMinute requests per unique client IP. Exceeding callers receive 429 Too Many Requests.
func RequireMasterKey ¶
func RequireMasterKey() gin.HandlerFunc
RequireMasterKey rejects requests where the caller is not authenticated with the master key. Must be applied after APIKeyAuth in the middleware chain (L-8).
Types ¶
type SessionVerifier ¶
SessionVerifier validates an SSO session cookie on the request and returns the API key ID bound to it. Implemented by the enterprise session manager (ee/sso); core builds pass nil and only header-based API keys are accepted.