Skip to content

Commit

Permalink
Adding well-known prefix to encoded multiline
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Nixon <[email protected]>
  • Loading branch information
Ryan Nixon committed May 29, 2019
1 parent a1018b8 commit 7126498
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
18 changes: 14 additions & 4 deletions keyring_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ import (

const (
execPathKeychain = "/usr/bin/security"

// encodingPrefix is a well-known prefix added to strings encoded by Set.
encodingPrefix = "go-keyring-encoded:"
)

type macOSXKeychain struct{}
Expand All @@ -48,15 +51,22 @@ func (k macOSXKeychain) Get(service, username string) (string, error) {
// if the added secret has multiple lines, osx will hex encode it
trimStr := strings.TrimSpace(string(out[:]))
dec, err := hex.DecodeString(trimStr)
// if there was an error hex decoding the string, assume it's not encoded
if err != nil {
return fmt.Sprintf("%s", trimStr), nil
// if there was no error hex decoding the string and it has the well-known prefix, assume it's encoded
if err == nil && strings.HasPrefix(string(dec), encodingPrefix) {
return fmt.Sprintf("%s", dec[len(encodingPrefix):]), nil
}
return fmt.Sprintf("%s", dec), err

return trimStr, nil
}

// Set stores a secret in the keyring given a service name and a user.
func (k macOSXKeychain) Set(service, username, password string) error {
// if the added secret has multiple lines, osx will hex encode it
// identify this with a well-known prefix.
if strings.ContainsRune(password, '\n') {
password = hex.EncodeToString([]byte(encodingPrefix + password))
}

return exec.Command(
execPathKeychain,
"add-generic-password",
Expand Down
18 changes: 18 additions & 0 deletions keyring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,24 @@ like osx`
}
}

// TestGetSingleLineHex tests getting a single line hex string password from the keyring.
func TestGetSingleLineHex(t *testing.T) {
hexPassword := "abcdef123abcdef123"
err := Set(service, user, hexPassword)
if err != nil {
t.Errorf("Should not fail, got: %s", err)
}

pw, err := Get(service, user)
if err != nil {
t.Errorf("Should not fail, got: %s", err)
}

if hexPassword != pw {
t.Errorf("Expected password %s, got %s", hexPassword, pw)
}
}

// TestGet tests getting a password from the keyring.
func TestGet(t *testing.T) {
err := Set(service, user, password)
Expand Down

0 comments on commit 7126498

Please sign in to comment.