-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
177 lines (146 loc) · 3.94 KB
/
Copy pathmain.py
File metadata and controls
177 lines (146 loc) · 3.94 KB
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
from flask import Flask, request
from replit import db
import hashlib
import random
import string
from flask_cors import CORS
import traceback
import json
def checkToken(token):
# check the token passed and return the username authenticated if the token is valid, otherwise return False
for usr, tok in db["tokens"].value.items():
# if the token is valid, return the username
if tok == token:
return usr
return False
# checks if the user already has a token
def tokenAlreadyExists(user):
# check if the user already has a token
for usr, tok in db["tokens"].value.items():
# if the user already has a token, return True
if usr == user:
return True
return False
app = Flask('workshop')
CORS(app)
@app.route('/')
def status():
return 'Clarity Workshop OK'
@app.route('/listall')
def list():
lvls = []
for lvl in db["lvls"]:
obj = {
"id": lvl["id"],
"name": lvl["name"],
"description": lvl["description"],
"author": lvl["author"],
}
lvls.append(obj)
return json.dumps(lvls)
@app.route('/list/<page>')
def listpage(page):
lvls = []
for lvl in db["lvls"]:
obj = {
"id": lvl["id"],
"name": lvl["name"],
"description": lvl["description"],
"author": lvl["author"],
}
lvls.append(obj)
stuff = []
pagelength = 4
for i in lvls[pagelength * (int(page) - 1):pagelength * (int(page))]:
stuff.append(i)
if stuff.__len__() == 0:
return 'No results'
else:
return json.dumps(stuff)
@app.route('/lvl/<id>')
def getlvl(id):
try:
for i in db["lvls"].value:
if int(i["id"]) == int(id):
return str(i["lvl"])
return "no such level"
except:
return "no such level"
@app.route('/lvlmeta/<id>')
def getlvlmeta(id):
try:
for i in db["lvls"].value:
if int(i["id"]) == int(id):
obj = {
"id": i["id"],
"name": i["name"],
"description": i["description"],
"author": i["author"],
}
return obj
return "no such level"
except:
return "no such level"
@app.route('/add', methods=["POST"])
def addlvl():
lvl = request.form.get("lvl")
try:
id = db["lvls"][-1]["id"] + 1
except:
id = 0
name = request.form.get("name")
description = request.form.get("description")
token = request.form.get("token")
author = checkToken(token)
if not author:
return "bad token"
db["lvls"].append({
"id": id,
"lvl": lvl,
"name": name,
"description": description,
"author": author
})
return "added level " + str(id)
@app.route('/signup', methods=["POST"])
def signup():
username = request.form.get("username")
password = request.form.get("password")
try:
db["users"][username]
return "username taken"
except:
# hash password
hashed = hashlib.sha224(bytes(password, "UTF-8")).hexdigest()
db["users"][username] = hashed
return "success"
@app.route('/login', methods=["POST"])
def login():
username = request.form.get("username")
password = request.form.get("password")
# Check if username is valid, then if the password is valid, return the user's token
try:
if db["users"][username] == hashlib.sha224(bytes(password,
"UTF-8")).hexdigest():
# check if a token for the user already exists
if not tokenAlreadyExists(username):
token = ''.join(
random.choice(string.ascii_lowercase) for i in range(50))
db["tokens"][username] = token
return token
else:
# Return the token that already exists
return db["tokens"][username]
else:
return "invalid"
except Exception as e:
print(traceback.format_exc())
return "internal server error " + str(e)
@app.route('/logout', methods=["POST"])
def logout():
try:
del db["tokens"][request.form.get("username")]
return "logged out"
except:
return "500 internal server error or whatever"
app.run(host='0.0.0.0', port=8080)