diff options
| author | Tom Harley | 2021-07-03 00:18:13 +0000 |
|---|---|---|
| committer | Tom Harley | 2021-07-03 00:18:13 +0000 |
| commit | 7c5c6c52250ca57257ab4af2cc9cdf4425595ec0 (patch) | |
| tree | d36019286125bf984e0c267645d53ff562533ebf /sw/auth.py | |
| download | swap-7c5c6c52250ca57257ab4af2cc9cdf4425595ec0.tar.gz swap-7c5c6c52250ca57257ab4af2cc9cdf4425595ec0.zip | |
Initial commit
Diffstat (limited to 'sw/auth.py')
| -rw-r--r-- | sw/auth.py | 119 |
1 files changed, 119 insertions, 0 deletions
diff --git a/sw/auth.py b/sw/auth.py new file mode 100644 index 0000000..5429171 --- /dev/null +++ b/sw/auth.py @@ -0,0 +1,119 @@ +import functools + +from flask import ( + Blueprint, + flash, + g, + redirect, + render_template, + request, + session, + url_for, + Response, +) + +from werkzeug.security import generate_password_hash, check_password_hash + +from sw.db import get_db + +bp = Blueprint("auth", __name__, url_prefix="/auth") + + +@bp.before_app_request +def load_logged_in_user(): + user_id = session.get("user_id") + + if user_id is None: + g.user = None + + else: + g.user = ( + get_db().execute("select * from user where id=?", (user_id,)).fetchone() + ) + + +@bp.route("/register", methods=["POST"]) +def register(): + if not request.is_json: + return "Request body is not JSON", 422 + + data = request.json + + if "username" not in data: + return "Username required", 400 + + if "password" not in data: + return "Password required", 400 + + username = data["username"] + password = data["password"] + + if username is None: + return "Username required", 400 + + if password is None: + return "Password required", 400 + + if username == "": + return "Username must not be empty", 400 + + if password == "": + return "Password must not be empty", 400 + + db = get_db() + existing_user = db.execute( + "select id from user where username=?", (username,) + ).fetchone() + + if existing_user is not None: + return f"Username {username} is taken", 409 + + db.execute( + "insert into user (username, password) values (?, ?)", + (username, generate_password_hash(password)), + ) + db.commit() + + response = Response(status=201) + # response.location = url_for("ap.get_user", user=username) + + return response + + +@bp.route("/login", methods=["POST"]) +def login(): + if not request.is_json: + return "Request body is not JSON", 422 + + username = request.json["username"] + password = request.json["password"] + + db = get_db() + user = db.execute("select * from user where username=?", (username,)).fetchone() + + if user is None: + return "Username or password incorrect", 403 + + if not check_password_hash(user["password"], password): + return "Username or password incorrect", 403 + + session.clear() + session["user_id"] = user["id"] + return 200 + + +@bp.route("/logout") +def logout(): + session.clear() + return 200 + + +def login_required(view): + @functools.wraps(view) + def wrapped_view(**kwargs): + if g.user is None: + return "Login required", 403 + + return view(**kwargs) + + return wrapped_view |
