python-gallery/gallery/auth.py

109 lines
3.1 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 sqlalchemy.orm import sessionmaker
2023-03-04 13:45:26 +00:00
from gallery import db
blueprint = Blueprint('auth', __name__, url_prefix='/auth')
db_session = sessionmaker(bind=db.engine)
db_session = db_session()
@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
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 = db_session.query(db.Users).filter_by(username=username).first()
if not user or not check_password_hash(user.password, password):
2023-04-04 14:00:00 +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)
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
@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
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
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):
error.append('Username is invalid!')
2023-03-04 13:45:26 +00:00
if not email or not email_regex.match(email):
error.append('Email is invalid!')
if not password:
error.append('Password is empty!')
elif len(password) < 8:
error.append('Password is too short! Longer than 8 characters pls')
if not password_repeat:
2023-03-04 13:45:26 +00:00
error.append('Enter password again!')
elif password_repeat != password:
error.append('Passwords do not match!')
2023-04-04 14:00:00 +00:00
user_exists = db_session.query(db.Users).filter_by(username=username).first()
if user_exists:
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 = db.Users(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
logging.info('User %s registered', username)
return 'ok', 200
@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()
flash(['Goodbye!!!', '4'])
return redirect(url_for('gallery.index'))