diff --git a/consts.go b/consts.go index 9243c0d..3616f29 100644 --- a/consts.go +++ b/consts.go @@ -21,6 +21,7 @@ const ( /* DbusServiceChangeLock has some references in the SecretService Dbus API but it seems to be obsolete - undocumented, at the least. + So we don't implement it. */ // DbusServiceChangeLock string = DbusInterfaceService + ".ChangeLock" @@ -33,7 +34,7 @@ const ( // DbusServiceLock is used by Service.Lock. DbusServiceLock string = DbusInterfaceService + ".Lock" - // DbusServiceLockService is [FUNCTION UNKNOWN/UNDOCUMENTED; TODO.] + // DbusServiceLockService is [FUNCTION UNKNOWN/UNDOCUMENTED; TODO? NOT IMPLEMENTED.] DbusServiceLockService string = DbusInterfaceService + ".LockService" // DbusServiceOpenSession is used by Service.Open. @@ -48,12 +49,12 @@ const ( // DbusServiceSetAlias is used by Service.SetAlias to set an alias for a Collection. DbusServiceSetAlias string = DbusInterfaceService + ".SetAlias" - // DbusServiceUnlock is used to unlock a Service. + // DbusServiceUnlock is used by Service.Unlock. DbusServiceUnlock string = DbusInterfaceService + ".Unlock" // Properties - // DbusServiceCollections is used to get a Dbus array of Collection items. + // DbusServiceCollections is used to get a Dbus array of Collection items (Service.Collections). DbusServiceCollections string = DbusInterfaceService + ".Collections" ) diff --git a/service_funcs.go b/service_funcs.go index e43befb..07b3546 100644 --- a/service_funcs.go +++ b/service_funcs.go @@ -23,6 +23,14 @@ func NewService() (service *Service, err error) { return } +// Close cleanly closes a Service and all its underlying connections (e.g. Service.Session). +func (s *Service) Close() (err error) { + + err = s.Session.Close() + + return +} + // Collections returns a slice of Collection items accessible to this Service. func (s *Service) Collections() (collections []Collection, err error) { @@ -99,6 +107,20 @@ func (s *Service) CreateCollection(label string) (collection *Collection, err er return } +/* + GetAlias allows one to fetch a Collection dbus.ObjectPath based on an alias name. + If the alias does not exist, objectPath will be dbus.ObjectPath("/"). + TODO: return a Collection instead of a dbus.ObjectPath. +*/ +func (s *Service) GetAlias(alias string) (objectPath dbus.ObjectPath, err error) { + + err = s.Dbus.Call( + DbusServiceReadAlias, 0, alias, + ).Store(&objectPath) + + return +} + /* GetSecrets allows you to fetch values (Secret) from multiple Item object paths using this Service's Session. An ErrMissingPaths will be returned for err if itemPaths is nil or empty. @@ -169,7 +191,10 @@ func (s *Service) Lock(objectPaths ...dbus.ObjectPath) (err error) { return } -// Open returns a pointer to a Session from the Service. +/* + Open returns a pointer to a Session from the Service. + It's a convenience function around NewSession. +*/ func (s *Service) Open() (session *Session, output dbus.Variant, err error) { var path dbus.ObjectPath @@ -179,12 +204,43 @@ func (s *Service) Open() (session *Session, output dbus.Variant, err error) { // 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. if err = s.Dbus.Call( - DbusServiceOpenSession, 0x0, "plain", dbus.MakeVariant(""), + DbusServiceOpenSession, 0, "plain", dbus.MakeVariant(""), ).Store(&output, &path); err != nil { return } - session = NewSession(s.Conn, path) + session = NewSession(s, path) + + return +} + +/* + SearchItems searches all Collection objects and returns all matches based on the map of attributes. + TODO: return arrays of Items instead of dbus.ObjectPaths. + TODO: check attributes for empty/nil. +*/ +func (s *Service) SearchItems(attributes map[string]string) (unlockedItems []dbus.ObjectPath, lockedItems []dbus.ObjectPath, err error) { + + err = s.Dbus.Call( + DbusServiceSearchItems, 0, attributes, + ).Store(&unlockedItems, &lockedItems) + + return +} + +/* + SetAlias sets an alias for an existing Collection. + To remove an alias, set objectPath to dbus.ObjectPath("/"). +*/ +func (s *Service) SetAlias(alias string, objectPath dbus.ObjectPath) (err error) { + + var c *dbus.Call + + c = s.Dbus.Call( + DbusServiceSetAlias, 0, alias, objectPath, + ) + + _ = c return } diff --git a/session_funcs.go b/session_funcs.go index 642dc35..4664698 100644 --- a/session_funcs.go +++ b/session_funcs.go @@ -4,24 +4,34 @@ import ( "github.com/godbus/dbus" ) -// NewSession returns a pointer to a new Session based on a Dbus connection and a Dbus path. -func NewSession(conn *dbus.Conn, path dbus.ObjectPath) (session *Session) { +/* + NewSession returns a pointer to a new Session based on a Service and a dbus.ObjectPath. + If path is empty (""), the default +*/ +func NewSession(service *Service, path dbus.ObjectPath) (session *Session) { - session = &Session{ + var ssn Session = Session{ &DbusObject{ - Conn: conn, - Dbus: conn.Object(DbusService, path), + Conn: service.Conn, }, } + session.Dbus = session.Conn.Object(DbusInterfaceSession, path) + + session = &ssn return } -// Path returns the path of the underlying Dbus connection. -func (s Session) Path() (path dbus.ObjectPath) { +// Close cleanly closes a Session. +func (s *Session) Close() (err error) { - // Remove this method in V1. It's bloat since we now have an exported Dbus. - path = s.Dbus.Path() + var c *dbus.Call + + c = s.Dbus.Call( + DbusSessionClose, 0, + ) + + _ = c return }