2021-11-21 23:05:13 -05:00
|
|
|
package gosecret
|
2021-11-21 18:07:52 -05:00
|
|
|
|
|
|
|
import (
|
2021-11-28 21:43:30 -05:00
|
|
|
"github.com/godbus/dbus"
|
2021-11-21 18:07:52 -05:00
|
|
|
)
|
|
|
|
|
2021-11-28 21:43:30 -05:00
|
|
|
// NewService returns a pointer to a new Service connection.
|
2021-11-21 18:07:52 -05:00
|
|
|
func NewService() (service *Service, err error) {
|
|
|
|
|
2021-11-28 21:43:30 -05:00
|
|
|
var svc Service = Service{}
|
2021-11-21 18:07:52 -05:00
|
|
|
|
2021-11-28 21:43:30 -05:00
|
|
|
if svc.Conn, err = dbus.SessionBus(); err != nil {
|
2021-11-21 18:07:52 -05:00
|
|
|
return
|
|
|
|
}
|
2021-11-28 21:43:30 -05:00
|
|
|
svc.Dbus = service.Conn.Object(DbusService, dbus.ObjectPath(DbusPath))
|
2021-11-21 18:07:52 -05:00
|
|
|
|
2021-11-28 21:43:30 -05:00
|
|
|
if svc.Session, err = svc.Open(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2021-11-21 18:07:52 -05:00
|
|
|
|
2021-11-28 21:43:30 -05:00
|
|
|
service = &svc
|
2021-11-21 18:07:52 -05:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open returns a pointer to a Session from the Service.
|
2021-11-28 21:43:30 -05:00
|
|
|
func (s *Service) Open() (session *Session, output dbus.Variant, err error) {
|
2021-11-21 18:07:52 -05:00
|
|
|
|
|
|
|
var path dbus.ObjectPath
|
|
|
|
|
2021-11-28 21:43:30 -05:00
|
|
|
// In *theory*, SecretService supports multiple "algorithms" for encryption in-transit, but I don't think it's implemented (yet)?
|
|
|
|
// TODO: confirm this.
|
|
|
|
// Possible flags are dbus.Flags consts: https://pkg.go.dev/github.com/godbus/dbus#Flags
|
|
|
|
// Oddly, there is no "None" flag. So it's explicitly specified as a null byte.
|
2021-11-21 18:07:52 -05:00
|
|
|
if err = s.Dbus.Call(
|
2021-11-28 21:43:30 -05:00
|
|
|
DbusServiceOpenSession, 0x0, "plain", dbus.MakeVariant(""),
|
2021-11-21 18:07:52 -05:00
|
|
|
).Store(&output, &path); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
session = NewSession(s.Conn, path)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-28 21:43:30 -05:00
|
|
|
// Collections returns a slice of Collection items accessible to this Service.
|
2021-11-21 18:07:52 -05:00
|
|
|
func (s *Service) Collections() (collections []Collection, err error) {
|
|
|
|
|
|
|
|
var paths []dbus.ObjectPath
|
|
|
|
var variant dbus.Variant
|
|
|
|
|
|
|
|
if variant, err = s.Dbus.GetProperty("org.freedesktop.Secret.Service.Collections"); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
paths = variant.Value().([]dbus.ObjectPath)
|
|
|
|
|
|
|
|
collections = make([]Collection, len(paths))
|
|
|
|
|
|
|
|
for idx, path := range paths {
|
|
|
|
collections[idx] = *NewCollection(s.Conn, path)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-28 21:43:30 -05:00
|
|
|
// CreateCollection creates a new Collection (keyring) via a Service with the name specified by label and returns the new Collection.
|
2021-11-21 18:07:52 -05:00
|
|
|
func (s *Service) CreateCollection(label string) (collection *Collection, err error) {
|
|
|
|
|
|
|
|
var variant *dbus.Variant
|
|
|
|
var path dbus.ObjectPath
|
|
|
|
var promptPath dbus.ObjectPath
|
|
|
|
var prompt *Prompt
|
|
|
|
var props map[string]dbus.Variant = make(map[string]dbus.Variant)
|
|
|
|
|
|
|
|
props["org.freedesktop.Secret.Collection.Label"] = dbus.MakeVariant(label)
|
|
|
|
|
|
|
|
if err = s.Dbus.Call("org.freedesktop.Secret.Service.CreateCollection", 0, props, "").Store(&path, &promptPath); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if isPrompt(promptPath) {
|
|
|
|
|
|
|
|
prompt = NewPrompt(s.Conn, promptPath)
|
|
|
|
|
|
|
|
if variant, err = prompt.Prompt(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
path = variant.Value().(dbus.ObjectPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
collection = NewCollection(s.Conn, path)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unlock unlocks a Locked Service.
|
|
|
|
func (s *Service) Unlock(object DBusObject) (err error) {
|
|
|
|
|
|
|
|
var unlocked []dbus.ObjectPath
|
|
|
|
var prompt *Prompt
|
|
|
|
var promptPath dbus.ObjectPath
|
|
|
|
var paths []dbus.ObjectPath = []dbus.ObjectPath{object.Path()}
|
|
|
|
|
|
|
|
if err = s.Dbus.Call("org.freedesktop.Secret.Service.Unlock", 0, paths).Store(&unlocked, &promptPath); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if isPrompt(promptPath) {
|
|
|
|
|
|
|
|
prompt = NewPrompt(s.Conn, promptPath)
|
|
|
|
|
|
|
|
if _, err = prompt.Prompt(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lock locks an Unlocked Service.
|
|
|
|
func (s *Service) Lock(object DBusObject) (err error) {
|
|
|
|
|
|
|
|
var locked []dbus.ObjectPath
|
|
|
|
var prompt *Prompt
|
|
|
|
var promptPath dbus.ObjectPath
|
|
|
|
var paths []dbus.ObjectPath = []dbus.ObjectPath{object.Path()}
|
|
|
|
|
|
|
|
if err = s.Dbus.Call("org.freedesktop.Secret.Service.Lock", 0, paths).Store(&locked, &promptPath); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if isPrompt(promptPath) {
|
|
|
|
|
|
|
|
prompt = NewPrompt(s.Conn, promptPath)
|
|
|
|
|
|
|
|
if _, err = prompt.Prompt(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|