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 ¶
- Variables
- type Cache
- func (c *Cache) Get(ctx context.Context, key Key, rejected string, ...) (Token, error)
- func (c *Cache) GetServiceAccount(ctx context.Context, key ServiceAccountKey, rejected string, ...) (Token, error)
- func (c *Cache) ReadTOTPStep(key Key, current int64) (int64, bool)
- func (c *Cache) WriteTOTPStep(ctx context.Context, key Key, step int64) error
- type Key
- type ServiceAccountKey
- func (ServiceAccountKey) Format(f fmt.State, _ rune)
- func (ServiceAccountKey) GoString() string
- func (k ServiceAccountKey) Hash() string
- func (ServiceAccountKey) LogValue() slog.Value
- func (ServiceAccountKey) MarshalJSON() ([]byte, error)
- func (ServiceAccountKey) MarshalText() ([]byte, error)
- func (ServiceAccountKey) String() string
- type Token
Constants ¶
This section is empty.
Variables ¶
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 ¶
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
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.
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