summaryrefslogtreecommitdiff
path: root/sw/auth.py
diff options
context:
space:
mode:
authorTom Harley2021-07-31 14:06:45 +0000
committerTom Harley2021-07-31 19:30:35 +0000
commit68703447d8fc57c7b77b66a6acc05bf22fb7d71b (patch)
tree715006453d66889e210fd41f17aff9153f9a5092 /sw/auth.py
parentAdd templates (diff)
downloadswap-68703447d8fc57c7b77b66a6acc05bf22fb7d71b.tar.gz
swap-68703447d8fc57c7b77b66a6acc05bf22fb7d71b.zip

Display inbox/outbox contents correctly

As per previous commit, the contents of inboxes and outboxes were not being properly displayed, which was difficult to test because of there being very few items in the test DB.

This commit includes moving functions such as prepared_output into a new file sw/util.py; including them in sw/users.py made little sense and caused a circular dependency with sw/auth.py.

A stub function for item info retreival has also been created, in new file sw/items.py.

Diffstat (limited to 'sw/auth.py')
-rw-r--r--sw/auth.py46
1 files changed, 37 insertions, 9 deletions
diff --git a/sw/auth.py b/sw/auth.py
index 4fd0f68..1f9e5d8 100644
--- a/sw/auth.py
+++ b/sw/auth.py
@@ -4,6 +4,7 @@ import logging
from flask import (
abort,
+ redirect,
Blueprint,
flash,
g,
@@ -18,6 +19,7 @@ from flask import (
from werkzeug.security import generate_password_hash, check_password_hash
from sw.db import get_db
+from sw.util import prepared_output
bp = Blueprint("auth", __name__, url_prefix="/auth")
@@ -92,24 +94,50 @@ def register():
return response
-@bp.route("/login", methods=["POST"])
-@json_input
-def login():
- username = request.json["username"]
- password = request.json["password"]
-
+def login(username, password):
db = get_db()
user = db.execute("select * from users where username=?", (username,)).fetchone()
if (user is None) or (password is None):
- return "Username or password incorrect", 403
+ abort(403)
if not check_password_hash(user["password"], password):
- return "Username or password incorrect", 403
+ return False
session.clear()
session["user_id"] = user["id"]
- return "Login Successful", 200
+
+ return True
+
+
+@bp.route("/login", methods=["GET"])
+def get_login():
+ if request.args:
+ username = request.args.get("username")
+ password = request.args.get("password")
+
+ if login(username, password):
+ url = url_for("users.get_user_info", username=username)
+ return redirect(url, 200)
+
+ # I saw it suggested online that this response should have a 401
+ # status code, but lynx acts funny when this is the case. Since
+ # other (maybe more popular) browsers might also behave this way,
+ # this is being left as 200 for now.
+ return prepared_output("login", status=200)
+
+
+@bp.route("/login", methods=["POST"])
+@json_input
+def post_login():
+ username = request.json.get("username")
+ password = request.json.get("password")
+
+ if login(username, password):
+ return Response(status=200)
+
+ else:
+ abort(403)
@bp.route("/logout")