Skip to content

Commit

Permalink
add support for multiline passwords on all platforms
Browse files Browse the repository at this point in the history
Signed-off-by: Royce Remer <rremer@salesforce.com>
  • Loading branch information
Royce Remer committed Apr 15, 2019
1 parent fbe81ae commit 41dbcc1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
10 changes: 9 additions & 1 deletion keyring_darwin.go
Expand Up @@ -15,6 +15,7 @@
package keyring

import (
"encoding/hex"
"fmt"
"os/exec"
"strings"
Expand Down Expand Up @@ -44,7 +45,14 @@ func (k macOSXKeychain) Get(service, username string) (string, error) {
}
return "", err
}
return strings.TrimSpace(fmt.Sprintf("%s", out)), nil
// 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
}
return fmt.Sprintf("%s", dec), err
}

// Get gets a secret from the keyring given a service name and a user.
Expand Down
22 changes: 22 additions & 0 deletions keyring_test.go
Expand Up @@ -16,6 +16,28 @@ func TestSet(t *testing.T) {
}
}

// TestGetMultiline tests getting a multi-line password from the keyring
func TestGetMultiLine(t *testing.T) {
multilinePassword := `this password
has multiple
lines and will be
encoded by some keyring implementiations
like osx`
err := Set(service, user, multilinePassword)
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 multilinePassword != pw {
t.Errorf("Expected password %s, got %s", multilinePassword, 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 41dbcc1

Please sign in to comment.