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: 4 Imported by: 0

Documentation

Overview

Package cache provides performance acceleration for bearer token validation using GoModularAuth's core cache adapter.

The database adapter remains the source of truth at all times; Cache is derived and ephemeral. Cache keys are strictly derived from cryptographic token hashes or IDs, never from plaintext secrets.

Index

Constants

View Source
const (
	// SubNamespace is the plugin sub-namespace within GoModularAuth cache keys.
	SubNamespace = "bearer"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

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

Cache wraps the core cache adapter for bearer token lookups.

func New

func New(core corecache.Cache) *Cache

New creates a new Cache wrapper. If core is nil, the cache operates in no-op mode.

func (*Cache) Available

func (c *Cache) Available() bool

Available reports whether a backing cache is configured.

func (*Cache) DeleteByHash

func (c *Cache) DeleteByHash(ctx context.Context, tokenHash string) error

DeleteByHash invalidates the cached record by token hash.

func (*Cache) DeleteByID

func (c *Cache) DeleteByID(ctx context.Context, id string) error

DeleteByID invalidates cache entries associated with the token ID.

func (*Cache) GetByHash

func (c *Cache) GetByHash(ctx context.Context, tokenHash string) (*CachedToken, error)

GetByHash retrieves a token record by the SHA-256 hash of its token.

func (*Cache) Set

func (c *Cache) Set(ctx context.Context, record *CachedToken, ttl time.Duration) error

Set stores a token record in cache for fast lookup by token hash and id.

type CachedToken

type CachedToken struct {
	ID            string            `json:"id"`
	TokenHash     string            `json:"token_hash"`
	Subject       string            `json:"subject"`
	PrincipalType string            `json:"principal_type"`
	Scopes        []string          `json:"scopes"`
	Metadata      map[string]string `json:"metadata,omitempty"`
	ExpiresAt     time.Time         `json:"expires_at"`
	RevokedAt     *time.Time        `json:"revoked_at,omitempty"`
	CreatedAt     time.Time         `json:"created_at"`
	UpdatedAt     time.Time         `json:"updated_at"`
}

CachedToken represents the cached attributes of a bearer token.

func (*CachedToken) Active

func (r *CachedToken) Active(now time.Time) bool

Active reports whether the token is neither expired nor revoked.

func (*CachedToken) Expired

func (r *CachedToken) Expired(now time.Time) bool

Expired reports whether the token has expired relative to the given time.

func (*CachedToken) HasScope

func (r *CachedToken) HasScope(scope string) bool

HasScope reports whether the record contains the specified scope.

func (*CachedToken) Revoked

func (r *CachedToken) Revoked() bool

Revoked reports whether the token has been marked as revoked.

Jump to

Keyboard shortcuts

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