>From 9e77cc4bf4131ef0fda205465cdf5ffdd6ef0a87 Mon Sep 17 00:00:00 2001 From: Keith Amidon Date: Tue, 30 Apr 2019 07:52:14 -0700 Subject: [PATCH 07/12] Split out the attribute retrieval form auth-source-pass-get Eliminate the need to repeatedly retrieve and parse the data for the entry. This is generally a good thing since it eliminates repetitions of the same crypto and parsing operations. It is especially valuable when protecting an entry with a yubikey with touch required for crypto operations as it eliminates the need to touch the yubikey sensor for each attribute retrieved. * lisp/auth-source-pass.el (auth-source-pass-get): Extract some code to `auth-source-pass--get-attr'. (auth-source-pass--get-attr): New function to get a field value from a parsed entry. (auth-source-pass--build-result): Make use of `auth-source-pass--get-attr` to avoid repeated parsing. --- lisp/auth-source-pass.el | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/lisp/auth-source-pass.el b/lisp/auth-source-pass.el index 9ad371ea7c..2203290d8c 100644 --- a/lisp/auth-source-pass.el +++ b/lisp/auth-source-pass.el @@ -76,11 +76,12 @@ auth-source-pass--build-result "Build auth-source-pass entry matching HOST, PORT and USER." (let ((entry (auth-source-pass--find-match host user port))) (when entry - (let ((retval (list - :host host - :port (or (auth-source-pass-get "port" entry) port) - :user (or (auth-source-pass-get "user" entry) user) - :secret (lambda () (auth-source-pass-get 'secret entry))))) + (let* ((entry-data (auth-source-pass-parse-entry entry)) + (retval (list + :host host + :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))))) (auth-source-pass--do-debug "return %s as final result (plus hidden password)" (seq-subseq retval 0 -2)) ;; remove password retval)))) @@ -125,9 +126,23 @@ auth-source-pass-get key1: value1 key2: value2" (let ((data (auth-source-pass-parse-entry entry))) - (or (cdr (assoc key data)) - (and (string= key "user") - (cdr (assoc "username" data)))))) + (auth-source-pass--get-attr key data))) + +(defun auth-source-pass--get-attr (key entry-data) + "Return the value associated to KEY in data from an already parsed entry. + +ENTRY-DATA is the data from a parsed password-store entry. +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 http://www.passwordstore.org/#organization): + +secret +key1: value1 +key2: value2" + (or (cdr (assoc key entry-data)) + (and (string= key "user") + (cdr (assoc "username" entry-data))))) (defun auth-source-pass--read-entry (entry) "Return a string with the file content of ENTRY." -- 2.21.0