clientauthcache

package
v2.6.0 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2026 License: MIT Imports: 5 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 the result of an OAuth client_secret verification (Argon2id PHC compare) so that hot endpoints (introspect, token, revoke) do not pay the ~27ms Argon2 cost on every request.

Contract:

  • The cache key is the client_id; the cache VALUE binds the verified client snapshot to the sha256 of the presented secret. A cache hit therefore requires both the client_id and the same presented secret bytes that produced the original Argon2 verify.
  • Entries expire after a fixed TTL (default 5 minutes), bounding the window during which a revoked-but-cached entry can authenticate.
  • The cache is bounded (default 1024 entries) via simple LRU eviction so a high cardinality of client_ids (or a credential-stuffing storm hitting many client_id values) cannot grow memory without bound.
  • Only successful verifications are cached. Failures are never cached so an attacker cannot poison the cache to lock out a legitimate client.
  • Invalidate(clientID) MUST be called by any code that mutates the stored client_secret_hash. The contract is documented at the call sites: UpdateOAuthClient (used by agent secret rotation) and DeleteOAuthClient (used by failed agent creation cleanup).
  • The cached entry is compared to the presented secret using a sha256 constant-time compare. The plaintext secret is never stored; only its 32-byte digest.

Index

Constants

View Source
const DefaultMaxEntries = 1024

DefaultMaxEntries bounds the cache to a reasonable upper limit so an attacker probing many synthetic client_ids cannot exhaust process memory. At roughly 200 bytes per entry plus the OAuthClient pointee, 1024 entries is well under 1 MiB.

View Source
const DefaultTTL = 5 * time.Minute

DefaultTTL bounds how long a verified entry remains usable. Five minutes keeps the hot-path benefit (a typical resource server introspects every request inside this window) while ensuring a revoked secret never lingers past the documented introspection-cache horizon.

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 plus the sha256 of the presented secret that authorized the entry.

Concurrency: every public method holds the cache's mutex for the duration of its critical section. Operations are O(1) under normal load. The underlying list.List plus map[string]*entry pattern is the same shape used by the standard library net/http transport.

func New

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

New returns a fresh cache. maxEntries less than or equal to 0 falls back to DefaultMaxEntries; ttl less than or equal to 0 falls back to DefaultTTL.

func (*Cache[V]) Get

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

Get returns the cached value for clientID if and only if (a) an entry exists, (b) it has not expired, and (c) the presented secret's sha256 matches the digest stored on the entry. On a hit the entry is moved to the LRU front. On a miss (including digest mismatch or expiry), the caller MUST fall back to the slow Argon2 verify path; this method makes no statement about the validity of the supplied credential beyond the strict bytes-equality check.

func (*Cache[V]) Invalidate

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

Invalidate drops the entry for clientID. MUST be called by any path that rotates or revokes the stored client_secret_hash (UpdateOAuthClient, DeleteOAuthClient). 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 for clientID. Always called after a successful Argon2 verify on the same (clientID, secret) pair. If an entry already exists it is replaced (the verified value is presumed fresher); the LRU position is moved to the front. If the cache is at capacity the least-recently-used entry is evicted.

Jump to

Keyboard shortcuts

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