-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecurity.py
291 lines (266 loc) · 13.1 KB
/
security.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import pickle
import re
from typing import Optional
import os
from pathlib import Path
import bcrypt
import mysql
from mysql.connector import Error
import pyodbc
class StreamlitAuthObject():
def __init__(self) -> None:
self.authpath = os.path.join(Path(__file__).parent, 'authobjectpickle.pkl')
if not os.path.exists(self.authpath):
config = {
'credentials':{
'usernames':{
'admin':{
'email': 'admin@mail.in',
'name': 'admin',
'password': 'admin123',
'role': 'admin',
'state':1
},
'second_admin':{
'email':'secadmin@mail.in',
'name': 'secadmin',
'password': 'secadmin123',
'role':'translator',
'state':1
}
},
'cookie':{
'expiry_days': 1,
'key': 'mycookiekeyisveryrandom',
'name': 'authcookieadmin'
}
}
}
nameList = [name for name in config['credentials']['usernames'].keys()]
self.authobject = {
'config': config,
'nameList':nameList
}
with open('authobjectpickle.pkl', 'wb') as cookie:
pickle.dump(obj=self.authobject, file=cookie)
else:
with open(self.authpath, 'rb') as authobj:
self.authobject = pickle.load(authobj)
print("Default auth object loaded.")
def get_auth(self):
return self.authobject
def adduser(self, username:str, email: Optional[str] = "", name: Optional[str] = "", password:str = "abc123", role: str = 'translator', status: int = 1):
try:
if username not in self.authobject['nameList']:
if re.match(pattern='[\w*]@[\w*].[\w]', string=email):
self.authobject['config']['credentials']['usernames'][username] = {'email':email, 'name':name, 'password':password, 'role':role, 'status':status}
self.authobject['nameList'].append(username)
with open('authobjectpickle.pkl', 'wb') as cookie:
pickle.dump(obj=self.authobject, file=cookie)
return {'message':f"User {username} added to auth object"}
else:
return {'message':f"Incorrect email format"}
else:
return {'message':f"User {username} already exists"}
except Exception as e:
return {"Error": f"{e}"}
def delete_user(self, username:str):
try:
if username in self.authobject['nameList']:
self.authobject['config']['credentials']['usernames'].pop(username)
self.authobject['nameList'].pop(username)
with open('authobjectpickle.pkl', 'wb') as cookie:
pickle.dump(obj=self.authobject, file=cookie)
return {"object": username, 'message': f"{username} removed from auth object"}
else:
return {'message': f'User {username} does not exist'}
except Exception as e:
return {'Error': f"{e}"}
def persist_auth_object(self):
with open('authobjectpickle.pkl', 'wb') as cookie:
pickle.dump(obj=self.authobject, file=cookie)
return {'message':'Auth object updated'}
def list_users(self):
return self.authobject['nameList']
class StandardAuth():
db_list = {
'MySQL':'mysql',
'MS-SQL':'msql'
}
def __init__(self, username: str, email: Optional[str], password: str, role: Optional[str], status: Optional[bool], new_user:bool=False):
self.username = username
self.email = email if re.match(r'^[\w\.-]+@[\w\.-]+\.\w+$', email) else (lambda: (_ for _ in ()).throw(Exception("Incorrect email format")))()
self.password = password if password else (lambda: (_ for _ in ()).throw(Exception("Passsord cannot be empty")))()
self.role = role
self.status = status if status in [0, 1] else (lambda: (_ for _ in ()).throw(TypeError("Status has to be boolean. Incorrect type passed.")))()
self.new_user = new_user
# Encrypt password
salt = bcrypt.gensalt()
self.hashed_password = bcrypt.hashpw(self.password.encode('utf-8'), salt)
if not self.new_user:
self.user_params = {
'user_name':self.username,
'email':self.email,
'password':self.hashed_password,
'role':self.role,
'status':self.status
}
def _connect_DB (self, db_type: str, host_name: str, user_name: str, user_password: str, db_name: str):
if db_type in self.db_list.values():
if db_type == 'mysql':
connection = None
try:
connection = mysql.connector.connect(
host = host_name,
user = user_name,
passwd = user_password,
database = db_name
)
return {'con_object':connection, 'conn_args':{'db_type':db_type, 'host_name':host_name, 'user_name':user_name, 'user_password':user_password, 'db_name':db_name}, 'status':f"Connected to {db_name} as {user_name} on host {host_name}"}
except Error as e:
return {'status': e}
elif db_type == 'msql':
connection = None
try:
connection = pyodbc.connect(
f"DRIVER={{ODBC Driver 17 for SQL Server}};"
f"SERVER={host_name};"
f"DATABASE={db_name};"
f"UID={user_name};"
f"PWD={user_password}"
)
return {'con_object':connection, 'conn_args':{'db_type':db_type, 'host_name':host_name, 'user_name':user_name, 'user_password':user_password, 'db_name':db_name}, 'status':f"Connected to {db_name} as {user_name} on host {host_name}"}
except Exception as e:
return {'status': e}
else:
return {'status':f"{db_type} Database not supported"}
def register_user(self, users_tbl_name:str, user_tbl_exists: bool=False, **connection):
con_object = connection['con_object']
db_type = connection['conn_args']['db_type']
if db_type in self.db_list.values():
if db_type == 'mysql':
cursor = con_object.cursor()
if not user_tbl_exists:
try:
cursor.execute(
f"""
CREATE TABLE IF NOT EXISTS {users_tbl_name} (
id INT AUTO_INCREMENT,
username TEXT NOT NULL,
email TEXT,
password TEXT,
role TEXT,
status INT,
PRIMARY KEY (id)
)
"""
)
cursor.commit()
return {'status': f'{users_tbl_name} table created.'}
except Exception as e:
return {'status': e}
else:
check_user = None
try:
cursor.execute(
f"""
SELECT username FROM {users_tbl_name} WHERE username = {self.user_params['user_name']}
"""
)
check_user = cursor.fetchone()
if check_user is None:
cursor.execute(
f"""
INSERT INTO {users_tbl_name} (username, email, password, role, status)
VALUES
({self.user_params['user_name']}, {self.user_params['email']}, {self.user_params['password']}, {self.user_params['role']}, {self.user_params['status']})
"""
)
connection.commit()
else:
return {'status': f'{self.user_params["user_name"]} already exists.'}
except Exception as e:
return {'status': e}
elif db_type == "msql":
cursor = con_object.cursor()
if not user_tbl_exists:
try:
cursor.execute(
f"""
IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='{users_tbl_name}' AND xtype='U')
CREATE TABLE {users_tbl_name} (
id INT IDENTITY(1,1) PRIMARY KEY,
username NVARCHAR(100) NOT NULL,
email NVARCHAR(100),
password NVARCHAR(100),
role NVARCHAR(50),
status INT
)
"""
)
con_object.commit()
return {'status': f'{users_tbl_name} table created.'}
except Exception as e:
return {'status': str(e)}
else:
check_user = None
try:
cursor.execute(
f"""
SELECT username FROM {users_tbl_name} WHERE username = ?
""", (self.user_params['user_name'])
)
check_user = cursor.fetchone()
if check_user is None:
cursor.execute(
f"""
INSERT INTO {users_tbl_name} (username, email, password, role, status)
VALUES (?, ?, ?, ?, ?)
""",
(self.user_params['user_name'], self.user_params['email'], self.user_params['password'], self.user_params['role'], self.user_params['status'])
)
con_object.commit()
return {'status': f'{self.user_params["user_name"]} added to {users_tbl_name}.'}
else:
return {'status': f'{self.user_params["user_name"]} already exists.'}
except Exception as e:
return {'status': str(e)}
else:
return {'status':f"{db_type} Database not supported"}
return self.user_params
def login_user(self, users_tbl_name: str, **connection):
con_object = connection['con_object']
db_type = connection['conn_args']['db_type']
if db_type in self.db_list.values():
if db_type == 'mysql':
cursor = con_object.cursor()
try:
cursor.execute(
f"""
SELECT password FROM {users_tbl_name} WHERE username = %s
""", (self.username,)
)
result = cursor.fetchone()
if result and self.hashed_password == result[0]:
return {'status': 'Login successful'}
else:
return {'status': 'Incorrect username or password'}
except Exception as e:
return {'status': str(e)}
elif db_type == 'mssql':
cursor = con_object.cursor()
try:
cursor.execute(
f"""
SELECT password FROM {users_tbl_name} WHERE username = ?
""", (self.username,)
)
result = cursor.fetchone()
if result and self.hashed_password == result[0]:
return {'status': 'Login successful'}
else:
return {'status': 'Incorrect username or password'}
except Exception as e:
return {'status': str(e)}
else:
return {'status': f"{db_type} Database not supported"}