credentials

package
v1.21.107 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2026 License: Apache-2.0 Imports: 11 Imported by: ? 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 credentials persists a CLI's per-context secrets separately from its context configuration, with the durability guarantees a rotating single-use refresh token requires: writes are atomic, mutations are serialised across processes via WithLock, and Writable reports up-front whether a credential can be stored at all — before one is spent.

Index

Constants

View Source
const (
	KindFile     = "file"
	KindKeychain = "keychain"
)

Kinds of credential store. The kind in use is recorded in the context configuration and is never re-detected at runtime: silently falling back to the file store would write a plaintext refresh token the user asked to keep in the keychain.

Variables

View Source
var LockTimeout = 10 * time.Second

LockTimeout bounds how long WithLock waits for another process. A refresh takes well under a second, so exceeding this means a stale or wedged holder and the user deserves an error rather than a hang.

Functions

func WithLock

func WithLock(dir string, fn func() error) error

WithLock runs fn holding an exclusive cross-process lock over dir.

Locking is deliberately separate from Store: a keychain has no lock primitive, so the lock is always a file and is shared by every backend. Do not nest WithLock calls — flock is per open file description, so a nested acquisition in the same process deadlocks against itself.

func WriteAtomic

func WriteAtomic(path string, data []byte) error

WriteAtomic replaces path with data via a same-directory temp file and a rename, fsyncing both the file and the directory so the replacement survives a crash. Rename is only atomic within a filesystem, hence the same directory. The result is always 0600 — every file this package writes holds secrets.

Types

type Credential

type Credential struct {
	Token string             `json:"token,omitempty"`
	OIDC  *oidcclient.Tokens `json:"oidc,omitempty"`

	// NeedsReauth is set when the server has terminally rejected the refresh
	// token. It is a durable marker: the credential cannot recover on its own
	// and the user must log in again.
	NeedsReauth string `json:"needs_reauth,omitempty"`
}

Credential is everything secret about a single context.

func (*Credential) IsZero

func (c *Credential) IsZero() bool

type FileStore

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

FileStore keeps credentials in a 0600 JSON file. Every write replaces the file atomically, so a crash or a full disk leaves the previous version intact rather than a truncated one.

func NewFileStore

func NewFileStore(dir string) *FileStore

func (*FileStore) Delete

func (s *FileStore) Delete(context string) error

func (*FileStore) Get

func (s *FileStore) Get(context string) (*Credential, error)

func (*FileStore) Name

func (s *FileStore) Name() string

func (*FileStore) Set

func (s *FileStore) Set(context string, cred *Credential) error

Set replaces one context's credential. It re-reads the file first so that two processes refreshing different contexts do not clobber each other; the caller must hold WithLock for that read-modify-write to be safe.

func (*FileStore) Writable

func (s *FileStore) Writable() error

Writable reports whether save would succeed, by round-tripping a probe file through the same directory. It never touches stored credentials.

type KeychainStore

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

KeychainStore keeps the long-lived secrets — the refresh token and any static API token — in the OS keychain, one item per context.

The short-lived access and ID tokens stay in the file store alongside. They expire within the hour, so the bounded exposure of a 0600 file is a fair trade for keeping the keychain item small: an ID token JWT on its own can exceed the ~2560-byte limit Windows credential storage imposes. Keeping it small also means the keychain is read once per refresh rather than once per command, which on macOS is the difference between one prompt an hour and one prompt per invocation.

func NewKeychainStore

func NewKeychainStore(dir, service string) *KeychainStore

NewKeychainStore keeps secrets under the OS keychain service, one item per context, and the short-lived tokens in a file store rooted at dir.

func (*KeychainStore) Delete

func (s *KeychainStore) Delete(context string) error

func (*KeychainStore) Get

func (s *KeychainStore) Get(context string) (*Credential, error)

func (*KeychainStore) Name

func (s *KeychainStore) Name() string

func (*KeychainStore) Set

func (s *KeychainStore) Set(context string, cred *Credential) error

func (*KeychainStore) Writable

func (s *KeychainStore) Writable() error

Writable round-trips a probe item through the keychain and the file store. On a headless Linux box with no Secret Service this is what reports that the keychain is unusable, and it is what Detect keys off at login.

type Store

type Store interface {
	Name() string
	Get(context string) (*Credential, error)
	Set(context string, cred *Credential) error
	Delete(context string) error

	// Writable reports whether Set can durably persist, without mutating any
	// stored credential. Call it before spending a single-use token.
	Writable() error
}

Store holds the secrets for every context.

Get is safe to call without the lock: writes land via an atomic rename, so a reader always sees a complete version. Set and Delete mutate and MUST be called with the store's lock held — see WithLock.

func Open

func Open(dir, kind, service string) (Store, error)

Open returns the store of the given kind rooted at dir. An empty kind means the file store, which is the only one that works everywhere. service names the OS keychain service (the application) a keychain store keeps items under.

Jump to

Keyboard shortcuts

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