collection, service, and session should be done

This commit is contained in:
2021-12-04 19:38:26 -05:00
parent c0a1e4a281
commit 1d093627f6
8 changed files with 176 additions and 203 deletions

View File

@@ -1,50 +1,40 @@
package gosecret
import (
"strings"
`strings`
"time"
"github.com/godbus/dbus"
`github.com/godbus/dbus`
)
/*
CreateCollection creates a new Collection named `name` using the dbus.BusObject `secretServiceConn`.
`secretServiceConn` should be the same as used for Collection.Dbus (and/or NewCollection).
It will be called by NewCollection if the Collection does not exist in Dbus.
Generally speaking, you should probably not use this function directly and instead use NewCollection.
NewCollection returns a pointer to a Collection based on a Service and a Dbus path.
You will almost always want to use Service.GetCollection instead.
*/
func CreateCollection(secretServiceConn *dbus.BusObject, name string) (c *Collection, err error) {
func NewCollection(service *Service, path dbus.ObjectPath) (coll *Collection, err error) {
var path dbus.ObjectPath
var splitPath []string
if secretServiceConn == nil {
if service == nil {
err = ErrNoDbusConn
return
}
path = dbus.ObjectPath(strings.Join([]string{DbusPath, ""}, "/"))
// TODO.
return
}
// NewCollection returns a pointer to a new Collection based on a Dbus connection and a Dbus path.
func NewCollection(conn *dbus.Conn, path dbus.ObjectPath) (coll *Collection, err error) {
if _, err = validConnPath(conn, path); err != nil {
if _, err = validConnPath(service.Conn, path); err != nil {
return
}
coll = &Collection{
DbusObject: &DbusObject{
Conn: conn,
Dbus: conn.Object(DbusService, path),
Conn: service.Conn,
Dbus: service.Conn.Object(DbusService, path),
},
// lastModified: time.Now(),
}
splitPath = strings.Split(string(coll.Dbus.Path()), "")
coll.name = splitPath[len(splitPath)-1]
_, _, err = coll.Modified()
return
@@ -54,16 +44,28 @@ func NewCollection(conn *dbus.Conn, path dbus.ObjectPath) (coll *Collection, err
func (c *Collection) Items() (items []*Item, err error) {
var paths []dbus.ObjectPath
var item *Item
var variant dbus.Variant
var errs []error = make([]error, 0)
if paths, err = pathsFromPath(c.Dbus, DbusCollectionItems); err != nil {
if variant, err = c.Dbus.GetProperty(DbusCollectionItems); err != nil {
return
}
paths = variant.Value().([]dbus.ObjectPath)
items = make([]*Item, len(paths))
for idx, path := range paths {
items[idx] = NewItem(c.Conn, path)
if item, err = NewItem(c, path); err != nil {
// return
errs = append(errs, err)
err = nil
continue
}
items[idx] = item
}
err = NewErrors(err)
return
}
@@ -90,27 +92,35 @@ func (c *Collection) Delete() (err error) {
}
// SearchItems searches a Collection for a matching profile string.
func (c *Collection) SearchItems(profile string) (items []Item, err error) {
func (c *Collection) SearchItems(profile string) (items []*Item, err error) {
var paths []dbus.ObjectPath
var errs []error = make([]error, 0)
var attrs map[string]string = make(map[string]string, 0)
attrs["profile"] = profile
if err = c.Dbus.Call("org.freedesktop.Secret.Collection.SearchItems", 0, attrs).Store(&paths); err != nil {
if err = c.Dbus.Call(
DbusCollectionSearchItems, 0, attrs,
).Store(&paths); err != nil {
return
}
items = make([]Item, len(paths))
items = make([]*Item, len(paths))
for idx, path := range paths {
items[idx] = *NewItem(c.Conn, path)
if items[idx], err = NewItem(c, path); err != nil {
errs = append(errs, err)
err = nil
continue
}
}
err = NewErrors(err)
return
}
// CreateItem returns a pointer to an Item based on a label, a Secret, and whether any existing secret should be replaced or not.
// CreateItem returns a pointer to an Item based on a label, a Secret, and whether any existing secret with the same label should be replaced or not.
func (c *Collection) CreateItem(label string, secret *Secret, replace bool) (item *Item, err error) {
var prompt *Prompt
@@ -140,7 +150,7 @@ func (c *Collection) CreateItem(label string, secret *Secret, replace bool) (ite
path = variant.Value().(dbus.ObjectPath)
}
item = NewItem(c.Conn, path)
item, err = NewItem(c, path)
return
}
@@ -150,7 +160,7 @@ func (c *Collection) Locked() (isLocked bool, err error) {
var variant dbus.Variant
if variant, err = c.Dbus.GetProperty("org.freedesktop.Secret.Collection.Locked"); err != nil {
if variant, err = c.Dbus.GetProperty(DbusCollectionLocked); err != nil {
isLocked = true
return
}
@@ -163,7 +173,13 @@ func (c *Collection) Locked() (isLocked bool, err error) {
// Label returns the Collection label (name).
func (c *Collection) Label() (label string, err error) {
// TODO.
var variant dbus.Variant
if variant, err = c.Dbus.GetProperty(DbusCollectionLabel); err != nil {
return
}
label = variant.Value().(string)
return
}
@@ -171,7 +187,16 @@ func (c *Collection) Label() (label string, err error) {
// Created returns the time.Time of when a Collection was created.
func (c *Collection) Created() (created time.Time, err error) {
// TODO.
var variant dbus.Variant
var timeInt uint64
if variant, err = c.Dbus.GetProperty(DbusCollectionCreated); err != nil {
return
}
timeInt = variant.Value().(uint64)
created = time.Unix(int64(timeInt), 0)
return
}
@@ -181,17 +206,30 @@ func (c *Collection) Created() (created time.Time, err error) {
that indicates if the collection has changed since the last call of Collection.Modified.
Note that when calling NewCollection, the internal library-tracked modification
time (Collection.lastModified) will be set to the modification time of the Collection
itself as reported by Dbus.
time (Collection.lastModified) will be set to the latest modification time of the Collection
itself as reported by Dbus rather than the time that NewCollection was called.
*/
func (c *Collection) Modified() (modified time.Time, isChanged bool, err error) {
// TODO.
var variant dbus.Variant
var timeInt uint64
if c.lastModified == time.Date(1, time.January, 1, 0, 0, 0, 0, time.UTC) {
// It's "nil", so set it to modified.
c.lastModified = modified
if variant, err = c.Dbus.GetProperty(DbusCollectionModified); err != nil {
return
}
timeInt = variant.Value().(uint64)
modified = time.Unix(int64(timeInt), 0)
if !c.lastModifiedSet {
// It's "nil", so set it to modified. We can't check for a zero-value in case Dbus has it as a zero-value.
c.lastModified = modified
c.lastModifiedSet = true
}
isChanged = modified.After(c.lastModified)
c.lastModified = modified
return
}