config

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2026 License: MIT Imports: 17 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 config saves a login (an API key or an OAuth token) in the keychain or the config file, and decides which credential and base URL a command uses.

Index

Constants

View Source
const (
	DefaultProfile = "default"

	MaxSecretLength = 1024
)

Variables

View Source
var (
	ErrSecretNotFound  = errors.New("secret not found in the keychain")
	ErrNoKeychain      = errors.New("no keychain is available")
	ErrKeychainTimeout = errors.New("the keychain did not answer")
)

Functions

func CheckBaseURL

func CheckBaseURL(raw string) error

CheckBaseURL rejects plain HTTP to anything but this machine, since the credential would cross the network unencrypted.

func Dir

func Dir(env Env) (string, error)

Dir returns the config directory: $CHRONOS_CONFIG_DIR, then $XDG_CONFIG_HOME/chronos, then ~/.config/chronos (the user config directory on Windows).

func EnvAPIKey

func EnvAPIKey(env Env) string

func IsDefaultBaseURL

func IsDefaultBaseURL(baseURL string) bool

func IsLoopback added in v0.4.0

func IsLoopback(host string) bool

func KeychainService

func KeychainService(baseURL string) string

KeychainService is the keychain service a login for baseURL is saved under, such as "chronos:api.chronos.sh". Changing it orphans every saved login.

func Path

func Path(dir string) string

func ResolveBaseURL

func ResolveBaseURL(env Env, flag string) string

ResolveBaseURL is the --base-url flag, else CHRONOS_BASE_URL, else the default API.

func ResolveBaseURLSource

func ResolveBaseURLSource(env Env, flag string) (baseURL, source string)

ResolveBaseURLSource is ResolveBaseURL plus the setting that chose it: "--base-url", "CHRONOS_BASE_URL", or "" for the default API.

func Save

func Save(dir string, f *File) error

Save writes f to dir atomically, readable only by the current user.

Types

type Account added in v0.3.0

type Account struct {
	ID   string `json:"id"`
	Name string `json:"name"`
}

type Credential

type Credential struct {
	Type    CredentialType `json:"type"`
	Storage Storage        `json:"storage"`
	Secret  string         `json:"secret,omitempty"`
	Grant
}

Credential points at a profile's secret. Secret is set only when Storage is StorageFile; otherwise the secret is in the keychain.

type CredentialType

type CredentialType string
const (
	CredentialAPIKey     CredentialType = "api_key"
	CredentialOAuthToken CredentialType = "oauth_token"
)

type Credentials

type Credentials struct {
	Secret  string
	Type    CredentialType
	Grant   Grant
	BaseURL string
	Source  Source
	Storage Storage
	// SavedFor is set when a saved login exists but was not used because it
	// belongs to this other base URL.
	SavedFor string
}

Credentials are what a command authenticates with and the base URL it calls. Secret is the API key or OAuth token. It is empty when there is none, and for one in the keychain until LoadSecret reads it.

func LoadSecret

func LoadSecret(k Keychain, c Credentials) (Credentials, error)

LoadSecret fills in Secret for credentials saved in the keychain and returns other credentials unchanged.

type Env

type Env func(string) string

Env reads an environment variable. os.Getenv in production.

type File

type File struct {
	Version  int                `json:"version"`
	Profiles map[string]Profile `json:"profiles"`
}

func Empty

func Empty() *File

func Load

func Load(dir string) (*File, error)

Load reads the config file in dir. A missing file is an empty config. A config file or directory that other users can change is refused, since the file decides where the saved login is sent.

func (*File) Resolve

func (f *File) Resolve(env Env, baseURLFlag string) Credentials

Resolve picks the credential and base URL. CHRONOS_API_KEY beats the saved login. A saved login is only sent to the base URL it was saved for, so pointing --base-url or CHRONOS_BASE_URL elsewhere never leaks it.

func (*File) Saved added in v0.3.0

func (f *File) Saved() (Credentials, bool)

Saved returns the saved login with the base URL it was saved for, whatever the environment says.

type Grant added in v0.3.0

type Grant struct {
	User      User      `json:"user,omitzero"`
	Account   Account   `json:"account,omitzero"`
	Scopes    []string  `json:"scopes,omitempty"`
	ExpiresAt time.Time `json:"expires_at,omitzero"`
}

Grant is what a browser login was granted: the person and workspace an OAuth token acts as, its scopes and when it expires. It is empty for an API key.

func (Grant) Expired added in v0.3.0

func (g Grant) Expired(now time.Time) bool

type Keychain

type Keychain interface {
	Get(service, account string) (string, error)
	Set(service, account, secret string) error
	Delete(service, account string) error
}

Keychain stores secrets in the operating system's credential store: the macOS keychain, the Secret Service on Linux, or Windows Credential Manager. Get and Delete return ErrSecretNotFound for a missing item. Every call returns ErrNoKeychain on a machine that has no keychain, and ErrKeychainTimeout when the keychain does not answer.

type Profile

type Profile struct {
	BaseURL    string     `json:"base_url,omitempty"`
	Credential Credential `json:"credential"`
}

Profile is one saved login. BaseURL is empty for the default API.

type RemoveResult

type RemoveResult struct {
	Removed  bool
	Warnings []string
}

func RemoveProfile

func RemoveProfile(dir string, k Keychain) (RemoveResult, error)

RemoveProfile deletes the default profile and its keychain item. A keychain item that cannot be deleted leaves a warning, not an error, so the profile is still removed.

type SaveOptions

type SaveOptions struct {
	Dir      string
	Keychain Keychain
	BaseURL  string
	Type     CredentialType
	Secret   string
	Grant    Grant
}

type SaveResult

type SaveResult struct {
	Credentials Credentials
	Warnings    []string
}

func SaveCredential added in v0.3.0

func SaveCredential(opts SaveOptions) (SaveResult, error)

SaveCredential saves the API key or OAuth token as the default profile for the base URL: in the keychain, or in the config file on a machine that has no keychain. A keychain that refuses or does not answer fails the save. A keychain item the old profile no longer needs is deleted.

type Source

type Source string

Source says where a credential came from.

const (
	SourceEnv   Source = "env"
	SourceSaved Source = "saved"
)

type Storage

type Storage string
const (
	StorageKeychain Storage = "keychain"
	StorageFile     Storage = "file"
)

type SystemKeychain

type SystemKeychain struct{}

func (SystemKeychain) Delete

func (SystemKeychain) Delete(service, account string) error

func (SystemKeychain) Get

func (SystemKeychain) Get(service, account string) (string, error)

func (SystemKeychain) Set

func (SystemKeychain) Set(service, account, secret string) error

type User added in v0.3.0

type User struct {
	ID    string `json:"id"`
	Email string `json:"email"`
	Name  string `json:"name"`
}

Jump to

Keyboard shortcuts

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