Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't prompt for passphrase unless unlocking fails #128

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 24 additions & 13 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,19 @@ func (k *fileKeyring) Get(key string) (Item, error) {
return Item{}, err
}

if err = k.unlock(); err != nil {
return Item{}, err
}

payload, _, err := jose.Decode(string(bytes), k.password)
if err != nil {
return Item{}, err
if k.password == "" { // try again after unlocking
if err = k.unlock(); err != nil {
return Item{}, err
}
payload, _, err = jose.Decode(string(bytes), k.password)
if err != nil {
return Item{}, err
}
} else {
return Item{}, err
}
}

var decoded Item
Expand Down Expand Up @@ -127,15 +133,20 @@ func (k *fileKeyring) Set(i Item) error {
return err
}

if err = k.unlock(); err != nil {
return err
}

token, err := jose.Encrypt(string(bytes), jose.PBES2_HS256_A128KW, jose.A256GCM, k.password,
jose.Headers(map[string]interface{}{
"created": time.Now().String(),
}))
key := jose.Headers(map[string]interface{}{
"created": time.Now().String(),
})
token, err := jose.Encrypt(string(bytes), jose.PBES2_HS256_A128KW, jose.A256GCM, k.password, key)
if err != nil {
if k.password == "" { // try again after unlocking
if err = k.unlock(); err != nil {
return err
}
token, err = jose.Encrypt(string(bytes), jose.PBES2_HS256_A128KW, jose.A256GCM, k.password, key)
if err != nil {
return err
}
}
return err
}

Expand Down
6 changes: 5 additions & 1 deletion file_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package keyring

import (
"errors"
"os"
"testing"
)

func TestFileKeyringSetWhenEmpty(t *testing.T) {
k := &fileKeyring{
dir: os.TempDir(),
passwordFunc: FixedStringPrompt("no more secrets"),
passwordFunc: func(_ string) (string, error) {
t.Fatal("passwordFunc called")
return "", errors.New("passwordFunc called")
},
}
item := Item{Key: "llamas", Data: []byte("llamas are great")}

Expand Down