Fix 1Password functions

This commit is contained in:
Jeremy Dormitzer 2019-04-24 11:38:35 -04:00
parent 77bf041606
commit 57f1fc72cf

View File

@ -2800,48 +2800,48 @@ The aws-mfa command:
(defun 1pass-signin () (defun 1pass-signin ()
(interactive) (interactive)
(let* ((signin-address "team-lolatravel.1password.com") (cl-letf* ((signin-address "team-lolatravel.1password.com")
(signin-email "jeremydormitzer@lola.com") (signin-email "jeremydormitzer@lola.com")
(secret-key (password-store-get "1pass-lola-secret-key")) (secret-key (password-store-get "1pass-lola-secret-key"))
(master-password (password-store-get "team-lolatravel.1password.com")) ((symbol-function 'op-signin)
(proc (start-process "op-signin" "*op-signin*" (make-shell-fn "pass" "team-lolatravel.1password.com" "|" "op" "signin" signin-address signin-email secret-key "--output=raw"))
"op" "signin" signin-address signin-email secret-key "--output=raw"))) (token (op-signin)))
(set-process-sentinel (if (string-match-p "ERROR" token)
proc (error "Unable to sign in to 1Password: %s" token)
(make-process-sentinel (setf op-token token)
(lambda () (message (format "Signed in to 1Password with session token %s" op-token))
(setq op-token op-token)))
(with-current-buffer "*op-signin*"
(goto-char (- (point-max) 1)) (defun op-fn (&rest args)
(buffer-substring-no-properties (line-beginning-position) (line-end-position)))) (lambda (&optional input)
(message (format "Signed in to 1Password with session token %s" op-token)) (cl-letf* (((symbol-function 'op-function)
(kill-buffer "*op-signin*")) (apply #'make-shell-fn "op" `(,@args ,(format "--session=%s" op-token))))
(lambda () (output (op-function input)))
(message "1Password signin failed, check *op-signin* buffer for details")))) (if (or (string-match-p "Authentication required" output)
(process-send-string proc (concat master-password "\n")))) (string-match-p "You are not currently signed in" output))
(cl-letf* ((new-token (1pass-signin))
((symbol-function 'op-function)
(apply #'make-shell-fn "op" `(,@args ,(format "--session=%s" new-token)))))
(op-function input))
output))))
(defun op-list-items () (defun op-list-items ()
(when (not op-token) (1pass-signin)) (cl-flet ((op-list-items-fn (op-fn "list" "items")))
(mapcar#'cdr (mapcar #'cdr
(mapcar (mapcar (apply-partially #'assoc 'title)
(apply-partially #'assoc 'title) (mapcar (apply-partially #'assoc 'overview)
(mapcar (json-read-from-string (op-list-items-fn)))))))
(apply-partially #'assoc 'overview)
(json-read-from-string
(funcall
(make-process-fn "op" "list" "items" (format "--session=%s" op-token))))))))
(defun op-get-item (item) (defun op-get-item (item)
(json-read-from-string (cl-flet ((op-get-item (op-fn "get" "item" item)))
(funcall (json-read-from-string (op-get-item))))
(make-process-fn "op" "get" "item" (format "--session=%s" op-token) item))))
(defun op-get-item-password (item-json) (defun op-get-item-password (item-json)
(let* ((fields (assoc-recursive item-json 'details 'fields)) (let* ((fields (assoc-recursive item-json 'details 'fields))
(pw-field (car (pw-field (car (seq-filter
(seq-filter (lambda (field)
(lambda (field) (string= "password" (cdr (assoc 'designation field)))) (string= "password" (cdr (assoc 'designation field))))
fields)))) fields))))
(when pw-field (cdr (assoc 'value pw-field))))) (when pw-field (cdr (assoc 'value pw-field)))))
(defun op-copy-password (item) (defun op-copy-password (item)
@ -2853,7 +2853,7 @@ The aws-mfa command:
(insert password) (insert password)
(copy-region-as-kill (point-min) (point-max)) (copy-region-as-kill (point-min) (point-max))
(message "Copied password for \"%s\" to kill ring." item)) (message "Copied password for \"%s\" to kill ring." item))
(message "No password found in 1Password for \"%s\"." item))) (error "No password found in 1Password for \"%s\"." item)))
(leader-def-key "ao" #'op-copy-password) (leader-def-key "ao" #'op-copy-password)
#+END_SRC #+END_SRC