-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeyringwarden.py
102 lines (82 loc) · 2.91 KB
/
keyringwarden.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
import keyring
import random
import json
# List of preprogrammed responses
responses = [
"Welcome, sire, your keyrings:",
"Yes sire, your keys are here",
"Hail my sire, I have your keys as you requested",
"Salutations, sire, I was not sleeping, here is your keyring"
]
# Randomly select a response
response = random.choice(responses)
def save_key(service, name):
keys_file = 'keys.json'
# Try to load existing keys
try:
with open(keys_file, 'r') as file:
keys = json.load(file)
except (FileNotFoundError, json.JSONDecodeError):
keys = []
# Add the new key info
keys.append({'service': service, 'name': name})
# Write the updated keys list to the file
with open(keys_file, 'w') as file:
json.dump(keys, file)
def add_key():
service = input("Enter service: ")
name = input("Enter name: ")
key = input("Enter key: ")
keyring.set_password(service, name, key)
save_key(service, name)
print("Your key has been saved!")
def remove_key_from_json(service, name):
keys_file = 'keys.json'
with open(keys_file, 'r') as file:
keys = json.load(file)
# Filter out the key to be removed
keys = [key_info for key_info in keys if not (key_info['service'] == service and key_info['name'] == name)]
# Write the updated keys list back to the file
with open(keys_file, 'w') as file:
json.dump(keys, file)
def remove_key():
service = input("Enter service: ")
name = input("Enter name: ")
try:
keyring.delete_password(service, name)
remove_key_from_json(service, name) # Call function to remove key from json
print("Your key has been removed!")
except keyring.errors.PasswordDeleteError:
print("The specified key was not found!")
def list_keys():
keys_file = 'keys.json'
# Try to load and display keys
try:
with open(keys_file, 'r') as file:
keys = json.load(file)
for key_info in keys:
service = key_info['service']
name = key_info['name']
# Retrieve and display the key for each stored service-name pair
key = keyring.get_password(service, name)
print(f"\t{service}/{name}: {key if key else 'Key not found'}")
except (FileNotFoundError, json.JSONDecodeError):
print("No keys found.")
while True:
print(f'\nThe Keyring Warden greets you: "{response}"')
print("1. Add a new key")
print("2. Remove an existing key")
print("3. List keys")
print("4. Exit")
choice = input("Enter your choice: ")
if choice == "1":
add_key()
elif choice == "2":
remove_key()
elif choice == "3":
list_keys()
elif choice == "4":
print("Leaving The Keyring Warden.")
break
else:
print("Invalid choice.")