clientauthcache

package
v2.14.1 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2026 License: MIT Imports: 6 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

Package clientauthcache caches successful client_secret verifications (Argon2id), so hot endpoints do not pay the Argon2 cost on every request. The key is the client_id and the value binds the verified snapshot to the sha256 of the presented secret. Entries expire after a TTL, failures are never cached, and the cache is a bounded LRU. Invalidate must run on every secret rotation or deletion, and the plaintext secret is never stored.

Index

Constants

View Source
const DefaultMaxEntries = 1024

DefaultMaxEntries bounds the cache so probing many synthetic client_ids cannot exhaust memory (about 1 MiB at the default).

View Source
const DefaultTTL = 5 * time.Minute

DefaultTTL bounds how long a verified entry stays usable. It stays within the introspection cache horizon, so a revoked secret does not linger.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache[V any] struct {
	// contains filtered or unexported fields
}

Cache is a bounded LRU keyed by client_id holding the verified client snapshot and the sha256 of the secret that authorized it. Every public method holds the mutex for its whole critical section, so operations stay O(1).

func New

func New[V any](maxEntries int, ttl time.Duration) *Cache[V]

New returns a cache. Non-positive limits fall back to DefaultMaxEntries and DefaultTTL.

func (*Cache[V]) Begin added in v2.14.0

func (c *Cache[V]) Begin(clientID string) uint64

Begin returns a token to take before the slow lookup and verify that precede a Put. Pass it to PutAt, which drops the entry when Invalidate ran for the client in between. Without it, a rotation that lands while an old secret is being verified would be undone by the Put, and the old secret would authenticate for the whole TTL.

func (*Cache[V]) Get

func (c *Cache[V]) Get(clientID, secret string) (V, bool)

Get returns the cached value only when the entry exists, has not expired, and the presented secret's sha256 matches. A hit moves the entry to the LRU front. On a miss the caller must run the slow Argon2 verify.

func (*Cache[V]) Invalidate

func (c *Cache[V]) Invalidate(clientID string)

Invalidate drops the entry for clientID. Any path that rotates or revokes client_secret_hash must call it. Safe on a missing key.

func (*Cache[V]) Len

func (c *Cache[V]) Len() int

Len returns the current entry count. Test-only helper.

func (*Cache[V]) Put

func (c *Cache[V]) Put(clientID, secret string, value V)

Put inserts or replaces the entry after a successful Argon2 verify of the same (clientID, secret) pair. It evicts the LRU entry at capacity. It is unguarded: a caller that verifies after a slow lookup must use Begin and PutAt.

func (*Cache[V]) PutAt added in v2.14.0

func (c *Cache[V]) PutAt(clientID, secret string, value V, tok uint64)

PutAt is Put guarded by a token from Begin.

Jump to

Keyboard shortcuts

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