-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautofiller.py
225 lines (203 loc) · 8.64 KB
/
autofiller.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
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
#!/usr/bin/env python
import clipboard
import pyautogui
import pywinctl
import platform
import psutil
import os
class AutoFiller:
def __init__(self, client, defaults):
self.sender = client.send_command
self.actions = defaults.fillactions
self.close = defaults.close
self.sync = defaults.sync
self.do_raise = defaults.do_raise
self.message = None
def get_active_process(self):
try:
# Get the focused window and retrieve the process name
active_window = pywinctl.getActiveWindow()
process_name = active_window.getAppName()
# Get a list of processes with matching names
# process_list = list(filter(lambda p: p.name() == process_name, psutil.process_iter()))
# process_list = None
if process_name:
process_info = {"window": active_window, "name": process_name}
return process_info
else:
print(f"Process is not set")
return None
# if process_list:
# # Sort the process list by creation time (newest first)
# sorted_processes = sorted(process_list, key=lambda p: p.create_time(), reverse=True)
#
# # Get the newest process (most recent)
# newest_process = sorted_processes[0]
# process_info = {
# 'name': process_name,
# 'pid': newest_process.pid,
# 'cmdline': newest_process.cmdline(),
# 'status': newest_process.status()
# }
# return process_info
except Exception as e:
print(f"Unable to find process name for active window: {e}")
return None
def get_login_ids(self, process_name):
"""Query Bitwarden for login item related to the process."""
if process_name:
query = f"pcprocess://{process_name}"
print(
f"Found active process, running query '{query}' to look for login item..."
)
response = self.sender(
"list",
["items"],
dictfilter={"uriquery": query, "returns": "id"},
)
return response
else:
print(
f"With the process name {process_name} could not be determined."
)
return None
def get_login_item(self, item_id):
if item_id:
response = self.sender("get", ["item", item_id])
return response
def get_login_totp(self, item_id):
if item_id:
response = self.sender("get", ["totp", item_id])
return response
def accountselect(self, items, allactions):
activeaccount = 0 # The current selected account
lastacc = len(items) # The number of available accounts
nractions = "" # Buffer for collecting numeric actions
letters = None # Current selected letter (account switcher)
haveyield = (
False # Whether a value has been yielded in this iteration
)
if allactions:
for action in str(
allactions + "C"
): # Append 'C' to ensure selection execution
# Handle letters (account switching commands)
if action in "ABCD":
if letters is None:
letters = int(activeaccount)
if action == "C": # Select first account
activeaccount = 0
elif action == "D": # Select last account
activeaccount = lastacc - 1
elif action == "A": # Move to the next account
activeaccount = (activeaccount + 1) % lastacc
elif action == "B": # Move to the previous account
activeaccount = (
activeaccount - 1 + lastacc
) % lastacc
# Handle previously collected actions for the last account (if any)
if letters is not None and nractions:
yield items[letters], nractions
haveyield = True
letters = None
nractions = ""
# Handle numeric actions (to be applied to the selected account)
elif action.isnumeric():
nractions += action
if not haveyield:
yield None, None
else:
yield None, None
def autofill(self, login_id, actions):
item = self.get_login_item(login_id)
logindata = item.get("data", {}).get("login", {})
store = ""
usern = logindata.get("username")
if usern:
store = str(usern)
passw = logindata.get("password")
if passw and not store:
store = str(usern)
istotp = logindata.get("totp")
totp = None
if actions:
for action in actions:
if action == "1":
if usern:
store = str(usern)
else:
store = ""
elif action == "2":
if passw:
store = str(passw)
else:
store = ""
elif action == "3":
if istotp:
if not totp:
totp = str(
self.get_login_totp(login_id)
.get("data", {})
.get("data", "")
)
store = str(totp)
else:
store = ""
elif action == "4" and store:
pyautogui.write(store)
elif action == "5" and store:
clipboard.copy(store)
elif action == "6":
pyautogui.press("enter")
else:
pyautogui.press("tab")
def fill_process(self):
"""Main function to perform autofill based on the active process."""
self.message = None
if self.close:
self.sender("exit")
print("Exit command was send to deamon and now closing client...")
self.message = "Exit command was send to deamon"
else:
if self.sync:
sres = self.sender("sync")
cac = self.sender(
"list", ["items"]
) # run list command to cache results
if not sres.get("success"):
print(f"Problem syncing, skipping: {sres.get("message")}")
else:
print("Syncing successful")
if not cac.get("success"):
print(
f"There was a issue with getting the bitwaden vault data {cac.get("message")}\nExiting..."
)
if self.do_raise:
raise ValueError(f"There was a issue with getting the bitwaden vault data {cac.get("message")}")
else:
exit(1)
process_info = self.get_active_process()
if process_info.get("name"):
process_name = process_info.get("name")
login_items = self.get_login_ids(process_name)
if login_items and login_items.get("success"):
print("Login item retrieved successfully.")
for item_id, actions in self.accountselect(
login_items.get("returns", []), self.actions
):
if item_id:
self.autofill(item_id, actions)
else:
print("Actions not set or valid, closing...")
self.message = "Actions not set or valid"
elif login_items.get("message") == "Not found.":
print("No login item with that process name, closing...")
self.message = "No login item with that process name"
else:
print(
f"Failed to retrieve login item with message: {login_items.get("message")}\nClosing..."
)
self.message = f"Failed to retrieve login item with message: {login_items.get("message")}"
else:
print("Could not determine the active process.")
self.message = "Could not determine the active process."