Skip to content
quarry Artifact [e84616c251]
Login

Artifact [e84616c251]

Artifact e84616c251ae7852a376caf14cababe440e52dcf79509afe367ec8898b981331:


package pickupclient

import (
	"errors"

	"github.com/zalando/go-keyring"
)

// ringKeychain is the Secret Service on Linux (over D-Bus) and the
// Credential Manager on Windows, through go-keyring: pure Go, no cgo. One
// item per host and account, service "quarry:HOST".
//
// Where there is no service to ask (a headless box with no D-Bus session)
// Put fails, so the store keeps the token in its private file, and Delete
// says yes: nothing can be held where nothing answers.
type ringKeychain struct{}

func ringService(host string) string { return "quarry:" + host }

func (ringKeychain) Put(host, account, value string) error {
	return keyring.Set(ringService(host), account, value)
}

func (ringKeychain) Get(host, account string) (string, error) {
	return keyring.Get(ringService(host), account)
}

func (ringKeychain) Delete(host, account string) error {
	err := keyring.Delete(ringService(host), account)
	if err == nil || errors.Is(err, keyring.ErrNotFound) {
		return nil
	}
	// Nothing readable under the name means nothing held, whether the
	// service is gone or the item never was.
	if _, gerr := keyring.Get(ringService(host), account); gerr != nil {
		return nil
	}
	return err
}