2021-11-21 23:05:13 -05:00
|
|
|
package gosecret
|
2021-11-21 18:07:52 -05:00
|
|
|
|
|
|
|
import (
|
2021-11-30 02:33:07 -05:00
|
|
|
"time"
|
2021-11-27 02:24:22 -05:00
|
|
|
|
2021-12-13 00:40:11 -05:00
|
|
|
"github.com/godbus/dbus/v5"
|
2021-11-21 18:07:52 -05:00
|
|
|
)
|
|
|
|
|
2021-11-27 02:24:22 -05:00
|
|
|
/*
|
2021-12-04 19:38:26 -05:00
|
|
|
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.
|
2021-11-27 02:24:22 -05:00
|
|
|
*/
|
2021-12-04 19:38:26 -05:00
|
|
|
func NewCollection(service *Service, path dbus.ObjectPath) (coll *Collection, err error) {
|
2021-11-27 02:24:22 -05:00
|
|
|
|
2021-12-04 19:38:26 -05:00
|
|
|
if service == nil {
|
2021-11-21 23:12:25 -05:00
|
|
|
err = ErrNoDbusConn
|
|
|
|
}
|
|
|
|
|
2021-12-04 19:38:26 -05:00
|
|
|
if _, err = validConnPath(service.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-30 02:33:07 -05:00
|
|
|
DbusObject: &DbusObject{
|
2021-12-04 19:38:26 -05:00
|
|
|
Conn: service.Conn,
|
|
|
|
Dbus: service.Conn.Object(DbusService, path),
|
2021-11-30 02:33:07 -05:00
|
|
|
},
|
2021-12-06 03:24:55 -05:00
|
|
|
service: service,
|
2021-12-13 04:04:03 -05:00
|
|
|
// LastModified: time.Now(),
|
2021-11-21 18:07:52 -05:00
|
|
|
}
|
|
|
|
|
2021-12-13 04:04:03 -05:00
|
|
|
// Populate the struct fields...
|
2021-12-13 04:33:43 -05:00
|
|
|
// TODO: use channel for errors; condense into a MultiError and switch to goroutines.
|
|
|
|
if _, err = coll.Locked(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if _, err = coll.Label(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if _, err = coll.Created(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if _, _, err = coll.Modified(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2021-11-27 02:24:22 -05:00
|
|
|
|
2021-11-21 18:07:52 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-13 00:15:38 -05:00
|
|
|
/*
|
|
|
|
CreateItem returns a pointer to an Item based on a label, some attributes, a Secret,
|
|
|
|
whether any existing secret with the same label should be replaced or not, and the optional itemType.
|
|
|
|
|
|
|
|
itemType is optional; if specified, it should be a Dbus interface (only the first element is used).
|
2021-12-13 00:40:11 -05:00
|
|
|
If not specified, the default DbusDefaultItemType will be used. The most common itemType is DbusDefaultItemType
|
|
|
|
and is the current recommendation.
|
|
|
|
Other types used are:
|
|
|
|
|
|
|
|
org.gnome.keyring.NetworkPassword
|
|
|
|
org.gnome.keyring.Note
|
|
|
|
|
|
|
|
These are libsecret schemas as defined at
|
|
|
|
https://gitlab.gnome.org/GNOME/libsecret/-/blob/master/libsecret/secret-schemas.c (and bundled in with libsecret).
|
|
|
|
Support for adding custom schemas MAY come in the future but is unsupported currently.
|
2021-12-13 00:15:38 -05:00
|
|
|
*/
|
|
|
|
func (c *Collection) CreateItem(label string, attrs map[string]string, secret *Secret, replace bool, itemType ...string) (item *Item, err error) {
|
2021-11-21 18:07:52 -05:00
|
|
|
|
2021-12-12 02:29:29 -05:00
|
|
|
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)
|
2021-12-13 00:15:38 -05:00
|
|
|
var typeString string
|
|
|
|
|
|
|
|
if itemType != nil && len(itemType) > 0 {
|
|
|
|
typeString = itemType[0]
|
|
|
|
} else {
|
|
|
|
typeString = DbusDefaultItemType
|
|
|
|
}
|
2021-11-21 18:07:52 -05:00
|
|
|
|
2021-12-12 02:29:29 -05:00
|
|
|
props[DbusItemLabel] = dbus.MakeVariant(label)
|
2021-12-13 00:15:38 -05:00
|
|
|
props[DbusItemType] = dbus.MakeVariant(typeString)
|
2021-12-12 02:29:29 -05:00
|
|
|
props[DbusItemAttributes] = dbus.MakeVariant(attrs)
|
2021-12-13 05:34:53 -05:00
|
|
|
props[DbusItemCreated] = dbus.MakeVariant(uint64(time.Now().Unix()))
|
|
|
|
// props[DbusItemModified] = dbus.MakeVariant(uint64(time.Now().Unix()))
|
2021-12-12 02:29:29 -05:00
|
|
|
|
|
|
|
if err = c.Dbus.Call(
|
|
|
|
DbusCollectionCreateItem, 0, props, secret, replace,
|
|
|
|
).Store(&path, &promptPath); err != nil {
|
2021-11-21 18:07:52 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-12 02:29:29 -05:00
|
|
|
if isPrompt(promptPath) {
|
|
|
|
prompt = NewPrompt(c.Conn, promptPath)
|
2021-11-21 18:07:52 -05:00
|
|
|
|
2021-12-12 02:29:29 -05:00
|
|
|
if variant, err = prompt.Prompt(); err != nil {
|
|
|
|
return
|
2021-12-04 19:38:26 -05:00
|
|
|
}
|
2021-12-12 02:29:29 -05:00
|
|
|
|
|
|
|
path = variant.Value().(dbus.ObjectPath)
|
2021-11-21 18:07:52 -05:00
|
|
|
}
|
2021-12-12 02:29:29 -05:00
|
|
|
|
|
|
|
item, err = NewItem(c, path)
|
2021-11-21 18:07:52 -05:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-12 02:29:29 -05:00
|
|
|
/*
|
|
|
|
Delete removes a Collection.
|
|
|
|
While *technically* not necessary, it is recommended that you iterate through
|
|
|
|
Collection.Items and do an Item.Delete for each item *before* calling Collection.Delete;
|
|
|
|
the item paths are cached as "orphaned paths" in Dbus otherwise if not deleted before deleting
|
|
|
|
their Collection. They should clear on a reboot or restart of Dbus (but rebooting Dbus on a system in use is... troublesome).
|
|
|
|
*/
|
2021-11-21 18:07:52 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-12-12 02:29:29 -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-12-12 02:29:29 -05:00
|
|
|
var item *Item
|
|
|
|
var variant dbus.Variant
|
2021-12-04 19:38:26 -05:00
|
|
|
var errs []error = make([]error, 0)
|
2021-11-21 18:07:52 -05:00
|
|
|
|
2021-12-12 02:29:29 -05:00
|
|
|
if variant, err = c.Dbus.GetProperty(DbusCollectionItems); err != nil {
|
2021-11-21 18:07:52 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-12 02:29:29 -05:00
|
|
|
paths = variant.Value().([]dbus.ObjectPath)
|
|
|
|
|
2021-12-18 00:57:41 -05:00
|
|
|
items = make([]*Item, 0)
|
2021-11-21 18:07:52 -05:00
|
|
|
|
2021-12-18 00:57:41 -05:00
|
|
|
for _, path := range paths {
|
|
|
|
item = nil
|
2021-12-12 02:29:29 -05:00
|
|
|
if item, err = NewItem(c, path); err != nil {
|
2021-12-04 19:38:26 -05:00
|
|
|
errs = append(errs, err)
|
|
|
|
err = nil
|
|
|
|
continue
|
|
|
|
}
|
2021-12-18 00:57:41 -05:00
|
|
|
items = append(items, item)
|
2021-11-21 18:07:52 -05:00
|
|
|
}
|
2021-12-04 19:38:26 -05:00
|
|
|
err = NewErrors(err)
|
2021-11-21 18:07:52 -05:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-12 02:29:29 -05:00
|
|
|
// Label returns the Collection label (name).
|
|
|
|
func (c *Collection) Label() (label string, err error) {
|
2021-11-21 18:07:52 -05:00
|
|
|
|
2021-12-12 02:29:29 -05:00
|
|
|
var variant dbus.Variant
|
2021-11-21 18:07:52 -05:00
|
|
|
|
2021-12-12 02:29:29 -05:00
|
|
|
if variant, err = c.Dbus.GetProperty(DbusCollectionLabel); err != nil {
|
2021-11-21 18:07:52 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-12 02:29:29 -05:00
|
|
|
label = variant.Value().(string)
|
2021-11-21 18:07:52 -05:00
|
|
|
|
2021-12-13 04:04:03 -05:00
|
|
|
c.LabelName = label
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lock will lock an unlocked Collection. It will no-op if the Collection is currently locked.
|
|
|
|
func (c *Collection) Lock() (err error) {
|
|
|
|
|
|
|
|
if _, err = c.Locked(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if c.IsLocked {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = c.service.Lock(c); err != nil {
|
|
|
|
return
|
2021-11-21 18:07:52 -05:00
|
|
|
}
|
2021-12-13 04:04:03 -05:00
|
|
|
c.IsLocked = true
|
2021-11-21 18:07:52 -05:00
|
|
|
|
2021-12-13 05:34:53 -05:00
|
|
|
if _, _, err = c.Modified(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-21 18:07:52 -05:00
|
|
|
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
|
|
|
|
|
2021-12-04 19:38:26 -05:00
|
|
|
if variant, err = c.Dbus.GetProperty(DbusCollectionLocked); err != nil {
|
2021-11-21 18:07:52 -05:00
|
|
|
isLocked = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
isLocked = variant.Value().(bool)
|
2021-12-13 04:04:03 -05:00
|
|
|
c.IsLocked = isLocked
|
2021-11-21 18:07:52 -05:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2021-11-27 02:24:22 -05:00
|
|
|
|
2021-12-12 02:29:29 -05:00
|
|
|
// Relabel modifies the Collection's label in Dbus.
|
|
|
|
func (c *Collection) Relabel(newLabel string) (err error) {
|
2021-11-27 02:24:22 -05:00
|
|
|
|
2021-12-12 02:29:29 -05:00
|
|
|
var variant dbus.Variant = dbus.MakeVariant(newLabel)
|
2021-12-04 19:38:26 -05:00
|
|
|
|
2021-12-12 02:29:29 -05:00
|
|
|
if err = c.Dbus.SetProperty(DbusCollectionLabel, variant); err != nil {
|
2021-12-04 19:38:26 -05:00
|
|
|
return
|
|
|
|
}
|
2021-12-13 04:04:03 -05:00
|
|
|
c.LabelName = newLabel
|
2021-12-04 19:38:26 -05:00
|
|
|
|
2021-12-13 05:34:53 -05:00
|
|
|
if _, _, err = c.Modified(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-27 02:24:22 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-12 02:29:29 -05:00
|
|
|
/*
|
|
|
|
SearchItems searches a Collection for a matching profile string.
|
2021-12-13 00:15:38 -05:00
|
|
|
It's mostly a carry-over from go-libsecret, and is here for convenience. IT MAY BE REMOVED IN THE FUTURE.
|
|
|
|
|
2021-12-12 02:29:29 -05:00
|
|
|
I promise it's not useful for any other implementation/storage of SecretService whatsoever.
|
2021-12-13 00:15:38 -05:00
|
|
|
|
|
|
|
Deprecated: Use Service.SearchItems instead.
|
2021-12-12 02:29:29 -05:00
|
|
|
*/
|
|
|
|
func (c *Collection) SearchItems(profile string) (items []*Item, err error) {
|
2021-12-07 02:56:15 -05:00
|
|
|
|
2021-12-12 02:29:29 -05:00
|
|
|
var paths []dbus.ObjectPath
|
|
|
|
var errs []error = make([]error, 0)
|
|
|
|
var attrs map[string]string = make(map[string]string, 0)
|
2021-12-18 00:57:41 -05:00
|
|
|
var item *Item
|
2021-12-12 02:29:29 -05:00
|
|
|
|
|
|
|
attrs["profile"] = profile
|
2021-12-07 02:56:15 -05:00
|
|
|
|
2021-12-12 02:29:29 -05:00
|
|
|
if err = c.Dbus.Call(
|
|
|
|
DbusCollectionSearchItems, 0, attrs,
|
|
|
|
).Store(&paths); err != nil {
|
2021-12-07 02:56:15 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-18 00:57:41 -05:00
|
|
|
items = make([]*Item, 0)
|
2021-12-12 02:29:29 -05:00
|
|
|
|
2021-12-18 00:57:41 -05:00
|
|
|
for _, path := range paths {
|
|
|
|
item = nil
|
|
|
|
if item, err = NewItem(c, path); err != nil {
|
2021-12-12 02:29:29 -05:00
|
|
|
errs = append(errs, err)
|
|
|
|
err = nil
|
|
|
|
continue
|
|
|
|
}
|
2021-12-18 00:57:41 -05:00
|
|
|
items = append(items, item)
|
2021-12-12 02:29:29 -05:00
|
|
|
}
|
|
|
|
err = NewErrors(err)
|
|
|
|
|
2021-12-07 02:56:15 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-13 04:04:03 -05:00
|
|
|
// SetAlias is a thin wrapper/shorthand for Service.SetAlias (but specific to this Collection).
|
|
|
|
func (c *Collection) SetAlias(alias string) (err error) {
|
|
|
|
|
|
|
|
var call *dbus.Call
|
|
|
|
|
|
|
|
call = c.service.Dbus.Call(
|
|
|
|
DbusServiceSetAlias, 0, alias, c.Dbus.Path(),
|
|
|
|
)
|
|
|
|
|
2021-12-13 05:34:53 -05:00
|
|
|
if err = call.Err; err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Alias = alias
|
|
|
|
|
|
|
|
if _, _, err = c.Modified(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2021-12-13 04:04:03 -05:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unlock will unlock a locked Collection. It will no-op if the Collection is currently unlocked.
|
|
|
|
func (c *Collection) Unlock() (err error) {
|
|
|
|
|
|
|
|
if _, err = c.Locked(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !c.IsLocked {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = c.service.Unlock(c); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.IsLocked = false
|
|
|
|
|
2021-12-13 05:34:53 -05:00
|
|
|
if _, _, err = c.Modified(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-13 04:04:03 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-27 02:24:22 -05:00
|
|
|
// Created returns the time.Time of when a Collection was created.
|
|
|
|
func (c *Collection) Created() (created time.Time, err error) {
|
|
|
|
|
2021-12-04 19:38:26 -05:00
|
|
|
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)
|
2021-12-13 05:34:53 -05:00
|
|
|
c.CreatedAt = created
|
2021-11-27 02:24:22 -05:00
|
|
|
|
|
|
|
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
|
2021-12-13 04:04:03 -05:00
|
|
|
time (Collection.LastModified) will be set to the latest modification time of the Collection
|
2021-12-04 19:38:26 -05:00
|
|
|
itself as reported by Dbus rather than the time that NewCollection was called.
|
2021-11-27 02:24:22 -05:00
|
|
|
*/
|
|
|
|
func (c *Collection) Modified() (modified time.Time, isChanged bool, err error) {
|
|
|
|
|
2021-12-04 19:38:26 -05:00
|
|
|
var variant dbus.Variant
|
|
|
|
var timeInt uint64
|
|
|
|
|
|
|
|
if variant, err = c.Dbus.GetProperty(DbusCollectionModified); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
timeInt = variant.Value().(uint64)
|
2021-11-27 02:24:22 -05:00
|
|
|
|
2021-12-04 19:38:26 -05:00
|
|
|
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.
|
2021-12-13 04:04:03 -05:00
|
|
|
c.LastModified = modified
|
2021-12-04 19:38:26 -05:00
|
|
|
c.lastModifiedSet = true
|
2021-11-27 02:24:22 -05:00
|
|
|
}
|
|
|
|
|
2021-12-13 04:04:03 -05:00
|
|
|
isChanged = modified.After(c.LastModified)
|
|
|
|
c.LastModified = modified
|
2021-12-04 19:38:26 -05:00
|
|
|
|
2021-11-27 02:24:22 -05:00
|
|
|
return
|
|
|
|
}
|
2021-12-12 02:29:29 -05:00
|
|
|
|
2021-12-13 04:04:03 -05:00
|
|
|
// path is a *very* thin wrapper around Collection.Dbus.Path(). It is needed for LockableObject interface membership.
|
|
|
|
func (c *Collection) path() (dbusPath dbus.ObjectPath) {
|
|
|
|
|
|
|
|
dbusPath = c.Dbus.Path()
|
2021-12-12 02:29:29 -05:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|