From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: =?UTF-8?Q?=C5=81ukasz_?= =?UTF-8?Q?J=C4=99drzejewski?= Newsgroups: gmane.emacs.bugs Subject: bug#29045: [PATCH] auth-source-pass: Extract user from host when searching for entries Date: Sat, 28 Oct 2017 20:53:01 +0200 Message-ID: NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="001a11475f3832acf1055c9fea9b" X-Trace: blaine.gmane.org 1509217041 4437 195.159.176.226 (28 Oct 2017 18:57:21 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Sat, 28 Oct 2017 18:57:21 +0000 (UTC) To: 29045@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Sat Oct 28 20:57:16 2017 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1e8WIC-0008Jb-PS for geb-bug-gnu-emacs@m.gmane.org; Sat, 28 Oct 2017 20:57:13 +0200 Original-Received: from localhost ([::1]:33720 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e8WIG-00023t-UX for geb-bug-gnu-emacs@m.gmane.org; Sat, 28 Oct 2017 14:57:16 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:60055) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e8WI4-000239-Rq for bug-gnu-emacs@gnu.org; Sat, 28 Oct 2017 14:57:06 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1e8WI2-00040C-SW for bug-gnu-emacs@gnu.org; Sat, 28 Oct 2017 14:57:04 -0400 Original-Received: from debbugs.gnu.org ([208.118.235.43]:59030) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1e8WI2-0003zl-NR for bug-gnu-emacs@gnu.org; Sat, 28 Oct 2017 14:57:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1e8WI2-0003wq-Ga for bug-gnu-emacs@gnu.org; Sat, 28 Oct 2017 14:57:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: =?UTF-8?Q?=C5=81ukasz_?= =?UTF-8?Q?J=C4=99drzejewski?= Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sat, 28 Oct 2017 18:57:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 29045 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch X-Debbugs-Original-To: bug-gnu-emacs@gnu.org Original-Received: via spool by submit@debbugs.gnu.org id=B.150921699315127 (code B ref -1); Sat, 28 Oct 2017 18:57:02 +0000 Original-Received: (at submit) by debbugs.gnu.org; 28 Oct 2017 18:56:33 +0000 Original-Received: from localhost ([127.0.0.1]:39476 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1e8WHZ-0003vu-0N for submit@debbugs.gnu.org; Sat, 28 Oct 2017 14:56:33 -0400 Original-Received: from eggs.gnu.org ([208.118.235.92]:40511) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1e8WEN-0003qW-0I for submit@debbugs.gnu.org; Sat, 28 Oct 2017 14:53:15 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1e8WEF-0008B0-VJ for submit@debbugs.gnu.org; Sat, 28 Oct 2017 14:53:09 -0400 Original-Received: from lists.gnu.org ([2001:4830:134:3::11]:34230) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1e8WEF-0008Au-RD for submit@debbugs.gnu.org; Sat, 28 Oct 2017 14:53:07 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:59714) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e8WED-0001ne-VC for bug-gnu-emacs@gnu.org; Sat, 28 Oct 2017 14:53:07 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1e8WEC-00089q-0p for bug-gnu-emacs@gnu.org; Sat, 28 Oct 2017 14:53:06 -0400 Original-Received: from mail-qk0-x236.google.com ([2607:f8b0:400d:c09::236]:57177) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1e8WEB-00089X-PD for bug-gnu-emacs@gnu.org; Sat, 28 Oct 2017 14:53:03 -0400 Original-Received: by mail-qk0-x236.google.com with SMTP id l194so11885135qke.13 for ; Sat, 28 Oct 2017 11:53:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:sender:from:date:message-id:subject:to; bh=CGbTOTPQMOSv/VjSNoyyPRiU09WmbBJ5npvjazfeJ+8=; b=ZS/iKuMJ+IAiTqCdAarXYm1OBwLSK7VGW8x9xIfUEhiYtH2+hxvYxXe8e7zI0XpYi7 R7fFsX1QA5FFiyiCrTU7OjeRG73Frg4/yoAVCVlZWO+b3v/Hg9i6MkpVNwj7PKpqNBs6 +BygjW2C777BgSO+4Ixl+/6sdVM6hC4bETWgXJrpXMEaKsCor2B4HhftUFcJYmJmhUjo AyoXs7LU3no5rBR6lVghIqr66VxocDH4Ai+tQAAQUaASmcgjVWRZhaviUQCPqV+s8u2U v4LWIPa20Q5m+9b8UvLK68vGFM1OX/4yL/Ygr7AdA0dY2ua8rHEEJ897B5gLONxiEuzV XnCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:sender:from:date:message-id:subject :to; bh=CGbTOTPQMOSv/VjSNoyyPRiU09WmbBJ5npvjazfeJ+8=; b=AWRfQbz6WYihsJoSDoR7AjiJYABPCqX99WsbfOC381+q/EoWZ0lFHvIwa6za41rEcJ qbOU7Jere6J6ua++0R2cAVy9VPDOQKeKHmpMzeAsARGn6nWhQOv5Es8GTAGG8xpfslH9 COk+zwEa34lCYZwKTxtLdX4awoT46ZlkSHZx2Y+L4+CDMBNcdFv2IQMyUZxzuFea/bFr QdDT/abdya+z9F4IrsLXgA8ZTk8LBsKXO9G0MuWhpt5MCS0uCl7kiVHvKXOLIqJ6c7tK o49RTNUvOrjddeJY23EMbCfEN60bX9hRJydMENKa/b0IybVM/JSC0HTQ/XMt4QVdPz6q YIOg== X-Gm-Message-State: AMCzsaU9CKvkcjMku2KNW1G2zdI2vUheR6GFXMtHyxgmEw7FEGAmtLfS NYPL+o/sjoUAfWQKHY5uxBg0ynI4uDt8Kb5yibtWSXCK X-Google-Smtp-Source: ABhQp+SnRS7ONgFtMUtqOWs3M0bD1/VGDSZaTSaxmsBILok+/Z8fCupv0UfkcC/SF9tXsQxxlfvPfbIudFIkrAM/DqM= X-Received: by 10.55.16.34 with SMTP id a34mr6706228qkh.353.1509216782296; Sat, 28 Oct 2017 11:53:02 -0700 (PDT) Original-Received: by 10.12.187.147 with HTTP; Sat, 28 Oct 2017 11:53:01 -0700 (PDT) X-Google-Sender-Auth: FmlwYGh6ZRlSdEfRxEVNTQM-thY X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6.x X-Mailman-Approved-At: Sat, 28 Oct 2017 14:56:31 -0400 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 208.118.235.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.org gmane.emacs.bugs:139123 Archived-At: --001a11475f3832acf1055c9fea9b Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 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, =C5=81ukasz J=C4=99drzejewski [1]: https://github.com/DamienCassou/auth-password-store/pull/37 >From e5c994b2e2c1e3180f3a74551507a3d3a243d906 Mon Sep 17 00:00:00 2001 From: =3D?UTF-8?q?=3DC5=3D81ukasz=3D20J=3DC4=3D99drzejewski?=3D Date: Sat, 28 Oct 2017 19:12:06 +0200 Subject: [PATCH] auth-source-pass: Extract user from host when searching fo= r 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=3D 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"), (auth-source-pass--find-one-by-entry-name (auth-source-pass--hostname-with-user host) user) ;; otherwise, if USER is provided, search for @ @@ -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 (=3D (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") --=20 2.14.2 --001a11475f3832acf1055c9fea9b Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hi,

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

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

What do you think?

=
Best regards,
=C5=81ukasz J=C4=99drzejewski

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

<= div>

From e5c994b2e2c1e3180f3a74551507a3d3a243d906 Mon Sep 17 00:00= :00 2001
From: =3D?UTF-8?q?=3DC5=3D81ukasz=3D20J=3DC4=3D99drzejewski?=3D=
=C2=A0<jedrzejewskiluk@= gmail.com>
Date: Sat, 28 Oct 2017 19:12:06 +0200
Subject: [PAT= CH] auth-source-pass: Extract user from host when searching for
=C2=A0en= tries

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

Copyright-paperwork-exempt: yes
---
=C2=A0lisp/= auth-source-pass.el=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0 | 13 +++++++------
=C2=A0test/lisp/auth-source-pass-tests.e= l |=C2=A0 5 +++++
=C2=A02 files changed, 12 insertions(+), 6 deletions(-= )

diff --git a/lisp/auth-source-pass.el b/lisp/auth-source-pass.elindex 8f69ce323e..e1c3a91a90 100644
--- a/lisp/auth-source-pass.el
= +++ b/lisp/auth-source-pass.el
@@ -139,11 +139,6 @@ CONTENTS is the cont= ents of a password-store formatted file."
=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (mapconcat #'identity (cdr p= air) ":")))))
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0 (cdr lines)))))
=C2=A0
-(defun auth-source-pass= --user-match-p (entry user)
-=C2=A0 "Return true iff ENTRY match US= ER."
-=C2=A0 (or (null user)
-=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (st= ring=3D user (auth-source-pass-get "user" entry))))
-
=C2= =A0(defun auth-source-pass--hostname (host)
=C2=A0=C2=A0 "Extract h= ostname from HOST."
=C2=A0=C2=A0 (let ((url (url-generic-parse-url = host)))
@@ -159,6 +154,10 @@ CONTENTS is the contents of a password-stor= e formatted file."
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (hostname hostnam= e)
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (t host))))
=C2=A0
+(defun auth-= source-pass--user (host)
+=C2=A0 "Extract user from HOST or return = nil."
+=C2=A0 (url-user (url-generic-parse-url host)))
+
=C2= =A0(defun auth-source-pass--do-debug (&rest msg)
=C2=A0=C2=A0 "= Call `auth-source-do-debug` with MSG and a prefix."
=C2=A0=C2=A0 (a= pply #'auth-source-do-debug
@@ -235,7 +234,7 @@ matching USER."=
=C2=A0If many matches are found, return the first one.=C2=A0 If no matc= h is
=C2=A0found, return nil."
=C2=A0=C2=A0 (or
-=C2=A0=C2=A0= (if (url-user (url-generic-parse-url host))
+=C2=A0=C2=A0 (if (auth-sou= rce-pass--user host)
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 ;; if HO= ST contains a user (e.g., "user@host.= com"), <HOST>
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 = (auth-source-pass--find-one-by-entry-name (auth-source-pass--hostname-with-= user host) user)
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 ;; otherwise, if USER is= provided, search for <USER>@<HOST>
@@ -243,6 +242,8 @@ foun= d, return nil."
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (auth-so= urce-pass--find-one-by-entry-name (concat user "@" (auth-source-p= ass--hostname host)) user)))
=C2=A0=C2=A0=C2=A0 ;; if that didn't wo= rk, search for HOST without it's user component if any
=C2=A0=C2=A0= =C2=A0 (auth-source-pass--find-one-by-entry-name (auth-source-pass--hostnam= e host) user)
+=C2=A0=C2=A0 ;; if that didn't work, search for HOST = with user extracted from it
+=C2=A0=C2=A0 (auth-source-pass--find-one-by= -entry-name (auth-source-pass--hostname host) (auth-source-pass--user host)= )
=C2=A0=C2=A0=C2=A0 ;; if that didn't work, remove subdomain: foo.bar.com -> = bar.com
=C2=A0=C2=A0=C2=A0 (let ((components (split-string host &quo= t;\\.")))
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (when (=3D (length compone= nts) 3)
diff --git a/test/lisp/auth-source-pass-tests.el b/test/lisp/aut= h-source-pass-tests.el
index 9b6b5687ca..84423b7d06 100644
--- a/test= /lisp/auth-source-pass-tests.el
+++ b/test/lisp/auth-source-pass-tests.e= l
@@ -128,6 +128,11 @@ This function is intended to be set to `auth-sour= ce-debug`."
=C2=A0=C2=A0=C2=A0=C2=A0 (should (equal (auth-source-pa= ss--find-match "foo.bar.com" n= il)
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 nil))))
=C2=A0
+(ert= -deftest auth-source-pass-find-match-matching-extracting-user-from-host ()<= br>+=C2=A0 (auth-source-pass--with-store '(("foo.com/bar"))
+=C2=A0=C2=A0=C2=A0 (should (equal (au= th-source-pass--find-match "https://ba= r@foo.com" nil)
+=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 "foo.com/bar"))))
+
=C2=A0(ert-deft= est auth-source-pass-search-with-user-first ()
=C2=A0=C2=A0 (auth-source= -pass--with-store '(("foo") ("user@foo"))
=C2=A0= =C2=A0=C2=A0=C2=A0 (should (equal (auth-source-pass--find-match "foo&q= uot; "user")
--
2.14.2


= --001a11475f3832acf1055c9fea9b--