service.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package keychainlinker
  2. import (
  3. "fmt"
  4. "path"
  5. "github.com/godbus/dbus/v5"
  6. )
  7. type Service struct {
  8. /*
  9. Working on implementing the org.freedesktop.Secret.Service interface, from their v0.2 spec:
  10. https://specifications.freedesktop.org/secret-service-spec/latest-single/#org.freedesktop.Secret.Service
  11. */
  12. Collections []dbus.ObjectPath
  13. SessionBase string // e.g. "/org/freedesktop/secrets/session/"
  14. CollectionBase string // e.g. "/org/freedesktop/secrets/collection/"
  15. }
  16. // implementing method to read the object property
  17. func (s *Service) Get(iface, property string) (dbus.Variant, *dbus.Error) {
  18. if iface != "org/freedesktop/secret/service" {
  19. return dbus.Variant{}, dbus.MakeFailedError(dbus.ErrMsgUnknownInterface)
  20. }
  21. switch property {
  22. case "Collections":
  23. return dbus.MakeVariant(s.Collections), nil
  24. default:
  25. return dbus.Variant{}, dbus.MakeFailedError(dbus.ErrMsgUnknownMethod)
  26. }
  27. }
  28. // implementing method to read the object property
  29. func (s *Service) Set(iface, property string, value dbus.Variant) *dbus.Error {
  30. if iface != "org/freedesktop/secret/service" {
  31. return dbus.MakeFailedError(dbus.ErrMsgUnknownInterface)
  32. }
  33. switch property {
  34. case "Collections":
  35. collections, ok := value.Value().([]dbus.ObjectPath)
  36. if !ok {
  37. return dbus.MakeFailedError(dbus.ErrMsgInvalidArg)
  38. }
  39. s.Collections = collections
  40. return nil
  41. default:
  42. return dbus.MakeFailedError(dbus.ErrMsgUnknownMethod)
  43. }
  44. }
  45. // implementing the get all method for the dbus interface
  46. func (s *Service) GetAll(iface string) (map[string]dbus.Variant, *dbus.Error) {
  47. if iface != "org.freedesktop.secret.Service" {
  48. return nil, dbus.MakeFailedError(dbus.ErrMsgUnknownInterface)
  49. }
  50. return map[string]dbus.Variant{
  51. "Collections": dbus.MakeVariant(s.Collections),
  52. }, nil
  53. }
  54. /*
  55. Opens a session for the Secret Service Interface
  56. :param algorithm: the encryption algorithm to use with the client
  57. :param input: the data used when implementing more advanced encryption algos
  58. */
  59. func (s *Service) OpenSession(algorithm string, input dbus.Variant) (dbus.Variant, dbus.ObjectPath, *dbus.Error) {
  60. if algorithm != "PLAIN" {
  61. return dbus.Variant{}, "/", dbus.MakeFailedError(fmt.Errorf("only PLAIN is supported"))
  62. }
  63. sessionPath := dbus.ObjectPath(path.Join(s.SessionBase, "1"))
  64. return input, sessionPath, nil
  65. }
  66. /*
  67. Creates a collection with the Service object
  68. :param properties: a set of properties that are used by client apps
  69. :param alias: the shortname of the collection
  70. */
  71. func (s *Service) CreateCollection(properties map[string]dbus.Variant, alias string) (dbus.ObjectPath, dbus.ObjectPath, *dbus.Error) {
  72. collPath := dbus.ObjectPath(path.Join(s.CollectionBase, "login"))
  73. s.Collections = append(s.Collections, collPath)
  74. return collPath, "/", nil
  75. }
  76. /*
  77. search for items in the keychain that satisfy 'attrs'
  78. :param attrs: a map of search criteria
  79. */
  80. func (s *Service) SearchItems(attrs map[string]string) ([]dbus.ObjectPath, []dbus.ObjectPath, *dbus.Error) {
  81. // Just return empty results for now
  82. return []dbus.ObjectPath{}, []dbus.ObjectPath{}, nil
  83. }
  84. /*
  85. attempts to return secrets that were either already unlocked, or unlocked without a prompt, in addition to
  86. a prompt path that can be used to unlock all remaining locked objects
  87. :param objects: a slice of dbus.Objects to unlock
  88. */
  89. func (s *Service) Unlock(objects []dbus.ObjectPath) ([]dbus.ObjectPath, dbus.ObjectPath, *dbus.Error) {
  90. return []dbus.ObjectPath{}, dbus.ObjectPath("/"), nil // No prompt
  91. }
  92. /*
  93. Sets all dbus.Objects in 'objects' to the 'locked' position
  94. :param objects: a slice of dbus.Objects to unlock
  95. */
  96. func (s *Service) Lock(objects []dbus.ObjectPath) ([]dbus.ObjectPath, dbus.ObjectPath, *dbus.Error) {
  97. return []dbus.ObjectPath{}, dbus.ObjectPath("/"), nil // No prompt
  98. }
  99. /*
  100. retrives secrets from an array of items/collections
  101. :param items: a slice of dbus.ObjectPath that will have their secrets returned
  102. */
  103. func (s *Service) GetSecrets(items []dbus.ObjectPath, session dbus.ObjectPath) (map[dbus.ObjectPath]SecretStruct, *dbus.Error) {
  104. return map[dbus.ObjectPath]SecretStruct{}, nil
  105. }
  106. /*
  107. Return a collection based on the alias name
  108. :param name: the alias to search for
  109. */
  110. func (s *Service) ReadAlias(name string) (dbus.ObjectPath, *dbus.Error) {
  111. return dbus.ObjectPath(""), nil
  112. }
  113. /*
  114. set the alias of the passed in collection
  115. :param name: the alias to set the collection to
  116. :param collection: the collection to modify
  117. */
  118. func (s *Service) SetAlias(name string, collection dbus.ObjectPath) *dbus.Error {
  119. return nil
  120. }