Subject: bug#49243: 27.2; [PATCH] Merge auth-source-pass changes from upstream

From: Damien Cassou <[email-masked]>
Date: Sun, 27 Jun 2021 18:02:01 +0200

Hi,

please find attached a few patches for auth-source-pass that were merged
upstream.

Damien Cassou (2):
  ; * lisp/auth-source-pass.el: Improve docstrings
  ; * lisp/auth-source-pass.el: Remove useless metadata

Iku Iwasa (1):
  lisp/auth-source-pass.el: Support multiple hosts in search spec

Tino Calancha (1):
  lisp/auth-source-pass.el: Keep legitimate spaces inside data

 lisp/auth-source-pass.el            | 61 ++++++++++++++++-------------
 test/lisp/auth-source-pass-tests.el | 24 +++++++++++-
 2 files changed, 57 insertions(+), 28 deletions(-)

-- 
Damien Cassou

"Success is the ability to go from one failure to another without
losing enthusiasm." --Winston Churchill
>From 622955a93036328d06daae4779299d53eb3115af Mon Sep 17 00:00:00 2001
From: Iku Iwasa <[email-masked]>
Date: Sun, 27 Jun 2021 17:36:00 +0200
Subject: [PATCH 1/4] lisp/auth-source-pass.el: Support multiple hosts in
 search spec

* lisp/auth-source-pass.el (auth-source-pass-search): Accept a list of
strings for argument HOST.
(auth-source-pass--build-result): Rename argument HOST to HOSTS. Also
return value "host" from entry if it exists.
(auth-source-pass--find-match): Rename argument HOST to HOSTS. Iterate
over each host in HOSTS.
* test/lisp/auth-source-pass-tests.el: Add corresponding tests
---
 lisp/auth-source-pass.el            | 40 ++++++++++++++++++-----------
 test/lisp/auth-source-pass-tests.el | 18 ++++++++++++-
 2 files changed, 42 insertions(+), 16 deletions(-)

diff --git a/lisp/auth-source-pass.el b/lisp/auth-source-pass.el
index a7b959c47f..6e33970486 100644
--- a/lisp/auth-source-pass.el
+++ b/lisp/auth-source-pass.el
@@ -61,13 +61,12 @@ auth-source-pass-search
                                          &key backend type host user port
                                          &allow-other-keys)
   "Given a property list SPEC, return search matches from the :backend.
-See `auth-source-search' for details on SPEC."
+See `auth-source-search' for details on SPEC.
+
+HOST can be a string or a list of strings, but USER and PORT are expected
+to be a string only."
   (cl-assert (or (null type) (eq type (oref backend type)))
              t "Invalid password-store search: %s %s")
-  (when (consp host)
-    (warn "auth-source-pass ignores all but first host in spec.")
-    ;; Take the first non-nil item of the list of hosts
-    (setq host (seq-find #'identity host)))
   (cond ((eq host t)
          (warn "auth-source-pass does not handle host wildcards.")
          nil)
@@ -78,12 +77,14 @@ auth-source-pass-search
          (when-let ((result (auth-source-pass--build-result host port user)))
            (list result)))))
 
-(defun auth-source-pass--build-result (host port user)
-  "Build auth-source-pass entry matching HOST, PORT and USER."
-  (let ((entry-data (auth-source-pass--find-match host user port)))
+(defun auth-source-pass--build-result (hosts port user)
+  "Build auth-source-pass entry matching HOSTS, PORT and USER.
+
+HOSTS can be a string or a list of strings."
+  (let ((entry-data (auth-source-pass--find-match hosts user port)))
     (when entry-data
       (let ((retval (list
-                     :host host
+                     :host (auth-source-pass--get-attr "host" entry-data)
                      :port (or (auth-source-pass--get-attr "port" entry-data) port)
                      :user (or (auth-source-pass--get-attr "user" entry-data) user)
                      :secret (lambda () (auth-source-pass--get-attr 'secret entry-data)))))
@@ -194,12 +195,21 @@ auth-source-pass-entries
      (lambda (file) (file-name-sans-extension (file-relative-name file store-dir)))
      (directory-files-recursively store-dir "\\.gpg\\'"))))
 
-(defun auth-source-pass--find-match (host user port)
-  "Return password-store entry data matching HOST, USER and PORT.
-
-Disambiguate between user provided inside HOST (e.g., [email-masked]) and
-inside USER by giving priority to USER.  Same for PORT."
-  (apply #'auth-source-pass--find-match-unambiguous (auth-source-pass--disambiguate host user port)))
+(defun auth-source-pass--find-match (hosts user port)
+  "Return password-store entry data matching HOSTS, USER and PORT.
+
+Disambiguate between user provided inside HOSTS (e.g., [email-masked]) and
+inside USER by giving priority to USER.  Same for PORT.
+HOSTS can be a string or a list of strings."
+  (seq-some (lambda (host)
+              (let ((entry (apply #'auth-source-pass--find-match-unambiguous
+                                   (auth-source-pass--disambiguate host user port))))
+                (if (or (null entry) (assoc "host" entry))
+                    entry
+                  (cons (cons "host" host) entry))))
+            (if (listp hosts)
+                hosts
+              (list hosts))))
 
 (defun auth-source-pass--disambiguate (host &optional user port)
   "Return (HOST USER PORT) after disambiguation.
diff --git a/test/lisp/auth-source-pass-tests.el b/test/lisp/auth-source-pass-tests.el
index bfbef53db9..a2f84f20e8 100644
--- a/test/lisp/auth-source-pass-tests.el
+++ b/test/lisp/auth-source-pass-tests.el
@@ -424,21 +424,37 @@ auth-source-pass-build-result-return-parameters
   (auth-source-pass--with-store-find-foo
       '(("foo" ("secret" . "foo password")))
     (let ((result (auth-source-pass--build-result "foo" 512 "user")))
+      (should (equal (plist-get result :host) "foo"))
       (should (equal (plist-get result :port) 512))
       (should (equal (plist-get result :user) "user")))))
 
 (ert-deftest auth-source-pass-build-result-return-entry-values ()
   (auth-source-pass--with-store-find-foo '(("foo" ("port" . 512) ("user" . "anuser")))
     (let ((result (auth-source-pass--build-result "foo" nil nil)))
+      (should (equal (plist-get result :host) "foo"))
       (should (equal (plist-get result :port) 512))
       (should (equal (plist-get result :user) "anuser")))))
 
 (ert-deftest auth-source-pass-build-result-entry-takes-precedence ()
-  (auth-source-pass--with-store-find-foo '(("foo" ("port" . 512) ("user" . "anuser")))
+  (auth-source-pass--with-store-find-foo '(("foo" ("host" . "bar") ("port" . 512) ("user" . "anuser")))
     (let ((result (auth-source-pass--build-result "foo" 1024 "anotheruser")))
+      (should (equal (plist-get result :host) "bar"))
       (should (equal (plist-get result :port) 512))
       (should (equal (plist-get result :user) "anuser")))))
 
+(ert-deftest auth-source-pass-build-result-with-multiple-hosts ()
+  (auth-source-pass--with-store-find-foo
+      '(("foo" ("secret" . "foo password")))
+    (let ((result (auth-source-pass--build-result '("bar" "foo") 512 "user")))
+      (should (equal (plist-get result :host) "foo"))
+      (should (equal (plist-get result :port) 512))
+      (should (equal (plist-get result :user) "user")))))
+
+(ert-deftest auth-source-pass-build-result-with-multiple-hosts-no-match ()
+  (auth-source-pass--with-store-find-foo
+      '(("foo" ("secret" . "foo password")))
+    (should-not (auth-source-pass--build-result '("bar" "baz") 512 "user"))))
+
 (ert-deftest auth-source-pass-can-start-from-auth-source-search ()
   (auth-source-pass--with-store '(("gitlab.com" ("user" . "someone")))
     (auth-source-pass-enable)
-- 
2.31.1

>From a65fe185e40fce68d7fdb7735b6e7effae31d0ee Mon Sep 17 00:00:00 2001
From: Damien Cassou <[email-masked]>
Date: Sun, 27 Jun 2021 17:38:59 +0200
Subject: [PATCH 2/4] ; * lisp/auth-source-pass.el: Improve docstrings

---
 lisp/auth-source-pass.el | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

diff --git a/lisp/auth-source-pass.el b/lisp/auth-source-pass.el
index 6e33970486..0f8be84dc2 100644
--- a/lisp/auth-source-pass.el
+++ b/lisp/auth-source-pass.el
@@ -60,11 +60,10 @@ auth-source-pass-port-separator
 (cl-defun auth-source-pass-search (&rest spec
                                          &key backend type host user port
                                          &allow-other-keys)
-  "Given a property list SPEC, return search matches from the :backend.
-See `auth-source-search' for details on SPEC.
+  "Given some search query, return matching credentials.
 
-HOST can be a string or a list of strings, but USER and PORT are expected
-to be a string only."
+See `auth-source-search' for details on the parameters SPEC, BACKEND, TYPE,
+HOST, USER and PORT."
   (cl-assert (or (null type) (eq type (oref backend type)))
              t "Invalid password-store search: %s %s")
   (cond ((eq host t)
@@ -126,7 +125,7 @@ auth-source-pass-get
 The key used to retrieve the password is the symbol `secret'.
 
 The convention used as the format for a password-store file is
-the following (see https://www.passwordstore.org/#organization):
+the following (see URL `https://www.passwordstore.org/#organization'):
 
 secret
 key1: value1
@@ -278,7 +277,7 @@ auth-source-pass--entries-matching-suffix
 (defun auth-source-pass--generate-entry-suffixes (hostname user port)
   "Return a list of possible entry path suffixes in the password-store.
 
-Based on the supported pathname patterns for HOSTNAME, USER, &
+Based on the supported filename patterns for HOSTNAME, USER, &
 PORT, return a list of possible suffixes for matching entries in
 the password-store.
 
@@ -326,3 +325,5 @@ auth-source-pass--name-port-user-suffixes
 
 (provide 'auth-source-pass)
 ;;; auth-source-pass.el ends here
+
+;; LocalWords:  backend hostname
-- 
2.31.1

>From 228647fdd051b3874561659d02f2f31d15b3c620 Mon Sep 17 00:00:00 2001
From: Damien Cassou <[email-masked]>
Date: Sun, 27 Jun 2021 17:51:54 +0200
Subject: [PATCH 3/4] ; * lisp/auth-source-pass.el: Remove useless metadata

---
 lisp/auth-source-pass.el | 2 --
 1 file changed, 2 deletions(-)

diff --git a/lisp/auth-source-pass.el b/lisp/auth-source-pass.el
index 0f8be84dc2..c512c6fe4f 100644
--- a/lisp/auth-source-pass.el
+++ b/lisp/auth-source-pass.el
@@ -6,8 +6,6 @@
 ;;         Nicolas Petton <[email-masked]>
 ;;         Keith Amidon <[email-masked]>
 ;; Version: 5.0.0
-;; Package-Requires: ((emacs "25"))
-;; Url: https://github.com/DamienCassou/auth-password-store
 ;; Created: 07 Jun 2015
 
 ;; This file is part of GNU Emacs.
-- 
2.31.1

>From 57f564556374f892a93c7879bc2629da8f58cb23 Mon Sep 17 00:00:00 2001
From: Tino Calancha <[email-masked]>
Date: Sun, 27 Jun 2021 17:53:30 +0200
Subject: [PATCH 4/4] lisp/auth-source-pass.el: Keep legitimate spaces inside
 data

Users should be able to store a field as follows:
message: remember: Destroy the image and you will break the enemy

and later, recover the message untouched, i.e.:
"remember: Destroy the image and you will break the enemy"

* lisp/auth-source-pass.el (auth-source-pass--parse-data): Preserve
inner spaces at data.
* test/lisp/auth-source-pass-tests.el
(auth-source-pass-parse-with-colons-in-data): Add test.
---
 lisp/auth-source-pass.el            | 12 +++++-------
 test/lisp/auth-source-pass-tests.el |  6 ++++++
 2 files changed, 11 insertions(+), 7 deletions(-)

diff --git a/lisp/auth-source-pass.el b/lisp/auth-source-pass.el
index c512c6fe4f..914f8d2f1b 100644
--- a/lisp/auth-source-pass.el
+++ b/lisp/auth-source-pass.el
@@ -167,15 +167,13 @@ auth-source-pass--parse-secret
 (defun auth-source-pass--parse-data (contents)
   "Parse the password-store data in the string CONTENTS and return an alist.
 CONTENTS is the contents of a password-store formatted file."
-  (let ((lines (split-string contents "\n" t "[ \t]+")))
+  (let ((lines (cdr (split-string contents "\n" t "[ \t]+"))))
     (seq-remove #'null
                 (mapcar (lambda (line)
-                          (let ((pair (mapcar (lambda (s) (string-trim s))
-                                              (split-string line ":"))))
-                            (when (> (length pair) 1)
-                              (cons (car pair)
-                                    (mapconcat #'identity (cdr pair) ":")))))
-                        (cdr lines)))))
+                          (when-let ((pos (seq-position line ?:)))
+                            (cons (string-trim (substring line 0 pos))
+                                  (string-trim (substring line (1+ pos))))))
+                        lines))))
 
 (defun auth-source-pass--do-debug (&rest msg)
   "Call `auth-source-do-debug` with MSG and a prefix."
diff --git a/test/lisp/auth-source-pass-tests.el b/test/lisp/auth-source-pass-tests.el
index a2f84f20e8..d050ac5b69 100644
--- a/test/lisp/auth-source-pass-tests.el
+++ b/test/lisp/auth-source-pass-tests.el
@@ -49,6 +49,12 @@ auth-source-pass-parse-with-trailing-spaces
                    '(("key1" . "val1")
                      ("key2" . "val2"))))))
 
+(ert-deftest auth-source-pass-parse-with-colons-in-data ()
+  (let ((content "pass\n--\nkey1 :val1\nkey2: please: keep my space after colon\n\n"))
+    (should (equal (auth-source-pass--parse-data content)
+                   '(("key1" . "val1")
+                     ("key2" . "please: keep my space after colon"))))))
+
 (defvar auth-source-pass--debug-log nil
   "Contains a list of all messages passed to `auth-source-do-debug`.")
 
-- 
2.31.1