From 809c6d6f9771a256ba9a924bde3bddc36d4ce1e5 Mon Sep 17 00:00:00 2001 From: brent s Date: Sun, 21 Nov 2021 14:25:31 -0500 Subject: [PATCH] some prelim work --- .gitignore | 36 ++++++++++ .ref | 1 + collection.go | 189 ++++++++++++++++++++++++++------------------------ errs.go | 10 +++ go.mod | 5 ++ go.sum | 2 + types.go | 16 +++++ 7 files changed, 170 insertions(+), 89 deletions(-) create mode 100644 .gitignore create mode 100644 .ref create mode 100644 errs.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 types.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5f32697 --- /dev/null +++ b/.gitignore @@ -0,0 +1,36 @@ +*.7z +*.bak +*.deb +*.jar +*.rar +*.run +*.sig +*.tar +*.tar.bz2 +*.tar.gz +*.tar.xz +*.tbz +*.tbz2 +*.tgz +*.txz +*.zip +.*.swp +.editix +.idea/ + +# https://github.com/github/gitignore/blob/master/Go.gitignore +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, built with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Dependency directories (remove the comment below to include it) +# vendor/ diff --git a/.ref b/.ref new file mode 100644 index 0000000..ab4db83 --- /dev/null +++ b/.ref @@ -0,0 +1 @@ +https://specifications.freedesktop.org/secret-service/latest/index.html diff --git a/collection.go b/collection.go index a835ae5..ba01775 100644 --- a/collection.go +++ b/collection.go @@ -1,124 +1,135 @@ package libsecret -import "github.com/godbus/dbus" +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) { -type Collection struct { - conn *dbus.Conn - dbus dbus.BusObject + 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 } +// Items returns a slice of Item items in the Collection. +func (collection *Collection) Items() (items []Item, err error) { -func NewCollection(conn *dbus.Conn, path dbus.ObjectPath) *Collection { - return &Collection{ - conn: conn, - dbus: conn.Object(DBusServiceName, path), - } + var variant dbus.Variant + var paths []dbus.ObjectPath + + if variant, err = collection.Dbus.GetProperty("org.freedesktop.Secret.Collection.Items"); err != nil { + return + } + + paths = variant.Value().([]dbus.ObjectPath) + + items = make([]Item, len(paths)) + + for idx, path := range paths { + items[idx] = *NewItem(collection.Conn, path) + } + + return } +// Delete removes a Collection from a Session. +func (collection *Collection) Delete() (err error) { -func (collection Collection) Path() dbus.ObjectPath { - return collection.dbus.Path() + var promptPath dbus.ObjectPath + var prompt *Prompt + + if err = collection.Dbus.Call("org.freedesktop.Secret.Collection.Delete", 0).Store(&prompt); err != nil { + return + } + + if isPrompt(promptPath) { + prompt = NewPrompt(collection.Conn, promptPath) + + if _, err := prompt.Prompt(); err != nil { + return + } + } + + return } - -// READ Array Items; -func (collection *Collection) Items() ([]Item, error) { - val, err := collection.dbus.GetProperty("org.freedesktop.Secret.Collection.Items") - if err != nil { - return []Item{}, err - } - - items := []Item{} - for _, path := range val.Value().([]dbus.ObjectPath) { - items = append(items, *NewItem(collection.conn, path)) - } - - return items, nil -} - - -// Delete (OUT ObjectPath prompt); -func (collection *Collection) Delete() error { - var prompt dbus.ObjectPath - - err := collection.dbus.Call("org.freedesktop.Secret.Collection.Delete", 0).Store(&prompt) - if err != nil { - return err - } - - if isPrompt(prompt) { - prompt := NewPrompt(collection.conn, prompt) - - _, err := prompt.Prompt() - if err != nil { - return err - } - } - - return nil -} - - // SearchItems (IN Dict attributes, OUT Array results); func (collection *Collection) SearchItems(profile string) ([]Item, error) { - attributes := make(map[string]string) - attributes["profile"] = profile + attributes := make(map[string]string) + attributes["profile"] = profile - var paths []dbus.ObjectPath + var paths []dbus.ObjectPath - err := collection.dbus.Call("org.freedesktop.Secret.Collection.SearchItems", 0, attributes).Store(&paths) - if err != nil { - return []Item{}, err - } + err := collection.Dbus.Call("org.freedesktop.Secret.Collection.SearchItems", 0, attributes).Store(&paths) + if err != nil { + return []Item{}, err + } - items := []Item{} - for _, path := range paths { - items = append(items, *NewItem(collection.conn, path)) - } + items := []Item{} + for _, path := range paths { + items = append(items, *NewItem(collection.Conn, path)) + } - return items, nil + return items, nil } - // CreateItem (IN Dict 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) { - properties := make(map[string]dbus.Variant) - attributes := make(map[string]string) + properties := make(map[string]dbus.Variant) + attributes := make(map[string]string) - attributes["profile"] = label - properties["org.freedesktop.Secret.Item.Label"] = dbus.MakeVariant(label) - properties["org.freedesktop.Secret.Item.Attributes"] = dbus.MakeVariant(attributes) + attributes["profile"] = label + properties["org.freedesktop.Secret.Item.Label"] = dbus.MakeVariant(label) + properties["org.freedesktop.Secret.Item.Attributes"] = dbus.MakeVariant(attributes) - var path dbus.ObjectPath - var prompt dbus.ObjectPath + var path dbus.ObjectPath + var prompt dbus.ObjectPath - err := collection.dbus.Call("org.freedesktop.Secret.Collection.CreateItem", 0, properties, secret, replace).Store(&path, &prompt) - if err != nil { - return &Item{}, err - } + err := collection.Dbus.Call("org.freedesktop.Secret.Collection.CreateItem", 0, properties, secret, replace).Store(&path, &prompt) + if err != nil { + return &Item{}, err + } - if isPrompt(prompt) { - prompt := NewPrompt(collection.conn, prompt) + if isPrompt(prompt) { + prompt := NewPrompt(collection.Conn, prompt) - result, err := prompt.Prompt() - if err != nil { - return &Item{}, err - } + result, err := prompt.Prompt() + if err != nil { + return &Item{}, err + } - path = result.Value().(dbus.ObjectPath) - } + path = result.Value().(dbus.ObjectPath) + } - return NewItem(collection.conn, path), nil + return NewItem(collection.Conn, path), nil } - // READ Boolean Locked; func (collection *Collection) Locked() (bool, error) { - val, err := collection.dbus.GetProperty("org.freedesktop.Secret.Collection.Locked") - if err != nil { - return true, err - } + val, err := collection.Dbus.GetProperty("org.freedesktop.Secret.Collection.Locked") + if err != nil { + return true, err + } - return val.Value().(bool), nil + return val.Value().(bool), nil } diff --git a/errs.go b/errs.go new file mode 100644 index 0000000..f42dd9f --- /dev/null +++ b/errs.go @@ -0,0 +1,10 @@ +package libsecret + +import ( + `errors` +) + +var ( + ErrNoDbusConn error = errors.New("no valid dbus connection") + ErrBadDbusPath error = errors.New("invalid dbus path") +) diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..477c7cb --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module r00t2.io/gosecret + +go 1.17 + +require github.com/godbus/dbus v4.1.0+incompatible diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..3a4ab68 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/godbus/dbus v4.1.0+incompatible h1:WqqLRTsQic3apZUK9qC5sGNfXthmPXzUZ7nQPrNITa4= +github.com/godbus/dbus v4.1.0+incompatible/go.mod h1:/YcGZj5zSblfDWMMoOzV4fas9FZnQYTkDnsGvmh2Grw= diff --git a/types.go b/types.go new file mode 100644 index 0000000..fe9fa04 --- /dev/null +++ b/types.go @@ -0,0 +1,16 @@ +package libsecret + +import ( + `github.com/godbus/dbus` +) + +/* + Collection is an accessor for libsecret collections, which contain multiple Secret items. + Reference: + https://developer-old.gnome.org/libsecret/0.18/SecretCollection.html + https://specifications.freedesktop.org/secret-service/latest/ch03.html +*/ +type Collection struct { + Conn *dbus.Conn + Dbus dbus.BusObject +}