Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
191 lines (166 loc) · 6.89 KB
/
Copy pathapp.py
File metadata and controls
191 lines (166 loc) · 6.89 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import os
import sqlite3
from datetime import date
from flask import Flask, render_template, request, jsonify, session
from werkzeug.security import generate_password_hash, check_password_hash
from functools import wraps
app = Flask(__name__)
# Set secret key securely, falling back to a local default if not in environment
app.secret_key = os.environ.get('SECRET_KEY', 'local_dev_fallback_secret')
# Security: Configure session cookies
app.config.update(
SESSION_COOKIE_HTTPONLY=True,
SESSION_COOKIE_SAMESITE='Strict',
)
DB_FILE = 'locktf.db'
def get_db():
conn = sqlite3.connect(DB_FILE)
conn.row_factory = sqlite3.Row
return conn
def init_db():
with get_db() as conn:
# Enable foreign key support in SQLite
conn.execute('PRAGMA foreign_keys = ON;')
# Users table for private accounts
conn.execute('''
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT UNIQUE NOT NULL,
password TEXT NOT NULL
)
''')
# Todos table now references the user_id
conn.execute('''
CREATE TABLE IF NOT EXISTS todos (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
task TEXT NOT NULL,
status INTEGER DEFAULT 0,
FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE
)
''')
# Sessions table uses a composite primary key and references user_id
conn.execute('''
CREATE TABLE IF NOT EXISTS sessions (
user_id INTEGER NOT NULL,
date TEXT NOT NULL,
count INTEGER DEFAULT 0,
PRIMARY KEY (user_id, date),
FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE
)
''')
init_db()
@app.after_request
def add_security_headers(response):
response.headers['X-Content-Type-Options'] = 'nosniff'
response.headers['X-Frame-Options'] = 'DENY'
response.headers['X-XSS-Protection'] = '1; mode=block'
response.headers['Content-Security-Policy'] = "default-src 'self'; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline'; connect-src 'self';"
return response
def login_required(f):
@wraps(f)
def decorated_function(*args, **kwargs):
if 'user_id' not in session:
return jsonify({"error": "Unauthorized"}), 401
return f(*args, **kwargs)
return decorated_function
@app.route("/")
def index():
return render_template("home.html")
@app.route("/api/auth/register", methods=["POST"])
def register():
data = request.json or {}
username, password = data.get("username", "").strip(), data.get("password", "")
if not username or not password:
return jsonify({"error": "Username and password required"}), 400
if len(username) < 3 or len(username) > 50:
return jsonify({"error": "Username must be between 3 and 50 characters"}), 400
if len(password) < 8 or len(password) > 128:
return jsonify({"error": "Password must be between 8 and 128 characters"}), 400
hashed_password = generate_password_hash(password)
try:
with get_db() as conn:
cursor = conn.execute("INSERT INTO users (username, password) VALUES (?, ?)", (username, hashed_password))
session['user_id'] = cursor.lastrowid
conn.commit()
return jsonify({"success": True}), 201
except sqlite3.IntegrityError:
return jsonify({"error": "Username already exists"}), 409
@app.route("/api/auth/login", methods=["POST"])
def login():
data = request.json or {}
username, password = data.get("username", "").strip(), data.get("password", "")
if not username or not password:
return jsonify({"error": "Username and password required"}), 400
with get_db() as conn:
user = conn.execute("SELECT * FROM users WHERE username = ?", (username,)).fetchone()
if user and check_password_hash(user['password'], password):
session['user_id'] = user['id']
return jsonify({"success": True})
return jsonify({"error": "Invalid credentials"}), 401
@app.route("/api/auth/logout", methods=["POST"])
def logout():
session.pop('user_id', None)
return jsonify({"success": True})
@app.route("/api/auth/me", methods=["GET"])
@login_required
def me():
return jsonify({"user_id": session['user_id']})
@app.route("/api/todos", methods=["GET", "POST"])
@login_required
def handle_todos():
user_id = session['user_id']
if request.method == "POST":
data = request.json or {}
task_text = data.get("task", "").strip()
if not task_text:
return jsonify({"error": "Task is required"}), 400
if len(task_text) > 500:
return jsonify({"error": "Task exceeds maximum length of 500 characters"}), 400
with get_db() as conn:
cursor = conn.execute("INSERT INTO todos (user_id, task) VALUES (?, ?)", (user_id, task_text))
conn.commit()
return jsonify({"id": cursor.lastrowid, "task": task_text, "status": 0}), 201
else:
with get_db() as conn:
todos = conn.execute("SELECT * FROM todos WHERE user_id = ?", (user_id,)).fetchall()
return jsonify([dict(t) for t in todos])
@app.route("/api/todos/<int:todo_id>", methods=["PUT", "DELETE"])
@login_required
def update_delete_todo(todo_id):
user_id = session['user_id']
if request.method == "PUT":
data = request.json or {}
if "status" not in data:
return jsonify({"error": "Status is required"}), 400
# Ensure status is integer 0 or 1
status = 1 if data["status"] else 0
with get_db() as conn:
conn.execute("UPDATE todos SET status = ? WHERE id = ? AND user_id = ?", (status, todo_id, user_id))
conn.commit()
return jsonify({"success": True})
else:
with get_db() as conn:
conn.execute("DELETE FROM todos WHERE id = ? AND user_id = ?", (todo_id, user_id))
conn.commit()
return jsonify({"success": True})
@app.route("/api/stats", methods=["GET", "POST"])
@login_required
def handle_stats():
user_id = session['user_id']
if request.method == "POST":
today = date.today().isoformat()
with get_db() as conn:
conn.execute('''
INSERT INTO sessions (user_id, date, count)
VALUES (?, ?, 1)
ON CONFLICT(user_id, date) DO UPDATE SET count = count + 1
''', (user_id, today))
conn.commit()
return jsonify({"success": True})
else:
with get_db() as conn:
sessions = conn.execute("SELECT date, count FROM sessions WHERE user_id = ?", (user_id,)).fetchall()
return jsonify({s['date']: s['count'] for s in sessions})
if __name__ == "__main__":
app.run()