Repository navigation
Expand file tree
/
Copy pathtigerpass.py
More file actions
429 lines (395 loc) · 16.6 KB
/
Copy pathtigerpass.py
File metadata and controls
429 lines (395 loc) · 16.6 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
import os
import json
import base64
import secrets
import string
import sys
from Crypto.Cipher import AES
from Crypto.Protocol.KDF import PBKDF2
from Crypto.Random import get_random_bytes
import pyotp
import qrcode
from PyQt6.QtWidgets import (QApplication, QMainWindow, QWidget, QVBoxLayout,
QHBoxLayout, QPushButton, QLabel, QLineEdit, QTreeWidget,
QTreeWidgetItem, QMessageBox, QInputDialog, QScrollArea, QSizePolicy)
from PyQt6.QtCore import Qt, QSize
from PyQt6.QtGui import QIcon, QPixmap, QFont, QFontDatabase
import qdarktheme
from pathlib import Path
class PasswordManager:
def __init__(self, db_file="passwords.dat"):
self.db_file = db_file
self.master_password = None
self.salt = None
self.key = None
self.data = {}
self.email = None
self.otp_secret = None # Persisted 2FA secret
def initialize(self, email, master_password):
self.email = email
self.master_password = master_password
self.salt = get_random_bytes(16)
self.key = self.derive_key(master_password, self.salt)
self.save_data()
def derive_key(self, master_password, salt):
return PBKDF2(master_password, salt, dkLen=32, count=1000000)
def pad(self, txt):
bs = AES.block_size
padding_len = bs - len(txt) % bs
return txt + bytes([padding_len]) * padding_len
def unpad(self, txt):
padding_len = txt[-1]
return txt[:-padding_len]
def encrypt(self, plaintext):
IV = get_random_bytes(16)
cipher = AES.new(self.key, AES.MODE_CBC, IV)
padded_text = self.pad(plaintext.encode("utf-8"))
ciphertext = cipher.encrypt(padded_text)
return base64.b64encode(IV + ciphertext).decode("utf-8")
def decrypt(self, b64_ciphertext):
data = base64.b64decode(b64_ciphertext)
IV = data[:16]
ciphertext = data[16:]
cipher = AES.new(self.key, AES.MODE_CBC, IV)
padded_plaintext = cipher.decrypt(ciphertext)
return self.unpad(padded_plaintext).decode("utf-8")
def add_entry(self, account, username, password):
entry = {"username": username, "password": password}
encrypted_entry = self.encrypt(json.dumps(entry))
self.data[account] = encrypted_entry
self.save_data()
def get_entry(self, account):
enc_entry = self.data.get(account)
if not enc_entry:
return None
plaintext = self.decrypt(enc_entry)
return json.loads(plaintext)
def get_all_accounts(self):
return list(self.data.keys())
def save_data(self):
with open(self.db_file, "w") as f:
json.dump({
"email": self.email,
"salt": base64.b64encode(self.salt).decode("utf-8") if self.salt else None,
"data": self.data,
"otp_secret": self.otp_secret
}, f)
def load_data(self):
if os.path.exists(self.db_file):
with open(self.db_file, "r") as f:
content = json.load(f)
self.email = content.get("email")
self.salt = base64.b64decode(content["salt"]) if content.get("salt") else None
self.key = self.derive_key(self.master_password, self.salt)
self.data = content.get("data", {})
self.otp_secret = content.get("otp_secret")
return True
return False
def generate_password(length=16):
chars = string.ascii_letters + string.digits + string.punctuation
return ''.join(secrets.choice(chars) for _ in range(length))
def generate_2fa_secret():
return pyotp.random_base32()
class ModernPasswordManagerGUI(QMainWindow):
def __init__(self):
super().__init__()
self.pm = PasswordManager()
self.totp = None
self.init_ui()
self.setup_styling()
def setup_styling(self):
# Custom widget styling; additional dark theme applied via qdarktheme.load_stylesheet.
self.setStyleSheet("""
QMainWindow {
background-color: #1a1a1a;
}
QLabel {
color: #ffffff;
font-size: 14px;
}
QLineEdit {
padding: 10px;
border: 2px solid #2979ff;
border-radius: 8px;
background-color: #2d2d2d;
color: white;
font-size: 14px;
min-height: 20px;
}
QPushButton {
background-color: #2979ff;
color: white;
border: none;
border-radius: 8px;
padding: 10px 20px;
font-size: 14px;
min-width: 120px;
min-height: 40px;
}
QPushButton:hover {
background-color: #1565c0;
}
QPushButton:pressed {
background-color: #0d47a1;
}
QTreeWidget {
background-color: #2d2d2d;
border: 1px solid #3d3d3d;
border-radius: 8px;
color: white;
}
QTreeWidget::item {
padding: 8px;
margin: 2px;
}
QTreeWidget::item:selected {
background-color: #2979ff;
border-radius: 4px;
}
QScrollBar:vertical {
border: none;
background-color: #2d2d2d;
width: 10px;
margin: 0px;
}
QScrollBar::handle:vertical {
background-color: #4d4d4d;
border-radius: 5px;
min-height: 20px;
}
QScrollBar::handle:vertical:hover {
background-color: #5d5d5d;
}
""")
def init_ui(self):
self.setWindowTitle("TigerPass")
# This is the window size, if anyone wants to change it.
self.setMinimumSize(1000, 800)
if os.path.exists(self.pm.db_file):
self.build_email_verification()
else:
self.build_first_time_setup()
def create_modern_button(self, text, on_click=None, accent=False):
btn = QPushButton(text)
if accent:
btn.setStyleSheet("""
QPushButton {
background-color: #2979ff;
color: white;
}
QPushButton:hover {
background-color: #1565c0;
}
""")
if on_click:
btn.clicked.connect(on_click)
return btn
def create_modern_input(self, placeholder="", password=False):
line_edit = QLineEdit()
line_edit.setPlaceholderText(placeholder)
if password:
line_edit.setEchoMode(QLineEdit.EchoMode.Password)
return line_edit
def build_first_time_setup(self):
central_widget = QWidget()
self.setCentralWidget(central_widget)
layout = QVBoxLayout(central_widget)
layout.setSpacing(20)
layout.setContentsMargins(40, 40, 40, 40)
header = QLabel("First Time Setup")
header.setStyleSheet("font-size: 24px; font-weight: bold; color: white;")
layout.addWidget(header, alignment=Qt.AlignmentFlag.AlignCenter)
self.email_input = self.create_modern_input("Enter Email")
self.master_pass_input = self.create_modern_input("Create Master Password", password=True)
self.confirm_pass_input = self.create_modern_input("Confirm Master Password", password=True)
for widget in [self.email_input, self.master_pass_input, self.confirm_pass_input]:
layout.addWidget(widget)
setup_btn = self.create_modern_button("Setup Account", self.complete_setup, accent=True)
layout.addWidget(setup_btn)
layout.addStretch()
def build_main_interface(self):
central_widget = QWidget()
self.setCentralWidget(central_widget)
layout = QVBoxLayout(central_widget)
layout.setSpacing(20)
layout.setContentsMargins(20, 20, 20, 20)
header_layout = QHBoxLayout()
add_btn = self.create_modern_button("Add Entry", self.add_entry_dialog)
generate_btn = self.create_modern_button("Generate Password", self.show_generated_password)
header_layout.addWidget(add_btn)
header_layout.addWidget(generate_btn)
layout.addLayout(header_layout)
self.tree = QTreeWidget()
self.tree.setHeaderLabels(["Account", "Username", "Password (Double-click to Reveal)"])
self.tree.setColumnWidth(0, 200)
self.tree.setColumnWidth(1, 200)
self.tree.setColumnWidth(2, 200)
self.tree.itemDoubleClicked.connect(self.toggle_password_visibility)
layout.addWidget(self.tree)
self.update_account_list()
def toggle_password_visibility(self, item, column):
if column == 2:
account = item.text(0)
entry = self.pm.get_entry(account)
if item.text(2) == '••••••••':
item.setText(2, entry['password'])
else:
item.setText(2, '••••••••')
def update_account_list(self):
self.tree.clear()
for account in self.pm.get_all_accounts():
entry = self.pm.get_entry(account)
if entry:
item = QTreeWidgetItem([account, entry['username'], '••••••••'])
self.tree.addTopLevelItem(item)
def add_entry_dialog(self):
account, ok = QInputDialog.getText(self, 'Add Entry', 'Enter Account Name:', QLineEdit.EchoMode.Normal)
if ok and account:
username, ok = QInputDialog.getText(self, 'Add Entry', 'Enter Username:', QLineEdit.EchoMode.Normal)
if ok and username:
msg_box = QMessageBox()
msg_box.setWindowTitle("Password Options")
msg_box.setText("Would you like to generate a secure random password?")
msg_box.setStandardButtons(QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No)
msg_box.setDefaultButton(QMessageBox.StandardButton.Yes)
if msg_box.exec() == QMessageBox.StandardButton.Yes:
password = generate_password()
else:
password, ok = QInputDialog.getText(self, 'Add Entry', 'Enter Password:', QLineEdit.EchoMode.Password)
if not ok or not password:
return
self.pm.add_entry(account, username, password)
self.update_account_list()
QMessageBox.information(self, 'Success', f"Entry for '{account}' added successfully.")
def show_generated_password(self):
password = generate_password()
msg = QMessageBox(self)
msg.setWindowTitle("Generated Password")
msg.setText("Your secure password:")
msg.setInformativeText(password)
msg.setStandardButtons(QMessageBox.StandardButton.Ok)
msg.exec()
def verify_login(self):
master_password = self.master_pass_input.text().strip()
code = self.totp_input.text().strip()
if not self.totp.verify(code):
QMessageBox.critical(self, "Error", "Invalid 2FA Code")
return
self.pm.master_password = master_password
try:
if not self.pm.load_data():
QMessageBox.critical(self, "Error", "Invalid master password!")
return
except Exception as e:
QMessageBox.critical(self, "Error", "Invalid master password!")
return
self.build_main_interface()
def complete_setup(self):
email = self.email_input.text().strip()
password = self.master_pass_input.text()
confirm = self.confirm_pass_input.text()
if not email or '@' not in email:
QMessageBox.critical(self, "Error", "Please enter a valid email address.")
return
if password != confirm:
QMessageBox.critical(self, "Error", "Passwords do not match!")
return
if len(password) < 8:
QMessageBox.critical(self, "Error", "Password must be at least 8 characters long!")
return
self.pm.initialize(email, password)
# Generate and persist the OTP secret during first-time setup.
secret = generate_2fa_secret()
self.pm.otp_secret = secret
self.pm.save_data()
self.totp = pyotp.TOTP(secret)
self.build_2fa_setup(email, secret)
def build_2fa_setup(self, email, secret):
central_widget = QWidget()
self.setCentralWidget(central_widget)
layout = QVBoxLayout(central_widget)
layout.setSpacing(20)
layout.setContentsMargins(40, 40, 40, 40)
header = QLabel("2FA Setup")
header.setStyleSheet("font-size: 24px; font-weight: bold; color: white;")
layout.addWidget(header, alignment=Qt.AlignmentFlag.AlignCenter)
uri = self.totp.provisioning_uri(name=email, issuer_name="TigerPass")
qr = qrcode.QRCode(box_size=10, border=2)
qr.add_data(uri)
qr.make(fit=True)
qr_image = qr.make_image(fill_color="black", back_color="white")
qr_pixmap = QPixmap.fromImage(qr_image.toqimage())
qr_label = QLabel()
qr_label.setPixmap(qr_pixmap)
qr_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
layout.addWidget(qr_label)
instructions = QLabel("Scan this QR code with your\n2FA app (e.g. Google Authenticator)")
instructions.setAlignment(Qt.AlignmentFlag.AlignCenter)
layout.addWidget(instructions)
self.verify_2fa_input = self.create_modern_input("Enter the 2FA Code from Your Authenticator App Here.")
layout.addWidget(self.verify_2fa_input)
verify_btn = self.create_modern_button("Complete Setup", self.verify_2fa_setup, accent=True)
layout.addWidget(verify_btn)
layout.addStretch()
def verify_2fa_setup(self):
code = self.verify_2fa_input.text().strip()
if not self.totp.verify(code):
QMessageBox.critical(self, "Error", "Invalid 2FA Code")
return
self.build_main_interface()
def build_email_verification(self):
central_widget = QWidget()
self.setCentralWidget(central_widget)
layout = QVBoxLayout(central_widget)
layout.setSpacing(20)
layout.setContentsMargins(40, 40, 40, 40)
header = QLabel("Email Verification")
header.setStyleSheet("font-size: 24px; font-weight: bold; color: white;")
layout.addWidget(header, alignment=Qt.AlignmentFlag.AlignCenter)
self.email_input = self.create_modern_input("Enter Email")
layout.addWidget(self.email_input)
continue_btn = self.create_modern_button("Continue", self.verify_email, accent=True)
layout.addWidget(continue_btn)
layout.addStretch()
def verify_email(self):
email = self.email_input.text().strip()
with open(self.pm.db_file, "r") as f:
content = json.load(f)
stored_email = content.get("email")
otp_secret = content.get("otp_secret")
if email != stored_email:
QMessageBox.critical(self, "Error", "Email not recognized!")
return
if not otp_secret:
QMessageBox.critical(self, "Error", "OTP Configuration Missing!")
return
self.pm.otp_secret = otp_secret
self.totp = pyotp.TOTP(otp_secret)
self.build_login_interface()
def build_login_interface(self):
central_widget = QWidget()
self.setCentralWidget(central_widget)
layout = QVBoxLayout(central_widget)
layout.setSpacing(20)
layout.setContentsMargins(40, 40, 40, 40)
header = QLabel("Login")
header.setStyleSheet("font-size: 24px; font-weight: bold; color: white;")
layout.addWidget(header, alignment=Qt.AlignmentFlag.AlignCenter)
self.master_pass_input = self.create_modern_input("Enter Master Password", password=True)
self.totp_input = self.create_modern_input("Enter 2FA Code")
layout.addWidget(self.master_pass_input)
layout.addWidget(self.totp_input)
login_btn = self.create_modern_button("Login", self.verify_login, accent=True)
layout.addWidget(login_btn)
layout.addStretch()
def main():
app = QApplication(sys.argv)
# Apply the dark theme using qdarktheme's load_stylesheet function.
app.setStyleSheet(qdarktheme.load_stylesheet())
QFontDatabase.addApplicationFont(":/fonts/Roboto-Regular.ttf")
app.setFont(QFont("Roboto", 10))
window = ModernPasswordManagerGUI()
window.show()
sys.exit(app.exec())
if __name__ == "__main__":
main()