This repository has been archived by the owner on Dec 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lpfinder.py
81 lines (61 loc) · 2.62 KB
/
lpfinder.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
welcome_msg = '''This script takes 2 arguments:
1) the name of the account
2) the url to be redirected to
It works best with Launch Center Pro installed.
https://launchcenterpro.com/q9qmfg'''
import clipboard, console, keychain, sys, ui, webbrowser, os
try:
_, account_name, redirect_url = sys.argv
account_name = account_name.lower()
except ValueError:
print(welcome_msg)
sys.exit()
class AccountFinder(object):
def __init__(self, blob=None):
self.accounts = self.get_services(blob)
self.has_blob = (True if blob else False)
def get_services(self, blob=None):
if blob and os.path.isfile(blob):
import lastpass
email = keychain.get_password('lastpass_email', 'lastpass')
password = keychain.get_password('lastpass_master', 'lastpass') or ''
email, password = console.login_alert('LastPass login', '', email, password)
vault = lastpass.Vault.open_local(blob, email, password)
return [(x.name, x.username, x.password) for x in vault.accounts]
else:
return keychain.get_services()
def get_password(self, name, username):
if self.has_blob:
return next((x[2] for x in self.accounts if x[0] == name and x[1] == username), None)
else:
return keychain.get_password(name, username)
def find_matching_accounts(self, account_name):
return [(x[0] + ' - ' + x[1]) for x in self.accounts
if account_name in x[0].lower()]
@ui.in_background
def item_selected(sender):
acct = sender.items[sender.selected_row]['title'].split(' - ')
pwd = account_finder.get_password(acct[0], acct[1])
clipboard.set(pwd)
account_finder_view.close()
webbrowser.open(redirect_url)
@ui.in_background
def info_tapped(sender):
row = sender.items[sender.tapped_accessory_row]
console.alert(row['title'])
def make_tableview():
tv = ui.TableView()
tv.flex = 'WH'
tv.name = 'tableview'
return tv
def make_list(account_finder, account_name):
ds = ui.ListDataSource({'title': x, 'accessory_type': 'detail_button'} for x in account_finder.find_matching_accounts(account_name))
ds.action = item_selected
ds.accessory_action = info_tapped
return ds
FILENAME = os.path.join(os.getcwd(), '.lastpass.blob')
account_finder = AccountFinder(blob=FILENAME if os.path.exists(FILENAME)else None)
account_finder_view = ui.View()
account_finder_view.add_subview(make_tableview())
account_finder_view['tableview'].data_source = account_finder_view['tableview'].delegate = make_list(account_finder, account_name)
account_finder_view.present('fullscreen')