some prelim work

This commit is contained in:
brent s 2021-11-21 14:25:31 -05:00
parent a6f4afe491
commit 809c6d6f97
Signed by: bts
GPG Key ID: 8C004C2F93481F6B
7 changed files with 170 additions and 89 deletions

36
.gitignore vendored Normal file
View File

@ -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/

1
.ref Normal file
View File

@ -0,0 +1 @@
https://specifications.freedesktop.org/secret-service/latest/index.html

View File

@ -1,124 +1,135 @@
package libsecret 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 { var dbusNames []string
conn *dbus.Conn
dbus dbus.BusObject 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 { var variant dbus.Variant
return &Collection{ var paths []dbus.ObjectPath
conn: conn,
dbus: conn.Object(DBusServiceName, path), 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 { var promptPath dbus.ObjectPath
return collection.dbus.Path() 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<ObjectPath> 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<String,String> attributes, OUT Array<ObjectPath> results); // SearchItems (IN Dict<String,String> attributes, OUT Array<ObjectPath> results);
func (collection *Collection) SearchItems(profile string) ([]Item, error) { func (collection *Collection) SearchItems(profile string) ([]Item, error) {
attributes := make(map[string]string) attributes := make(map[string]string)
attributes["profile"] = profile attributes["profile"] = profile


var paths []dbus.ObjectPath var paths []dbus.ObjectPath


err := collection.dbus.Call("org.freedesktop.Secret.Collection.SearchItems", 0, attributes).Store(&paths) err := collection.Dbus.Call("org.freedesktop.Secret.Collection.SearchItems", 0, attributes).Store(&paths)
if err != nil { if err != nil {
return []Item{}, err return []Item{}, err
} }


items := []Item{} items := []Item{}
for _, path := range paths { for _, path := range paths {
items = append(items, *NewItem(collection.conn, path)) items = append(items, *NewItem(collection.Conn, path))
} }


return items, nil return items, nil
} }



// CreateItem (IN Dict<String,Variant> properties, IN Secret secret, IN Boolean replace, OUT ObjectPath item, OUT ObjectPath prompt); // 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) { func (collection *Collection) CreateItem(label string, secret *Secret, replace bool) (*Item, error) {
properties := make(map[string]dbus.Variant) properties := make(map[string]dbus.Variant)
attributes := make(map[string]string) attributes := make(map[string]string)


attributes["profile"] = label attributes["profile"] = label
properties["org.freedesktop.Secret.Item.Label"] = dbus.MakeVariant(label) properties["org.freedesktop.Secret.Item.Label"] = dbus.MakeVariant(label)
properties["org.freedesktop.Secret.Item.Attributes"] = dbus.MakeVariant(attributes) properties["org.freedesktop.Secret.Item.Attributes"] = dbus.MakeVariant(attributes)


var path dbus.ObjectPath var path dbus.ObjectPath
var prompt dbus.ObjectPath var prompt dbus.ObjectPath


err := collection.dbus.Call("org.freedesktop.Secret.Collection.CreateItem", 0, properties, secret, replace).Store(&path, &prompt) err := collection.Dbus.Call("org.freedesktop.Secret.Collection.CreateItem", 0, properties, secret, replace).Store(&path, &prompt)
if err != nil { if err != nil {
return &Item{}, err return &Item{}, err
} }


if isPrompt(prompt) { if isPrompt(prompt) {
prompt := NewPrompt(collection.conn, prompt) prompt := NewPrompt(collection.Conn, prompt)


result, err := prompt.Prompt() result, err := prompt.Prompt()
if err != nil { if err != nil {
return &Item{}, err 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; // READ Boolean Locked;
func (collection *Collection) Locked() (bool, error) { func (collection *Collection) Locked() (bool, error) {
val, err := collection.dbus.GetProperty("org.freedesktop.Secret.Collection.Locked") val, err := collection.Dbus.GetProperty("org.freedesktop.Secret.Collection.Locked")
if err != nil { if err != nil {
return true, err return true, err
} }


return val.Value().(bool), nil return val.Value().(bool), nil
} }

10
errs.go Normal file
View File

@ -0,0 +1,10 @@
package libsecret

import (
`errors`
)

var (
ErrNoDbusConn error = errors.New("no valid dbus connection")
ErrBadDbusPath error = errors.New("invalid dbus path")
)

5
go.mod Normal file
View File

@ -0,0 +1,5 @@
module r00t2.io/gosecret

go 1.17

require github.com/godbus/dbus v4.1.0+incompatible

2
go.sum Normal file
View File

@ -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=

16
types.go Normal file
View File

@ -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
}