-
Notifications
You must be signed in to change notification settings - Fork 58
/
API.py
464 lines (443 loc) · 19.8 KB
/
API.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
from flask import Blueprint, request, session, redirect, render_template
import core
import tools
import logging
import bcrypt
import json
import traceback
import sys
from whenareyou import whenareyou
try:
import queue as Queue
except ImportError:
import Queue
db = None
configuration_data = None
log = logging.getLogger()
api = Blueprint('api', __name__, template_folder='templates')
@api.route('/new_user', methods=["GET","POST"])
def new_user():
'''
Create new user in the database
:param: username
:param: password
:param: first_name
:param: email
:param: city
:param: country
:param: state
'''
log.info(":API:/api/new_user")
response = {"type": None, "data": {}, "text": None}
try:
if request.is_json:
request_data = request.get_json()
else:
request_data = request.form
username = str(request_data["username"])
log.debug("Username is {0}".format(username))
password = str(request_data["password"])
first_name = str(request_data["first_name"])
last_name = str(request_data["last_name"])
email = str(request_data["email"])
city = str(request_data["city"])
country = str(request_data["country"])
state = str(request_data["state"])
check_list = [username, password, first_name, last_name, email, city, country, state]
passed = tools.check_string(check_list)
if passed:
log.debug("Attempting to create new user with username {0} and email {1}".format(username, email))
# Check to see if the username exists
users = db["users"]
if users.find_one(username=username):
# If that username is already taken
taken_message = "Username {0} is already taken".format(username)
log.debug(taken_message)
response["type"] = "error"
response["text"] = taken_message
else:
# Add the new user to the database
log.info(":{0}:Adding a new user to the database".format(username))
db.begin()
# Hash the password
log.debug("Hashing password")
hashed = bcrypt.hashpw(password.encode('utf8'), bcrypt.gensalt())
log.debug("Hashed password is {0}".format(hashed))
is_admin = username in configuration_data["admins"]
try:
db['users'].insert({
"username": username,
"first_name": first_name,
"last_name": last_name,
"email": email,
"password": hashed,
"admin": is_admin,
"default_plugin": "search",
"notifications": json.dumps(["email"]),
"ip": request.environ.get('HTTP_X_REAL_IP', request.remote_addr),
"news_site": "http://reuters.com",
"city": city,
"country": country,
"state": state,
"temp_unit": "fahrenheit",
"timezone": whenareyou(city)
})
db.commit()
response["type"] = "success"
response["text"] = "Thank you {0}, you are now registered for W.I.L.L".format(first_name)
except:
db.rollback()
response["type"] = "error"
response["text"] = "There was an error in signing you up for W.I.L.L. Please check the information you entered"
else:
log.warning(":{0}:Failed SQL evaluation".format(username))
response["type"] = "error"
response["text"] = "Invalid input, valid chars are {0}".format(tools.valid_chars)
except KeyError:
log.error("Needed data not found in new user request")
response["type"] = "error"
response["text"] = "Couldn't find required data in request. " \
"To create a new user, a username, password, first name, last name," \
"and email is required"
return tools.return_json(response)
@api.route("/settings", methods=["POST"])
def settings():
"""
:param username:
:param password:
:param Optional - setting to be changed:
Change the users settings
:return:
"""
log.info(":API:/api/settings")
response = {"type": None, "text": None, "data": {}}
if request.is_json:
request_data = request.get_json()
else:
request_data = request.form
if "username" in request_data.keys() and "password" in request_data.keys():
username = request_data["username"]
password = request_data["password"]
if tools.check_string(request_data.values()):
user_table = db["users"].find_one(username=username)
if user_table:
db_hash = user_table["password"]
if bcrypt.checkpw(password.encode('utf8'), db_hash.encode('utf8')):
#TODO: write a framework that allows changing of notifications
immutable_settings = ["username", "admin", "id", "user_token", "notifications", "password"]
db.begin()
log.info(":{0}:Changing settings for user".format(username))
try:
for setting in request_data.keys():
if setting not in immutable_settings:
db["users"].upsert({"username": username, setting: request.form[setting]}, ['username'])
db.commit()
response["type"] = "success"
response["text"] = "Updated settings"
except Exception as db_error:
log.debug("Exception {0}, {1} occurred while trying to commit changes to the database".format(
db_error.message, db_error.args
))
response["type"] = "error"
response["text"] = "Error encountered while trying to update db, changes not committed"
db.rollback()
else:
response["type"] = "error"
response["text"] = "User {0} doesn't exist".format(username)
else:
response["type"] = "error"
response["text"] = "Invalid input"
else:
response["type"] = "error"
response["text"] = "Couldn't find username or password in request data"
return tools.return_json(response)
@api.route('/get_sessions', methods=["GET", "POST"])
def get_sessions():
"""
Return list of active sessions for user
:param: username
:param: password
:return: list of sessions
"""
log.info(":API:/api/get_sessions")
response = {"type": None, "data": {}, "text": None}
sessions = core.sessions
if request.is_json:
request_data = request.get_json()
else:
request_data = request.form
try:
username = request_data["username"]
password = request_data["password"]
if tools.check_string(request_data.values()):
db_hash = db['users'].find_one(username=username)["password"]
user_auth = bcrypt.checkpw(password.encode('utf8'), db_hash.encode('utf8'))
if user_auth:
response["data"].update({"sessions":[]})
for user_session in sessions:
if sessions[user_session]["username"] == username:
response["data"]["sessions"].append(session)
response["type"] = "success"
response["text"] = "Fetched active sessions"
else:
response["type"] = "error"
response["text"] = "Invalid username/password combination"
else:
response["type"] = "error"
response["text"] = "One of the submitted parameters contained an invalid character. " \
"Valid characters are {0}".format(tools.valid_chars)
except KeyError:
response["type"] = "error"
response["text"] = "Couldn't find username and password in request"
return tools.return_json(response)
@api.route('/start_session', methods=["GET","POST"])
def start_session():
'''
:param: username
:param: password
Generate a session id and start a new session
:return:
'''
log.info(":API:/api/start_session")
# Check the information that the user has submitted
response = {"type": None, "data": {}, "text": None}
if request.is_json:
request_data = request.get_json()
else:
request_data = request.form
try:
if request.method == "POST":
username = request_data["username"]
password = request_data["password"]
client = "API-POST"
elif request.method == "GET":
username = request.args.get("username", "")
password = request.args.get("password", "")
client = "API-GET"
if not (username and password):
raise KeyError()
if tools.check_string([username, password]):
log.info(":{0}:Checking password".format(username))
users = db["users"]
user_data = users.find_one(username=username)
if user_data:
user_data = db["users"].find_one(username=username)
# Check the password
db_hash = user_data["password"]
user_auth = bcrypt.checkpw(password.encode('utf8'), db_hash.encode('utf8'))
if user_auth:
log.info(":{0}:Authentication successful".format(username))
# Return the session id to the user
session_id = tools.gen_session(username, client, db)
if session_id:
response["type"] = "success"
response["text"] = "Authentication successful"
response["data"].update({"session_id": session_id})
else:
response["type"] = "error"
response["text"] = "Invalid username/password"
else:
response["type"] = "error"
response["text"] = "Couldn't find user with username {0}".format(username)
else:
response["type"] = "error"
response["text"] = "Invalid input"
except KeyError:
response["type"] = "error"
response["text"] = "Couldn't find username and password in request data"
# Render the response as json
if request.method == "GET":
session.update({"session_data": response})
if response["type"] == "success":
return redirect("/")
log.debug("Rendering command template")
return render_template("command.html")
else:
return tools.return_json(response)
@api.route('/end_session', methods=["GET", "POST"])
def end_session():
"""
End a session
:param session_id:
:return End the session:
"""
log.info(":API:/api/end_session")
response = {"type": None, "data": {}, "text": None}
if request.is_json:
request_data = request.get_json()
else:
request_data = request.form
try:
session_id = request_data["session_id"]
# Check for the session id in the core.sessions dictionary
if session_id in core.sessions.keys():
log.info(":{0}:Ending session".format(session_id))
del core.sessions[session_id]
response["type"] = "success"
response["text"] = "Ended session"
else:
response["type"] = "error"
response["text"] = "Session id {0} wasn't found in core.sessions".format(session_id)
except KeyError:
response["type"] = "error"
response["text"] = "Couldn't find session id in request data"
# Render the response as json
return tools.return_json(response)
@api.route('/check_session', methods=["GET", "POST"])
def check_session():
"""
Check if a session is valid
:param: session_id
:return:
"""
log.info(":API:/api/check_session")
response = {"type": None, "text": None, "data": {}}
if request.is_json:
request_data = request.get_json()
else:
request_data = request.form
try:
session_id = request_data["session_id"]
session_valid = (session_id in core.sessions.keys())
response["data"].update({"valid": session_valid})
response["type"] = "success"
if tools.check_string(session_id):
if session_valid:
response["text"] = "Session id {0} is valid".format(session_id)
else:
response["text"] = "Session id {0} is invalid".format(session_id)
else:
response["type"] = "error"
response["text"] = "Invalid input"
except KeyError:
response["type"] = "error"
response["text"] = "Couldn't find session_id in request data"
response["data"].update({"valid": False})
return tools.return_json(response)
@api.route('/respond', methods=["GET", "POST"])
def command_response():
"""
Api path for responding to a command question
:param session_id:
:param command_id:
:return:
"""
log.info(":API:/api/respond")
response = {"type": None, "text": None, "data": {}}
if request.is_json:
request_data = request.get_json()
try:
log.debug(request_data.keys())
command_id = request_data["command_id"]
session_id = request_data["session_id"]
response_value = request_data["value"]
#Validate the JSON response object
if tools.check_string([command_id, session_id]):
if session_id in core.sessions.keys():
session_data = core.sessions[session_id]
session_commands = session_data["commands"]
response_command = None
for command_obj in session_commands:
if command_obj["id"] == command_id:
response_command = command_obj
if response_command:
if "function" in response_command.keys() and "event" in response_command.keys():
response_function = response_command["function"]
log.info(":{0}: Executing response function {1} with response {2}".format(
command_id, response_function, response_value
))
#Execute the response
try:
response_result = response_function(response_value, response_command["event"])
log.info(":{0}:Successfully executed response, returning {1}".format(
session_id, tools.fold(response_result)
))
response = response_result
except Exception:
exc_type, exc_value, exc_traceback = sys.exc_info()
error_string = repr(traceback.format_exception(exc_type, exc_value,
exc_traceback))
log.error(error_string)
username = session_data["username"]
user_table = db["users"].find_one(username=username)
if user_table:
response["type"] = "error"
if user_table["admin"]:
response["text"] = error_string
else:
response["text"] = "An error has occurred while trying to fetch a response." \
"Please contact [email protected] to report the error and " \
"get more information"
else:
log.error("USER {0} NOT FOUND IN DATABASE. WARNING.".format(username))
response["type"] = "error"
response["text"] = "A database error has occurred. Please contact [email protected]" \
"to report the error, along with the circumstances under which it" \
"occurred."
else:
response["type"] = "error"
response["text"] = "Command {0} didn't register for a response or didn't" \
" register the required data for a response.".format(command_id)
else:
response["type"] = "error"
response["text"] = "Couldn't find a command object in session {0} with command id {1}".format(
session_id, command_id
)
else:
response["type"] = "error"
response["text"] = "Invalid session id {0}".format(session_id)
else:
response["type"] = "error"
response["text"] = "Submitted response data {0} failed string validation. Valid characters are {0}".format(
tools.valid_chars
)
except KeyError:
response["type"] = "error"
response["text"] = "command_id, session_id, and JSON response object required"
else:
response["type"] = "error"
response["text"] = "/api/respond requires a JSON request"
return tools.return_json(response)
@api.route('/command', methods=["GET", "POST"])
def process_command():
"""
Api path for processing a command
:param command:
:param session_id:
:return response object:
"""
log.info(":API:/api/command")
response = {"type": None, "data": {}, "text": None}
if request.is_json:
request_data = request.get_json()
else:
request_data = request.form
try:
command = request_data["command"]
session_id = request_data["session_id"]
log.debug(":{1}:Processing command {0}".format(command, session_id))
if session_id in core.sessions.keys():
# Add the command to the core.sessions command queue
session_data = core.sessions[session_id]
log.info(":{1}:Adding command {0} to the command queue".format(command, session_id))
command_data = tools.create_command_obj(session_id, command)
command_response = core.sessions_monitor.command(
command_data, core.sessions[session_id], db, add_to_updates_queue=False
)
if session_id in core.commands.keys():
core.commands[session_id].append(command_data)
else:
core.commands.update({session_id: [command_data]})
session_data["commands"].append(command_data)
core.commands.update(command_data)
log.info(":{0}:Returning command response {1}".format(session_id, tools.fold(str(command_response))))
response = command_response
else:
log.info(":{0}:Couldn't find session id in sessions".format(session_id))
response["type"] = "error"
response["text"] = "Invalid session id"
except KeyError:
log.debug("Couldn't find session id and command in request data")
response["type"] = "error"
response["text"] = "Couldn't find session id and command in request data"
return tools.return_json(response)