tokencache

package
v1.8.2 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: Apache-2.0 Imports: 15 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 tokencache stores one access token per credential set on disk, so several processes sharing one profile's credentials log in only once and each later process reuses the cached token until it nears expiry.

Index

Constants

This section is empty.

Variables

View Source
var ErrUnavailable = errors.New("tokencache: cache unavailable")

ErrUnavailable marks a service-account filesystem failure without retaining paths, hashes, or filesystem causes. Login errors pass through unchanged.

Functions

This section is empty.

Types

type Cache

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

Cache stores tokens under a directory, one file per Key. now is called for every freshness check and for stamping a written token's obtain time, so tests can inject a fake clock.

func New

func New(dir string, now func() time.Time) *Cache

New returns a Cache rooted at dir. dir is created, with mode 0700, on first use rather than by New itself.

func (*Cache) Get

func (c *Cache) Get(ctx context.Context, key Key, rejected string, login func(context.Context) (Token, error)) (Token, error)

Get returns a usable token for key, calling login only when the cache has none for key, the cached one is expiring within 30 seconds, or it equals rejected and has been held at least 30 seconds (the caller passes rejected after a 401; a token still under 30 seconds old is reused as is, so a rejection that a new login cannot fix ends in one failed retry instead of a login inside the same 30-second TOTP window).

The whole read-check-login-write sequence runs under one lock on <dir>/<hash>.lock, so two processes sharing key never log in for the same request. The lock is tried without blocking, then retried with backoff up to 500ms until ctx ends, at which point Get returns ctx.Err().

func (*Cache) GetServiceAccount added in v0.75.0

func (c *Cache) GetServiceAccount(ctx context.Context, key ServiceAccountKey, rejected string, login func(context.Context) (Token, error)) (Token, error)

GetServiceAccount uses the shared cache sequence with strict file checks and a separate namespace. An ended caller context takes precedence.

func (*Cache) ReadTOTPStep added in v0.98.0

func (c *Cache) ReadTOTPStep(key Key, current int64) (int64, bool)

func (*Cache) WriteTOTPStep added in v0.98.0

func (c *Cache) WriteTOTPStep(ctx context.Context, key Key, step int64) error

type Key

type Key struct {
	Profile   string
	RootEmail string
	Username  string
	SigninURL string
	TokenURL  string
}

Key identifies one cached credential set. Any differing field is a separate cache entry.

func (Key) Hash

func (k Key) Hash() string

Hash is the cache entry's file name stem: SHA-256 hex over each field of Key in order, length-prefixed so no combination of field values can collide with another (Key{"ab","c"} and Key{"a","bc"} hash differently).

type ServiceAccountKey added in v0.75.0

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

ServiceAccountKey holds only a digest, never client credentials.

func NewServiceAccountKey added in v0.75.0

func NewServiceAccountKey(profile, clientID, tokenURL, secret string) ServiceAccountKey

NewServiceAccountKey binds the cache entry to the immutable secret snapshot.

func (ServiceAccountKey) Format added in v0.75.0

func (ServiceAccountKey) Format(f fmt.State, _ rune)

Format hides the cache key in formatted output.

func (ServiceAccountKey) GoString added in v0.75.0

func (ServiceAccountKey) GoString() string

GoString hides the cache key in Go formatting.

func (ServiceAccountKey) Hash added in v0.75.0

func (k ServiceAccountKey) Hash() string

Hash returns the hexadecimal cache key digest.

func (ServiceAccountKey) LogValue added in v0.75.0

func (ServiceAccountKey) LogValue() slog.Value

LogValue hides the cache key in structured logs.

func (ServiceAccountKey) MarshalJSON added in v0.75.0

func (ServiceAccountKey) MarshalJSON() ([]byte, error)

MarshalJSON replaces the cache key with a redacted value.

func (ServiceAccountKey) MarshalText added in v0.75.0

func (ServiceAccountKey) MarshalText() ([]byte, error)

MarshalText replaces the cache key with a redacted value.

func (ServiceAccountKey) String added in v0.75.0

func (ServiceAccountKey) String() string

type Token

type Token struct {
	AccessToken string
	ExpiresAt   time.Time
	ObtainedAt  time.Time
}

Token is an access token, its expiry, and the time it was obtained. Get sets ObtainedAt itself, from disk on a cache hit or from its own clock on a fresh login; a login callback's returned Token need not set it.

Jump to

Keyboard shortcuts

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