Skip to content

Commit aa94f2c

Browse files
authored
Tweak structure slightly (#2)
Dataflow breaks in the current setup, changing the code is quicker than a fix atm.
1 parent c32880d commit aa94f2c

File tree

4 files changed

+21
-16
lines changed

4 files changed

+21
-16
lines changed

flask_webgoat/__init__.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,26 @@
44

55
from flask import Flask, g
66

7+
DB_FILENAME = 'database.db'
8+
9+
10+
def query_db(query, args = (), one=False, commit=False):
11+
with sqlite3.connect(DB_FILENAME) as conn:
12+
cur = conn.cursor().execute(query, args)
13+
if commit:
14+
conn.commit()
15+
return cur.fetchone() if one else cur.fetchall()
16+
717

818
def create_app():
919
app = Flask(__name__)
1020
app.secret_key = 'aeZ1iwoh2ree2mo0Eereireong4baitixaixu5Ee'
1121

12-
db_filename = 'database.db'
13-
db_path = Path(db_filename)
22+
db_path = Path(DB_FILENAME)
1423
if db_path.exists():
1524
db_path.unlink()
1625

17-
conn = sqlite3.connect(db_filename)
26+
conn = sqlite3.connect(DB_FILENAME)
1827
create_table_query = """CREATE TABLE IF NOT EXISTS user
1928
(id INTEGER PRIMARY KEY, username TEXT, password TEXT, access_level INTEGER)"""
2029
conn.execute(create_table_query)
@@ -25,13 +34,6 @@ def create_app():
2534
conn.commit()
2635
conn.close()
2736

28-
def query_db(query, args = (), one=False, commit=False):
29-
with sqlite3.connect(db_filename) as conn:
30-
cur = conn.execute(query, args)
31-
if commit:
32-
conn.commit()
33-
return cur.fetchone() if one else cur.fetchall()
34-
app.query_db = query_db
3537

3638
with app.app_context():
3739
from . import status

flask_webgoat/actions.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,13 @@ def log_entry():
2828
return jsonify({'error': 'text parameter is required'})
2929

3030
user_id = auth[0]
31-
user_dir = Path("data/" + str(user_id))
32-
if not user_dir.exists():
33-
user_dir.mkdir()
31+
user_dir = "data/" + str(user_id)
32+
user_dir_path = Path(user_dir)
33+
if not user_dir_path.exists():
34+
user_dir_path.mkdir()
3435

3536
filename = filename_param + ".txt"
36-
path = user_dir / filename
37+
path = Path(user_dir + "/" + filename)
3738
with path.open("w", encoding ="utf-8") as f:
3839
f.write(text_param)
3940
return jsonify({'success': True})

flask_webgoat/auth.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
)
44
from werkzeug.security import check_password_hash
55
from flask import current_app as app
6+
from . import query_db
67

78
bp = Blueprint('auth', __name__)
89

@@ -15,7 +16,7 @@ def login():
1516
return jsonify({'error': 'username and password parameter have to be provided'}), 400
1617

1718
query = "SELECT id, username, access_level FROM user WHERE username = '%s' AND password = '%s'" % (username, password)
18-
result = app.query_db(query, [], True)
19+
result = query_db(query, [], True)
1920
if result is None:
2021
return jsonify({'bad_login': True}), 400
2122
session['user_info'] = (result[0], result[1], result[2])

flask_webgoat/users.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
)
44
from werkzeug.security import check_password_hash
55
from flask import current_app as app
6+
from . import query_db
67

78
bp = Blueprint('users', __name__)
89

@@ -27,7 +28,7 @@ def create_user():
2728
query = "INSERT INTO user (username, password, access_level) VALUES ('%s', '%s', %d)" % (username, password, int(access_level))
2829

2930
try:
30-
app.query_db(query, [], False, True)
31+
query_db(query, [], False, True)
3132
return jsonify({'success': True})
3233
except sqlite3.Error as err:
3334
return jsonify({'error': 'could not create user:' + err})

0 commit comments

Comments
 (0)