2023-03-04 13:45:26 +00:00
|
|
|
"""
|
|
|
|
OnlyLegs - Authentification
|
|
|
|
User registration, login and logout and locking access to pages behind a login
|
|
|
|
"""
|
|
|
|
import re
|
|
|
|
import uuid
|
|
|
|
import logging
|
2023-03-05 16:22:11 +00:00
|
|
|
from datetime import datetime as dt
|
2023-03-04 13:45:26 +00:00
|
|
|
|
2023-01-10 12:39:29 +00:00
|
|
|
import functools
|
2023-03-04 13:45:26 +00:00
|
|
|
from flask import Blueprint, flash, g, redirect, request, session, url_for, abort, jsonify
|
2023-01-10 12:39:29 +00:00
|
|
|
from werkzeug.security import check_password_hash, generate_password_hash
|
2023-03-01 23:29:34 +00:00
|
|
|
|
2023-03-03 00:26:46 +00:00
|
|
|
from sqlalchemy.orm import sessionmaker
|
2023-03-04 13:45:26 +00:00
|
|
|
from sqlalchemy import exc
|
|
|
|
|
|
|
|
from gallery import db
|
|
|
|
|
|
|
|
|
|
|
|
blueprint = Blueprint('auth', __name__, url_prefix='/auth')
|
2023-03-03 00:26:46 +00:00
|
|
|
db_session = sessionmaker(bind=db.engine)
|
|
|
|
db_session = db_session()
|
2023-03-01 23:29:34 +00:00
|
|
|
|
|
|
|
|
2023-03-04 13:45:26 +00:00
|
|
|
def login_required(view):
|
|
|
|
"""
|
|
|
|
Decorator to check if a user is logged in before accessing a page
|
|
|
|
"""
|
|
|
|
@functools.wraps(view)
|
|
|
|
def wrapped_view(**kwargs):
|
|
|
|
if g.user is None or session.get('uuid') is None:
|
|
|
|
logging.error('Authentification failed')
|
|
|
|
session.clear()
|
|
|
|
return redirect(url_for('gallery.index'))
|
2023-01-10 12:39:29 +00:00
|
|
|
|
2023-03-04 13:45:26 +00:00
|
|
|
return view(**kwargs)
|
|
|
|
|
|
|
|
return wrapped_view
|
2023-01-10 12:39:29 +00:00
|
|
|
|
|
|
|
|
2023-01-10 14:40:43 +00:00
|
|
|
@blueprint.before_app_request
|
2023-01-10 12:39:29 +00:00
|
|
|
def load_logged_in_user():
|
2023-03-04 13:45:26 +00:00
|
|
|
"""
|
|
|
|
Runs before every request and checks if a user is logged in
|
|
|
|
"""
|
2023-01-10 12:39:29 +00:00
|
|
|
user_id = session.get('user_id')
|
2023-03-01 23:29:34 +00:00
|
|
|
user_uuid = session.get('uuid')
|
2023-01-10 12:39:29 +00:00
|
|
|
|
2023-03-01 23:29:34 +00:00
|
|
|
if user_id is None or user_uuid is None:
|
2023-01-10 12:39:29 +00:00
|
|
|
g.user = None
|
2023-03-01 23:29:34 +00:00
|
|
|
session.clear()
|
2023-01-10 12:39:29 +00:00
|
|
|
else:
|
2023-03-05 16:22:11 +00:00
|
|
|
is_alive = db_session.query(db.Sessions).filter_by(session_uuid=user_uuid).first()
|
2023-03-01 23:29:34 +00:00
|
|
|
|
|
|
|
if is_alive is None:
|
2023-03-04 13:45:26 +00:00
|
|
|
logging.info('Session expired')
|
2023-03-01 23:29:34 +00:00
|
|
|
flash(['Session expired!', '3'])
|
|
|
|
session.clear()
|
|
|
|
else:
|
2023-03-05 16:22:11 +00:00
|
|
|
g.user = db_session.query(db.Users).filter_by(id=user_id).first()
|
2023-03-04 13:45:26 +00:00
|
|
|
|
2023-01-10 12:39:29 +00:00
|
|
|
|
2023-01-25 15:13:56 +00:00
|
|
|
@blueprint.route('/register', methods=['POST'])
|
2023-01-10 12:39:29 +00:00
|
|
|
def register():
|
2023-03-04 13:45:26 +00:00
|
|
|
"""
|
|
|
|
Register a new user
|
|
|
|
"""
|
2023-01-25 15:13:56 +00:00
|
|
|
username = request.form['username']
|
|
|
|
email = request.form['email']
|
|
|
|
password = request.form['password']
|
|
|
|
password_repeat = request.form['password-repeat']
|
2023-03-04 13:45:26 +00:00
|
|
|
|
2023-01-25 15:13:56 +00:00
|
|
|
error = []
|
|
|
|
|
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')
|
|
|
|
|
|
|
|
|
|
|
|
if not username or not username_regex.match(username):
|
|
|
|
error.append('Username is invalid!')
|
2023-03-01 23:29:34 +00:00
|
|
|
|
2023-03-04 13:45:26 +00:00
|
|
|
if not email or not email_regex.match(email):
|
2023-01-25 15:13:56 +00:00
|
|
|
error.append('Email is invalid!')
|
2023-03-01 23:29:34 +00:00
|
|
|
|
2023-01-25 15:13:56 +00:00
|
|
|
if not password:
|
|
|
|
error.append('Password is empty!')
|
|
|
|
elif len(password) < 8:
|
|
|
|
error.append('Password is too short! Longer than 8 characters pls')
|
2023-03-01 23:29:34 +00:00
|
|
|
|
2023-01-25 15:13:56 +00:00
|
|
|
if not password_repeat:
|
2023-03-04 13:45:26 +00:00
|
|
|
error.append('Enter password again!')
|
2023-01-25 15:13:56 +00:00
|
|
|
elif password_repeat != password:
|
|
|
|
error.append('Passwords do not match!')
|
|
|
|
|
2023-03-04 13:45:26 +00:00
|
|
|
if error:
|
|
|
|
return jsonify(error)
|
|
|
|
|
2023-03-01 23:29:34 +00:00
|
|
|
|
2023-03-04 13:45:26 +00:00
|
|
|
try:
|
2023-03-05 16:22:11 +00:00
|
|
|
register_user = db.Users(username=username,
|
|
|
|
email=email,
|
|
|
|
password=generate_password_hash(password),
|
2023-03-08 21:58:58 +00:00
|
|
|
created_at=dt.utcnow())
|
2023-03-05 16:22:11 +00:00
|
|
|
db_session.add(register_user)
|
2023-03-04 13:45:26 +00:00
|
|
|
db_session.commit()
|
|
|
|
except exc.IntegrityError:
|
|
|
|
return f'User {username} is already registered!'
|
|
|
|
except Exception as err:
|
|
|
|
logging.error('User %s could not be registered: %s', username, err)
|
|
|
|
return 'Something went wrong!'
|
|
|
|
|
|
|
|
logging.info('User %s registered', username)
|
|
|
|
return 'gwa gwa'
|
2023-01-25 15:13:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
@blueprint.route('/login', methods=['POST'])
|
2023-01-10 12:39:29 +00:00
|
|
|
def login():
|
2023-03-04 13:45:26 +00:00
|
|
|
"""
|
|
|
|
Log in a registered user by adding the user id to the session
|
|
|
|
"""
|
2023-01-25 15:13:56 +00:00
|
|
|
username = request.form['username']
|
|
|
|
password = request.form['password']
|
2023-03-04 13:45:26 +00:00
|
|
|
|
2023-03-05 16:22:11 +00:00
|
|
|
user = db_session.query(db.Users).filter_by(username=username).first()
|
2023-03-04 13:45:26 +00:00
|
|
|
error = []
|
|
|
|
|
2023-01-25 15:13:56 +00:00
|
|
|
|
|
|
|
if user is None:
|
2023-03-04 13:45:26 +00:00
|
|
|
logging.error('User %s does not exist. Login attempt from %s',
|
|
|
|
username, request.remote_addr)
|
|
|
|
error.append('Username or Password is incorrect!')
|
2023-03-03 00:26:46 +00:00
|
|
|
elif not check_password_hash(user.password, password):
|
2023-03-04 13:45:26 +00:00
|
|
|
logging.error('User %s entered wrong password. Login attempt from %s',
|
|
|
|
username, request.remote_addr)
|
|
|
|
error.append('Username or Password is incorrect!')
|
|
|
|
|
|
|
|
if error:
|
2023-01-25 15:13:56 +00:00
|
|
|
abort(403)
|
|
|
|
|
2023-03-04 13:45:26 +00:00
|
|
|
|
2023-03-01 23:29:34 +00:00
|
|
|
try:
|
2023-01-25 15:13:56 +00:00
|
|
|
session.clear()
|
2023-03-03 00:26:46 +00:00
|
|
|
session['user_id'] = user.id
|
2023-03-01 23:29:34 +00:00
|
|
|
session['uuid'] = str(uuid.uuid4())
|
2023-03-04 13:45:26 +00:00
|
|
|
|
2023-03-05 16:22:11 +00:00
|
|
|
session_query = db.Sessions(user_id=user.id,
|
|
|
|
session_uuid=session.get('uuid'),
|
|
|
|
ip_address=request.remote_addr,
|
|
|
|
user_agent=request.user_agent.string,
|
|
|
|
active=True,
|
2023-03-08 21:58:58 +00:00
|
|
|
created_at=dt.utcnow())
|
2023-03-05 16:22:11 +00:00
|
|
|
|
|
|
|
db_session.add(session_query)
|
2023-03-03 00:26:46 +00:00
|
|
|
db_session.commit()
|
2023-03-04 13:45:26 +00:00
|
|
|
except Exception as err:
|
|
|
|
logging.error('User %s could not be logged in: %s', username, err)
|
2023-03-01 23:29:34 +00:00
|
|
|
abort(500)
|
|
|
|
|
2023-03-04 13:45:26 +00:00
|
|
|
logging.info('User %s logged in from %s', username, request.remote_addr)
|
|
|
|
flash(['Logged in successfully!', '4'])
|
|
|
|
return 'gwa gwa'
|
2023-03-01 23:29:34 +00:00
|
|
|
|
2023-01-10 12:39:29 +00:00
|
|
|
|
2023-01-10 14:40:43 +00:00
|
|
|
@blueprint.route('/logout')
|
2023-01-10 12:39:29 +00:00
|
|
|
def logout():
|
2023-03-04 13:45:26 +00:00
|
|
|
"""
|
|
|
|
Clear the current session, including the stored user id
|
|
|
|
"""
|
|
|
|
logging.info('User (%s) %s logged out', session.get('user_id'), g.user.username)
|
2023-01-10 12:39:29 +00:00
|
|
|
session.clear()
|
|
|
|
return redirect(url_for('index'))
|