Documentation
¶
Overview ¶
Package cache provides caching and brute force prevention abstractions for the API keys module.
Index ¶
- Constants
- func HashIdentifier(identifier string) string
- type KeyRecord
- type Limiter
- func (l *Limiter) CheckRateLimit(ctx context.Context, identifier string) (bool, error)
- func (l *Limiter) GetKey(ctx context.Context, keyHash string) (*KeyRecord, error)
- func (l *Limiter) Invalidate(ctx context.Context, id string, keyHash string) error
- func (l *Limiter) RecordFailure(ctx context.Context, identifier string) error
- func (l *Limiter) SetKey(ctx context.Context, key *KeyRecord) error
Constants ¶
const ( DefaultKeyTTL = 5 * time.Minute DefaultBruteForceLimit = 10 DefaultBruteForceWindow = 1 * time.Minute )
Default settings for caching and abuse mitigation.
Variables ¶
This section is empty.
Functions ¶
func HashIdentifier ¶
HashIdentifier produces an opaque hash of sensitive identifiers (such as IP addresses) to avoid exposing plaintext client identifiers in cache keys or logs.
Types ¶
type KeyRecord ¶
type KeyRecord struct {
ID string `json:"id"`
Name string `json:"name"`
Prefix string `json:"prefix"`
KeyHash string `json:"key_hash"`
UserID string `json:"user_id,omitempty"`
OrganizationID string `json:"organization_id,omitempty"`
ServiceID string `json:"service_id,omitempty"`
Scopes []string `json:"scopes,omitempty"`
Permissions []string `json:"permissions,omitempty"`
Metadata map[string]string `json:"metadata,omitempty"`
ExpiresAt *time.Time `json:"expires_at,omitempty"`
RevokedAt *time.Time `json:"revoked_at,omitempty"`
LastUsedAt *time.Time `json:"last_used_at,omitempty"`
LastUsedIP string `json:"last_used_ip,omitempty"`
LastUsedUA string `json:"last_used_ua,omitempty"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
KeyRecord represents cached API key attributes.
type Limiter ¶
type Limiter struct {
// contains filtered or unexported fields
}
Limiter coordinates cached key resolution and brute-force mitigation using the shared cache.
func NewLimiter ¶
func NewLimiter(c corecache.Cache, maxAttempts int, window time.Duration, keyTTL time.Duration) *Limiter
NewLimiter constructs an API key cache and abuse limiter.
func (*Limiter) CheckRateLimit ¶
CheckRateLimit checks whether the given client identifier is currently rate limited. Returns true if requests should be blocked.
func (*Limiter) Invalidate ¶
Invalidate immediately purges the cache entries for an API key by ID and hash.
func (*Limiter) RecordFailure ¶
RecordFailure increments the failure count for brute-force protection.