2021-11-21 23:05:13 -05:00
|
|
|
package gosecret
|
2021-11-21 18:07:52 -05:00
|
|
|
|
|
|
|
import (
|
2021-11-27 02:24:22 -05:00
|
|
|
`fmt`
|
|
|
|
`path/filepath`
|
|
|
|
`strings`
|
|
|
|
`time`
|
|
|
|
|
2021-11-21 18:07:52 -05:00
|
|
|
`github.com/godbus/dbus`
|
|
|
|
)
|
|
|
|
|
2021-11-27 02:24:22 -05:00
|
|
|
/*
|
|
|
|
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.
|
2021-11-21 18:07:52 -05:00
|
|
|
|
2021-11-27 02:24:22 -05:00
|
|
|
Generally speaking, you should probably not use this function directly and instead use NewCollection.
|
|
|
|
*/
|
|
|
|
func CreateCollection(secretServiceConn *dbus.BusObject, name string) (c *Collection, err error) {
|
|
|
|
|
|
|
|
var path dbus.ObjectPath
|
|
|
|
|
|
|
|
if secretServiceConn == nil {
|
2021-11-21 23:12:25 -05:00
|
|
|
err = ErrNoDbusConn
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-27 02:24:22 -05:00
|
|
|
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 {
|
2021-11-21 23:12:25 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-21 18:07:52 -05:00
|
|
|
coll = &Collection{
|
2021-11-27 02:24:22 -05:00
|
|
|
Conn: conn,
|
|
|
|
Dbus: conn.Object(DbusService, path),
|
|
|
|
// lastModified: time.Now(),
|
2021-11-21 18:07:52 -05:00
|
|
|
}
|
|
|
|
|
2021-11-27 02:24:22 -05:00
|
|
|
_, _, err = coll.Modified()
|
|
|
|
|
2021-11-21 18:07:52 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-21 23:12:25 -05:00
|
|
|
// Items returns a slice of Item pointers in the Collection.
|
|
|
|
func (c *Collection) Items() (items []*Item, err error) {
|
2021-11-21 18:07:52 -05:00
|
|
|
|
|
|
|
var paths []dbus.ObjectPath
|
|
|
|
|
2021-11-27 02:24:22 -05:00
|
|
|
if paths, err = pathsFromPath(c.Dbus, DbusCollectionItems); err != nil {
|
2021-11-21 18:07:52 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-21 23:12:25 -05:00
|
|
|
items = make([]*Item, len(paths))
|
2021-11-21 18:07:52 -05:00
|
|
|
|
|
|
|
for idx, path := range paths {
|
2021-11-21 23:12:25 -05:00
|
|
|
items[idx] = NewItem(c.Conn, path)
|
2021-11-21 18:07:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete removes a Collection.
|
|
|
|
func (c *Collection) Delete() (err error) {
|
|
|
|
|
|
|
|
var promptPath dbus.ObjectPath
|
|
|
|
var prompt *Prompt
|
|
|
|
|
2021-11-21 23:12:25 -05:00
|
|
|
if err = c.Dbus.Call(DbusCollectionDelete, 0).Store(&promptPath); err != nil {
|
2021-11-21 18:07:52 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if isPrompt(promptPath) {
|
|
|
|
|
|
|
|
prompt = NewPrompt(c.Conn, promptPath)
|
|
|
|
if _, err = prompt.Prompt(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// SearchItems searches a Collection for a matching profile string.
|
|
|
|
func (c *Collection) SearchItems(profile string) (items []Item, err error) {
|
|
|
|
|
|
|
|
var paths []dbus.ObjectPath
|
|
|
|
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 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
items = make([]Item, len(paths))
|
|
|
|
|
|
|
|
for idx, path := range paths {
|
|
|
|
items[idx] = *NewItem(c.Conn, path)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateItem returns a pointer to an Item based on a label, a Secret, and whether any existing secret should be replaced or not.
|
|
|
|
func (c *Collection) CreateItem(label string, secret *Secret, replace bool) (item *Item, err error) {
|
|
|
|
|
|
|
|
var prompt *Prompt
|
|
|
|
var path dbus.ObjectPath
|
|
|
|
var promptPath dbus.ObjectPath
|
|
|
|
var variant *dbus.Variant
|
|
|
|
var props map[string]dbus.Variant = make(map[string]dbus.Variant)
|
|
|
|
var attrs map[string]string = make(map[string]string)
|
|
|
|
|
|
|
|
attrs["profile"] = label
|
|
|
|
props["org.freedesktop.Secret.Item.Label"] = dbus.MakeVariant(label)
|
|
|
|
props["org.freedesktop.Secret.Item.Attributes"] = dbus.MakeVariant(attrs)
|
|
|
|
|
|
|
|
if err = c.Dbus.Call(
|
|
|
|
"org.freedesktop.Secret.Collection.CreateItem", 0, props, secret, replace,
|
|
|
|
).Store(&path, &promptPath); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if isPrompt(promptPath) {
|
|
|
|
prompt = NewPrompt(c.Conn, promptPath)
|
|
|
|
|
|
|
|
if variant, err = prompt.Prompt(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
path = variant.Value().(dbus.ObjectPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
item = NewItem(c.Conn, path)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-27 02:24:22 -05:00
|
|
|
// Locked indicates if a Collection is locked (true) or unlocked (false).
|
2021-11-21 18:07:52 -05:00
|
|
|
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 {
|
|
|
|
isLocked = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
isLocked = variant.Value().(bool)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2021-11-27 02:24:22 -05:00
|
|
|
|
|
|
|
// Label returns the Collection label (name).
|
|
|
|
func (c *Collection) Label() (label string, err error) {
|
|
|
|
|
|
|
|
// TODO.
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Created returns the time.Time of when a Collection was created.
|
|
|
|
func (c *Collection) Created() (created time.Time, err error) {
|
|
|
|
|
|
|
|
// TODO.
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Modified returns the time.Time of when a Collection was last modified along with a boolean
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
func (c *Collection) Modified() (modified time.Time, isChanged bool, err error) {
|
|
|
|
|
|
|
|
// TODO.
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|