-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oath.plugin.zsh
425 lines (328 loc) · 7.62 KB
/
oath.plugin.zsh
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
# Oath manages private keys securely in order to generate one-time 6 digit
# tokens.
# shellcheck disable=SC2012
# shellcheck disable=SC2162
# shellcheck disable=SC2181
# shellcheck disable=SC2207
##################
# Global variables
export OATH_GNUPG="$HOME/.gnupg"
export OATH_DIR="$HOME/.oath"
[ ! -d "$OATH_DIR" ] && mkdir "$OATH_DIR"
###################
# Private functions
# Prints an error
function __oath_error() {
local message="$1"
(>&2 echo -e "\033[1;91m[ERROR] $message\033[0;0m")
}
# Prints a warning
function __oath_warn() {
local message="$1"
(>&2 echo -e "\033[1;93m[WARN] $message\033[0;0m")
}
# Prints a success message
function __oath_success() {
local message="$1"
(echo -e "\033[1;92m[SUCCESS] $message\033[0;0m")
}
# Checks pre-requisites.
function __oath_check_prerequisites() {
if [ -z "$OATH_EMAIL" ]
then
__oath_error "Missing \$OATH_EMAIL variable in $HOME/.zshrc"
return 1
fi
if [ -z "$OATH_KEY" ]
then
__oath_error "Missing \$OATH_KEY variable in $HOME/.zshrc"
return 1
fi
return 0
}
# Show usage
function __oath_usage() {
echo "\033[1;1mUsage:\033[0;0m
~ $ oath [add | delete | show | list | update | help] <key identifier>
\033[1;1mExamples:\033[0;0m
\033[1;1m1. \033[0;0m Adding a key:
~ $ oath add twitter.com
Private key:
$(__oath_success "Key added for twitter.com")
\033[1;1m2. \033[0;0m Deleting a key:
~ $ oath delete twitter.com
$(__oath_success "Key deleted for twitter.com")
\033[1;1m3. \033[0;0m Showing and copying a 6 digit code for a key:
~ $ oath twitter.com
012345
$(__oath_success "Code copied to clipboard")
\033[1;1m4. \033[0;0m Listing keys:
~ $ oath list
twitter.com
github.com
\033[1;1m5. \033[0;0m Updating oath:
~ $ oath update
From https://github.com/alexdesousa/oath
* branch master -> FETCH_HEAD
Already up to date."
}
# Gets secret dir.
function __oath_secret_dir() {
local name="$1"
local secret_dir="$OATH_DIR/$name"
echo "$secret_dir"
}
# Gets secret filename.
function __oath_secret_filename() {
local name="$1"
local secret_dir=""
local secret_filename=""
secret_dir=$(__oath_secret_dir "$name")
secret_filename="$secret_dir/$OATH_KEY.gpg"
echo "$secret_filename"
}
# Gets private key.
function __oath_get_private_key() {
local name="$1"
local secret_filename=""
local private_key=""
secret_filename=$(__oath_secret_filename "$name")
private_key=$(
gpg2 --quiet -u "$OATH_KEY" -r "$OATH_EMAIL" --decrypt "$secret_filename"
)
if [ $? -ne 0 ] || [ -z "$private_key" ]
then
__oath_error "Cannot retrieve private key for $name"
return 1
fi
echo "$private_key"
}
# Adds a new private key.
function __oath_add() {
local name="$1"
local private_key=""
local secret_dir=""
local secret_filename=""
secret_dir=$(__oath_secret_dir "$name")
secret_filename=$(__oath_secret_filename "$name")
# Parameters and pre-requisites
if [ -f "$secret_filename" ]
then
__oath_warn "File $secret_filename already exists"
return 1
fi
# Functionality
echo -n "Private key: " && read -s private_key
echo ""
if [ -z "$private_key" ]
then
__oath_warn "Private key cannot be empty"
return 1
fi
mkdir -p "$secret_dir" 2> /dev/null
gpg2 -u "$OATH_KEY" -r "$OATH_EMAIL" --encrypt -o "$secret_filename" <(echo "$private_key")
if [ $? -ne 0 ]
then
__oath_error "Cannot add key due to a problem"
return 1
fi
__oath_success "Key created for $name"
return 0
}
# Deletes a private key.
function __oath_delete() {
local name="$1"
local secret_dir=""
local secret_filename=""
local private_key=""
secret_dir=$(__oath_secret_dir "$name")
secret_filename=$(__oath_secret_filename "$name")
# Functionality
private_key=$(__oath_get_private_key "$name")
if [ $? -ne 0 ] || [ -z "$private_key" ]
then
__oath_error "Cannot delete key due to a problem"
return 1
fi
if [ -f "$secret_filename" ]
then
__oath_warn "Deleting $secret_filename"
rm "$secret_filename"
fi
if [ -d "$secret_dir" ] && [ -z "$(ls -A "$secret_dir")" ]
then
__oath_warn "Deleting $secret_dir"
rm -rf "$secret_dir"
fi
__oath_success "Key deleted for $name"
return 0
}
# Shows 6 digit code.
function __oath_show() {
local name="$1"
local private_key=""
local code=""
private_key=$(__oath_get_private_key "$name")
if [ $? -ne 0 ] || [ -z "$private_key" ]
then
__oath_error "Cannot get the private key for $name"
return 1
fi
code=$(oathtool -b --totp "$private_key")
if [ $? -ne 0 ] || [ -z "$code" ]
then
__oath_error "Cannot get code for $name"
return 1
fi
echo "$code"
xclip -sel clip <(echo -n "$code")
if [ $? -ne 0 ] || [ -z "$code" ]
then
__oath_error "Cannot copy code to clipboard"
return 1
fi
__oath_success "Code copied to clipboard"
return 0
}
# Shows the private key.
function __oath_pk() {
local name="$1"
local private_key=""
private_key=$(__oath_get_private_key "$name")
if [ $? -ne 0 ] || [ -z "$private_key" ]
then
__oath_error "Cannot get the private key for $name"
return 1
fi
echo "$private_key"
xclip -sel clip <(echo -n "$private_key")
if [ $? -ne 0 ] || [ -z "$private_key" ]
then
__oath_error "Cannot copy private key to clipboard"
return 1
fi
__oath_success "Private key copied to clipboard"
return 0
}
# Lists keys.
function __oath_list() {
local keys=""
keys=$(
ls -A "$OATH_DIR"/**/"$OATH_KEY".gpg |
sed 's#^'"$OATH_DIR"'/\(.*\)/'"$OATH_KEY"'\.gpg$#\1#g'
)
echo "$keys"
}
# Updates Oath.
function __oath_update() {
if [ -d "$ZSH_CUSTOM/plugins/oath/.git" ]
then
(
cd "$ZSH_CUSTOM/plugins/oath" &&
git pull origin master
)
fi
}
# Oath Completions.
function __oath() {
local current=""
local previous=""
local cmd=""
local cmds="add delete show pk list update help"
local keys=""
if ! __oath_check_prerequisites
then
return 1
fi
COMPREPLY=()
keys=$(__oath_list | tr '\n' ' ')
current="${COMP_WORDS[COMP_CWORD]}"
previous="${COMP_WORDS[COMP_CWORD - 1]}"
if [ "$COMP_CWORD" -eq 1 ]
then
case "$current" in
add | update | list | help)
;;
show | delete)
COMPREPLY=($(compgen -W "$keys" -- "$current"))
;;
*)
COMPREPLY=($(compgen -W "$cmds $keys" -- "$current"))
;;
esac
elif [ "$COMP_CWORD" -eq 2 ]
then
case "$previous" in
show | delete | pk)
COMPREPLY=($(compgen -W "$keys" -- "$current"))
;;
*)
;;
esac
fi
return 0
}
##################
# Public functions
# Oath command.
function oath() {
local cmd="$1"
local name="$2"
local secret_dir=""
local secret_filename=""
local private_key=""
local code=""
# Check commands
if ! __oath_check_prerequisites
then
return 1
fi
case "$cmd" in
add | delete | show)
if [ -z "$name" ]
then
__oath_warn "Missing key identifier"
__oath_usage
return 1
fi
;;
list | update | help)
;;
*)
if [ -z "$name" ]
then
name="$cmd"
cmd="show"
fi
;;
esac
secret_dir=$(__oath_secret_dir "$name")
secret_filename=$(__oath_secret_filename "$name")
# Functionality
case "$cmd" in
show)
__oath_show "$name"
;;
pk)
__oath_pk "$name"
;;
add)
__oath_add "$name"
;;
delete)
__oath_delete "$name"
;;
list)
__oath_list
;;
update)
__oath_update
;;
help)
__oath_usage
;;
esac
return $?
}
# Completions
complete -F __oath oath