-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathingpass-helper
executable file
·67 lines (55 loc) · 1.84 KB
/
ingpass-helper
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
#!/usr/bin/env python
import getpass
import sys
import lastpass
import configparser
import os.path
config_path_template = '~/.ingpass-helper/config'
class AmbigousAccountException(Exception):
pass
def get_user_name():
config_path = os.path.expanduser(config_path_template)
parser = configparser.ConfigParser(allow_no_value=True)
if os.path.isfile(config_path):
parser.read(config_path)
return parser.get('base','user.name')
else:
user_name = input('Enter LastPass user name (email): ')
parser.add_section('base')
parser.set('base','user.name',user_name)
with open(config_path,'w') as config_file:
print(f'Writing config file to {config_path}')
parser.write(config_file)
print('User name was saved to ' + config_path)
return user_name
def get_lastpass_vault():
user_name = get_user_name()
password = getpass.getpass('Enter Lastpass password for user ' + user_name + ':')
return lastpass.Vault.open_remote(user_name,password)
def print_row(account_id,account_url):
print(account_id.rjust(15,' ') + ' | ' + account_url.rjust(50,' '))
def search(pattern):
vault = get_lastpass_vault()
return [a for a in vault.accounts if str(pattern) in str(a.url)]
def print_accounts(accounts):
print_row('id','url')
for a in accounts:
print_row(a.id,a.url)
def show_filtered_password(pattern, positions):
accounts = search(pattern)
if len(accounts) > 1:
raise AmbigousAccountException("Provided pattern does not identify account unambigously. Try to narrow it.")
account = accounts[0]
print(''.join([str(account.password)[int(i)-1] for i in positions]))
def main():
mode = sys.argv[1]
key_name = sys.argv[2]
if mode == "search":
accounts = search(key_name)
print_accounts(accounts)
elif mode == "get":
positions = sys.argv[3:]
show_filtered_password(key_name, positions)
else:
raise ValueError("Incorrect parameter name")
main()