cache

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package cache persists tokens on disk, keyed by (issuer, client_id), with 0600/0700 permissions, atomic writes, and advisory file locking for concurrency-safe refresh.

Index

Constants

This section is empty.

Variables

View Source
var ErrLockTimeout = errors.New("cache: timed out waiting for the profile lock")

ErrLockTimeout is returned when WithLock could not acquire the profile lock within lockAcquireTimeout, or the caller's ctx was done first.

Functions

func DefaultDir

func DefaultDir(getenv func(string) string) (string, error)

DefaultDir returns the default cache directory: $XDG_CACHE_HOME/oidc-token if XDG_CACHE_HOME is set, else ~/.cache/oidc-token.

Types

type Cache

type Cache struct {
	Dir string
}

Cache is a directory of per-profile JSON token files.

func New

func New(dir string) *Cache

New returns a Cache rooted at dir. The directory is created lazily on first write, with 0700 permissions.

func (*Cache) Load

func (c *Cache) Load(issuer, clientID string) (Entry, bool, error)

Load reads the cache entry for (issuer, clientID). An absent or unparseable file is reported as ok==false, err==nil (re-authenticate); other I/O errors (e.g. permission denied) return a non-nil error.

func (*Cache) Path

func (c *Cache) Path(issuer, clientID string) string

Path returns the on-disk file path for the given (issuer, clientID) profile. Exposed for tests that need to manipulate the cache file directly (e.g. simulating corruption).

func (*Cache) Save

func (c *Cache) Save(e Entry) error

Save atomically writes entry to its cache file: dir 0700, file 0600, write-to-temp-then-rename in the same directory so a crash never leaves a partially written cache file.

func (*Cache) WithLock

func (c *Cache) WithLock(ctx context.Context, issuer, clientID string, fn func() error) error

WithLock runs fn while holding an advisory file lock scoped to the (issuer, clientID) profile, waiting up to lockAcquireTimeout (or ctx's deadline, whichever is sooner) so a wedged holder can't hang forever.

type Entry

type Entry struct {
	Issuer       string    `json:"issuer"`
	ClientID     string    `json:"client_id"`
	AccessToken  string    `json:"access_token,omitempty"`
	IDToken      string    `json:"id_token,omitempty"`
	RefreshToken string    `json:"refresh_token,omitempty"`
	Expiry       time.Time `json:"expiry"`
}

Entry is the on-disk cache record for a single (issuer, client_id) profile.

Jump to

Keyboard shortcuts

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