diff options
Diffstat (limited to 'sw/auth.py')
| -rw-r--r-- | sw/auth.py | 46 |
1 files changed, 37 insertions, 9 deletions
@@ -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") |
