python-gallery/onlylegs/auth.py

110 lines
3 KiB
Python
Raw Normal View History

2023-03-04 13:45:26 +00:00
"""
OnlyLegs - Authentication
2023-03-04 13:45:26 +00:00
User registration, login and logout and locking access to pages behind a login
"""
import re
import logging
2023-04-04 14:00:00 +00:00
from flask import Blueprint, flash, redirect, request, url_for, abort, jsonify
from werkzeug.security import check_password_hash, generate_password_hash
from flask_login import login_user, logout_user, login_required
2023-03-04 13:45:26 +00:00
from onlylegs.extensions import db
from onlylegs.models import User
2023-03-04 13:45:26 +00:00
2023-04-07 12:35:30 +00:00
blueprint = Blueprint("auth", __name__, url_prefix="/auth")
2023-04-07 12:35:30 +00:00
@blueprint.route("/login", methods=["POST"])
def login():
2023-03-04 13:45:26 +00:00
"""
Log in a registered user by adding the user id to the session
2023-03-04 13:45:26 +00:00
"""
error = []
2023-04-04 14:00:00 +00:00
2023-04-07 12:35:30 +00:00
username = request.form["username"].strip()
password = request.form["password"].strip()
remember = bool(request.form["remember-me"])
2023-03-04 13:45:26 +00:00
user = User.query.filter_by(username=username).first()
if not user or not check_password_hash(user.password, password):
2023-04-07 12:35:30 +00:00
logging.error("Login attempt from %s", request.remote_addr)
error.append("Username or Password is incorrect!")
if error:
abort(403)
2023-04-04 17:46:07 +00:00
login_user(user, remember=remember)
2023-04-07 12:35:30 +00:00
logging.info("User %s logged in from %s", username, request.remote_addr)
flash(["Logged in successfully!", "4"])
return "ok", 200
2023-03-04 13:45:26 +00:00
2023-04-07 12:35:30 +00:00
@blueprint.route("/register", methods=["POST"])
def register():
2023-03-04 13:45:26 +00:00
"""
Register a new user
"""
error = []
2023-04-04 14:00:00 +00:00
# Thanks Fennec for reminding me to strip out the whitespace lol
2023-04-07 12:35:30 +00:00
username = request.form["username"].strip()
email = request.form["email"].strip()
password = request.form["password"].strip()
password_repeat = request.form["password-repeat"].strip()
2023-03-04 13:45:26 +00:00
2023-04-07 12:35:30 +00:00
email_regex = re.compile(r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b")
username_regex = re.compile(r"\b[A-Za-z0-9._-]+\b")
2023-03-04 13:45:26 +00:00
# Validate the form
2023-03-04 13:45:26 +00:00
if not username or not username_regex.match(username):
2023-04-07 12:35:30 +00:00
error.append("Username is invalid!")
2023-03-04 13:45:26 +00:00
if not email or not email_regex.match(email):
2023-04-07 12:35:30 +00:00
error.append("Email is invalid!")
if not password:
2023-04-07 12:35:30 +00:00
error.append("Password is empty!")
elif len(password) < 8:
2023-04-07 12:35:30 +00:00
error.append("Password is too short! Longer than 8 characters pls")
if not password_repeat:
2023-04-07 12:35:30 +00:00
error.append("Enter password again!")
elif password_repeat != password:
2023-04-07 12:35:30 +00:00
error.append("Passwords do not match!")
2023-04-04 14:00:00 +00:00
user_exists = User.query.filter_by(username=username).first()
if user_exists:
2023-04-07 12:35:30 +00:00
error.append("User already exists!")
# If there are errors, return them
2023-03-04 13:45:26 +00:00
if error:
print(error)
return jsonify(error), 400
2023-03-04 13:45:26 +00:00
register_user = User(
2023-04-07 12:35:30 +00:00
username=username,
email=email,
password=generate_password_hash(password, method="sha256"),
)
db.session.add(register_user)
db.session.commit()
2023-03-04 13:45:26 +00:00
2023-04-07 12:35:30 +00:00
logging.info("User %s registered", username)
return "ok", 200
2023-04-07 12:35:30 +00:00
@blueprint.route("/logout")
@login_required
def logout():
2023-03-04 13:45:26 +00:00
"""
Clear the current session, including the stored user id
"""
logout_user()
2023-04-07 12:35:30 +00:00
flash(["Goodbye!!!", "4"])
return redirect(url_for("gallery.index"))