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 ¶
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 ¶
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 ¶
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 ¶
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 (*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.
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.