1111import openai
1212
1313from flask import Flask
14+ from flask import request
1415from flask import render_template
1516from flask_restful import Api
1617from flask_restful import Resource
@@ -35,43 +36,161 @@ def doc() -> Any:
3536 return render_template ("doc.html" )
3637
3738
38- @app .route ('/register/<int:user_id>/<string:password>/<string:unique_key>' )
39- def store_auth_key (user_id : int , password : str , unique_key : str ) -> str :
39+ @app .route ('/register' , methods = ['POST' ])
40+ def store_auth_key ():
41+ data = request .get_json ()
42+
43+ user_id = data .get ('user_id' )
44+ uname = data .get ('username' )
45+ passwd = data .get ('password' )
46+ u_key = data .get ('unique_key' )
47+ role = data .get ('role' )
48+ priority = data .get ('priority' )
49+
4050 sanitized_username = user_id
41- sanitized_passwd = password
42- sanitized_key = unique_key
43- # Hash the user's ID, password, and unique key together
51+ sanitized_passwd = passwd
52+ sanitized_key = u_key
53+
4454 hash = hashlib .sha256 ()
4555 hash .update (str (sanitized_username ).encode ('utf-8' ))
4656 hash .update (sanitized_passwd .encode ('utf-8' ))
4757 hash .update (sanitized_key .encode ('utf-8' ))
48- # Use the hash to generate the auth key
49- auth_key = hash .hexdigest ()[:20 ] # Get the first 20 characters
58+
59+ auth_key = hash .hexdigest ()[:20 ]
60+
61+ user_db_file = 'users.db'
62+ conn_user = sqlite3 .connect (user_db_file )
63+ cursor_user = conn_user .cursor ()
64+
65+ cursor_user .execute ('''CREATE TABLE IF NOT EXISTS users
66+ (user_id INT PRIMARY KEY NOT NULL,
67+ username TEXT NOT NULL,
68+ role TEXT NOT NULL,
69+ priority TEXT NOT NULL);''' )
70+
71+ query_user = (
72+ "INSERT INTO users "
73+ "(user_id, username, role, priority) "
74+ "VALUES (?, ?, ?, ?)"
75+ )
76+ cursor_user .execute (
77+ query_user ,
78+ (sanitized_username , uname , role , priority )
79+ )
80+
81+ conn_user .commit ()
82+ conn_user .close ()
83+
5084 db_file = 'auth_keys.db'
5185 need_create_table = not os .path .exists (db_file )
52- conn = sqlite3 .connect (db_file )
53- cursor = conn .cursor ()
86+ conn_auth = sqlite3 .connect (db_file )
87+ cursor_auth = conn_auth .cursor ()
88+
5489 if need_create_table :
55- cursor .execute ('''CREATE TABLE auth_keys
56- (user_id INT PRIMARY KEY NOT NULL,
57- auth_key TEXT NOT NULL,
58- unique_key TEXT NOT NULL);''' )
59- query = (
90+ cursor_auth .execute ('''CREATE TABLE IF NOT EXISTS auth_keys
91+ (user_id INT PRIMARY KEY NOT NULL,
92+ auth_key TEXT NOT NULL,
93+ unique_key TEXT NOT NULL,
94+ role TEXT NOT NULL,
95+ priority TEXT NOT NULL);''' )
96+
97+ query_auth = (
6098 "INSERT INTO auth_keys "
61- "(user_id, auth_key, unique_key) "
62- "VALUES (?, ?, ?)"
99+ "(user_id, auth_key, unique_key, role, priority ) "
100+ "VALUES (?, ?, ?, ?, ? )"
63101 )
64- cursor .execute (
65- query ,
66- (sanitized_username , auth_key , sanitized_key )
102+ cursor_auth .execute (
103+ query_auth ,
104+ (sanitized_username , auth_key , sanitized_key , priority , priority )
67105 )
68106
69- conn .commit ()
70- conn .close ()
107+ conn_auth .commit ()
108+ conn_auth .close ()
71109
72110 return auth_key
73111
74112
113+ @app .route ('/getuser/<string:admin_key>' )
114+ def get_all_users (admin_key : str ) -> str :
115+ conn_auth = sqlite3 .connect ('auth_keys.db' )
116+ cursor_auth = conn_auth .cursor ()
117+ sanitized_key = sanitize (admin_key )
118+ query = f"SELECT role FROM auth_keys WHERE auth_key = '{ sanitized_key } '"
119+ cursor_auth .execute (
120+ query
121+ )
122+ auth_row = cursor_auth .fetchone ()
123+ if auth_row :
124+ conn_users = sqlite3 .connect ('users.db' )
125+ cursor_users = conn_users .cursor ()
126+
127+ cursor_users .execute ("SELECT * FROM users" )
128+ rows = cursor_users .fetchall ()
129+
130+ users = []
131+ for row in rows :
132+ user = {
133+ "user_id" : row [0 ],
134+ "username" : row [1 ],
135+ "role" : row [2 ],
136+ "priority" : row [3 ]
137+ }
138+ users .append (user )
139+
140+ conn_users .close ()
141+ conn_auth .close ()
142+ return json .dumps (users )
143+
144+ conn_auth .close ()
145+ return json .dumps ({"error" : "Unauthorized access. Admin key required." })
146+
147+
148+ # Admin : 60e709884276ce6096d1
149+ @app .route ('/rmuser/<int:id>/<string:username>/<string:key>' )
150+ def remove_user (id : int , username : str , key : str ) -> Any :
151+ conn_auth = sqlite3 .connect ('auth_keys.db' )
152+ cursor_auth = conn_auth .cursor ()
153+
154+ cursor_auth .execute (
155+ "SELECT user_id, role FROM auth_keys WHERE auth_key = ?" , (key ,))
156+ auth_row = cursor_auth .fetchone ()
157+
158+ if auth_row :
159+ role = auth_row [1 ]
160+ if role == "admin" :
161+ conn_auth .close ()
162+ pass
163+ else :
164+ return {"error" : "Unauthorized access. Admin key required." }
165+
166+ conn_users = sqlite3 .connect ('users.db' )
167+ cursor_users = conn_users .cursor ()
168+ conn_auth = sqlite3 .connect ('auth_keys.db' )
169+ cursor_auth = conn_auth .cursor ()
170+
171+ cursor_users .execute (
172+ "DELETE FROM users WHERE user_id = ? AND username = ?" ,
173+ (id , username )
174+ )
175+
176+ cursor_auth .execute (
177+ "DELETE FROM auth_keys WHERE user_id = ?" ,
178+ (id ,)
179+ )
180+
181+ conn_users .commit ()
182+ conn_auth .commit ()
183+ conn_users .close ()
184+ conn_auth .close ()
185+
186+ removed_user = {
187+ "username" : username ,
188+ "user_id" : id
189+ }
190+
191+ return removed_user
192+
193+
75194def to_int (s : str ) -> int :
76195 return int (s )
77196
@@ -155,18 +274,37 @@ def AI(analize: str) -> dict[str, Any]:
155274
156275
157276def authenticate (auth_key : str ) -> bool :
158- conn = sqlite3 .connect ('auth_keys.db' )
159- cursor = conn .cursor ()
277+ conn_auth = sqlite3 .connect ('auth_keys.db' )
278+ cursor_auth = conn_auth .cursor ()
279+ conn_users = sqlite3 .connect ('users.db' )
280+ cursor_users = conn_users .cursor ()
281+
160282 key = sanitize (auth_key )
161- # Check if the given auth_key exists in the database
162- cursor .execute ("SELECT 1 FROM auth_keys WHERE auth_key = ?" , (key ,))
163- row = cursor .fetchone ()
164- conn .close ()
165- # If the auth_key is found, return True, else False
166- if row :
167- return True
168- else :
169- return False
283+
284+ # Check if the given auth_key exists in the auth_keys table
285+ cursor_auth .execute (
286+ "SELECT user_id FROM auth_keys WHERE auth_key = ?" , (key ,))
287+ auth_row = cursor_auth .fetchone ()
288+
289+ if auth_row :
290+ user_id = auth_row [0 ]
291+
292+ # Check if the user ID exists in the users table
293+ cursor_users .execute (
294+ "SELECT user_id FROM users WHERE user_id = ?" , (user_id ,))
295+ user_row = cursor_users .fetchone ()
296+
297+ if user_row :
298+ # If the user IDs match, return True
299+ conn_auth .close ()
300+ conn_users .close ()
301+ return True
302+
303+ conn_auth .close ()
304+ conn_users .close ()
305+
306+ # Return an error message if the keys provided are incorrect
307+ return False
170308
171309
172310def extract_ai_output (ai_output : str ) -> dict [str , Any ]:
0 commit comments