-
Notifications
You must be signed in to change notification settings - Fork 209
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support delete all credentials from keyring
- Loading branch information
1 parent
787bc92
commit 90220bb
Showing
6 changed files
with
126 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
//go:build darwin | ||
|
||
package credentials | ||
|
||
import ( | ||
"os/exec" | ||
|
||
"github.com/go-errors/errors" | ||
) | ||
|
||
const execPathKeychain = "/usr/bin/security" | ||
|
||
func deleteAll(service string) error { | ||
if len(service) == 0 { | ||
return errors.New("missing service name") | ||
} | ||
// Delete each secret in a while loop until there is no more left | ||
for { | ||
if err := exec.Command( | ||
execPathKeychain, | ||
"delete-generic-password", | ||
"-s", service, | ||
).Run(); err == nil { | ||
continue | ||
} else if errors.Is(err, exec.ErrNotFound) { | ||
return errors.New(ErrNotSupported) | ||
} else if exitError, ok := err.(*exec.ExitError); ok && exitError.ExitCode() == 44 { | ||
// Exit 44 means no item exists for this service name | ||
return nil | ||
} else { | ||
return errors.Errorf("failed to delete all credentials: %w", err) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
//go:build linux | ||
|
||
package credentials | ||
|
||
import ( | ||
"github.com/go-errors/errors" | ||
ss "github.com/zalando/go-keyring/secret_service" | ||
) | ||
|
||
func deleteAll(service string) error { | ||
svc, err := ss.NewSecretService() | ||
if err != nil { | ||
return errors.Errorf("failed to create secret service: %w", err) | ||
} | ||
|
||
collection := svc.GetLoginCollection() | ||
if err := svc.Unlock(collection.Path()); err != nil { | ||
return errors.Errorf("failed to unlock collection: %w", err) | ||
} | ||
|
||
search := map[string]string{"service": service} | ||
results, err := svc.SearchItems(collection, search) | ||
if err != nil { | ||
return errors.Errorf("failed to search items: %w", err) | ||
} | ||
|
||
for _, item := range results { | ||
if err := svc.Delete(item); err != nil { | ||
return errors.Errorf("failed to delete all credentials: %w", err) | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package credentials | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/zalando/go-keyring" | ||
) | ||
|
||
func TestDeleteAll(t *testing.T) { | ||
service := "test-cli" | ||
// Nothing to delete | ||
err := deleteAll(service) | ||
assert.NoError(t, err) | ||
// Setup 2 items | ||
err = keyring.Set(service, "key1", "value") | ||
assert.NoError(t, err) | ||
err = keyring.Set(service, "key2", "value") | ||
assert.NoError(t, err) | ||
// Delete all items | ||
err = deleteAll(service) | ||
assert.NoError(t, err) | ||
// Check items are gone | ||
_, err = keyring.Get(service, "key1") | ||
assert.ErrorIs(t, err, keyring.ErrNotFound) | ||
_, err = keyring.Get(service, "key2") | ||
assert.ErrorIs(t, err, keyring.ErrNotFound) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
//go:build windows | ||
|
||
package credentials | ||
|
||
import ( | ||
"github.com/danieljoos/wincred" | ||
"github.com/go-errors/errors" | ||
) | ||
|
||
func deleteAll(service string) error { | ||
if err := assertKeyringSupported(); err != nil { | ||
return err | ||
} | ||
creds, err := wincred.FilteredList(service + ":") | ||
if err != nil { | ||
return errors.Errorf("failed to list credentials: %w", err) | ||
} | ||
for _, c := range creds { | ||
gc := wincred.GenericCredential{Credential: *c} | ||
if err := gc.Delete(); err != nil { | ||
return errors.Errorf("failed to delete all credentials: %w", err) | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters