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
- Variables
- func CheckBaseURL(raw string) error
- func Dir(env Env) (string, error)
- func EnvAPIKey(env Env) string
- func IsDefaultBaseURL(baseURL string) bool
- func IsLoopback(host string) bool
- func KeychainService(baseURL string) string
- func Path(dir string) string
- func ResolveBaseURL(env Env, flag string) string
- func ResolveBaseURLSource(env Env, flag string) (baseURL, source string)
- func Save(dir string, f *File) error
- type Account
- type Credential
- type CredentialType
- type Credentials
- type Env
- type File
- type Grant
- type Keychain
- type Profile
- type RemoveResult
- type SaveOptions
- type SaveResult
- type Source
- type Storage
- type SystemKeychain
- type User
Constants ¶
const ( DefaultProfile = "default" MaxSecretLength = 1024 )
Variables ¶
Functions ¶
func CheckBaseURL ¶
CheckBaseURL rejects plain HTTP to anything but this machine, since the credential would cross the network unencrypted.
func Dir ¶
Dir returns the config directory: $CHRONOS_CONFIG_DIR, then $XDG_CONFIG_HOME/chronos, then ~/.config/chronos (the user config directory on Windows).
func IsDefaultBaseURL ¶
func IsLoopback ¶ added in v0.4.0
func KeychainService ¶
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 ResolveBaseURL ¶
ResolveBaseURL is the --base-url flag, else CHRONOS_BASE_URL, else the default API.
func ResolveBaseURLSource ¶
ResolveBaseURLSource is ResolveBaseURL plus the setting that chose it: "--base-url", "CHRONOS_BASE_URL", or "" for the default API.
Types ¶
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 File ¶
func Load ¶
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.
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 ¶
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 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 SystemKeychain ¶
type SystemKeychain struct{}
func (SystemKeychain) Delete ¶
func (SystemKeychain) Delete(service, account string) error
func (SystemKeychain) Set ¶
func (SystemKeychain) Set(service, account, secret string) error