@@ -32,6 +32,49 @@ def check_ERROR(value):
32
32
return False
33
33
34
34
35
+ def check_admin_password (password ):
36
+ """
37
+ Check if the provided password matches the password of the 'admin' user in the SQLite database.
38
+
39
+ Args:
40
+ password (str): The password to be checked.
41
+
42
+ Returns:
43
+ bool: True if the password matches, False otherwise.
44
+
45
+ Raises:
46
+ Exception: If an error occurs while executing the SQL query or fetching the result.
47
+
48
+ """
49
+ # Connect to the SQLite database (or create it if it doesn't exist)
50
+ conn = sqlite3 .connect ('users.db' )
51
+
52
+ # Create a cursor object using the cursor() method
53
+ cursor = conn .cursor ()
54
+
55
+ # SQL query to select the admin user's username and password
56
+ query = "SELECT username, password FROM Users WHERE username='admin'"
57
+
58
+ try :
59
+ # Execute the query
60
+ cursor .execute (query )
61
+
62
+ # Fetch the result
63
+ result = cursor .fetchone ()
64
+
65
+ # Check if the fetched row exists and the password matches
66
+ if result and result [1 ] == password : # Compare the second column (index 1) with the provided password
67
+ return True
68
+ else :
69
+ return False
70
+ except Exception as e :
71
+ print (f"An error occurred: { e } " )
72
+ return False
73
+ finally :
74
+ # Close the connection
75
+ conn .close ()
76
+
77
+
35
78
class UserManager :
36
79
# Class to handle user management
37
80
def __init__ (self , db_name = "users.db" ):
@@ -131,7 +174,8 @@ def verify_password(self, username, password):
131
174
if password == stored_password :
132
175
return True
133
176
return False
134
- except Exception :
177
+ except Exception as e :
178
+ log .info (f"An error occurred while verifying the password. as { e } " )
135
179
return False
136
180
137
181
def create_db (self , username , exclusion_titles , password = None ):
@@ -915,15 +959,15 @@ def init():
915
959
log .info (
916
960
f"A request has been made to remove the user { username } from the database"
917
961
)
918
- if username is not "admin" :
962
+ if username != "admin" :
919
963
DATA = um .remove (username , password )
920
964
if not check_ERROR (DATA ):
921
965
log .info ("User removed successfully based on the request" )
922
966
else :
923
967
DATA = "ERROR Admin cannot be removed && 401"
924
968
925
969
elif api == "RLR" :
926
- if um . verify_password ( username , password ) and username == "admin" :
970
+ if check_admin_password ( password ):
927
971
DATA = "LOG"
928
972
else :
929
973
DATA = "ERROR Invalid Username or Password && 401"
@@ -948,6 +992,8 @@ def init():
948
992
try :
949
993
with open ("Admin.secrets" , "r" ) as admin :
950
994
password = admin .read ()
951
- except Exception :
995
+ except Exception as e :
996
+ log .info ("Admin password not found" + str (e ))
952
997
password = None
953
998
um .create_db ("admin" , "" , password )
999
+ os .remove ("passwords.txt" )
0 commit comments