Skip to content

Commit bf6234d

Browse files
authored
Merge pull request #6 from morpheuslord/admin_panel
added admin panel code
2 parents e00dd23 + 3d34c8e commit bf6234d

File tree

5 files changed

+211
-38
lines changed

5 files changed

+211
-38
lines changed

README.md

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,51 @@ This is an implementation for our college PCL project which is still under devel
4242
| `p5` | `json` | Complete Intense Scan | `-Pn -sS -sU -T4 -A -PE -PP -PY -g 53 --script=vuln`|
4343

4444

45-
#### Auth and User management
45+
### Auth and User management
4646

47+
#### Registration
4748
```text
48-
GET /register/<int:user_id>/<string:password>/<string:unique_key>
49+
GET /register
4950
```
51+
Payload:
5052
| Parameter | Type | Description |
5153
| :-------- | :------- | :------------------------- |
52-
|`ID`|`Int`|user ID|
53-
|`Passwd`| `String`| User Passwd|
54-
|`Unique_Key`| `String`| User Unique_Key|
54+
|`JSON_Payload`|`JSON`|`PARAMETERS`|
55+
56+
The parameters should look like this while registering a new user:
57+
```json
58+
{
59+
"user_id": 1,
60+
"username": "tim",
61+
"role": "user",
62+
"priority": "low"
63+
}
64+
```
65+
for a new admin user the role must be changes to admin.
66+
67+
The current default admin user has a key of `60e709884276ce6096d1`
68+
69+
#### Remove user
70+
```text
71+
POST /rmuser/<int:id>/<string:username>/<string:key>
72+
```
73+
| Parameter | Type | Description |
74+
| :-------- | :------- | :------------------------- |
75+
|`ID`|`int`|`users id`|
76+
|`username`|`string`|`username assigned`|
77+
|`key`|`string`|`admin key`|
78+
79+
for this function to work we need to add the `admin key` and it wont work if the key is not correct.
80+
81+
#### Get users
82+
```text
83+
GET /getuser/<string:admin_key>
84+
```
85+
| Parameter | Type | Description |
86+
| :-------- | :------- | :------------------------- |
87+
|`admin_key`|`string`|`the admin key`|
88+
89+
This is for varifying the users and for implementing the front end section of the code which will be implemented in the future.
5590

5691
## Improvements
5792
Added GPT functionality with chunking module.
@@ -278,4 +313,4 @@ sudo systemctl enable nmapapi
278313
- *Step 4:* I guess the final step changes per individual it is suggested to setup firewall rules and redirect port 80 to 443
279314

280315
#### Default User Keys
281-
**Default_Key**: **cff649285012c6caae4d**
316+
**Default_Admin_Key**: **60e709884276ce6096d1**

__pycache__/app.cpython-310.pyc

2.4 KB
Binary file not shown.

app.py

Lines changed: 170 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import openai
1212

1313
from flask import Flask
14+
from flask import request
1415
from flask import render_template
1516
from flask_restful import Api
1617
from 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+
75194
def to_int(s: str) -> int:
76195
return int(s)
77196

@@ -155,18 +274,37 @@ def AI(analize: str) -> dict[str, Any]:
155274

156275

157276
def 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

172310
def extract_ai_output(ai_output: str) -> dict[str, Any]:

auth_keys.db

0 Bytes
Binary file not shown.

users.db

12 KB
Binary file not shown.

0 commit comments

Comments
 (0)