-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathots.py
More file actions
executable file
·583 lines (406 loc) · 10.9 KB
/
ots.py
File metadata and controls
executable file
·583 lines (406 loc) · 10.9 KB
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
#!/usr/bin/env python3
from json import loads as jsonparse
from json.decoder import JSONDecodeError
import requests
from requests.auth import HTTPBasicAuth
# Configuration loading
from os.path import expanduser
from configparser import ConfigParser
from time import time, ctime
import re
from getopt import getopt
from getopt import GetoptError as goerror
from sys import argv
'''
TODO:
1. perms check and error
2. Metakeys database
3. clipboard
MODES:
main = share
generate - ?
retrieve
'''
def usage():
USAGE = '''
usage: ots [mode] [options] [arguments]
'''
configdir='~/.config/ots'
status_url = 'https://onetimesecret.com/api/v1/status'
share_url = 'https://onetimesecret.com/api/v1/share'
generate_url = 'https://onetimesecret.com/api/v1/generate'
retrieve_url = 'https://onetimesecret.com/api/v1/secret/{}' # {} = SECRET_KEY
metadata_url = 'https://onetimesecret.com/api/v1/private/{}' # {} = METADATA_KEY
metadata_recent_url = 'https://onetimesecret.com/api/v1/private/recent'
burn_url = 'https://onetimesecret.com/api/v1/private/{}/burn' # {} = METADATA_KEY
secret_url = 'https://onetimesecret.com/secret/{}' # {} = SECRET_KEY
private_url = 'https://onetimesecret.com/private/{}' # {} = METADATA_KEY
timepat = re.compile(
'''
(?:([1-9][0-9]*)[C])?\s*
(?:([1-9][0-9]*)[D])?\s*
(?:([1-9][0-9]*)[Y])?\s*
(?:([1-9][0-9]*)[M])?\s*
(?:([1-9][0-9]*)[W])?\s*
(?:([1-9][0-9]*)[d])?\s*
(?:([1-9][0-9]*)[h])?\s*
(?:([1-9][0-9]*)[m])?\s*
(?:([1-9][0-9]*)[s]?)?\s*
''',
flags=re.VERBOSE)
def print_dummy(a):
"""
Does nothing
Used as a (print=print_dummy) when -q quiet option provided
"""
return
def print_request_error(request, process_name):
print('[ERROR] Request error during', process_name)
print(' Code:', request.status_code)
print(' Response:', request.text)
def api_call(url, data, process_name, auth=None, forcereturn=False):
r = requests.post(url, data=data, auth=auth)
# print(r.url)
if r.status_code != 200:
print('[ERROR] Request error during', process_name)
print(' Code:', r.status_code)
print(' Response:', r.text)
if not forcereturn:
return None
try:
data = jsonparse(r.text)
except JSONDecodeError:
data = {}
return data
def check_availability():
r = requests.get(status_url);
if r.status_code==200:
status = jsonparse(r.text)['status']
if status == 'nominal':
print('Online')
return True
print('Offline. Status:', status)
else:
print('Offline')
return False;
def share_secret(data, auth=None):
"""
TODO: docstring
"""
return api_call(share_url, data, 'sharing a Secret', auth=auth)
def retrieve_secret(data, auth=None):
return api_call(retrieve_url.format(data['secret_key']), data, 'retrieving a Secret', auth=auth)
def retrieve_metadata(data, auth=None):
return api_call(metadata_url.format(data['metadata_key']), None, 'retrieving Metadata', auth=auth)
def retrieve_latest_metadata(data, auth=None):
#TODO
return
def generate_secret(data, auth=None):
return api_call(generate_url, data, 'generating a Secret', auth=auth)
def burn_secret(data, auth=None):
return api_call(burn_url.format(data['metadata_key']), None, 'burning a secret', auth=auth)
config = None
# initialization is before __main__ block below
userdir = expanduser('~')
config = ConfigParser()
r = config.read( ('{}/.config/otsrc'.format(userdir), '{}/otsrc'.format(userdir)) ) # for Unix and Windows
if len(r) == 0:
c = None
def get_identity(name):
if config == None:
return None
if '' == name:
name = 'DEFAULT'
if name not in config:
return None
data = config[name]
if 'email' not in data and 'apikey' not in data:
return None
return data['email'], data['apikey']
def parsetime(s):
"""
Parses string in a form '1C 2D 3Y4M5W 6d7h8m9s' to time units respectively
(Centuries, Decades, Years, Months and etc.)
Returns time in seconds
"""
a = list(timepat.match(s).groups())
for i in range(0,len(a)):
if a[i] == None:
a[i] = 0;
a[i]=int(a[i])
centuries, decades, years, months, weeks, days, hours, minutes, seconds = a;
days = (centuries * 100 + decades * 10 + years) * 365.25 + months * 30.4375 + weeks * 7 + days
seconds = ((days * 24 + hours) * 60 + minutes ) * 60 + seconds
return int(seconds)
def print_metadata_default(meta, print_recieve=True, print_meta=True):
"""
Default function for printing received information about metadata
meta -- dictionary (parsed from json)
Keyword arguments:
print_recieve -- if secret was just created so information about receivement's not needed
print_meta -- print information about metadata itself: key, expiration date (default: True)
"""
t = time()
# Burn call specific problem
if 'secret_shortkey' in meta:
meta = meta['state']
# print(meta)
if 'secret_key' in meta and meta['secret_key']:
print( 'Secret key:', meta['secret_key'] )
if 'state' in meta and print_recieve: # new, viewed (via metadata) or received (burn may be?)
"""
4 possible state:
new - created and metadata wasn't checked
viewed - metadata was checked at least once
received - secret was received
burned - secret was burned
"""
if meta['state'] == 'received':
print( 'Recieved in {}'.format(ctime(meta['received'])) )
elif meta['state'] == 'burned':
print( 'Burned in {}'.format(ctime(meta['updated'])))
else:
print('Not Recieved')
else:
expired = meta['secret_ttl']<=0;
print( 'Expired' if expired else 'Expiring:', ctime(t+meta['secret_ttl']) )
if 'passphrase_required' in meta:
print( 'Passphrase required' if meta['passphrase_required'] else 'No passphrase' )
if 'recipient' in meta and meta['recipient']:
print( 'Recipient:', meta['recipient'] )
if print_meta:
mexpired = meta['metadata_ttl']<=0;
print( 'Metadata key:', meta['metadata_key'] )
print( 'Expired' if mexpired else 'Expiring:', ctime(t+meta['metadata_ttl']) )
if __name__ == '__main__':
print_metadata = print_metadata_default
MODES = 'CSGRMLB'
try:
opts, args = getopt(argv[1:],
MODES #Modes
+ 'vqi:e:a:k:u' # Common options
+ 's:p:t:f:r:m' # -S share options
)
except goerror as err:
print(err)
usage()
exit(2)
modes=[ k[0] for k in filter(lambda a: a[0][1:] in MODES and not a[0][2:], opts)]
if len(modes) == 0:
mode = '-S'
else:
mode = modes[-1]
if mode == '-C':
if check_availability():
exit(0)
else:
exit(1)
key = ''
identity = list(get_identity('DEFAULT'))
email = ''
apikey = ''
verbose = False
print_url = False
for o in opts:
k = o[0]
arg = o[1]
if '-e' == k:
email = arg
continue
if '-a' == k:
apikey = arg
continue
if '-i' == k:
i = get_identity(arg)
if i == None:
print('[ERROR] No identity {} found'.format(arg))
exit(1)
identity = i
if '-q' == k:
print = print_dummy
if '-v' == k:
verbose = True
if '-u' == k:
print_url = True
if len(email) != 0:
identity[0] = email
if len(apikey) != 0:
identity[1] = apikey
if len(identity[0]) == 0 or len(identity[1]) == 0:
auth = None
else:
auth = HTTPBasicAuth(*identity)
# Sharing a secret
if mode == '-S':
data = {
'secret' : '',
'passphrase' : '',
'ttl' : str(7*24*60*60), # default: 1 week
'recipient' : ''
}
quiet=False
secret=[]
print_secret=True
for o in opts:
k = o[0]
arg = o[1]
if '-f' == k:
try:
with open(arg) as f:
secret.append(f.read())
except IOError as err:
print(err)
exit(1)
if '-s' == k:
secret.append(arg)
continue
if '-t' == k:
data['ttl'] = str(parsetime(arg))
continue
if '-r' == k:
data['recipient'] = arg;
continue
if '-p' == k:
data['passphrase'] = arg
continue
if '-m' == k:
print_secret = False
continue
if len(args) != 0:
secret.append(' '.join(args))
if len(secret) == 0:
print( 'ERROR: No secret provided' )
exit(1)
data['secret'] = '\n'.join(secret)
# print(data)
meta = share_secret(data, auth)
if meta == None:
exit(3)
if verbose:
print_metadata(meta)
elif print_secret:
if print_url:
print(secret_url.format(meta['secret_key']))
else:
print(meta['secret_key'])
else:
if print_url:
print(private_url.format(meta['metadata_key']))
else:
print(meta['metadata_key'])
exit(0)
# Checking a metadata
if mode == '-M':
data = { 'metadata_key' : '' }
metadata_key = key
if len(metadata_key) == 0:
metadata_key=' '.join(args)
if len(metadata_key) == 0:
print('[ERROR] No metadata key was provided')
exit(2)
data['metadata_key'] = metadata_key
meta = retrieve_metadata(data, auth=auth)
if meta == None:
exit(3)
if verbose:
print_metadata(meta, print_meta=False)
elif print_secret:
if 'secret_key' not in meta:
print('No secret key')
exit(4)
if print_url:
print(secret_url.format(meta['secret_key']))
else:
print(meta['secret_key'])
else:
if 'metadata_key' not in meta:
print('No metadata key')
exit(3)
if print_url:
print(metadata_url.format(meta['metadata_key']))
else:
print(meta['metadata_key'])
exit(0)
# Retrieve a secret
if mode == '-R':
data = { 'secret_key' : '' }
secret_key = key
if len(secret_key) == 0:
secret_key = ' '.join(args)
if len(secret_key) == 0:
print('[ERROR] No secret key was provided')
exit(2)
data['secret_key'] = secret_key
response = retrieve_secret(data, auth=auth)
if response == None:
exit(3)
if verbose:
print('Secret Key:', response['secret_key'])
print('Value:', response['value'])
else:
print(response['value'])
exit(0)
# Generate a secret
if mode == '-G':
# print( 'Not supported yet' )
# exit(1)
data = {
'passphrase' : '',
'ttl' : str(7*24*60*60), # default: 1 week
'recipient' : ''
}
print_secret = True
for o in opts:
k = o[0]
arg = o[1]
if '-t' == k:
data['ttl'] = str(parsetime(arg))
continue
if '-r' == k:
data['recipient'] = arg;
continue
if '-p' == k:
data['passphrase'] = arg
continue
if '-m' == k:
print_secret = False
continue
# print(data)
meta = generate_secret(data, auth)
if meta == None:
exit(3)
if verbose:
print_metadata(meta)
elif print_secret:
if print_url:
print(secret_url.format(meta['secret_key']))
else:
print(meta['secret_key'])
else:
if print_url:
print(private_url.format(meta['metadata_key']))
else:
print(meta['metadata_key'])
exit(0)
# Retrieve set of metadata for last secrets
if mode == '-L':
print( 'Not supported yet' )
exit(1)
# Burn a Secret
if mode == '-B':
data = { 'metadata_key' : '' }
metadata_key = key
if len(metadata_key) == 0:
metadata_key=' '.join(args)
if len(metadata_key) == 0:
print('[ERROR] No metadata key was provided')
exit(2)
data['metadata_key'] = metadata_key
meta = burn_secret(data, auth=auth)
if meta == None:
exit(3)
# print("Burned")
print_metadata(meta, print_meta=False)
exit(0)
usage()
exit(1)