-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpwarden.py
97 lines (80 loc) · 2.42 KB
/
pwarden.py
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
#!/bin/python
import subprocess
import os
import json
import re
import pyperclip
import argparse
from termcolor import colored
from PyInquirer import prompt, Separator
from shutil import which
def is_tool(name):
"""Check whether `name` is on PATH and marked as executable."""
return which(name) is not None
if is_tool('bw') is False:
exit('bw bin not found in your path')
bw_path = which('bw')
command = '{} list items'.format(bw_path).split(' ')
my_env = os.environ.copy()
# Openssl fix https://github.com/bitwarden/clients/issues/2726
my_env['OPENSSL_CONF'] = '/dev/null'
#my_env['BW_SESSION'] = '<super_secret_key>'
parser = argparse.ArgumentParser(
description='Find & copy passwords to your clipboard from Bitwarden vault'
)
parser.add_argument('string',
help='String to find')
args = vars(parser.parse_args())
to_find = args['string']
result = subprocess.Popen(command, env=my_env, stdout=subprocess.PIPE)
output = result.stdout.read().decode("utf-8")
vault_items = json.loads(output)
items = {}
# Re-arrange the items
for item in vault_items:
if 'login' in item:
items[item['name']] = item['login']
#del items[None]
items_keys = list(items.keys())
r = re.compile(".*" + to_find + ".*", re.IGNORECASE)
match_items = list(filter(r.match, items_keys))
if len(match_items) > 1:
action_question = [
{
'type': 'list',
'name': 'item_selected',
'message': 'Which credential do you need?',
'choices': match_items,
},
]
action_selected = prompt(action_question)
# print('selected:' + action_selected['item_selected'])
item_name = action_selected['item_selected']
elif len(match_items) == 1 :
item_name = match_items[0]
else:
print('No credentials found.')
exit(1)
item_credentials = items[item_name]
pyperclip.copy(str(item_credentials['password']))
if 'uris' in item_credentials:
if len(item_credentials['uris']) > 0:
uri = item_credentials['uris'][0]['uri']
else:
uri = 'None'
else:
uri = 'None'
colored_passwd = colored(
item_credentials['password'],
'white',
'on_white')
colored_item_name = colored(
item_name,
'yellow',
attrs=["bold"],
)
print('Full Item Name: {}'.format(colored_item_name))
print('URL: {}'.format(uri))
print('username: {}'.format(item_credentials['username']))
print('Password: {}'.format(colored_passwd))
print('Password copied to clipboard')