-
Notifications
You must be signed in to change notification settings - Fork 27
/
routes.py
340 lines (284 loc) · 9.38 KB
/
routes.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
import json
import server
def row2dict(row):
"""Takes sqlite3.Row objects and converts them to dictionaries.
This is important for JSON serialization because otherwise
Python has no idea how to turn a sqlite3.Row into JSON."""
x = {}
for col in row.keys():
x[col] = row[col]
return x
def check_token(api_token):
# TODO: try some fancy JOIN for user + company
c = server.get_db().cursor()
c.execute("SELECT * FROM tokens WHERE api_token=? LIMIT 1", [api_token])
user = c.fetchone()
if user is not None:
response = {"success": True}
response["user"] = row2dict(user)
c.execute("SELECT * FROM companies WHERE id=? LIMIT 1", [user["company"]])
company = c.fetchone()
response["company"] = {"name": company["name"], "id": company["id"]}
else:
response = {"success": False, "error": "Invalid API token."}
return response
@server.app.route('/authenticate', methods=['POST'])
def authenticate():
content = server.request.json
try:
api_token = content["api_token"]
response = check_token(api_token)
return response
except KeyError:
response = {"success": False, "error": "No API token given."}
return response
@server.app.route('/get_customers')
def get_customers():
api_token = server.request.headers.get('X-API-Token')
auth = check_token(api_token)
if not auth["success"]:
return auth
c = server.get_db().cursor()
c.execute("SELECT * FROM customers WHERE company=?", [auth["company"]["id"]])
rows = c.fetchall()
response = {
"success": True,
"customers": []
}
for row in rows:
x = row2dict(row)
response["customers"].append(x)
return response
# goatnote:
# SQLi
@server.app.route('/get_customer_v2/<id>')
def get_customer2(id):
api_token = server.request.headers.get('X-API-Token')
auth = check_token(api_token)
if not auth["success"]:
return auth
c = server.get_db().cursor()
c.execute("SELECT * FROM customers WHERE company=" + str(auth["company"]["id"]) + " AND id=" + id)
rows = c.fetchall()
response = {
"success": True,
"customers": []
}
for row in rows:
x = {}
for col in row.keys():
x[col] = row[col]
response["customers"].append(x)
return response
# goatnote:
# IDOR
@server.app.route('/get_customer_v1/<id>')
def get_customer1(id):
api_token = server.request.headers.get('X-API-Token')
auth = check_token(api_token)
if not auth["success"]:
return auth
c = server.get_db().cursor()
c.execute("SELECT * FROM customers WHERE id=?", [id])
rows = c.fetchall()
response = {
"success": True,
"customers": []
}
for row in rows:
x = {}
for col in row.keys():
x[col] = row[col]
response["customers"].append(x)
return response
@server.app.route('/transfer', methods=['PUT'])
def create_transfer():
api_token = server.request.headers.get('X-API-Token')
auth = check_token(api_token)
if not auth["success"]:
return auth
content = server.request.json
if content is None:
response = {"success": False, "error": "Expected JSON content"}
return response
try:
cust_from = int(content["from"])
cust_to = int(content["to"])
ammount = content["ammount"]
except KeyError:
response = {"success": False, "error": "Missing 'from', 'to', or 'ammount'"}
return response
except ValueError:
response = {"success": False, "error": "Values of 'from' and 'to' should be customer IDs."}
return response
# Decimals maintain more accuracy than floats (or should) so we don't want
# this number as a float. However, all decimals should be valid floats.
# TODO: Python has a decimal type, should probably use that?
try:
float(ammount)
except ValueError:
response = {"success": False, "error": "Value of 'ammount' should be a decimal number"}
return response
# goatnote:
# Not doing: validation of cust_from or cust_to
db = server.get_db()
c = db.cursor()
try:
c.execute('INSERT INTO transfers VALUES (null, ?, ?, ?, ?)', [cust_from, cust_to, ammount, 'CREATED'])
db.commit()
except:
response = {"success": False, "error": "Unknown SQL error"}
raise
return response
response = {"success": True, "id": c.lastrowid}
return response
@server.app.route('/get_transfers')
@server.app.route('/get_transfers/<status>')
def get_transfers(status=None):
api_token = server.request.headers.get('X-API-Token')
auth = check_token(api_token)
if not auth["success"]:
return auth
c = server.get_db().cursor()
if status is None:
c.execute("SELECT * FROM transfers")
else:
c.execute("SELECT * FROM transfers WHERE status=?", [status])
rows = c.fetchall()
response = {
"success": True,
"transfers": [],
"where": status
}
for row in rows:
x = {}
for col in row.keys():
x[col] = row[col]
response["transfers"].append(x)
return response
@server.app.route('/process_transfers/', methods=['POST'])
def process_transfers():
"""Advances all CREATED transfers to pending if they look ok.
Will not complete the transfer; use /confirm-transfer/<id> for that."""
api_token = server.request.headers.get('X-API-Token')
auth = check_token(api_token)
if not auth["success"]:
return auth
db = server.get_db()
c = db.cursor()
transfers = c.execute("SELECT * FROM transfers WHERE status=?", ['CREATED']).fetchall()
#x = []
#for row in transfers:
# x.append(row2dict(row))
#return {"data": x}
ok = []
error = []
# goatnote:
# Logic error here allows balances to become overdrawn
for xfer in transfers:
balance = c.execute("SELECT balance from customers WHERE id=?", [xfer["custID_from"]]).fetchone()
if balance["balance"] > xfer["amount"]:
c.execute("UPDATE transfers SET status=? WHERE id=?", ['PENDING', xfer['id']])
ok.append(xfer['id'])
else:
error.append(xfer['id'])
response = {"success": True, "pending": ok, "failed": error}
db.commit()
return response
# What even is database optimization?
@server.app.route('/confirm_transfer/<xfer_id>', methods=['POST'])
def confirm_transfer(xfer_id):
db = server.get_db()
c = db.cursor()
try:
xfer_id = int(xfer_id)
except ValueError:
response = {"success": False, "error": "Transfer ID must be an integer"}
return response
xfer = c.execute("SELECT * FROM transfers WHERE id=? LIMIT 1", [xfer_id]).fetchone()
if xfer is None:
response = {"success": False, "error": "Transfer ID invalid"}
return response
c.execute("UPDATE transfers SET status=? WHERE id=? AND status=?", ['COMPLETE', xfer['id'], 'PENDING'])
if c.rowcount == 0:
response = {"success": False, "error": "Transfer ID {} was not in the 'PENDING' state".format(xfer_id)}
return response
else:
customer_to = c.execute("SELECT * from customers WHERE id=?", [xfer['custID_to']]).fetchone()
customer_from = c.execute("SELECT * from customers WHERE id=?", [xfer['custID_from']]).fetchone()
new_balance_from = customer_from['balance'] - xfer['amount']
c.execute('UPDATE customers SET balance=? WHERE id=?', [new_balance_from, xfer['custID_from']])
new_balance_to = customer_to['balance'] + xfer['amount']
c.execute('UPDATE customers SET balance=? WHERE id=?', [new_balance_to, xfer['custID_to']])
response = {"success": True}
db.commit()
return response
@server.app.route('/token/<token>', methods=['DELETE'])
def delete_token(token):
api_token = server.request.headers.get('X-API-Token')
auth = check_token(api_token)
if not auth["success"]:
return auth
db = server.get_db()
c = db.cursor()
# Crazy logic to say "Delete only if there's at least 1 more token for this company."
c.execute('DELETE FROM tokens WHERE api_token=?', [token])
db.commit()
if c.rowcount > 0:
result = {"success": True}
else:
result = {"success": False, "error": "No such token"}
return result
@server.app.route('/new_token', methods=["POST", "PUT"])
def assign_new_token():
"""Builds a new API token and assigns it to the current user's
company. If a bank wanted each client to use a unique token,
perhaps so that they can identify which API client from logged
data, they might use this method.
If you think this method looks too sloppy to be real code,
you don't do enough source code assessments."""
from hashlib import sha256
api_token = server.request.headers.get('X-API-Token')
auth = check_token(api_token)
if not auth["success"]:
return auth
db = server.get_db()
c = db.cursor()
# TODO: SHA256 is secure, but is there a better way to do this?
row = c.execute('SELECT MAX(id) FROM tokens').fetchone()
old_token_id = row[0]
# Hashing methods only take bytes objects.
# All this says is "give me '5' as a byte array"
num = bytes(str(old_token_id), 'utf-8')
h = sha256()
h.update(num)
out = h.digest()
# We don't want bytes, we want 20-character strings using 0-9 and a-z
# This causes some truncation and loss of entropy
# TODO: longer tokens to maintain entropy?
out = out[:20]
out_modulo = [int(x) % 36 for x in out] # 36 = 26 letters + 10 digits
for i in range(len(out_modulo)):
if out_modulo[i] < 10:
out_modulo[i] = str(out_modulo[i])
else:
out_modulo[i] = chr(ord('a') + out_modulo[i] - 10)
new_token = ''.join(out_modulo)
c.execute('INSERT INTO tokens VALUES (null, ?, ?)', [new_token, auth["user"]["company"]])
db.commit()
result = {
"success": True,
"id": c.lastrowid,
"token": new_token
}
return result
@server.app.route('/get_company/<comp_id>')
def get_company(comp_id):
c = server.get_db().cursor()
company = c.execute("SELECT * FROM companies WHERE id=?", [comp_id]).fetchone()
if company is not None:
result = {"success": True, "data": row2dict(company)}
return result
else:
result = {"success": False, "error": "Company ID {} not found".format(comp_id)}
return result