token

package
v0.20.0 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2026 License: Apache-2.0 Imports: 16 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 token provides encrypted token persistence for the krci CLI.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidCiphertext indicates data shorter than a nonce and tag, or data
	// that does not decrypt under a loaded key.
	ErrInvalidCiphertext = errors.New("invalid ciphertext")
	// ErrKeyNotFound indicates no encryption key is stored.
	ErrKeyNotFound = errors.New("encryption key not found")
)
View Source
var ErrKeyringAccess = errors.New("unable to access OS keyring")

ErrKeyringAccess indicates the OS keyring is not available.

View Source
var ErrNoToken = errors.New("no stored token")

ErrNoToken indicates no usable stored token.

Functions

This section is empty.

Types

type EncryptedFileStore

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

EncryptedFileStore persists tokens as AES-256-GCM encrypted JSON files. Save, Load, and Clear are safe for concurrent use; each holds mu for its whole duration.

func NewEncryptedStore

func NewEncryptedStore(path string, enc Encryptor, w io.Writer) *EncryptedFileStore

NewEncryptedStore creates a token store that encrypts data at rest. Token-recovery warnings are written to w, which must be non-nil. Panics if w is nil.

func (*EncryptedFileStore) Clear

func (s *EncryptedFileStore) Clear() error

Clear removes the stored token file.

func (*EncryptedFileStore) Load

func (s *EncryptedFileStore) Load() (*StoredToken, error)

Load reads and decrypts the token from disk.

  • ErrInvalidCiphertext: the file is reported on the store's writer and removed; once removed, Load returns ErrNoToken silently.
  • ErrKeyNotFound: the file is kept and reported on the writer once per store; Load returns ErrNoToken and the next Save replaces the file.
  • Any other Decrypt failure, such as a key that cannot be loaded: the file is kept and the error returned.

func (*EncryptedFileStore) Save

func (s *EncryptedFileStore) Save(tok *StoredToken) error

Save encrypts and writes the token to disk atomically (temp file + fsync + rename).

type Encryptor

type Encryptor interface {
	Encrypt(plaintext []byte) ([]byte, error)
	Decrypt(ciphertext []byte) ([]byte, error)
}

Encryptor encrypts and decrypts token data using AES-256-GCM. Decrypt wraps ErrInvalidCiphertext only for data too short to decrypt or data that does not decrypt under the loaded key, and ErrKeyNotFound only when no key is stored. Decrypt does not create a key.

func NewAESEncryptor

func NewAESEncryptor(keyringService, configDir string, w io.Writer) Encryptor

NewAESEncryptor creates an AES-256-GCM encryptor. The encryption key is stored in the OS keyring with file fallback. Key-storage warnings are written to w, which must be non-nil. Panics if w is nil.

type Store

type Store interface {
	Save(tok *StoredToken) error
	Load() (*StoredToken, error)
	Clear() error
}

Store persists and retrieves encrypted OAuth tokens.

type StoredToken

type StoredToken struct {
	AccessToken  string `json:"access_token"`
	RefreshToken string `json:"refresh_token"`
	IDToken      string `json:"id_token"`
	// ExpiresAt is the access-token expiry.
	ExpiresAt time.Time `json:"expires_at"`
	IssuerURL string    `json:"issuer_url"`
	ClientID  string    `json:"client_id"`
	// PortalURL is the portal the token was bound to at login or refresh. Empty
	// means unbound; the provider rejects unbound tokens.
	PortalURL string `json:"portal_url,omitempty"`
}

StoredToken holds the persisted OAuth token data.

func NewStoredToken

func NewStoredToken(tok *oauth2.Token, issuerURL, clientID, portalURL string) *StoredToken

NewStoredToken creates a StoredToken from an oauth2.Token and metadata.

Jump to

Keyboard shortcuts

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