Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
bb85cb8b52
|
|||
|
9d3299c9dc
|
|||
|
3f4db5e82c
|
|||
|
fa3061ee7a
|
@@ -65,6 +65,7 @@ func NewCollection(service *Service, path dbus.ObjectPath) (coll *Collection, er
|
|||||||
*/
|
*/
|
||||||
func (c *Collection) CreateItem(label string, attrs map[string]string, secret *Secret, replace bool, itemType ...string) (item *Item, err error) {
|
func (c *Collection) CreateItem(label string, attrs map[string]string, secret *Secret, replace bool, itemType ...string) (item *Item, err error) {
|
||||||
|
|
||||||
|
var call *dbus.Call
|
||||||
var prompt *Prompt
|
var prompt *Prompt
|
||||||
var path dbus.ObjectPath
|
var path dbus.ObjectPath
|
||||||
var promptPath dbus.ObjectPath
|
var promptPath dbus.ObjectPath
|
||||||
@@ -84,9 +85,13 @@ func (c *Collection) CreateItem(label string, attrs map[string]string, secret *S
|
|||||||
props[DbusItemCreated] = dbus.MakeVariant(uint64(time.Now().Unix()))
|
props[DbusItemCreated] = dbus.MakeVariant(uint64(time.Now().Unix()))
|
||||||
// props[DbusItemModified] = dbus.MakeVariant(uint64(time.Now().Unix()))
|
// props[DbusItemModified] = dbus.MakeVariant(uint64(time.Now().Unix()))
|
||||||
|
|
||||||
if err = c.Dbus.Call(
|
if call = c.Dbus.Call(
|
||||||
DbusCollectionCreateItem, 0, props, secret, replace,
|
DbusCollectionCreateItem, 0, props, secret, replace,
|
||||||
).Store(&path, &promptPath); err != nil {
|
); call.Err != nil {
|
||||||
|
err = call.Err
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err = call.Store(&path, &promptPath); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -114,10 +119,17 @@ func (c *Collection) CreateItem(label string, attrs map[string]string, secret *S
|
|||||||
*/
|
*/
|
||||||
func (c *Collection) Delete() (err error) {
|
func (c *Collection) Delete() (err error) {
|
||||||
|
|
||||||
|
var call *dbus.Call
|
||||||
var promptPath dbus.ObjectPath
|
var promptPath dbus.ObjectPath
|
||||||
var prompt *Prompt
|
var prompt *Prompt
|
||||||
|
|
||||||
if err = c.Dbus.Call(DbusCollectionDelete, 0).Store(&promptPath); err != nil {
|
if call = c.Dbus.Call(
|
||||||
|
DbusCollectionDelete, 0,
|
||||||
|
); call.Err != nil {
|
||||||
|
err = call.Err
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err = call.Store(&promptPath); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -146,15 +158,16 @@ func (c *Collection) Items() (items []*Item, err error) {
|
|||||||
|
|
||||||
paths = variant.Value().([]dbus.ObjectPath)
|
paths = variant.Value().([]dbus.ObjectPath)
|
||||||
|
|
||||||
items = make([]*Item, len(paths))
|
items = make([]*Item, 0)
|
||||||
|
|
||||||
for idx, path := range paths {
|
for _, path := range paths {
|
||||||
|
item = nil
|
||||||
if item, err = NewItem(c, path); err != nil {
|
if item, err = NewItem(c, path); err != nil {
|
||||||
errs = append(errs, err)
|
errs = append(errs, err)
|
||||||
err = nil
|
err = nil
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
items[idx] = item
|
items = append(items, item)
|
||||||
}
|
}
|
||||||
err = NewErrors(err)
|
err = NewErrors(err)
|
||||||
|
|
||||||
@@ -242,26 +255,34 @@ func (c *Collection) Relabel(newLabel string) (err error) {
|
|||||||
*/
|
*/
|
||||||
func (c *Collection) SearchItems(profile string) (items []*Item, err error) {
|
func (c *Collection) SearchItems(profile string) (items []*Item, err error) {
|
||||||
|
|
||||||
|
var call *dbus.Call
|
||||||
var paths []dbus.ObjectPath
|
var paths []dbus.ObjectPath
|
||||||
var errs []error = make([]error, 0)
|
var errs []error = make([]error, 0)
|
||||||
var attrs map[string]string = make(map[string]string, 0)
|
var attrs map[string]string = make(map[string]string, 0)
|
||||||
|
var item *Item
|
||||||
|
|
||||||
attrs["profile"] = profile
|
attrs["profile"] = profile
|
||||||
|
|
||||||
if err = c.Dbus.Call(
|
if call = c.Dbus.Call(
|
||||||
DbusCollectionSearchItems, 0, attrs,
|
DbusCollectionSearchItems, 0, attrs,
|
||||||
).Store(&paths); err != nil {
|
); call.Err != nil {
|
||||||
|
err = call.Err
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err = call.Store(&paths); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
items = make([]*Item, len(paths))
|
items = make([]*Item, 0)
|
||||||
|
|
||||||
for idx, path := range paths {
|
for _, path := range paths {
|
||||||
if items[idx], err = NewItem(c, path); err != nil {
|
item = nil
|
||||||
|
if item, err = NewItem(c, path); err != nil {
|
||||||
errs = append(errs, err)
|
errs = append(errs, err)
|
||||||
err = nil
|
err = nil
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
items = append(items, item)
|
||||||
}
|
}
|
||||||
err = NewErrors(err)
|
err = NewErrors(err)
|
||||||
|
|
||||||
@@ -273,11 +294,10 @@ func (c *Collection) SetAlias(alias string) (err error) {
|
|||||||
|
|
||||||
var call *dbus.Call
|
var call *dbus.Call
|
||||||
|
|
||||||
call = c.service.Dbus.Call(
|
if call = c.service.Dbus.Call(
|
||||||
DbusServiceSetAlias, 0, alias, c.Dbus.Path(),
|
DbusServiceSetAlias, 0, alias, c.Dbus.Path(),
|
||||||
)
|
); call.Err != nil {
|
||||||
|
err = call.Err
|
||||||
if err = call.Err; err != nil {
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
package gosecret
|
package gosecret
|
||||||
|
|
||||||
import (
|
import (
|
||||||
`testing`
|
"testing"
|
||||||
|
|
||||||
`github.com/godbus/dbus/v5`
|
"github.com/godbus/dbus/v5"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Some functions are covered in the Service tests.
|
// Some functions are covered in the Service tests.
|
||||||
@@ -57,7 +57,8 @@ func TestCollection_Items(t *testing.T) {
|
|||||||
var collection *Collection
|
var collection *Collection
|
||||||
var items []*Item
|
var items []*Item
|
||||||
var item *Item
|
var item *Item
|
||||||
var searchItemResults []*Item
|
var searchResultsUnlocked []*Item
|
||||||
|
var searchResultsLocked []*Item
|
||||||
var secret *Secret
|
var secret *Secret
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
@@ -109,12 +110,12 @@ func TestCollection_Items(t *testing.T) {
|
|||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
if searchItemResults, err = collection.SearchItems(testItemLabel); err != nil {
|
if searchResultsUnlocked, searchResultsLocked, err = collection.service.SearchItems(itemAttrs); err != nil {
|
||||||
t.Errorf("failed to find item '%v' via Collection.SearchItems: %v", string(item.Dbus.Path()), err.Error())
|
t.Errorf("failed to find item '%v' via Collection.SearchItems: %v", string(item.Dbus.Path()), err.Error())
|
||||||
} else if len(searchItemResults) == 0 {
|
} else if (len(searchResultsLocked) + len(searchResultsUnlocked)) == 0 {
|
||||||
t.Errorf("failed to find item '%v' via Collection.SearchItems, returned 0 results (should be at least 1)", testItemLabel)
|
t.Errorf("failed to find item '%v' via Collection.SearchItems, returned 0 results (should be at least 1)", testItemLabel)
|
||||||
} else {
|
} else {
|
||||||
t.Logf("found %v results for Collection.SearchItems", len(searchItemResults))
|
t.Logf("found %v results for Collection.SearchItems", len(searchResultsUnlocked)+len(searchResultsLocked))
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = item.Delete(); err != nil {
|
if err = item.Delete(); err != nil {
|
||||||
@@ -148,6 +149,7 @@ func TestCollection_Label(t *testing.T) {
|
|||||||
t.Fatalf("NewService failed: %v", err.Error())
|
t.Fatalf("NewService failed: %v", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
t.Logf("Attempting to get label of collection: %v", defaultCollectionLabel)
|
||||||
if collection, err = svc.GetCollection(defaultCollectionLabel); err != nil {
|
if collection, err = svc.GetCollection(defaultCollectionLabel); err != nil {
|
||||||
t.Errorf(
|
t.Errorf(
|
||||||
"failed when fetching collection '%v': %v",
|
"failed when fetching collection '%v': %v",
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package gosecret
|
package gosecret
|
||||||
|
|
||||||
import (
|
import (
|
||||||
`github.com/godbus/dbus/v5`
|
"github.com/godbus/dbus/v5"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Constants for use with gosecret.
|
// Constants for use with gosecret.
|
||||||
@@ -30,6 +30,7 @@ const (
|
|||||||
|
|
||||||
// Libsecret/SecretService special values.
|
// Libsecret/SecretService special values.
|
||||||
var (
|
var (
|
||||||
|
// DbusRemoveAliasPath is used to remove an alias from a Collection and/or Item.
|
||||||
DbusRemoveAliasPath dbus.ObjectPath = dbus.ObjectPath("/")
|
DbusRemoveAliasPath dbus.ObjectPath = dbus.ObjectPath("/")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -106,10 +106,17 @@ func (i *Item) ChangeItemType(newItemType string) (err error) {
|
|||||||
// Delete removes an Item from a Collection.
|
// Delete removes an Item from a Collection.
|
||||||
func (i *Item) Delete() (err error) {
|
func (i *Item) Delete() (err error) {
|
||||||
|
|
||||||
|
var call *dbus.Call
|
||||||
var promptPath dbus.ObjectPath
|
var promptPath dbus.ObjectPath
|
||||||
var prompt *Prompt
|
var prompt *Prompt
|
||||||
|
|
||||||
if err = i.Dbus.Call(DbusItemDelete, 0).Store(&promptPath); err != nil {
|
if call = i.Dbus.Call(
|
||||||
|
DbusItemDelete, 0,
|
||||||
|
); call.Err != nil {
|
||||||
|
err = call.Err
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err = call.Store(&promptPath); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -127,6 +134,8 @@ func (i *Item) Delete() (err error) {
|
|||||||
// GetSecret returns the Secret in an Item using a Session.
|
// GetSecret returns the Secret in an Item using a Session.
|
||||||
func (i *Item) GetSecret(session *Session) (secret *Secret, err error) {
|
func (i *Item) GetSecret(session *Session) (secret *Secret, err error) {
|
||||||
|
|
||||||
|
var call *dbus.Call
|
||||||
|
|
||||||
if session == nil {
|
if session == nil {
|
||||||
err = ErrNoDbusConn
|
err = ErrNoDbusConn
|
||||||
}
|
}
|
||||||
@@ -135,9 +144,13 @@ func (i *Item) GetSecret(session *Session) (secret *Secret, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = i.Dbus.Call(
|
if call = i.Dbus.Call(
|
||||||
DbusItemGetSecret, 0, session.Dbus.Path(),
|
DbusItemGetSecret, 0, session.Dbus.Path(),
|
||||||
).Store(&secret); err != nil {
|
); call.Err != nil {
|
||||||
|
err = call.Err
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err = call.Store(&secret); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -246,15 +259,15 @@ func (i *Item) ReplaceAttributes(newAttrs map[string]string) (err error) {
|
|||||||
// SetSecret sets the Secret for an Item.
|
// SetSecret sets the Secret for an Item.
|
||||||
func (i *Item) SetSecret(secret *Secret) (err error) {
|
func (i *Item) SetSecret(secret *Secret) (err error) {
|
||||||
|
|
||||||
var c *dbus.Call
|
var call *dbus.Call
|
||||||
|
|
||||||
c = i.Dbus.Call(
|
if call = i.Dbus.Call(
|
||||||
DbusItemSetSecret, 0,
|
DbusItemSetSecret, 0,
|
||||||
)
|
); call.Err != nil {
|
||||||
if c.Err != nil {
|
err = call.Err
|
||||||
err = c.Err
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
i.Secret = secret
|
i.Secret = secret
|
||||||
|
|
||||||
if _, _, err = i.Modified(); err != nil {
|
if _, _, err = i.Modified(); err != nil {
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package gosecret
|
package gosecret
|
||||||
|
|
||||||
import (
|
import (
|
||||||
`fmt`
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -35,6 +35,7 @@ func NewErrors(errs ...error) (err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Error makes a MultiError conform to the error interface.
|
||||||
func (e *MultiError) Error() (errStr string) {
|
func (e *MultiError) Error() (errStr string) {
|
||||||
|
|
||||||
var numErrs int
|
var numErrs int
|
||||||
@@ -47,7 +48,7 @@ func (e *MultiError) Error() (errStr string) {
|
|||||||
|
|
||||||
for idx, err := range e.Errors {
|
for idx, err := range e.Errors {
|
||||||
if (idx + 1) < numErrs {
|
if (idx + 1) < numErrs {
|
||||||
errStr += fmt.Sprintf(err.Error(), e.ErrorSep)
|
errStr += fmt.Sprintf("%v%v", err.Error(), e.ErrorSep)
|
||||||
} else {
|
} else {
|
||||||
errStr += err.Error()
|
errStr += err.Error()
|
||||||
}
|
}
|
||||||
|
|||||||
108
service_funcs.go
108
service_funcs.go
@@ -5,7 +5,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
`time`
|
"time"
|
||||||
|
|
||||||
"github.com/godbus/dbus/v5"
|
"github.com/godbus/dbus/v5"
|
||||||
)
|
)
|
||||||
@@ -38,7 +38,13 @@ func NewService() (service *Service, err error) {
|
|||||||
// Close cleanly closes a Service and all its underlying connections (e.g. Service.Session).
|
// Close cleanly closes a Service and all its underlying connections (e.g. Service.Session).
|
||||||
func (s *Service) Close() (err error) {
|
func (s *Service) Close() (err error) {
|
||||||
|
|
||||||
err = s.Session.Close()
|
if err = s.Session.Close(); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = s.Conn.Close(); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -57,16 +63,17 @@ func (s *Service) Collections() (collections []*Collection, err error) {
|
|||||||
|
|
||||||
paths = variant.Value().([]dbus.ObjectPath)
|
paths = variant.Value().([]dbus.ObjectPath)
|
||||||
|
|
||||||
collections = make([]*Collection, len(paths))
|
collections = make([]*Collection, 0)
|
||||||
|
|
||||||
for idx, path := range paths {
|
for _, path := range paths {
|
||||||
|
coll = nil
|
||||||
if coll, err = NewCollection(s, path); err != nil {
|
if coll, err = NewCollection(s, path); err != nil {
|
||||||
// return
|
// return
|
||||||
errs = append(errs, err)
|
errs = append(errs, err)
|
||||||
err = nil
|
err = nil
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
collections[idx] = coll
|
collections = append(collections, coll)
|
||||||
}
|
}
|
||||||
err = NewErrors(err)
|
err = NewErrors(err)
|
||||||
|
|
||||||
@@ -79,6 +86,7 @@ func (s *Service) Collections() (collections []*Collection, err error) {
|
|||||||
*/
|
*/
|
||||||
func (s *Service) CreateAliasedCollection(label, alias string) (collection *Collection, err error) {
|
func (s *Service) CreateAliasedCollection(label, alias string) (collection *Collection, err error) {
|
||||||
|
|
||||||
|
var call *dbus.Call
|
||||||
var variant *dbus.Variant
|
var variant *dbus.Variant
|
||||||
var path dbus.ObjectPath
|
var path dbus.ObjectPath
|
||||||
var promptPath dbus.ObjectPath
|
var promptPath dbus.ObjectPath
|
||||||
@@ -89,9 +97,13 @@ func (s *Service) CreateAliasedCollection(label, alias string) (collection *Coll
|
|||||||
props[DbusCollectionCreated] = dbus.MakeVariant(uint64(time.Now().Unix()))
|
props[DbusCollectionCreated] = dbus.MakeVariant(uint64(time.Now().Unix()))
|
||||||
props[DbusCollectionModified] = dbus.MakeVariant(uint64(time.Now().Unix()))
|
props[DbusCollectionModified] = dbus.MakeVariant(uint64(time.Now().Unix()))
|
||||||
|
|
||||||
if err = s.Dbus.Call(
|
if call = s.Dbus.Call(
|
||||||
DbusServiceCreateCollection, 0, props, alias,
|
DbusServiceCreateCollection, 0, props, alias,
|
||||||
).Store(&path, &promptPath); err != nil {
|
); call.Err != nil {
|
||||||
|
err = call.Err
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err = call.Store(&path, &promptPath); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -196,6 +208,7 @@ func (s *Service) GetSecrets(itemPaths ...dbus.ObjectPath) (secrets map[dbus.Obj
|
|||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
var results map[dbus.ObjectPath][]interface{}
|
var results map[dbus.ObjectPath][]interface{}
|
||||||
|
var call *dbus.Call
|
||||||
|
|
||||||
if itemPaths == nil || len(itemPaths) == 0 {
|
if itemPaths == nil || len(itemPaths) == 0 {
|
||||||
err = ErrMissingPaths
|
err = ErrMissingPaths
|
||||||
@@ -206,9 +219,13 @@ func (s *Service) GetSecrets(itemPaths ...dbus.ObjectPath) (secrets map[dbus.Obj
|
|||||||
results = make(map[dbus.ObjectPath][]interface{}, len(itemPaths))
|
results = make(map[dbus.ObjectPath][]interface{}, len(itemPaths))
|
||||||
|
|
||||||
// TODO: trigger a Service.Unlock for any locked items?
|
// TODO: trigger a Service.Unlock for any locked items?
|
||||||
if err = s.Dbus.Call(
|
if call = s.Dbus.Call(
|
||||||
DbusServiceGetSecrets, 0, itemPaths, s.Session.Dbus.Path(),
|
DbusServiceGetSecrets, 0, itemPaths, s.Session.Dbus.Path(),
|
||||||
).Store(&results); err != nil {
|
); call.Err != nil {
|
||||||
|
err = call.Err
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err = call.Store(&results); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -235,6 +252,7 @@ func (s *Service) GetSession() (ssn *Session, err error) {
|
|||||||
// Lock locks an Unlocked Collection or Item (LockableObject).
|
// Lock locks an Unlocked Collection or Item (LockableObject).
|
||||||
func (s *Service) Lock(objects ...LockableObject) (err error) {
|
func (s *Service) Lock(objects ...LockableObject) (err error) {
|
||||||
|
|
||||||
|
var call *dbus.Call
|
||||||
var toLock []dbus.ObjectPath
|
var toLock []dbus.ObjectPath
|
||||||
// We only use these as destinations.
|
// We only use these as destinations.
|
||||||
var locked []dbus.ObjectPath
|
var locked []dbus.ObjectPath
|
||||||
@@ -252,9 +270,13 @@ func (s *Service) Lock(objects ...LockableObject) (err error) {
|
|||||||
toLock[idx] = o.path()
|
toLock[idx] = o.path()
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = s.Dbus.Call(
|
if call = s.Dbus.Call(
|
||||||
DbusServiceLock, 0, toLock,
|
DbusServiceLock, 0, toLock,
|
||||||
).Store(&locked, &promptPath); err != nil {
|
); call.Err != nil {
|
||||||
|
err = call.Err
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err = call.Store(&locked, &promptPath); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -284,6 +306,7 @@ func (s *Service) Lock(objects ...LockableObject) (err error) {
|
|||||||
*/
|
*/
|
||||||
func (s *Service) OpenSession(algo, input string) (session *Session, output dbus.Variant, err error) {
|
func (s *Service) OpenSession(algo, input string) (session *Session, output dbus.Variant, err error) {
|
||||||
|
|
||||||
|
var call *dbus.Call
|
||||||
var path dbus.ObjectPath
|
var path dbus.ObjectPath
|
||||||
var inputVariant dbus.Variant
|
var inputVariant dbus.Variant
|
||||||
|
|
||||||
@@ -297,9 +320,13 @@ func (s *Service) OpenSession(algo, input string) (session *Session, output dbus
|
|||||||
// TODO: confirm this.
|
// TODO: confirm this.
|
||||||
// Possible flags are dbus.Flags consts: https://pkg.go.dev/github.com/godbus/dbus#Flags
|
// Possible flags are dbus.Flags consts: https://pkg.go.dev/github.com/godbus/dbus#Flags
|
||||||
// Oddly, there is no "None" flag. So it's explicitly specified as a null byte.
|
// Oddly, there is no "None" flag. So it's explicitly specified as a null byte.
|
||||||
if err = s.Dbus.Call(
|
if call = s.Dbus.Call(
|
||||||
DbusServiceOpenSession, 0, algo, inputVariant,
|
DbusServiceOpenSession, 0, algo, inputVariant,
|
||||||
).Store(&output, &path); err != nil {
|
); call.Err != nil {
|
||||||
|
err = call.Err
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err = call.Store(&output, &path); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -315,17 +342,20 @@ func (s *Service) OpenSession(algo, input string) (session *Session, output dbus
|
|||||||
*/
|
*/
|
||||||
func (s *Service) ReadAlias(alias string) (collection *Collection, err error) {
|
func (s *Service) ReadAlias(alias string) (collection *Collection, err error) {
|
||||||
|
|
||||||
|
var call *dbus.Call
|
||||||
var objectPath dbus.ObjectPath
|
var objectPath dbus.ObjectPath
|
||||||
|
|
||||||
err = s.Dbus.Call(
|
if call = s.Dbus.Call(
|
||||||
DbusServiceReadAlias, 0, alias,
|
DbusServiceReadAlias, 0, alias,
|
||||||
).Store(&objectPath)
|
); call.Err != nil {
|
||||||
|
err = call.Err
|
||||||
|
return
|
||||||
|
}
|
||||||
/*
|
/*
|
||||||
TODO: Confirm that a nonexistent alias will NOT cause an error to return.
|
TODO: Confirm that a nonexistent alias will NOT cause an error to return.
|
||||||
If it does, alter the below logic.
|
If it does, alter the below logic.
|
||||||
*/
|
*/
|
||||||
if err != nil {
|
if err = call.Store(&objectPath); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -357,6 +387,7 @@ func (s *Service) RemoveAlias(alias string) (err error) {
|
|||||||
*/
|
*/
|
||||||
func (s *Service) SearchItems(attributes map[string]string) (unlockedItems []*Item, lockedItems []*Item, err error) {
|
func (s *Service) SearchItems(attributes map[string]string) (unlockedItems []*Item, lockedItems []*Item, err error) {
|
||||||
|
|
||||||
|
var call *dbus.Call
|
||||||
var locked []dbus.ObjectPath
|
var locked []dbus.ObjectPath
|
||||||
var unlocked []dbus.ObjectPath
|
var unlocked []dbus.ObjectPath
|
||||||
var collectionObjs []*Collection
|
var collectionObjs []*Collection
|
||||||
@@ -364,6 +395,7 @@ func (s *Service) SearchItems(attributes map[string]string) (unlockedItems []*It
|
|||||||
var ok bool
|
var ok bool
|
||||||
var c *Collection
|
var c *Collection
|
||||||
var cPath dbus.ObjectPath
|
var cPath dbus.ObjectPath
|
||||||
|
var item *Item
|
||||||
var errs []error = make([]error, 0)
|
var errs []error = make([]error, 0)
|
||||||
|
|
||||||
if attributes == nil || len(attributes) == 0 {
|
if attributes == nil || len(attributes) == 0 {
|
||||||
@@ -371,12 +403,16 @@ func (s *Service) SearchItems(attributes map[string]string) (unlockedItems []*It
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = s.Dbus.Call(
|
if call = s.Dbus.Call(
|
||||||
DbusServiceSearchItems, 0, attributes,
|
DbusServiceSearchItems, 0, attributes,
|
||||||
).Store(&unlocked, &locked)
|
); call.Err != nil {
|
||||||
|
}
|
||||||
|
if err = call.Store(&unlocked, &locked); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
lockedItems = make([]*Item, len(locked))
|
lockedItems = make([]*Item, 0)
|
||||||
unlockedItems = make([]*Item, len(unlocked))
|
unlockedItems = make([]*Item, 0)
|
||||||
|
|
||||||
if collectionObjs, err = s.Collections(); err != nil {
|
if collectionObjs, err = s.Collections(); err != nil {
|
||||||
return
|
return
|
||||||
@@ -389,8 +425,9 @@ func (s *Service) SearchItems(attributes map[string]string) (unlockedItems []*It
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Locked items
|
// Locked items
|
||||||
for idx, i := range locked {
|
for _, i := range locked {
|
||||||
|
|
||||||
|
item = nil
|
||||||
cPath = dbus.ObjectPath(filepath.Dir(string(i)))
|
cPath = dbus.ObjectPath(filepath.Dir(string(i)))
|
||||||
|
|
||||||
if c, ok = collections[cPath]; !ok {
|
if c, ok = collections[cPath]; !ok {
|
||||||
@@ -400,18 +437,20 @@ func (s *Service) SearchItems(attributes map[string]string) (unlockedItems []*It
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if lockedItems[idx], err = NewItem(c, i); err != nil {
|
if item, err = NewItem(c, i); err != nil {
|
||||||
errs = append(errs, errors.New(fmt.Sprintf(
|
errs = append(errs, errors.New(fmt.Sprintf(
|
||||||
"could not create Item for locked item %v", string(i),
|
"could not create Item for locked item %v", string(i),
|
||||||
)))
|
)))
|
||||||
err = nil
|
err = nil
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
lockedItems = append(lockedItems, item)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unlocked items
|
// Unlocked items
|
||||||
for idx, i := range unlocked {
|
for _, i := range unlocked {
|
||||||
|
|
||||||
|
item = nil
|
||||||
cPath = dbus.ObjectPath(filepath.Dir(string(i)))
|
cPath = dbus.ObjectPath(filepath.Dir(string(i)))
|
||||||
|
|
||||||
if c, ok = collections[cPath]; !ok {
|
if c, ok = collections[cPath]; !ok {
|
||||||
@@ -421,13 +460,14 @@ func (s *Service) SearchItems(attributes map[string]string) (unlockedItems []*It
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if unlockedItems[idx], err = NewItem(c, i); err != nil {
|
if item, err = NewItem(c, i); err != nil {
|
||||||
errs = append(errs, errors.New(fmt.Sprintf(
|
errs = append(errs, errors.New(fmt.Sprintf(
|
||||||
"could not create Item for unlocked item %v", string(i),
|
"could not create Item for unlocked item %v", string(i),
|
||||||
)))
|
)))
|
||||||
err = nil
|
err = nil
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
unlockedItems = append(unlockedItems, item)
|
||||||
}
|
}
|
||||||
|
|
||||||
if errs != nil && len(errs) > 0 {
|
if errs != nil && len(errs) > 0 {
|
||||||
@@ -444,18 +484,17 @@ func (s *Service) SearchItems(attributes map[string]string) (unlockedItems []*It
|
|||||||
*/
|
*/
|
||||||
func (s *Service) SetAlias(alias string, objectPath dbus.ObjectPath) (err error) {
|
func (s *Service) SetAlias(alias string, objectPath dbus.ObjectPath) (err error) {
|
||||||
|
|
||||||
var c *dbus.Call
|
var call *dbus.Call
|
||||||
var collection *Collection
|
var collection *Collection
|
||||||
|
|
||||||
if collection, err = s.GetCollection(alias); err != nil {
|
if collection, err = s.GetCollection(alias); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
c = s.Dbus.Call(
|
if call = s.Dbus.Call(
|
||||||
DbusServiceSetAlias, 0, alias, objectPath,
|
DbusServiceSetAlias, 0, alias, objectPath,
|
||||||
)
|
); call.Err != nil {
|
||||||
|
err = call.Err
|
||||||
if err = c.Err; err != nil {
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -471,6 +510,7 @@ func (s *Service) SetAlias(alias string, objectPath dbus.ObjectPath) (err error)
|
|||||||
// Unlock unlocks a locked Collection or Item (LockableObject).
|
// Unlock unlocks a locked Collection or Item (LockableObject).
|
||||||
func (s *Service) Unlock(objects ...LockableObject) (err error) {
|
func (s *Service) Unlock(objects ...LockableObject) (err error) {
|
||||||
|
|
||||||
|
var call *dbus.Call
|
||||||
var toUnlock []dbus.ObjectPath
|
var toUnlock []dbus.ObjectPath
|
||||||
// We only use these as destinations.
|
// We only use these as destinations.
|
||||||
var unlocked []dbus.ObjectPath
|
var unlocked []dbus.ObjectPath
|
||||||
@@ -488,9 +528,13 @@ func (s *Service) Unlock(objects ...LockableObject) (err error) {
|
|||||||
toUnlock[idx] = o.path()
|
toUnlock[idx] = o.path()
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = s.Dbus.Call(
|
if call = s.Dbus.Call(
|
||||||
DbusServiceUnlock, 0, toUnlock,
|
DbusServiceUnlock, 0, toUnlock,
|
||||||
).Store(&unlocked, &resultPath); err != nil {
|
); call.Err != nil {
|
||||||
|
err = call.Err
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err = call.Store(&unlocked, &resultPath); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
package gosecret
|
package gosecret
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"github.com/godbus/dbus/v5"
|
"github.com/godbus/dbus/v5"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -32,13 +34,21 @@ func NewSession(service *Service, path dbus.ObjectPath) (session *Session, err e
|
|||||||
// Close cleanly closes a Session.
|
// Close cleanly closes a Session.
|
||||||
func (s *Session) Close() (err error) {
|
func (s *Session) Close() (err error) {
|
||||||
|
|
||||||
var c *dbus.Call
|
var call *dbus.Call
|
||||||
|
|
||||||
c = s.Dbus.Call(
|
if call = s.Dbus.Call(
|
||||||
DbusSessionClose, 0,
|
DbusSessionClose, 0,
|
||||||
)
|
); call.Err != nil {
|
||||||
|
/*
|
||||||
_ = c
|
I... still haven't 100% figured out why this happens, but the session DOES seem to close...?
|
||||||
|
PRs or input welcome.
|
||||||
|
TODO: figure out why this error gets triggered.
|
||||||
|
*/
|
||||||
|
if call.Err.Error() != fmt.Sprintf("The name %v was not provided by any .service files", DbusInterfaceSession) {
|
||||||
|
err = call.Err
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user