Skip to content

Commit

Permalink
Feat/access denied error (#1)
Browse files Browse the repository at this point in the history
* Update gitignore

* Add ErrAccessDenied error

* Add error description on errSecUserCanceled

* Run CI on pull requests
  • Loading branch information
raynaudoe committed Aug 2, 2022
1 parent 81fed19 commit d12f60a
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 8 deletions.
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

0 comments on commit d12f60a

Please sign in to comment.