tokenstore

package
v0.13.2 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2026 License: MIT Imports: 22 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 tokenstore provides a pluggable credential store shared by the entiredb and entire-core CLIs.

By default it delegates to the OS keyring (macOS Keychain, Linux Secret Service, etc.). Set ENTIRE_TOKEN_STORE=file to use a JSON file instead, which is useful in CI environments that lack a keyring daemon. Without the variable, "token_store": "file" in the per-user config.json does the same, e.g. once in /root/.config/entire for root's unattended jobs. A set ENTIRE_TOKEN_STORE, even an empty one, overrides config.json. An unusable config.json is always an error, whatever the environment says.

When using the file backend the tokens are stored in $ENTIRE_TOKEN_STORE_PATH (default: tokens.json in the per-user config directory — see internal/entireclient/userdirs).

Service-name conventions:

  • "entire:<cluster-host>" — entiredb cluster login tokens
  • "entire-core:<core-base-url>" — entire-core control-plane tokens
  • "entire-jurisdiction:<audience>" — jurisdiction (data-plane) access tokens, keyed by jurisdiction audience
  • "<service>:refresh" — refresh-token entry paired with the corresponding access-token service

Index

Constants

View Source
const (
	CoreKeyringPrefix         = "entire-core:"         // entire-core control-plane tokens
	JurisdictionKeyringPrefix = "entire-jurisdiction:" // jurisdiction (data-plane) access tokens
)

Keyring service-name prefixes. Tokens are filed under whichever issuer vouched for them, so a JWT obtained via an entire-core login flow lives at "entire-core:<base-url>" regardless of which CLI wrote it. Two CLIs sharing this prefix on the same machine read each other's writes.

View Source
const (
	BackendEnvVar = "ENTIRE_TOKEN_STORE"
	PathEnvVar    = "ENTIRE_TOKEN_STORE_PATH"
)

BackendEnvVar selects the credential backend: set to "file" to use the JSON file store instead of the OS keyring. PathEnvVar overrides where the file store lives (default: tokens.json in the per-user config directory). Exported so user-facing guidance (e.g. login's headless hint) names the same variables this package actually reads.

View Source
const TokenExpirationSeparator = "|"

TokenExpirationSeparator separates the token from its expiration timestamp. Format: "token|expires_at_unix"

Variables

View Source
var ErrBadConfig = userconfig.ErrBadConfig

ErrBadConfig marks an unusable config.json.

View Source
var ErrKeyringUnavailable = errors.New("the macOS Keychain is not accessible from this process, so the saved login cannot be read " +
	"(a sandbox is the usual cause); re-run the command where it can reach the Keychain — `entire login` will not fix this")

ErrKeyringUnavailable is returned instead of ErrNotFound when the OS keyring reports a credential missing but could not be searched at all, so a saved login is not misreported as "not logged in". The text is the whole explanation because it is what the user (or their agent) reads: logging in again cannot help, since the new token could not be stored either.

View Source
var ErrNotFound = keyring.ErrNotFound

ErrNotFound is returned when a credential is not present in the store.

Functions

func BackendDescription added in v0.9.0

func BackendDescription() string

BackendDescription names the credential backend the current environment resolves to, for user-facing provenance lines (e.g. `entire auth status`). It uses chooseBackend, like resolveBackendLocked, rather than introspecting the live backend, so test-only overrides don't leak into user-facing wording. A bad config.json returns its error.

func ConfigFilePath added in v0.13.0

func ConfigFilePath() string

ConfigFilePath is config.json's full path.

func CoreKeyringService

func CoreKeyringService(coreURL string) string

CoreKeyringService returns the service name for tokens issued by entire-core. coreURL is the base URL of the issuer; trailing slashes are normalized away so callers don't have to.

func DecodeTokenWithExpiration

func DecodeTokenWithExpiration(encoded string) (token string, expiresAt time.Time)

DecodeTokenWithExpiration decodes a token that may have an expiration timestamp. Returns the token and expiration time. If no expiration is encoded, returns the token as-is and a zero time.

func Delete

func Delete(service, user string) error

Delete removes a credential.

func EncodeTokenWithExpiration

func EncodeTokenWithExpiration(token string, expiresIn int64) string

EncodeTokenWithExpiration encodes a token with its expiration time as a "token|expires_at_unix" suffix. Callers must pass a positive expiresIn — the encoded suffix is the contract that tells future readers when to refresh, and a zero/negative TTL would record an immediately-expired token.

func FileBackendPath added in v0.9.0

func FileBackendPath() string

FileBackendPath resolves where the file backend stores (or would store) tokens: PathEnvVar when set, else tokens.json in the per-user config directory. Exported so user-facing guidance can name the concrete path.

The string form cannot report a rejected override, so it returns the path it would use; fileBackendPathChecked is what the store itself calls.

func FileBackendSelected added in v0.9.0

func FileBackendSelected() bool

FileBackendSelected reports chooseBackend's file choice.

func Get

func Get(service, user string) (string, error)

Get retrieves a credential.

func JurisdictionService added in v0.10.0

func JurisdictionService(audience string) string

JurisdictionService returns the service name for a jurisdiction (data-plane) access token, keyed by the jurisdiction audience so tokens for different jurisdictions — and for prod vs staging — can't be confused. The account key is the login context's handle. Trailing slashes are normalized away so callers don't have to.

func RefreshService added in v0.7.4

func RefreshService(service string) string

RefreshService returns the paired refresh-token service name for an access-token service, following the "<service>:refresh" convention documented in this package's service-name conventions. Callers store the raw refresh token under (RefreshService(service), user) alongside the access token at (service, user).

func Set

func Set(service, user, password string) error

Set stores a credential.

func UseFailingBackendForTesting added in v0.7.4

func UseFailingBackendForTesting(path string, failSet func(service, user string) bool) func()

UseFailingBackendForTesting wraps a file backend so Set returns an error for any (service, user) pair where failSet reports true; all other operations behave normally. It lets tests exercise partial-write failure paths (e.g. the refresh-then-access ordering in contextTokenStore) without exposing the unexported store interface. Returns a cleanup function.

func UseFailingDeleteBackendForTesting added in v0.7.7

func UseFailingDeleteBackendForTesting(path string, failDelete func(service, user string) bool) func()

UseFailingDeleteBackendForTesting is the delete-side analogue: Delete returns an error for any (service, user) pair where failDelete reports true. Used to test that logout treats credential deletion as part of its success contract.

func UseFailingGetBackendForTesting added in v0.7.4

func UseFailingGetBackendForTesting(path string, failGet func(service, user string) bool) func()

UseFailingGetBackendForTesting is the read-side analogue: Get returns an error for any (service, user) pair where failGet reports true. Used to test that callers surface a real store failure rather than swallowing it.

func UseFileBackendForTesting

func UseFileBackendForTesting(path string) func()

UseFileBackendForTesting points the package-level token store at a per-test JSON file and returns a cleanup function that restores the previous backend (re-resolving on next use). It serializes against concurrent Get/Set/Delete calls via the same mutex they use, so it is safe to call from any test even if other goroutines are mid-call.

Each call replaces the active backend; tests that invoke this from parallel subtests will trample each other and should arrange their own per-subtest paths if they actually need isolation.

func UseObservingBackendForTesting added in v0.10.0

func UseObservingBackendForTesting(path string, observe func(op, service, user string)) func()

UseObservingBackendForTesting wraps a file backend so observe is called with the operation ("get", "set" or "delete") and the (service, user) pair before each call is applied; operations otherwise behave normally. Lets tests assert ordering between a credential write and other bookkeeping around it.

func UseUnreachableKeyringForTesting added in v0.11.5

func UseUnreachableKeyringForTesting(path string) func()

UseUnreachableKeyringForTesting makes the store behave like a macOS Keychain the process cannot search: every "not found" from the file backend at path is reported through the production classifier as ErrKeyringUnavailable. Returns a cleanup function.

Types

This section is empty.

Jump to

Keyboard shortcuts

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