This repository has been archived by the owner on May 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
org-password-manager.el
259 lines (200 loc) · 10.7 KB
/
org-password-manager.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
;;; org-password-manager.el --- Password manager for Org Mode.
;; Copyright (C) 2015 - Leandro Facchinetti <me@leafac.com>
;; Author: Leandro Facchinetti <me@leafac.com>
;; Version: 0.0.1
;; Keywords: password
;; URL: https://git.leafac.com/org-password-manager
;; Package-Requires: ((org "8.2.10") (s "1.9.0") (dash "2.13.0"))
;; This file is not part of GNU Emacs.
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; [ARCHIVED] Org Password Manager
;; Leandro Facchinetti <me@leafac.com>
;; Password manager for Org Mode.
;; Version 0.0.1
;; --------------------------------------------------------------------
;; Documentation https://www.leafac.com/software/org-password-manager
;; --------------------------------------------------------------------
;; License GNU General Public License Version 3
;; --------------------------------------------------------------------
;; Code of Conduct Contributor Covenant v1.4.0
;; --------------------------------------------------------------------
;; Distribution MELPA
;; --------------------------------------------------------------------
;; Source https://git.leafac.com/org-password-manager
;; --------------------------------------------------------------------
;; Bug Reports Write emails to org-password-manager@leafac.com.
;; --------------------------------------------------------------------
;; Contributions Send patches and pull requests via email to
;; org-password-manager@leafac.com.
;; --------------------------------------------------------------------
;; 1. Overview
;; Use GnuPG to encrypt the Org Mode files that contains credentials
;; instead of storing sensitive information in plain text.
;; Use Org Mode files to store credentials and retrieve them securely.
;; Integrate with pwgen to generate passwords.
;; 2. Installation
;; Available from MELPA, add the repository to Emacs and install with
;; M-x package-install. Password creation requires pwgen.
;; 3. Usage
;; This section assumes the default configuration.
;; Add credentials as properties named USERNAME and PASSWORD to headings in
;; Org Mode files. For example:
;; * [[http://example.com][My favorite website]]
;; :PROPERTIES:
;; :USERNAME: leandro
;; :PASSWORD: chunky-tempeh
;; :END:
;;
;; * SSH key
;; :PROPERTIES:
;; :PASSWORD: tofu
;; :END:
;; Passwords are cleared from the clipboard after 30 seconds.
;; Retrieve usernames with C-c C-p u (org-password-manager-get-username)
;; and passwords with C-c C-p p (org-password-manager-get-password). If
;; point is not under a heading that contains credentials, Org Password
;; Manager asks for a heading. To force this behavior even when the point
;; is under a heading that contains credentials, use the C-u argument (for
;; example, C-u C-c C-p u).
;; Generate passwords with C-c C-p g
;; (org-password-manager-generate-password). To customize the parameters to
;; pwgen, use the C-u argument (C-u C-c C-p g).
;; 4. Configuration
;; For the default configuration with the keybindings covered in the Usage
;; section, add the following to the Emacs configuration:
;; (add-hook 'org-mode-hook 'org-password-manager-key-bindings)
;; To customize the key bindings, start with the following code:
;; (defun org-password-manager-key-bindings ()
;; "Binds keys for org-password-manager."
;; (local-set-key (kbd "C-c C-p u") 'org-password-manager-get-username)
;; (local-set-key (kbd "C-c C-p p") 'org-password-manager-get-password)
;; (local-set-key (kbd "C-c C-p
;; g") 'org-password-manager-generate-password))
;; For Interactive Do (ido) support, add the following to the Emacs
;; configuration:
;; (setq org-completion-use-ido t)
;; For advanced configuration, refer to
;; M-x customize-group org-password-manager.
;; 5. Changelog
;; This section documents all notable changes to Org Password Manager. It
;; follows recommendations from Keep a CHANGELOG and uses Semantic
;; Versioning. Each released version is a Git tag.
;; 5.1. Archived · 2018-02-27
;; This project is archived and no longer maintained.
;; 5.2. 0.0.1 · 2015-07-29
;; 5.2.1. Added
;; * Core functionality.
;;; Code:
(require 'org)
(require 's)
(require 'dash)
(defgroup org-password-manager nil
"Minimal password manager for Emacs Org Mode."
:group 'org)
(defcustom org-password-manager-default-pwgen-command "pwgen --secure --symbols --capitalize --numerals 25 1"
"The default `pwgen' command to use when generating passwords."
:group 'org-password-manager)
(defcustom org-password-manager-default-password-wait-time "30 sec"
"The default period to wait before erasing the password from the clipboard.
Must be compatible with `run-at-time'."
:group 'org-password-manager)
(defcustom org-password-manager-scope 'agenda
"The scope used to search in org buffers.
Supported scopes are defined in `org-map-entries'. By default,
the `agenda' scope searches through all agenda files."
:group 'org-password-manager)
(defvar org-password-manager-history ()
"The history of headings that were chosen for `org-password-manager'.")
(defun org-password-manager-get-property (property-name &optional ask-for-input?)
"Get PROPERTY-NAME.
If ASK-FOR-INPUT? is t, will ask for input even if point is on a
heading that contains the property."
(let ((display-property-name (capitalize property-name))
(property (org-entry-get (point) property-name t))
output-message heading)
(if (and property (not ask-for-input?))
(setq heading (org-link-display-format (org-get-heading t t)))
(let* ((property-entries
(-filter #'cadr
(org-map-entries
(lambda ()
(list
(org-link-display-format (org-get-heading t t))
(org-entry-get (point) property-name)))
nil
org-password-manager-scope)))
(chosen-heading (funcall 'org-completing-read
(concat display-property-name " for: ")
property-entries
nil
nil
nil
'org-password-manager-history
(car org-password-manager-history)))
(header-property-list (assoc chosen-heading property-entries)))
(if header-property-list
(setq heading (nth 0 header-property-list)
property (nth 1 header-property-list))
(setq output-message (concat display-property-name
" for `"
chosen-heading
"' not found!")))))
(if (and heading property)
(if (string= property-name "PASSWORD")
(progn
(funcall interprogram-cut-function property)
(run-at-time org-password-manager-default-password-wait-time nil (lambda () (funcall interprogram-cut-function "")))
(setq output-message
(concat display-property-name " for `" heading "' securely copied to system's clipboard avoiding kill ring and will be removed in " org-password-manager-default-password-wait-time ".")))
(progn (kill-new property)
(setq output-message
(concat display-property-name " for `" heading "' copied to clipboard."))))
(add-to-history 'org-password-manager-history heading))
(message output-message)))
;;;###autoload
(defun org-password-manager-get-username (&optional ask-for-input?)
"Get username.
If ASK-FOR-INPUT? is t, will ask for input even if point is on a
heading that contains the username property."
(interactive "P")
(org-password-manager-get-property "USERNAME" ask-for-input?))
;;;###autoload
(defun org-password-manager-get-password (&optional ask-for-input?)
"Get password.
If ASK-FOR-INPUT? is t, will ask for input even if point is on a
heading that contains the password property."
(interactive "P")
(org-password-manager-get-property "PASSWORD" ask-for-input?))
;;;###autoload
(defun org-password-manager-generate-password (&optional edit-pwgen-string?)
"Generate password.
If EDIT-PWGEN-STRING? is t, let the user edit the pwgen command
line before running it."
(interactive "P")
(let* ((pwgen-string (if edit-pwgen-string?
(read-from-minibuffer "pwgen command to run: " org-password-manager-default-pwgen-command)
org-password-manager-default-pwgen-command))
(generated-password (s-trim (shell-command-to-string pwgen-string))))
(insert generated-password)
(funcall interprogram-cut-function generated-password)
(run-at-time org-password-manager-default-password-wait-time nil (lambda () (funcall interprogram-cut-function "")))
(message (concat "Generated password inserted on buffer, securely copied to system's clipboard avoiding kill ring and will be removed in " org-password-manager-default-password-wait-time "."))))
;; Key bindings.
;;;###autoload
(defun org-password-manager-key-bindings ()
"Binds keys for org-password-manager."
(local-set-key (kbd "C-c C-p u") 'org-password-manager-get-username)
(local-set-key (kbd "C-c C-p p") 'org-password-manager-get-password)
(local-set-key (kbd "C-c C-p g") 'org-password-manager-generate-password))
(provide 'org-password-manager)
;;; org-password-manager.el ends here