diff options
| author | Michael Albinus | 2021-12-27 19:53:00 +0100 |
|---|---|---|
| committer | Michael Albinus | 2021-12-27 19:53:00 +0100 |
| commit | 4ab5b89de84c8ae6254e11330b783a42e2c40121 (patch) | |
| tree | dc8fc9c803066bd431f6d6a30db1ec541f284170 | |
| parent | 01df347947c8785c7c21d5a2baa12de2ffde80dd (diff) | |
| download | emacs-4ab5b89de84c8ae6254e11330b783a42e2c40121.tar.gz emacs-4ab5b89de84c8ae6254e11330b783a42e2c40121.zip | |
The temprary "session" collection might not exist in Secret Service
* doc/misc/auth.texi (Secret Service API):
* test/lisp/net/secrets-tests.el (secrets--test-delete-all-session-items)
(secrets-test02-collections, secrets-test03-items)
(secrets-test04-search): The temporary "session" collection might
not exist.
* lisp/net/secrets.el (secrets-struct-secret-content-type):
Remove compatibility hack.
(secrets-create-item): Adapt accordingly.
| -rw-r--r-- | doc/misc/auth.texi | 6 | ||||
| -rw-r--r-- | lisp/net/secrets.el | 51 | ||||
| -rw-r--r-- | test/lisp/net/secrets-tests.el | 11 |
3 files changed, 20 insertions, 48 deletions
diff --git a/doc/misc/auth.texi b/doc/misc/auth.texi index 034004d1df4..6602f9dc2e4 100644 --- a/doc/misc/auth.texi +++ b/doc/misc/auth.texi | |||
| @@ -370,6 +370,10 @@ items should not live longer than Emacs. The session collection can | |||
| 370 | be specified either by the string @code{"session"}, or by @code{nil}, | 370 | be specified either by the string @code{"session"}, or by @code{nil}, |
| 371 | whenever a collection parameter is needed in the following functions. | 371 | whenever a collection parameter is needed in the following functions. |
| 372 | 372 | ||
| 373 | However, not all Secret Service provider create this temporary | ||
| 374 | @code{"session"} collection, like KeePassXC. You shall check first | ||
| 375 | that this collection exists, before you use it. | ||
| 376 | |||
| 373 | @defun secrets-list-items collection | 377 | @defun secrets-list-items collection |
| 374 | Returns all the item labels of @var{collection} as a list. | 378 | Returns all the item labels of @var{collection} as a list. |
| 375 | @end defun | 379 | @end defun |
| @@ -382,7 +386,7 @@ pairs set for the created item. The keys are keyword symbols, | |||
| 382 | starting with a colon. Example: | 386 | starting with a colon. Example: |
| 383 | 387 | ||
| 384 | @example | 388 | @example |
| 385 | ;;; The session is "session", the label is "my item" | 389 | ;;; The collection is "session", the label is "my item" |
| 386 | ;;; and the secret (password) is "geheim". | 390 | ;;; and the secret (password) is "geheim". |
| 387 | (secrets-create-item "session" "my item" "geheim" | 391 | (secrets-create-item "session" "my item" "geheim" |
| 388 | :method "sudo" :user "joe" :host "remote-host") | 392 | :method "sudo" :user "joe" :host "remote-host") |
diff --git a/lisp/net/secrets.el b/lisp/net/secrets.el index 4217c219ad9..25802f2c463 100644 --- a/lisp/net/secrets.el +++ b/lisp/net/secrets.el | |||
| @@ -263,6 +263,7 @@ It returns t if not." | |||
| 263 | ;; </signal> | 263 | ;; </signal> |
| 264 | ;; </interface> | 264 | ;; </interface> |
| 265 | 265 | ||
| 266 | ;; This is not guaranteed to exist. For example, KeePassXC does not offer this. | ||
| 266 | (defconst secrets-session-collection-path | 267 | (defconst secrets-session-collection-path |
| 267 | "/org/freedesktop/secrets/collection/session" | 268 | "/org/freedesktop/secrets/collection/session" |
| 268 | "The D-Bus temporary session collection object path.") | 269 | "The D-Bus temporary session collection object path.") |
| @@ -311,43 +312,8 @@ It returns t if not." | |||
| 311 | (defconst secrets-interface-item-type-generic "org.freedesktop.Secret.Generic" | 312 | (defconst secrets-interface-item-type-generic "org.freedesktop.Secret.Generic" |
| 312 | "The default item type we are using.") | 313 | "The default item type we are using.") |
| 313 | 314 | ||
| 314 | ;; We cannot use introspection, because some servers, like | 315 | (defconst secrets-struct-secret-content-type "text/plain" |
| 315 | ;; mate-keyring-daemon, don't provide relevant data. Once the dust | 316 | "The content_type of a secret struct.") |
| 316 | ;; has settled, we shall assume the new interface, and get rid of the test. | ||
| 317 | (defconst secrets-struct-secret-content-type | ||
| 318 | (ignore-errors | ||
| 319 | (let ((content-type "text/plain") | ||
| 320 | (path (cadr | ||
| 321 | (dbus-call-method | ||
| 322 | :session secrets-service secrets-path | ||
| 323 | secrets-interface-service | ||
| 324 | "OpenSession" "plain" '(:variant "")))) | ||
| 325 | result) | ||
| 326 | ;; Create a dummy item. | ||
| 327 | (setq result | ||
| 328 | (dbus-call-method | ||
| 329 | :session secrets-service secrets-session-collection-path | ||
| 330 | secrets-interface-collection "CreateItem" | ||
| 331 | ;; Properties. | ||
| 332 | `(:array | ||
| 333 | (:dict-entry ,(concat secrets-interface-item ".Label") | ||
| 334 | (:variant " "))) | ||
| 335 | ;; Secret. | ||
| 336 | `(:struct :object-path ,path | ||
| 337 | (:array :signature "y") | ||
| 338 | ,(dbus-string-to-byte-array " ") | ||
| 339 | :string ,content-type) | ||
| 340 | ;; Don't replace. | ||
| 341 | nil)) | ||
| 342 | ;; Remove it. | ||
| 343 | (dbus-call-method | ||
| 344 | :session secrets-service (car result) | ||
| 345 | secrets-interface-item "Delete") | ||
| 346 | ;; Result. | ||
| 347 | `(,content-type))) | ||
| 348 | "The content_type of a secret struct. | ||
| 349 | It must be wrapped as list, because we add it via `append'. This | ||
| 350 | is an interface introduced in 2011.") | ||
| 351 | 317 | ||
| 352 | (defconst secrets-interface-session "org.freedesktop.Secret.Session" | 318 | (defconst secrets-interface-session "org.freedesktop.Secret.Session" |
| 353 | "A session tracks state between the service and a client application.") | 319 | "A session tracks state between the service and a client application.") |
| @@ -696,13 +662,10 @@ The object path of the created item is returned." | |||
| 696 | `((:dict-entry ,(concat secrets-interface-item ".Attributes") | 662 | `((:dict-entry ,(concat secrets-interface-item ".Attributes") |
| 697 | (:variant ,(append '(:array) props)))))) | 663 | (:variant ,(append '(:array) props)))))) |
| 698 | ;; Secret. | 664 | ;; Secret. |
| 699 | (append | 665 | `(:struct :object-path ,secrets-session-path |
| 700 | `(:struct :object-path ,secrets-session-path | 666 | (:array :signature "y") ;; No parameters. |
| 701 | (:array :signature "y") ;; No parameters. | 667 | ,(dbus-string-to-byte-array password) |
| 702 | ,(dbus-string-to-byte-array password)) | 668 | ,secrets-struct-secret-content-type) |
| 703 | ;; We add the content_type. In backward compatibility | ||
| 704 | ;; mode, nil is appended, which means nothing. | ||
| 705 | secrets-struct-secret-content-type) | ||
| 706 | ;; Do not replace. Replace does not seem to work. | 669 | ;; Do not replace. Replace does not seem to work. |
| 707 | nil)) | 670 | nil)) |
| 708 | (secrets-prompt (cadr result)) | 671 | (secrets-prompt (cadr result)) |
diff --git a/test/lisp/net/secrets-tests.el b/test/lisp/net/secrets-tests.el index b392c4d1847..03d3e26faa6 100644 --- a/test/lisp/net/secrets-tests.el +++ b/test/lisp/net/secrets-tests.el | |||
| @@ -57,8 +57,11 @@ | |||
| 57 | 57 | ||
| 58 | (defun secrets--test-delete-all-session-items () | 58 | (defun secrets--test-delete-all-session-items () |
| 59 | "Delete all items of collection \"session\" bound to this Emacs." | 59 | "Delete all items of collection \"session\" bound to this Emacs." |
| 60 | (dolist (item (secrets-list-items "session")) | 60 | ;; If the "session" collection does not exist, a `dbus-error' is |
| 61 | (secrets-delete-item "session" item))) | 61 | ;; fired, which we ignore. |
| 62 | (dbus-ignore-errors | ||
| 63 | (dolist (item (secrets-list-items "session")) | ||
| 64 | (secrets-delete-item "session" item)))) | ||
| 62 | 65 | ||
| 63 | (ert-deftest secrets-test01-sessions () | 66 | (ert-deftest secrets-test01-sessions () |
| 64 | "Test opening / closing a secrets session." | 67 | "Test opening / closing a secrets session." |
| @@ -93,7 +96,7 @@ | |||
| 93 | (unwind-protect | 96 | (unwind-protect |
| 94 | (progn | 97 | (progn |
| 95 | (should (secrets-open-session)) | 98 | (should (secrets-open-session)) |
| 96 | (should (member "session" (secrets-list-collections))) | 99 | (skip-unless (member "session" (secrets-list-collections))) |
| 97 | 100 | ||
| 98 | ;; Create a random collection. This asks for a password | 101 | ;; Create a random collection. This asks for a password |
| 99 | ;; outside our control, so we make it in the interactive case | 102 | ;; outside our control, so we make it in the interactive case |
| @@ -153,6 +156,7 @@ | |||
| 153 | (unwind-protect | 156 | (unwind-protect |
| 154 | (let (item-path) | 157 | (let (item-path) |
| 155 | (should (secrets-open-session)) | 158 | (should (secrets-open-session)) |
| 159 | (skip-unless (member "session" (secrets-list-collections))) | ||
| 156 | 160 | ||
| 157 | ;; Cleanup. There could be items in the "session" collection. | 161 | ;; Cleanup. There could be items in the "session" collection. |
| 158 | (secrets--test-delete-all-session-items) | 162 | (secrets--test-delete-all-session-items) |
| @@ -214,6 +218,7 @@ | |||
| 214 | (unwind-protect | 218 | (unwind-protect |
| 215 | (progn | 219 | (progn |
| 216 | (should (secrets-open-session)) | 220 | (should (secrets-open-session)) |
| 221 | (skip-unless (member "session" (secrets-list-collections))) | ||
| 217 | 222 | ||
| 218 | ;; Cleanup. There could be items in the "session" collection. | 223 | ;; Cleanup. There could be items in the "session" collection. |
| 219 | (secrets--test-delete-all-session-items) | 224 | (secrets--test-delete-all-session-items) |