Skip to content

Commit

Permalink
Merge pull request #278 from thaJeztah/osxkeychain_typed_error
Browse files Browse the repository at this point in the history
osxkeychain: Delete(): return typed errors
  • Loading branch information
thaJeztah authored May 27, 2023
2 parents 9ff5b61 + b21b69c commit f09e79d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
36 changes: 20 additions & 16 deletions osxkeychain/osxkeychain_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type Osxkeychain struct{}

// Add adds new credentials to the keychain.
func (h Osxkeychain) Add(creds *credentials.Credentials) error {
h.Delete(creds.ServerURL)
_ = h.Delete(creds.ServerURL) // ignore errors as existing credential may not exist.

s, err := splitServer(creds.ServerURL)
if err != nil {
Expand Down Expand Up @@ -66,10 +66,16 @@ func (h Osxkeychain) Delete(serverURL string) error {
}
defer freeServer(s)

errMsg := C.keychain_delete(s)
if errMsg != nil {
if errMsg := C.keychain_delete(s); errMsg != nil {
defer C.free(unsafe.Pointer(errMsg))
return errors.New(C.GoString(errMsg))
switch goMsg := C.GoString(errMsg); goMsg {
case errCredentialsNotFound:
return credentials.NewErrCredentialsNotFound()
case errInteractionNotAllowed:
return ErrInteractionNotAllowed
default:
return errors.New(goMsg)
}
}

return nil
Expand All @@ -93,15 +99,14 @@ func (h Osxkeychain) Get(serverURL string) (string, string, error) {
errMsg := C.keychain_get(s, &usernameLen, &username, &secretLen, &secret)
if errMsg != nil {
defer C.free(unsafe.Pointer(errMsg))
goMsg := C.GoString(errMsg)
if goMsg == errCredentialsNotFound {
switch goMsg := C.GoString(errMsg); goMsg {
case errCredentialsNotFound:
return "", "", credentials.NewErrCredentialsNotFound()
}
if goMsg == errInteractionNotAllowed {
case errInteractionNotAllowed:
return "", "", ErrInteractionNotAllowed
default:
return "", "", errors.New(goMsg)
}

return "", "", errors.New(goMsg)
}

user := C.GoStringN(username, C.int(usernameLen))
Expand All @@ -124,15 +129,14 @@ func (h Osxkeychain) List() (map[string]string, error) {
defer C.freeListData(&acctsC, listLenC)
if errMsg != nil {
defer C.free(unsafe.Pointer(errMsg))
goMsg := C.GoString(errMsg)
if goMsg == errCredentialsNotFound {
switch goMsg := C.GoString(errMsg); goMsg {
case errCredentialsNotFound:
return make(map[string]string), nil
}
if goMsg == errInteractionNotAllowed {
case errInteractionNotAllowed:
return nil, ErrInteractionNotAllowed
default:
return nil, errors.New(goMsg)
}

return nil, errors.New(goMsg)
}

var listLen int
Expand Down
9 changes: 7 additions & 2 deletions osxkeychain/osxkeychain_darwin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,14 @@ func TestOSXKeychainHelperStoreRetrieve(t *testing.T) {
}

func TestMissingCredentials(t *testing.T) {
const nonExistingCred = "https://adsfasdf.invalid/asdfsdddd"
helper := Osxkeychain{}
_, _, err := helper.Get("https://adsfasdf.wrewerwer.com/asdfsdddd")
_, _, err := helper.Get(nonExistingCred)
if !credentials.IsErrCredentialsNotFound(err) {
t.Fatalf("expected ErrCredentialsNotFound, got %v", err)
t.Errorf("expected ErrCredentialsNotFound, got %v", err)
}
err = helper.Delete(nonExistingCred)
if !credentials.IsErrCredentialsNotFound(err) {
t.Errorf("expected ErrCredentialsNotFound, got %v", err)
}
}

0 comments on commit f09e79d

Please sign in to comment.