Skip to content
This repository was archived by the owner on Jan 30, 2025. It is now read-only.

Commit 623b82e

Browse files
Completing 1.2.2 - QoL updates, Security and bug fixes
1 parent 8c6b158 commit 623b82e

File tree

3 files changed

+38
-19
lines changed

3 files changed

+38
-19
lines changed

API/API_FrameWork.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,3 @@ def framework():
9292
msg = "An unexpected error occurred."
9393

9494
return msg, code
95-
96-
97-
print(framework())

DataBase.py

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,14 @@ def verify_password(self, username, password):
134134
except Exception:
135135
return False
136136

137-
def create_db(self, username, exclusion_titles):
137+
def create_db(self, username, exclusion_titles, password=None):
138138
"""
139139
Creates a new database entry for a user with the given username and exclusion titles.
140140
141141
Args:
142142
username (str): The username of the user.
143143
exclusion_titles (str): The exclusion titles for the user.
144+
password (str, optional): The password for the user. Defaults to None.
144145
145146
Returns:
146147
str: The password for the newly created user, or an error message if the username already exists or an exception occurs.
@@ -154,12 +155,15 @@ def create_db(self, username, exclusion_titles):
154155
existing_user = self.cursor.fetchone()
155156
self.disconnect()
156157

158+
alphabet = string.ascii_letters + string.digits
159+
if password is None:
160+
password_new = "".join(secrets.choice(alphabet) for _ in range(12))
161+
else:
162+
password_new = password
163+
157164
if existing_user:
158165
return "ERROR Username already exists. && 409"
159166

160-
alphabet = string.ascii_letters + string.digits
161-
password_new = "".join(secrets.choice(alphabet) for _ in range(12))
162-
163167
self.connect()
164168
self.cursor.execute(
165169
"INSERT INTO users (username, password) VALUES (?,?)",
@@ -476,13 +480,13 @@ def read_csv(file_path):
476480
# Populate the list with indices to check, excluding the URL column index
477481
for i in range(len(row)):
478482
if (
479-
i != 4
483+
i != 4
480484
): # Excluding the URL column index (assuming it's always the 5th column)
481485
indices_to_check.append(i)
482486

483487
# Use a generator expression to strip values and check for emptiness across the specified indices
484488
if not all(
485-
value.strip() for value in (row[i] for i in indices_to_check)
489+
value.strip() for value in (row[i] for i in indices_to_check)
486490
):
487491
return "ERROR Empty value found in CSV. && 400"
488492

@@ -569,16 +573,16 @@ def read_config(file_path):
569573
if missing_options:
570574
return f"ERROR Missing required options in config file: {missing_options} && 400"
571575
for option in required_options[
572-
:-2
573-
]: # Exclude 'debug' and 'points' from this check
576+
:-2
577+
]: # Exclude 'debug' and 'points' from this check
574578
try:
575579
int(config.get(section, option))
576580
except ValueError:
577581
return (
578582
f"ERROR Invalid value type for {option}: expected integer. && 400"
579583
)
580584
if config.getint(section, "hard") + config.getint(
581-
section, "medium"
585+
section, "medium"
582586
) + config.getint(section, "easy") != config.getint(
583587
section, "questions_amount"
584588
):
@@ -890,29 +894,40 @@ def init():
890894
log.info("Exam generated successfully based on the request")
891895
else:
892896
DATA = "ERROR Invalid Username or Password && 401"
897+
893898
elif api == "RUG":
894899
log.info(
895900
f"A request has been made to create a new user by the following username {username}"
896901
)
897902
DATA = um.create_db(username, exclusion_titles)
898903
if not check_ERROR(DATA):
899904
log.info("User created successfully based on the request")
905+
900906
elif api == "RUD":
901907
log.info(
902908
f"A request has been made to add the following exclusion titles {exclusion_titles} to the database for user {username}"
903909
)
904910
DATA = um.add_exclusion_db(username, exclusion_titles, password)
905911
if not check_ERROR(DATA):
906912
log.info("Exclusion titles added successfully based on the request")
913+
907914
elif api == "RUR":
908915
log.info(
909916
f"A request has been made to remove the user {username} from the database"
910917
)
911-
DATA = um.remove(username, password)
912-
if not check_ERROR(DATA):
913-
log.info("User removed successfully based on the request")
918+
if username is not "admin":
919+
DATA = um.remove(username, password)
920+
if not check_ERROR(DATA):
921+
log.info("User removed successfully based on the request")
922+
else:
923+
DATA = "ERROR Admin cannot be removed && 401"
924+
914925
elif api == "RLR":
915-
DATA = "LOG"
926+
if um.verify_password(username, password) and username == "admin":
927+
DATA = "LOG"
928+
else:
929+
DATA = "ERROR Invalid Username or Password && 401"
930+
916931
else:
917932
DATA = "ERROR Invalid API && 404"
918933

@@ -930,3 +945,9 @@ def init():
930945
if not os.path.exists("users.db"):
931946
log.info("Creating user database from scratch using SQLite")
932947
um.create_db_initial()
948+
try:
949+
with open("Admin.secrets", "r") as admin:
950+
password = admin.read()
951+
except Exception:
952+
password = None
953+
um.create_db("admin", "", password)

ReadMe.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,13 @@ To use this do the following steps:
6262
1) Clone the repository: `git clone https://github.com/DefinetlyNotAI/Test-generator.git`
6363
2) Navigate to the directory: `cd Test-generator`
6464
3) Open a terminal and install all required packages: `.\setup.ps1` [This will do the [Server Setup](#server-setup-) as well and run the [Server](wsgi_server.py).]
65-
4) Move the API directory to your other WEB server, BUT make both use the same `LOCALHOST`.
66-
5) Make the other WEB server initiate the framework `API_FrameWork.py` as well as generate and create the following files:
65+
4) Create a new file in the root directory, call it `Admin.secret` and add your admin password.
66+
5) Move the API directory to your other WEB server, BUT make both use the same `LOCALHOST`.
67+
6) Make the other WEB server initiate the framework `API_FrameWork.py` as well as generate and create the following files:
6768
- `Test.csv`: This should be made from a person, and include your questions
6869
- `API.json`: This should be dynamically changed as it decides what the server should do, check [Table of contents](#table-of-contents-) for more details.
6970
- `db.config`: This should rarely change and be made from a person, it decides the exam generation parameters.
70-
6) Now you can use the framework to create, manage, and distribute exams by executing `waitress-serve --listen=*:5000 wsgi_server:app`.
71+
7) Now you can use the framework to create, manage, and distribute exams by executing `waitress-serve --listen=*:5000 wsgi_server:app`.
7172

7273

7374
## Logging Information 📝

0 commit comments

Comments
 (0)