tokencache

package
v0.14.1 Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package tokencache provides a generic LRU-bounded token cache for short-lived access tokens. It is provider-neutral: any credential provider that mints tokens with an expiry (OIDC exchange, GitHub App installation tokens, etc.) can use it directly with an arbitrary string key.

Index

Constants

View Source
const (
	// DefaultMaxEntries is the default maximum number of entries in the cache.
	DefaultMaxEntries = 10000
)

Variables

This section is empty.

Functions

func GenerateCacheKey

func GenerateCacheKey(tokenEndpoint, connectorID, userID string) string

GenerateCacheKey creates a cache key from the token endpoint, connector ID, and user ID. The key is a SHA-256 hash of the inputs so it is fixed-length and does not expose sensitive values if the cache is inspected.

The userID MUST come from validated, trusted claims (e.g. the "sub" claim after JWT signature verification), not from raw user input. Untrusted values can cause cache poisoning.

Types

type Cache

type Cache struct {
	// contains filtered or unexported fields
}

Cache is a generic LRU-bounded token cache for short-lived access tokens.

It is provider-neutral: any credential provider that mints tokens with an expiry can use it with an arbitrary string key. Keys should be constructed so distinct scopes (endpoint, connector, user; or installationID, permissions) get separate entries.

Thread-safe. When the cache reaches its maximum size, the least recently used entry is evicted. Tokens are considered expired 30 seconds before their actual expiry to account for clock skew and network latency.

func New

func New() *Cache

New creates a new Cache with the default maximum entry count.

func NewWithMaxEntries

func NewWithMaxEntries(maxEntries int) *Cache

NewWithMaxEntries creates a new Cache with a custom maximum entry count. Pass 0 for unlimited (not recommended in production).

func (*Cache) Cleanup

func (c *Cache) Cleanup() int

Cleanup removes expired tokens. Call periodically in long-running services to prevent memory growth from accumulated expired entries.

func (*Cache) Clear

func (c *Cache) Clear()

Clear removes all tokens from the cache.

func (*Cache) Delete

func (c *Cache) Delete(key string)

Delete removes a token from the cache.

func (*Cache) Get

func (c *Cache) Get(key string) *Token

Get retrieves a cached token by key. Returns nil if the token is absent or expired. A successful lookup moves the entry to the front of the LRU list.

func (*Cache) GetStats

func (c *Cache) GetStats() Stats

GetStats returns cache statistics.

func (*Cache) Set

func (c *Cache) Set(key, accessToken, issuedTokenType string, expiresIn int)

Set stores a token in the cache. expiresIn is the token lifetime in seconds; a 30-second buffer is applied so the cached entry is treated as expired before the actual expiry. If the cache is at capacity, the LRU entry is evicted.

func (*Cache) Size

func (c *Cache) Size() int

Size returns the number of tokens in the cache, including expired ones.

type Stats

type Stats struct {
	// CurrentEntries is the number of entries currently in the cache.
	CurrentEntries int
	// MaxEntries is the maximum allowed entries (0 = unlimited).
	MaxEntries int
	// TotalEvictions is the number of LRU evictions performed.
	TotalEvictions int64
	// MemoryPressure is the percentage of max capacity used (0-100).
	MemoryPressure float64
}

Stats provides cache statistics for monitoring.

type Token

type Token struct {
	// AccessToken is the cached access token value.
	AccessToken string

	// ExpiresAt is when the token expires (after the 30s buffer).
	ExpiresAt time.Time

	// IssuedTokenType is the RFC 8693 token-type URN of the cached token.
	IssuedTokenType string
}

Token holds a cached token with its expiration.

Jump to

Keyboard shortcuts

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