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

Catch keychain's errSecUserCanceled #119

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: golangci-lint
on: push
on: pull_request
jobs:
golangci:
strategy:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: Continuous Integration
on: push
on: pull_request
jobs:
linux:
runs-on: ubuntu-latest
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.vagrant
.idea
34 changes: 28 additions & 6 deletions keychain.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,26 @@

package keyring

/*
#cgo LDFLAGS: -framework CoreFoundation -framework Security

#include <CoreFoundation/CoreFoundation.h>
#include <Security/Security.h>
*/
import "C"
import (
"errors"
"fmt"

gokeychain "github.com/99designs/go-keychain"
)

// Extended error list that gokeychain doesn't catch
var (
// ErrorUserCanceled corresponds to errSecUserCanceled result code
ErrorUserCanceled = gokeychain.Error(C.errSecUserCanceled)
)

type keychain struct {
path string
service string
Expand Down Expand Up @@ -58,14 +71,23 @@ func (k *keychain) Get(key string) (Item, error) {

debugf("Querying keychain for service=%q, account=%q, keychain=%q", k.service, key, k.path)
results, err := gokeychain.QueryItem(query)
if err == gokeychain.ErrorItemNotFound || len(results) == 0 {
debugf("No results found")
return Item{}, ErrKeyNotFound
if err != nil {
switch err {
case ErrorUserCanceled:
debugf("Keychain access denied")
return Item{}, ErrAccessDenied
case gokeychain.ErrorItemNotFound:
debugf("Item not found in the keyring")
return Item{}, ErrKeyNotFound
default:
debugf("Error: %#v", err)
return Item{}, err
}
}

if err != nil {
debugf("Error: %#v", err)
return Item{}, err
if len(results) == 0 {
debugf("No results found")
return Item{}, ErrKeyNotFound
}

item := Item{
Expand Down
3 changes: 3 additions & 0 deletions keyring.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ var ErrMetadataNeedsCredentials = errors.New("The keyring backend requires crede
// ErrMetadataNotSupported is returned when Metadata is not available for the backend.
var ErrMetadataNotSupported = errors.New("The keyring backend does not support metadata access")

// ErrAccessDenied is returned by Keyring Get when access to Keychain is denied by the user
var ErrAccessDenied = errors.New("Keyring backend access denied by user")

var (
// Debug specifies whether to print debugging output.
Debug bool
Expand Down