Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error handling when a key is not loaded #57

Merged
merged 1 commit into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added __pycache__/manager.cpython-312.pyc
Binary file not shown.
14 changes: 10 additions & 4 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
from manager import PasswordManager


def validate_key_loaded(pm : PasswordManager):
if not pm.keyloaded:
print("Key not loaded. Please load a key first.")
return False
return True

def main():
password = {
"gmail": "password1",
Expand Down Expand Up @@ -29,17 +35,17 @@ def main():
elif choice == '2':
path = input("Enter key file path: ").strip()
pm.load_key(path)
elif choice == '3':
elif choice == '3' and validate_key_loaded(pm):
path = input("Enter password file path: ").strip()
pm.create_password_file(path, password)
elif choice == '4':
elif choice == '4' and validate_key_loaded(pm):
path = input("Enter password file path: ").strip()
pm.load_password_file(path)
elif choice == '5':
elif choice == '5' and validate_key_loaded(pm):
site = input("Enter site: ").strip()
password = input("Enter password: ").strip()
pm.add_password(site, password)
elif choice == '6':
elif choice == '6' and validate_key_loaded(pm):
site = input("Enter site: ").strip()
print(f"Password for {site}: {pm.get_password(site)}")
elif choice == 'q':
Expand Down
3 changes: 3 additions & 0 deletions manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@ def __init__(self):
self.key = None
self.password_file = None
self.password_dict = {}
self.keyloaded = False

def create_key(self, path):
self.key = Fernet.generate_key()
with open(path, 'wb') as f:
f.write(self.key)
self.keyloaded = True

def load_key(self, path):
with open(path, 'rb') as f:
self.key = f.read()
self.keyloaded = True

def create_password_file(self, path, initial_values=None):
self.password_file = path
Expand Down