From 68703447d8fc57c7b77b66a6acc05bf22fb7d71b Mon Sep 17 00:00:00 2001 From: Tom Harley Date: Sat, 31 Jul 2021 14:06:45 +0000 Subject: 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. --- sw/auth.py | 46 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 37 insertions(+), 9 deletions(-) (limited to 'sw/auth.py') 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") -- cgit v1.2.1