Help us shape the future of Go! Take the 2026 Go Developer Survey

storage

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2026 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Overview

Package storage persists the Cognito token sets obtained by github.com/wego/pkg/cognito.

Tokens live under an opaque namespace, e.g. "my-cli/staging". The caller chooses the string and the store never interprets it; namespacing is what keeps a staging login from overwriting a production one.

Use NewMemory in tests and anywhere a keychain is unavailable; it satisfies the same interface as NewKeyring, so a caller swaps one for the other without touching its own code.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultFileDir added in v0.2.0

func DefaultFileDir(service string) (string, error)

DefaultFileDir is where NewAuto puts sessions when it falls back. It follows the XDG state convention, which is the right home for data that persists between runs but is not configuration the operator edits.

func NewAuto added in v0.2.0

func NewAuto(service, dir string) (Store, *Downgrade)

NewAuto returns the OS keychain store where one is usable, and a file store under dir where it is not, along with a non-nil *Downgrade saying why.

The fallback exists because the keychain is simply absent on a headless Linux host: there is no org.freedesktop.secrets D-Bus service to talk to, so every write fails and the sign-in an operator just completed is thrown away at the last step. That is precisely the host where the paste-back flow is most needed.

It is a real downgrade and the caller should say so out loud - see fileStore's doc comment. A caller that would rather fail than write a long-lived refresh token to disk should use NewKeyring directly.

Types

type Downgrade added in v0.2.0

type Downgrade struct {
	// Dir is where sessions are now written.
	Dir string
	// Cause is the keychain error that forced the fallback.
	Cause error
}

Downgrade explains why NewAuto chose the file store over the keychain.

It is returned rather than logged because a library has no business writing to a caller's stderr, and the caller is the only one that knows whether this deserves a warning, a one-line note, or silence.

func (*Downgrade) Error added in v0.2.0

func (d *Downgrade) Error() string

func (*Downgrade) Unwrap added in v0.2.0

func (d *Downgrade) Unwrap() error

type Store

type Store interface {
	Load(namespace string) (*cognito.TokenSet, error)
	Save(namespace string, tokens *cognito.TokenSet) error
	Delete(namespace string) error
}

Store persists a TokenSet under an opaque namespace.

A missing entry is NOT an error: Load returns (nil, nil) when nothing is stored, because "not logged in" is a normal state. Callers can therefore tell it apart from a backend that is genuinely broken, which does return an error.

func NewFile added in v0.2.0

func NewFile(dir string) Store

NewFile returns a Store writing one owner-only JSON file per namespace under dir. The directory is created on first write, not here, so constructing a store this process never writes to touches no disk.

The owner-only guarantee is a Unix one. Wego runs this on macOS workstations and Linux hosts, where 0600 and 0700 mean what they say. Go maps those bits onto Windows ACLs only approximately, so on Windows the file would be created but its protection would not be the one documented here - and NewAuto would not choose this store there anyway, since Windows has a working credential manager for go-keyring to use.

func NewKeyring

func NewKeyring(service string) Store

NewKeyring returns a Store backed by the operating system keychain, with every entry filed under service.

func NewMemory

func NewMemory() Store

NewMemory returns a Store that holds tokens for the lifetime of the process and nothing longer. It serves tests and callers with no usable keychain; a new process always starts signed out.

Jump to

Keyboard shortcuts

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