cache

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2026 License: MIT, MIT Imports: 7 Imported by: 0

Documentation

Overview

Package cache provides caching and brute force prevention abstractions for the API keys module.

Index

Constants

View Source
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

func HashIdentifier(identifier string) string

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

func (l *Limiter) CheckRateLimit(ctx context.Context, identifier string) (bool, error)

CheckRateLimit checks whether the given client identifier is currently rate limited. Returns true if requests should be blocked.

func (*Limiter) GetKey

func (l *Limiter) GetKey(ctx context.Context, keyHash string) (*KeyRecord, error)

GetKey retrieves a cached API key by its SHA-256 hash.

func (*Limiter) Invalidate

func (l *Limiter) Invalidate(ctx context.Context, id string, keyHash string) error

Invalidate immediately purges the cache entries for an API key by ID and hash.

func (*Limiter) RecordFailure

func (l *Limiter) RecordFailure(ctx context.Context, identifier string) error

RecordFailure increments the failure count for brute-force protection.

func (*Limiter) SetKey

func (l *Limiter) SetKey(ctx context.Context, key *KeyRecord) error

SetKey stores an active API key in cache by both hash and ID to enable fast lookup and invalidation.

Jump to

Keyboard shortcuts

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