gosecret/collection.go

136 lines
3.3 KiB
Go
Raw Normal View History

2016-10-01 04:13:38 -04:00
package libsecret
2021-11-21 14:25:31 -05:00
import (
"github.com/godbus/dbus"
)
// NewCollection returns a pointer to a new Collection based on a dbus connection and a dbus path for a specific Session.
func NewCollection(conn *dbus.Conn, path dbus.ObjectPath) (c *Collection, err error) {
var dbusNames []string
if conn == nil {
err = ErrNoDbusConn
return
} else if path == "" {
err = ErrBadDbusPath
return
}
// dbus.Conn.Names() will (should) ALWAYS return a slice at LEAST one element in length.
if dbusNames = conn.Names(); dbusNames == nil || len(dbusNames) < 1 {
err = ErrNoDbusConn
return
}
c = &Collection{
Conn: conn,
Dbus: conn.Object(DBusServiceName, path),
}
return
2016-10-01 04:13:38 -04:00
}
2021-11-21 14:25:31 -05:00
// Items returns a slice of Item items in the Collection.
func (collection *Collection) Items() (items []Item, err error) {
2016-10-01 04:13:38 -04:00
2021-11-21 14:25:31 -05:00
var variant dbus.Variant
var paths []dbus.ObjectPath
2016-10-01 04:13:38 -04:00
2021-11-21 14:25:31 -05:00
if variant, err = collection.Dbus.GetProperty("org.freedesktop.Secret.Collection.Items"); err != nil {
return
}
2016-10-01 04:13:38 -04:00
2021-11-21 14:25:31 -05:00
paths = variant.Value().([]dbus.ObjectPath)
2016-10-01 04:13:38 -04:00
2021-11-21 14:25:31 -05:00
items = make([]Item, len(paths))
2016-10-01 04:13:38 -04:00
2021-11-21 14:25:31 -05:00
for idx, path := range paths {
items[idx] = *NewItem(collection.Conn, path)
}
2016-10-01 04:13:38 -04:00
2021-11-21 14:25:31 -05:00
return
2016-10-01 04:13:38 -04:00
}
2021-11-21 14:25:31 -05:00
// Delete removes a Collection from a Session.
func (collection *Collection) Delete() (err error) {
2016-10-01 04:13:38 -04:00
2021-11-21 14:25:31 -05:00
var promptPath dbus.ObjectPath
var prompt *Prompt
2016-10-01 04:13:38 -04:00
2021-11-21 14:25:31 -05:00
if err = collection.Dbus.Call("org.freedesktop.Secret.Collection.Delete", 0).Store(&prompt); err != nil {
return
}
2016-10-01 04:13:38 -04:00
2021-11-21 14:25:31 -05:00
if isPrompt(promptPath) {
prompt = NewPrompt(collection.Conn, promptPath)
2016-10-01 04:13:38 -04:00
2021-11-21 14:25:31 -05:00
if _, err := prompt.Prompt(); err != nil {
return
}
}
2016-10-01 04:13:38 -04:00
2021-11-21 14:25:31 -05:00
return
2016-10-01 04:13:38 -04:00
}
// SearchItems (IN Dict<String,String> attributes, OUT Array<ObjectPath> results);
func (collection *Collection) SearchItems(profile string) ([]Item, error) {
2021-11-21 14:25:31 -05:00
attributes := make(map[string]string)
attributes["profile"] = profile
2016-10-01 04:13:38 -04:00
2021-11-21 14:25:31 -05:00
var paths []dbus.ObjectPath
2016-10-01 04:13:38 -04:00
2021-11-21 14:25:31 -05:00
err := collection.Dbus.Call("org.freedesktop.Secret.Collection.SearchItems", 0, attributes).Store(&paths)
if err != nil {
return []Item{}, err
}
2016-10-01 04:13:38 -04:00
2021-11-21 14:25:31 -05:00
items := []Item{}
for _, path := range paths {
items = append(items, *NewItem(collection.Conn, path))
}
2016-10-01 04:13:38 -04:00
2021-11-21 14:25:31 -05:00
return items, nil
2016-10-01 04:13:38 -04:00
}
// CreateItem (IN Dict<String,Variant> properties, IN Secret secret, IN Boolean replace, OUT ObjectPath item, OUT ObjectPath prompt);
func (collection *Collection) CreateItem(label string, secret *Secret, replace bool) (*Item, error) {
2021-11-21 14:25:31 -05:00
properties := make(map[string]dbus.Variant)
attributes := make(map[string]string)
2016-10-01 04:13:38 -04:00
2021-11-21 14:25:31 -05:00
attributes["profile"] = label
properties["org.freedesktop.Secret.Item.Label"] = dbus.MakeVariant(label)
properties["org.freedesktop.Secret.Item.Attributes"] = dbus.MakeVariant(attributes)
2016-10-01 04:13:38 -04:00
2021-11-21 14:25:31 -05:00
var path dbus.ObjectPath
var prompt dbus.ObjectPath
2016-10-01 04:13:38 -04:00
2021-11-21 14:25:31 -05:00
err := collection.Dbus.Call("org.freedesktop.Secret.Collection.CreateItem", 0, properties, secret, replace).Store(&path, &prompt)
if err != nil {
return &Item{}, err
}
2016-10-01 04:13:38 -04:00
2021-11-21 14:25:31 -05:00
if isPrompt(prompt) {
prompt := NewPrompt(collection.Conn, prompt)
2016-10-01 04:13:38 -04:00
2021-11-21 14:25:31 -05:00
result, err := prompt.Prompt()
if err != nil {
return &Item{}, err
}
2016-10-01 04:13:38 -04:00
2021-11-21 14:25:31 -05:00
path = result.Value().(dbus.ObjectPath)
}
2016-10-01 04:13:38 -04:00
2021-11-21 14:25:31 -05:00
return NewItem(collection.Conn, path), nil
2016-10-01 04:13:38 -04:00
}
// READ Boolean Locked;
func (collection *Collection) Locked() (bool, error) {
2021-11-21 14:25:31 -05:00
val, err := collection.Dbus.GetProperty("org.freedesktop.Secret.Collection.Locked")
if err != nil {
return true, err
}
2016-10-01 04:13:38 -04:00
2021-11-21 14:25:31 -05:00
return val.Value().(bool), nil
2016-10-01 04:13:38 -04:00
}