-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitwarden.el
103 lines (86 loc) · 3.44 KB
/
bitwarden.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
(use-package bitwarden
:straight (bitwarden :type git :host github :repo "seanfarley/emacs-bitwarden"
:fork t)
:config
(setq bitwarden-user "[email protected]")
(setq bitwarden-api-secret-key
(plist-get (car (auth-source-search :host "bitwarden.jsilve24.key"))
:secret))
(setq bitwarden-api-client-id
(plist-get (car (auth-source-search :host "bitwarden.jsilve24.id"))
:secret))
(setq bitwarden-automatic-unlock
(let* ((matches (auth-source-search :user "[email protected]"
:host "bitwarden.jsilve24.login"
:require '(:secret)
:max 1))
(entry (nth 0 matches)))
(plist-get entry :secret)))
;; (bitwarden-auth-source-enable)
(bitwarden-login)
(bitwarden-unlock))
(with-eval-after-load 'exwm
(exwm-input-set-key (kbd "s-u") #'bitwarden-kill-username)
(exwm-input-set-key (kbd "s-p") #'bitwarden-kill-password)
(exwm-input-set-key (kbd "s-P") #'jds/kill-psu-pass))
(defvar bitwarden-time-to-store "2 min"
"Length of time to store last selected username and password before deleting. String should be recognized by the command run-at-time.")
;;;###autoload
(defun bitwarden--parse-hash-to-plist (e)
"Pass emacs-bitwarden hash-table format to a plist."
(let* ((login (gethash "login" e)))
(message (gethash "name" e))
(if login
(list (gethash "name" e)
(list
:username (gethash "username" login)
:password (gethash "password" login)))
nil)))
;;;###autoload
(defun bitwarden-list-completing-read ()
"A completing read interface built on top of emacs-bitwarden."
(interactive)
(let* ((vault (bitwarden-search))
(vault (remq nil (mapcar 'bitwarden--parse-hash-to-plist vault)))
(select (completing-read "Select:" vault))
(select (car (cdr (assoc select vault)))))
(if (and (boundp 'bitwarden--unstore-timer)
(timerp bitwarden--unstore-timer))
(progn
(cancel-timer bitwarden--unstore-timer)
(makunbound 'bitwarden--unstore-timer)))
(setq bitwarden--username (plist-get select :username)
bitwarden--password (plist-get select :password)
bitwarden--unstore-timer (run-at-time
bitwarden-time-to-store nil
(lambda ()
(makunbound 'bitwarden--username)
(makunbound 'bitwarden--password)))))
(message "Username and Passwored temporarily stored."))
;;;###autoload
(defun bitwarden-kill-password (&optional arg)
"Yank password from bitwarden--username.
With prefix argument, repeat completin-read selection even if there was a recent selection (e.g., the variable bitwarden--password is bound)."
(interactive "P")
(if (or (not (boundp 'bitwarden--password))
arg)
(bitwarden-list-completing-read))
(kill-new bitwarden--password))
;;;###autoload
(defun bitwarden-kill-username (&optional arg)
"Yank password from bitwarden--username.
With prefix argument, repeat completin-read selection even if there was a recent selection (e.g., the variable bitwarden--username is bound)."
(interactive "P")
(if (or (not (boundp 'bitwarden--username))
arg)
(bitwarden-list-completing-read))
(kill-new bitwarden--username))
;;; Password stuff but not bitwarden ------------------------------------------------------
;;;###autoload
(defun jds/kill-psu-pass ()
"Fast access to a commonly used password."
(interactive)
(kill-new
(funcall (plist-get (car (auth-source-search :host "localhost" :user "[email protected]"))
:secret)))
(message "password coppied to clipboard."))