-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathversion9.py
644 lines (467 loc) · 27 KB
/
version9.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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
#This works fine
from nested_dictionaries import NestedDictionaries as nd
import flask
import pymongo as pym
from hashlib import sha256,md5
from Crypto.Random import get_random_bytes
from secrets import token_hex
import time
from bson.objectid import ObjectId
#EVERYTHING IS UTF-8
#######################--3-Level Dictionary concater and decryptor################################################
def gav(nested_dictionary): #concates all the values in a two nested dictionary
a=b""
for key, value in nested_dictionary.items():
try:
a=a+value
except:
try:
for key2 in value:
a=a+value[key2]
except:
for key3 in value[key2]:
a=a+value[key2][key3]
return(a)###'a' returns the concated valye while 'b' returns the decoded dictionary
#######################--3-Level Dictionary concater and aes decryptor################################################
#########################Initialization of asserting variables##################################
gender=["male","female","other","Apache attack helicopter"] #Add in genders here
occupation=["","",""] #Add in occupations here
accemails=["gmail","yahoo","rocketmail","outlook"] #Add in acceptable email domains
states=["Telangana","Andhra Pradesh"]
countries=["India","Sweden"]
#########################Initialization of asserting variables##################################
app=flask.Flask(__name__)
db=pym.MongoClient("mongodb://localhost:27017/") # connecting to the local Mongodb
app.config['MAX_CONTENT_LENGTH'] = 13 * 1000 * 1000
@app.route('/api/register',methods=['POST'])
def register():
try:
try:
data=flask.request.data
data=flask.request.get_json() # Simply puts the JSON_data which I got into the data var
#####################Check the content here#################################################
assert int((data["user"]["age"])) <= 120 and type(data["user"]["age"])==str #Defending against stupid attacks
assert data["user"]["sex"] in gender and type(data["user"]["sex"])==str
assert len(data["user"]["password"])==19 and type(data["user"]["password"])==str#Defending against stupid attacks
assert len(data["user"]["email"])<40 and type(data["user"]["email"])==str #Defending against stupid attacks
assert len(data["user"]["occupation"])<20 and type(data["user"]["occupation"])==str #Defending against stupid attacks
assert len(data["user"]["politicalleaning"])<15 and type(data["user"]["politicalleaning"])==str #Defending against stupid attacks
assert len(data["user"]["supporting party"]["name"])<50 and type(data["user"]["supporting party"]["name"])==str #Defending against stupid attacks
assert int(data["user"]["supporting party"]["chance"])<=100 and type(data["user"]["supporting party"]["chance"])==str #Defending against stupid attacks
assert len(data["user"]["election circles"])<=10 and type(data["user"]["election circles"])==list #Defending against stupid attacks
for i in data["user"]["election circles"]:assert type(i)==str
assert len(data["user"]["phone"])<=10 and len(data["user"]["phone"])>8 and type(data["user"]["phone"])==str #Defending against stupid attacks
assert type(data["user"]["state"])==str
assert type(data["user"]["nation"])==str and len(data["user"]["nation"])<=25
assert type(data["user"]["name"])==str and 3<len(data["user"]["name"])<20
assert data["user"]["nation"] in countries ###Change to database
except:
return {"Error":"Error in assertions"}
try:
x=data["user"]["email"].split("@")#Seperating the username from domain
x1=x[1].split(".") #seperating the domain and .
name=x[0] #Assingning the starting name to the name
edomain=x1[0] #Assigning the domain to the domain
assert len(name.split('+'))==1 ###Defending against idioittto
dab=db["users"] #This is the users data database
dac=dab[data["user"]["state"]] #This is a collection
assert dac.find_one({"user.email":data["user"]["email"]})==None #Checking for existing users
except:
return {"Error":"User already exists"}
#####################Check the content here#################################################
dab=db["states_list"]
dac=dab["list"]
assert dac.find_one({"state":data["user"]["state"]},{"_id":0,"state":1})!=None
dab=db["ecs"]
dac=dab[data["user"]["state"]]
for i in data["user"]["election circles"]:
fire=dac.find_one({"ec":i},{"_id":0,"ec":1})
if fire==None:
return {"error":"Election circle not supported"}
#################Expected contents(Add Here to upgrade)#####################################################
me=nd()
me["user"]["email"]=data["user"]["email"] #1
me["user"]["password"]=data["user"]["password"] #2
me["user"]["age"]=data["user"]["age"] #3
me["user"]["sex"]=data["user"]["sex"] #4
me["user"]["occupation"]=data["user"]["occupation"] #5
me["user"]["Political Leaning"]=data["user"]["politicalleaning"] #6
me["user"]["supporting party"]["name"]=data["user"]["supporting party"]["name"] #7
me["user"]["supporting party"]["chance"]=data["user"]["supporting party"]["chance"]#8
me["user"]["election circles"]=data["user"]["election circles"] #9
me["user"]["state"]=data["user"]["state"] #10
me["user"]["phone"]=data["user"]["phone"] #11
me["user"]["nation"]=data["user"]["nation"] #12
me["user"]["name"]=data["user"]["name"] #13
#################Expected contents(Add Here to upgrade)#####################################################
assert edomain in accemails #Hopefully he has email which is verified
tes=md5((data["user"]["password"]).encode('utf-8'))
me["user"]["password"]=tes.hexdigest()
dab=db["users"] #This is the users data database
dac=dab[me["user"]["state"]] #This is a collection
dac.insert_one(me) #Inserts the email and password into the required collection
return {"Operation Status":"Successful"}
except:
return {"Error":"Check if all the contents are exact"}
@app.route('/api/login',methods=['POST'])
def tokens():
try:
data=flask.request.data
data=flask.request.get_json()
assert len(data["user"]["email"])<40 and type(data["user"]["email"])==str
assert type(data["user"]["state"])==str and len(data["user"]["state"])<=40
###################################### Add captcha ################################################
###################################### Add captcha ###############################################
x=data["user"]["email"].split("@") #Seperating the username from domain
x1=x[1].split(".") #seperating the domain and .
name=x[0] #Assingning the starting name to the name
edomain=x1[0] #Assigning the domain to the domain
assert edomain in accemails #Cheking if the email exists at all
tes=md5((data["user"]["password"]).encode('utf-8'))
dab=db["users"] #This is the database
dac=dab[data["user"]["state"]] #This is a collection
fire=dac.find_one({"user.email":data["user"]["email"]},
{"_id":0,"user.password":1,"user.email":1,
"user.state":1,"user.election circles":1,
"user.nation":1,"user.name":1})
if fire==None:
return {"Result":"NO USER FOUND"}
else:
try:
assert fire["user"]["password"]==tes.hexdigest() #Fire dictionary stores the user data
a=token_hex(4)
b=token_hex(4)
c=str(a)+str(round(time.time()))+str(b)
me=nd()
me["auth"]["token"]=c #The token that will be linked
me["user"]["email"]=fire["user"]["email"] #The email that will be linked
me["user"]["election circles"]=fire["user"]["election circles"] #The election circles
me["user"]["state"]=fire["user"]["state"] #The state is stored
me["user"]["name"]=fire["user"]["name"]
dab1=db["Tokens"]
dac1=dab1[fire["user"]["state"]]
dac1.insert_one(me)
return {"Token":c,
"election circles":fire["user"]["election circles"],
"state":fire["user"]["state"],
"nation":fire["user"]["nation"],
}
except:
return {"Error":"either the database flunked or the password"}
except:
return{"Error":"Entry/database error"}
@app.route('/api/makequestion',methods=['POST'])
def makepost():
data=flask.request.data
data=flask.request.get_json()
#####################Check the content here#################################################
try:
assert type(data["auth"]["token"])==str ###Put this in cookie
assert type(data["auth"]["state"])==str ###Put this in cookie
assert type(data["user"]["m_question"])==str
assert type(data["user"]["d_question"])==str
assert type(data["meta"]["election circles"])==str
assert type(data["meta"]["tags"])==list and len(data["meta"]["tags"])<=4
## for i in data["user"]["Election circles"]:assert type(i)==str
assert type(data["auth"]["nation"])==str ###Put this in cookie
##Make a list for tags which keeps updating
except:
return {"Result":"Stop sending random data type or injection attacks bakayaro"}
#####################Check the content here#################################################
try:
######################Veriying the token###########################################
dab=db["Tokens"]
dac=dab[data["user"]["state"]]
fire=dac.find_one({"auth.token":data["auth"]["token"]},{"_id":0,"user.nation":1,"user.email":1,"user.state":1,"user.election circles":1,"user.name":1})
if fire==None:
return {"Error":"User Does not exist"}
######################Veriying the token###########################################
else:
assert data["user"]["state"] == fire["user"]["state"]
assert data["auth"]["nation"]== fire["user"]["nation"]
assert data["meta"]["election circles"] in fire["user"]["election circles"]
except:
return {"Error":"SHit went south"}
try:
me=nd()
me["q"]["m_question"]=data["user"]["m_question"]
me["q"]["d_question"]=data["user"]["d_question"]
me["q"]["tags"]=data["meta"]["tags"]
me["q"]["election circles"]=data["meta"]["election circles"]
me["q"]["time_stamp"]=str(round(time.time()))
me["q"]["followers"]=1
me["q"]["author"]=fire["user"]["name"]
me["q"]["state"]=fire["user"]["state"]
me["q"]["nation"]=fire["user"]["nation"]
me["q"]["noofanswer"]=0
me["auth"]["users"]=[fire["user"]["email"],"[email protected]"] #Add the ability to add security staff
me["auth"]["m_user"]=fire["user"]["email"]
dab=db["Feed"]
dac=dab[fire["user"]["state"]]
dac.insert_one(me)
return {"Result":"okay","hash":str(me["_id"])}
except:
return {"error":"Don't know wtf went wrong"}
@app.route('/api/makesolution',methods=['POST'])
def makesolution():
data=flask.request.data
data=flask.request.get_json()
try:
assert type(data["auth"]["token"])==str
assert type(data["s"]["solution"])==str
assert type(data["q"]["hash"])==str
assert type(data["auth"]["state"])==str
except:
a=flask.make_response({"error":"Honey, stop trying and go to mama. *-* "})
a.headers["Server"]="node.js"
return a
######################Veriying the token###########################################
try:
dab=db["Tokens"]
dac=dab[data["auth"]["state"]]
fire=dac.find_one({"auth.token":data["auth"]["token"]},{"_id":0,"user.nation":1,"user.email":1,"user.state":1,"user.election circles":1,"user.name":1})
if fire==None:
a=flask.make_response({"Error":"User Does not exist"})
a.headers["Server"]="node.js"
return a
else:
dab=db["feed"]
dac=dab[fire["user"]["state"]]
fire2=dac.find_one({ObjectId(data["q"]["hash"])},
{"_id":0,"q.state":1,"q.election circles":1,"q.nation":1})
assert fire2["q"]["state"] == fire["user"]["state"]
assert fire2["q"]["nation"]== fire["user"]["nation"]
assert fire2["q"]["election circles"] in fire["user"]["election circles"]
######################Veriying the token###########################################
dab=db["solutions"]
dac=dab[fire["user"]["state"]]
assert dac.find_one({"q.hash":data["q"]["hash"],"auth.m_user":fire["user"]["email"]})==None
me=nd()
me["q"]["hash"]=data["q"]["hash"]
me["s"]["solution"]=data["s"]["solution"]
me["s"]["author"]=fire["user"]["name"]
me["s"]["time_stamp"]=str(round(time.time()))
me["s"]["upvotes"]=0
me["s"]["downvotes"]=0
me["s"]["election circles"]=fire2["q"]["election circles"]
me["s"]["state"]=fire2["q"]["state"]
me["s"]["nation"]=fire2["q"]["nation"]
me["s"]["comments"]="no"
me["auth"]["emails"]=["[email protected]",fire["user"]["email"]]
dac.insert_one(me)
dab=db["feed"]
dac=dab[fire["user"]["state"]]
dac.update_one({"_id":ObjectId(me["q"]["hash"])},{"$inc":{"q.noofanswers":1}})###
a=flask.make_response({"ok":"Done","s_hash":str(me["_id"])})
a.headers["Server"]="node.js"
return a
except:
a=flask.make_response({"error":"Already wrote a solution"})
a.headers["Server"]="node.js"
return a
############IN the state, there are election circles, In which there are multiple elections.
#So check the make question route again and add the elections
@app.route('/api/makecomment',methods=['POST'])
def makecomment():
data=flask.request.data
data=flask.request.get_json()
try:
assert type(data["auth"]["token"])==str
assert type(data["user"]["comment"])==str
assert type(data["s"]["hash"])==str
assert type(data["auth"]["state"])==str
except:
a=flask.make_response({"error":"Check if every shit is the right data type"})
a.headers["Server"]="node.js"
return a
######################Veriying the token###########################################
try:
dab=db["Tokens"]
dac=dab[data["auth"]["state"]]
fire=dac.find_one({"auth.token":data["auth"]["token"]},
{"_id":0,"user.nation":1,"user.email":1,
"user.state":1,"user.election circles":1,
"user.name":1})
if fire==None:
return {"Error":"User Does not exist"}
else:
######################Veriying the token###########################################
dab=db["solutions"]
dac=dab[fire["user"]["state"]]
fire2=dac.find_one({ObjectId(data["s"]["hash"])},
{"_id":0,"s.state":1,"s.election circles":1,"s.nation":1})
assert fire2["s"]["state"] == fire["user"]["state"]
assert fire2["s"]["nation"]== fire["user"]["nation"]
assert fire2["s"]["election circles"] in fire["user"]["election circles"]
except:
a=flask.make_response({"error":"Some ridiculous error"})
a.headers["Server"]="node.js"
return a
try:
dab=db["comments"]
dac=dab[fire["user"]["state"]]
me=nd()
me["s"]["hash"]=data["s"]["hash"]
me["c"]["comment"]=data["user"]["comment"]
me["c"]["author"]=fire["user"]["name"]
me["c"]["time_stamp"]=str(round(time.time()))
me["c"]["upvotes"]=0
me["c"]["downvotes"]=0
me["c"]["state"]=fire2["s"]["state"]
me["c"]["election circles"]=fire2["s"]["election circles"]
me["c"]["nation"]=fire2["s"]["election circles"]
me["auth"]["emails"]=["[email protected]",fire["user"]["email"]]
me["c"]["comments"]="no"
dac.insert_one(me)
dab=db["solutions"]
dac=dab[fire["user"]["state"]]
dac.update_one({"_id":ObjectId(me["s"]["hash"])},{"$set":{"s.comments":"yes"}})
a=flask.make_response({"OK":"Operation successfull"})
a.headers["Server"]="node.js"
return a
except:
a=flask.make_response({"error":"SOmething went wrrrroooong"})
a.headers["Server"]="node.js"
return a
@app.route('/api/makereply',methods=['POST'])
def makecommentreply():
data=flask.request.data
data=flask.request.get_json()
try:
assert type(data["auth"]["token"])==str
assert type(data["user"]["comment"])==str
assert type(data["c"]["hash"])==str
assert type(data["auth"]["state"])==str
except:
a=flask.make_response({"error":"Check if every shit is the right data type"})
a.headers["Server"]="node.js"
return a
######################Veriying the token###########################################
try:
dab=db["Tokens"]
dac=dab[data["auth"]["state"]]
fire=dac.find_one({"auth.token":data["auth"]["token"]},
{"_id":0,"user.nation":1,"user.email":1,
"user.state":1,"user.election circles":1,
"user.name":1})
if fire==None:
a=flask.make_response({"Error":"User Does not exist"})
a.headers["Server"]="node.js"
return a
else:
######################Veriying the token###########################################
dab=db["comments"]
dac=dab[fire["user"]["state"]]
fire2=dac.find_one({ObjectId(data["c"]["hash"])},
{"_id":0,"c.state":1,"c.election circles":1,"c.nation":1})
assert fire2["c"]["state"] == fire["user"]["state"]
assert fire2["c"]["nation"]== fire["user"]["nation"]
assert fire2["c"]["election circles"] in fire["user"]["election circles"]
me=nd()
me["c"]["hash"]=data["c"]["hash"]
me["c"]["comment"]=data["user"]["comment"]
me["c"]["time_stamp"]=str(round(time.time()))
me["c"]["upvotes"]=0
me["c"]["downvotes"]=0
me["c"]["comments"]="no"
me["c"]["state"]=fire2["c"]['state']
me["c"]["election circles"]=fire2["c"]["election circles"]
me["c"]["nation"]=fire2["c"]["nation"]
me["auth"]["emails"]=["[email protected]",fire["user"]["email"]]
dac.insert_one(me)
dac.update_one({"_id":ObjectId(data["c"]["hash"])},{"$set":{"c.comments":"yes"}})
a=flask.make_response({"ok":"Comment posted"})
a.headers["Server"]="node.js"
return a
except:
a=flask.make_response({"error":"Probably not of the same region?"})
a.headers["Server"]="node.js"
return a
#########################From here we set on on editing the adventures###################################
#########################################################################################################
@app.route('/api/feed',methods=['POST'])
def feeder():
data=flask.request.data
data=flask.request.get_json()
try:
assert type(data["auth"]["token"])==str
assert type(data["auth"]["state"])==str
assert type(data["user"]["election circles"])==str
except:
a=flask.make_response({"error":"Check if every shit is the right data type"})
a.headers["Server"]="node.js"
return a
try:
dab=db["Tokens"]
dac=dab[data["user"]["state"]]
fire=dac.find_one({"auth.token":data["auth"]["token"]},
{"_id":0,"user.nation":1,"user.email":1,
"user.state":1,"user.election circles":1,
"user.name":1})
assert data["auth"]["state"]==fire["user"]["state"]
assert data["user"]["election circles"] in fire["user"]["election circles"]
if fire==None:
a=flask.make_response({"error":"Oolalalala looo"})
a.headers["Server"]="node.js"
return a
else:
dab=db["feed"]
dac=dab[fire["user"]["state"]]
####Change this as more questions come into the feed, to
####make it promote new questions then most followed, then
fire2=dac.find({"election circles":data["user"]["election circles"]},
{"_id":1,"q.m_question":1,"q.d_question":1}).limit(4)
que=nd()
count=0
for ques in fire2:
count=count+1
que["question_"+str(count)]["hash"]=str(ques["_id"])
que["question_"+str(count)]["m_question"]=ques["q"]["m_question"]
que["question_"+str(count)]["d_question"]=ques["q"]["d_question"]
a=flask.make_response(que)
a.headers["Server"]="node.js"
return a
except:
a=flask.make_response({"error":"Oolalalala looo"})
a.headers["Server"]="node.js"
return a
@app.route('/api/question/<state_hash_ec>',methods=['GET'])
def question(state_hash_ec):
try:
assert type(state_hash_ec) == str #Use compound index
s=state_hash.split('_')
assert len(s)==3
dab=db["feed"]
dac=dab[s[0]]
fire=dac.find_one({"_id":ObjectId(s[1]),"election circles":s[2]},
{"_id":0,"q.m_question":1,"q.d_question":1,
"q.author":1,"q.time_stamp":1,"q.followers":1,"q.noofanswers":1,
"q.state":1,"q.election circles":1})
me=nd()
me["question"]=fire
dab=db["solutions"]
dac=dab[s[0]]
fire=dac.find({"election circles":s[2],"q.hash":s[1]},
{"_id":1,"s.solution":1,"s.upvotes":1,"s.downvotes":1,
"s.author":1,"s.comments":1,"s.time_stamp":1}).limit(5)
count=0
for i in fire:
count=count+1
me["solution_"+str(count)]=i
if me["solution_"+str(count)]!=None:
me["solution_"+str(count)]["_id"]=str(me["solution_"+str(count)]["_id"])
me["solution_"+str(count)]["s"]["hash"]=me["solution_"+str(count)]["_id"]
del me["solution_"+str(count)]["_id"]
a=flask.make_response(me)
a.headers["Server"]="node.js"
return a
except:
a=flask.make_response({"error":"Oolalalala looo"})
a.headers["Server"]="node.js"
return a
#Databases required still:
# 1)Upvotes downvotes user ids #Hidden and only visible to us.
# 2)Followers #Hidden and only visible to us and the users
# 3)Meta in user profile Visble to users
if __name__ == '__main__':
app.run()