unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#29045: [PATCH] auth-source-pass: Extract user from host when searching for entries
@ 2017-10-28 18:53 Łukasz Jędrzejewski
  2017-11-04 10:28 ` Eli Zaretskii
  0 siblings, 1 reply; 2+ messages in thread
From: Łukasz Jędrzejewski @ 2017-10-28 18:53 UTC (permalink / raw)
  To: 29045

[-- Attachment #1: Type: text/plain, Size: 4632 bytes --]

Hi,

I have recently contributed to auth-password-store package created by
Damien Cassou [1] and the proposed change has been successfully merged.
After that Damien suggested me to propose the same change to the Emacs
repository itself since the auth-password-store has been integrated inside
the core.

Regarding the attached patch: When auth-source-search is asked to find an
entry only by :host key; e.g. login@hostname.com and having an entry
hostname.com/login located in .password-store nothing is found. With this
patch, such an entry would be found. I have added a test case to document
the change.

What do you think?

Best regards,
Łukasz Jędrzejewski

[1]: https://github.com/DamienCassou/auth-password-store/pull/37


From e5c994b2e2c1e3180f3a74551507a3d3a243d906 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=C5=81ukasz=20J=C4=99drzejewski?=
 <jedrzejewskiluk@gmail.com>
Date: Sat, 28 Oct 2017 19:12:06 +0200
Subject: [PATCH] auth-source-pass: Extract user from host when searching for
 entries

* lisp/auth-source-pass.el: Extract user from host when searching for
  entries.  When the user is not explicitly specified and no entry is
  found, extract the user from the host and then search again.
  (auth-source-pass--user-match-p): Remove unused function.
* test/lisp/auth-source-pass-tests.el: Add a new test case to check
  it.

Copyright-paperwork-exempt: yes
---
 lisp/auth-source-pass.el            | 13 +++++++------
 test/lisp/auth-source-pass-tests.el |  5 +++++
 2 files changed, 12 insertions(+), 6 deletions(-)

diff --git a/lisp/auth-source-pass.el b/lisp/auth-source-pass.el
index 8f69ce323e..e1c3a91a90 100644
--- a/lisp/auth-source-pass.el
+++ b/lisp/auth-source-pass.el
@@ -139,11 +139,6 @@ CONTENTS is the contents of a password-store formatted
file."
                                     (mapconcat #'identity (cdr pair)
":")))))
                         (cdr lines)))))

-(defun auth-source-pass--user-match-p (entry user)
-  "Return true iff ENTRY match USER."
-  (or (null user)
-      (string= user (auth-source-pass-get "user" entry))))
-
 (defun auth-source-pass--hostname (host)
   "Extract hostname from HOST."
   (let ((url (url-generic-parse-url host)))
@@ -159,6 +154,10 @@ CONTENTS is the contents of a password-store formatted
file."
      (hostname hostname)
      (t host))))

+(defun auth-source-pass--user (host)
+  "Extract user from HOST or return nil."
+  (url-user (url-generic-parse-url host)))
+
 (defun auth-source-pass--do-debug (&rest msg)
   "Call `auth-source-do-debug` with MSG and a prefix."
   (apply #'auth-source-do-debug
@@ -235,7 +234,7 @@ matching USER."
 If many matches are found, return the first one.  If no match is
 found, return nil."
   (or
-   (if (url-user (url-generic-parse-url host))
+   (if (auth-source-pass--user host)
        ;; if HOST contains a user (e.g., "user@host.com"), <HOST>
        (auth-source-pass--find-one-by-entry-name
(auth-source-pass--hostname-with-user host) user)
      ;; otherwise, if USER is provided, search for <USER>@<HOST>
@@ -243,6 +242,8 @@ found, return nil."
        (auth-source-pass--find-one-by-entry-name (concat user "@"
(auth-source-pass--hostname host)) user)))
    ;; if that didn't work, search for HOST without it's user component if
any
    (auth-source-pass--find-one-by-entry-name (auth-source-pass--hostname
host) user)
+   ;; if that didn't work, search for HOST with user extracted from it
+   (auth-source-pass--find-one-by-entry-name (auth-source-pass--hostname
host) (auth-source-pass--user host))
    ;; if that didn't work, remove subdomain: foo.bar.com -> bar.com
    (let ((components (split-string host "\\.")))
      (when (= (length components) 3)
diff --git a/test/lisp/auth-source-pass-tests.el
b/test/lisp/auth-source-pass-tests.el
index 9b6b5687ca..84423b7d06 100644
--- a/test/lisp/auth-source-pass-tests.el
+++ b/test/lisp/auth-source-pass-tests.el
@@ -128,6 +128,11 @@ This function is intended to be set to
`auth-source-debug`."
     (should (equal (auth-source-pass--find-match "foo.bar.com" nil)
                    nil))))

+(ert-deftest
auth-source-pass-find-match-matching-extracting-user-from-host ()
+  (auth-source-pass--with-store '(("foo.com/bar"))
+    (should (equal (auth-source-pass--find-match "https://bar@foo.com" nil)
+                   "foo.com/bar"))))
+
 (ert-deftest auth-source-pass-search-with-user-first ()
   (auth-source-pass--with-store '(("foo") ("user@foo"))
     (should (equal (auth-source-pass--find-match "foo" "user")
-- 
2.14.2

[-- Attachment #2: Type: text/html, Size: 5885 bytes --]

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* bug#29045: [PATCH] auth-source-pass: Extract user from host when searching for entries
  2017-10-28 18:53 bug#29045: [PATCH] auth-source-pass: Extract user from host when searching for entries Łukasz Jędrzejewski
@ 2017-11-04 10:28 ` Eli Zaretskii
  0 siblings, 0 replies; 2+ messages in thread
From: Eli Zaretskii @ 2017-11-04 10:28 UTC (permalink / raw)
  To: Łukasz Jędrzejewski; +Cc: 29045-done

> From: Łukasz Jędrzejewski
> 	<jedrzejewskiluk@gmail.com>
> Date: Sat, 28 Oct 2017 20:53:01 +0200
> 
> I have recently contributed to auth-password-store package created by
> Damien Cassou [1] and the proposed change has been successfully merged.
> After that Damien suggested me to propose the same change to the Emacs
> repository itself since the auth-password-store has been integrated inside
> the core.
> 
> Regarding the attached patch: When auth-source-search is asked to find an
> entry only by :host key; e.g. login@hostname.com and having an entry
> hostname.com/login located in .password-store nothing is found. With this
> patch, such an entry would be found. I have added a test case to document
> the change.
> 
> What do you think?

Thanks, I pushed your changes to the master branch.

Please note that your mailer wraps long lines, which makes the patches
unsuitable for applying.  Please in the future send patches as
attachments.

I would also encourage you to start your legal paperwork for assigning
copyright of your changes to the FSF, so that we could accept your
future contributions without limitations.





^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2017-11-04 10:28 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-10-28 18:53 bug#29045: [PATCH] auth-source-pass: Extract user from host when searching for entries Łukasz Jędrzejewski
2017-11-04 10:28 ` Eli Zaretskii

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).