Skip to content

Commit 27d6c6a

Browse files
committed
Change interface and add checks to edit/remove for client ids that do not exist
1 parent 6a657ce commit 27d6c6a

File tree

1 file changed

+64
-26
lines changed

1 file changed

+64
-26
lines changed

server/src/tools/client_credentials_admin.py

Lines changed: 64 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def confirm_dialogue(entry: dict) -> bool:
9191
"""Prompts the user to confirm their changes"""
9292
print(entry)
9393
while True:
94-
confirm = input("Confirm(y/n) that this entry looks correct: ")
94+
confirm = input("Confirm that this entry looks correct (y/n): ")
9595
if confirm == "y":
9696
return True
9797

@@ -102,22 +102,34 @@ def confirm_dialogue(entry: dict) -> bool:
102102
print("Enter y/n")
103103

104104

105+
def check_client_exists(db, client_id: str) -> bool:
106+
"""Checks if client id exists in client_permissions"""
107+
return (
108+
db.client_permissions.count_documents(
109+
{"client_id": client_id}, limit=1
110+
)
111+
!= 0
112+
)
113+
114+
105115
def create_client_credential(db):
106116
"""
107117
Creates new client_id, client_secret pair with max_priority
108118
and allowed_queues
109119
"""
110120
client_id = input("Enter the new client_id: ")
111-
if (
112-
db.client_permissions.count_documents(
113-
{"client_id": client_id}, limit=1
114-
)
115-
== 1
116-
):
117-
print("Client id already exists!")
121+
if check_client_exists(db, client_id):
122+
print("Client id already exists!\n")
118123
return
119124

120125
client_secret = getpass.getpass("Enter the new client_secret: ")
126+
client_secret_confirm = getpass.getpass(
127+
"Enter the new client_secret again: "
128+
)
129+
if client_secret != client_secret_confirm:
130+
print("New client secrets do not match!\n")
131+
return
132+
121133
client_secret_hash = bcrypt.hashpw(
122134
client_secret.encode("utf-8"), bcrypt.gensalt()
123135
).decode()
@@ -133,7 +145,7 @@ def create_client_credential(db):
133145
if confirm_dialogue(entry):
134146
entry["client_secret_hash"] = client_secret_hash
135147
db.client_permissions.insert_one(entry)
136-
print("Entry has been created and stored in database")
148+
print("Entry has been created and stored in database\n")
137149

138150

139151
def edit_client_credential(db):
@@ -143,6 +155,10 @@ def edit_client_credential(db):
143155
"""
144156

145157
client_id = input("Enter the client_id you wish to edit: ")
158+
if not check_client_exists(db, client_id):
159+
print("Client id not in database!\n")
160+
return
161+
146162
max_priority = handle_max_priority_input()
147163
allowed_queues = handle_allowed_queues_input()
148164
confirm_output = confirm_dialogue(
@@ -163,39 +179,57 @@ def edit_client_credential(db):
163179
},
164180
return_document=ReturnDocument.AFTER,
165181
)
166-
print("Entry updated successfully")
182+
print("Entry updated successfully\n")
167183

168184

169185
def remove_client_credential(db):
170186
"""Removes a client_id and client_secret pair from the database"""
171187
client_id = input("Enter the client_id you wish to delete: ")
188+
if not check_client_exists(db, client_id):
189+
print("Client id not in database!\n")
190+
return
191+
172192
if confirm_dialogue({"client_id": client_id}):
173193
db.client_permissions.delete_one({"client_id": client_id})
174-
print("Entry deleted successfully")
194+
print("Entry deleted successfully\n")
195+
196+
197+
def check_queue_exists(db, queue_name: str) -> bool:
198+
"""Checks if queue is in the restricted_queues collection"""
199+
return (
200+
db.restricted_queues.count_documents(
201+
{"queue_name": queue_name}, limit=1
202+
)
203+
!= 0
204+
)
175205

176206

177207
def add_restricted_queue(db):
178208
"""Adds a restricted queue to the database"""
179209
queue_name = input("Enter the name of the restricted queue to add: ")
210+
if check_queue_exists(db, queue_name):
211+
print("Restricted queue already exists!\n")
212+
return
213+
180214
queue_entry = {"queue_name": queue_name}
181215
if confirm_dialogue(queue_entry):
182-
queue_exists_num = db.restricted_queues.count_documents(
183-
queue_entry, limit=1
184-
)
185-
if queue_exists_num != 0:
186-
print("Restricted queue already exists!")
187-
return
188216
db.restricted_queues.insert_one(queue_entry)
189-
print("Restricted queue sucessfully added")
217+
print("Restricted queue sucessfully added\n")
190218

191219

192220
def remove_restricted_queue(db):
193221
"""Removes a restricted queue from the database"""
194-
queue_name = input("Enter the client_id you wish to delete: ")
222+
queue_name = input(
223+
"Enter the name of the restricted queue you wish to delete: "
224+
)
225+
if not check_queue_exists(db, queue_name):
226+
print("Restricted queue not in database!\n")
227+
return
228+
195229
queue_entry = {"queue_name": queue_name}
196230
if confirm_dialogue(queue_entry):
197231
db.restricted_queues.delete_one(queue_entry)
198-
print("Entry deleted successfully")
232+
print("Entry deleted successfully\n")
199233

200234

201235
def main():
@@ -205,11 +239,14 @@ def main():
205239
"""
206240
db = setup_database()
207241
while True:
208-
user_input = input(
209-
"Do you wish to create(c), edit(e), or remove(r) a client?"
210-
+ " You can also add a restricted queue(aq)"
211-
+ " or remove a restricted queue(rq). Enter q to quit: "
212-
)
242+
print("(c) Create client")
243+
print("(e) Edit client")
244+
print("(r) Remove client")
245+
print("(aq) Add restricted queue")
246+
print("(rq) Remove restricted queue")
247+
print("(q) Quit")
248+
249+
user_input = input("Enter your selection: ")
213250
if user_input == "c":
214251
create_client_credential(db)
215252
elif user_input == "e":
@@ -224,7 +261,8 @@ def main():
224261
sys.exit()
225262
else:
226263
print(
227-
"Invalid selection. Please enter 'c', 'e', 'r', 'aq', or 'rq'"
264+
"Invalid selection. Please enter "
265+
+ "'c', 'e', 'r', 'aq', 'rq', or 'q'\n"
228266
)
229267

230268

0 commit comments

Comments
 (0)