diff --git a/website/app.py b/website/app.py index 7a728c0..93bbe2f 100644 --- a/website/app.py +++ b/website/app.py @@ -12,6 +12,7 @@ from website.bp.system import system_bp from website.config import BaseConfig from website.model import db +from website.session import get_user def create_app(): @@ -39,4 +40,17 @@ def favicon(): mimetype="image/vnd.microsoft.icon", ) + class UserTemplate: + def __init__(self, auth): + self.is_authenticated = auth + + @app.context_processor + def inject_user(): + user = get_user() + if user is not None: + user = UserTemplate(True) + else: + user = UserTemplate(False) + return dict(user=user) + return app diff --git a/website/bp/auth.py b/website/bp/auth.py index 9377c35..50b7aca 100644 --- a/website/bp/auth.py +++ b/website/bp/auth.py @@ -10,55 +10,47 @@ auth_bp = Blueprint("auth", __name__) -@auth_bp.route("/register") +@auth_bp.route("/register/") def register(): return render_template("auth/register.html") -@auth_bp.route("/register-api") +@auth_bp.post("/register-api/") def register_api(): - if request.method == "POST": - data_json = json.loads(request.data) - username = data_json["username"] - password = data_json["password"] - requested_user = db.session.query(User).filter_by(username=username).first() - if requested_user is None: - user = User(username=username, password=werkzeug.security.generate_password_hash(password)) - db.session.add(user) - db.session.commit() - login_session(user) - return jsonify({"success": True}) - else: - abort(401) + username = request.form["username"] + password = request.form["password"] + requested_user = db.session.query(User).filter_by(username=username).first() + if requested_user is None: + user = User(username=username, password=werkzeug.security.generate_password_hash(password)) + db.session.add(user) + db.session.commit() + login_session(user.username) + return jsonify({"success": True}) else: - abort(405) + abort(401) -@auth_bp.route("/login") +@auth_bp.route("/login/") def login(): return render_template("auth/login.html") -@auth_bp.route("/login-api") +@auth_bp.post("/login-api/") def login_api(): - if request.method == "POST": - data_json = json.loads(request.data) - username = data_json["username"] - password = data_json["password"] - requested_user = db.session.query(User).filter_by(username=username).first() - if requested_user is None: - abort(404) - else: - if werkzeug.security.check_password_hash(requested_user.password, password): - login_session(requested_user) - return jsonify({"success": True}) - else: - return jsonify({"success": False}) + username = request.form["username"] + password = request.form["password"] + requested_user = db.session.query(User).filter_by(username=username).first() + if requested_user is None: + abort(404) else: - abort(405) + if werkzeug.security.check_password_hash(requested_user.password, password): + login_session(requested_user.username) + return jsonify({"success": True}) + else: + return jsonify({"success": False}) -@auth_bp.route("/logout") +@auth_bp.route("/logout/") @login_required def logout(user): logout_session() diff --git a/website/bp/errors.py b/website/bp/errors.py index 9795d0f..0949c26 100644 --- a/website/bp/errors.py +++ b/website/bp/errors.py @@ -16,7 +16,7 @@ def error_500(e): original_exception = e.original_exception if isinstance(original_exception, SpaceTradersException): # TODO: Not for every issue though flash(str(original_exception), "danger") - return redirect(url_for("local.select_user")) + return redirect(url_for("local.select_token")) resp = Response(render_template("error/500.html")) resp.status_code = 500 return resp diff --git a/website/bp/faction.py b/website/bp/faction.py index 1f16c38..4eae1f3 100644 --- a/website/bp/faction.py +++ b/website/bp/faction.py @@ -6,6 +6,7 @@ from flask import * from website.paginated_return import paginated_return +from website.session import get_session, anonymous_session from website.wrappers import token_required, minify_html faction_bp = Blueprint("faction", __name__) @@ -13,8 +14,10 @@ @faction_bp.route("/factions/") @minify_html -@token_required -def factions(session): +def factions(): + session = get_session() + if session is None: + session = anonymous_session() page = int(request.args.get("page", default=1)) factions = Faction.all(session) new_li = paginated_return(factions, page) @@ -23,8 +26,10 @@ def factions(session): @faction_bp.route("/faction//") @minify_html -@token_required -def faction(symbol, session): +def faction(symbol): + session = get_session() + if session is None: + session = anonymous_session() light_background = {}.get(symbol, "") dark_background = {}.get(symbol, "") force_dark = {"VOID": True}.get(symbol, False) diff --git a/website/bp/local.py b/website/bp/local.py index 8896a91..ed1e98c 100644 --- a/website/bp/local.py +++ b/website/bp/local.py @@ -16,10 +16,10 @@ local_bp = Blueprint("local", __name__) -@local_bp.route("/create-user/") +@local_bp.route("/add-token/") @minify_html @login_required -def create_user(user): +def add_token(user): return render_template("local/create_user.html") @@ -41,13 +41,13 @@ def create_user_with_token_api(user): return jsonify({}) -@local_bp.route("/select-user/") +@local_bp.route("/select-token/") @minify_html @login_required -def select_user(user): +def select_token(user): if db.session.query(User).count() == 0: - flash("No users found, please create one", "info") - return redirect(url_for("local.create_user")) + flash("No tokens found, please create one", "info") + return redirect(url_for("local.add_token")) class MockAgent: def __init__(self, token, id, active): @@ -64,7 +64,7 @@ def __init__(self, token, id, active): users.append(a) except Exception as e: users.append(MockAgent(user.token, user.id, user.active)) - return render_template("local/select_user.html", users=users) + return render_template("local/select_token.html", users=users) @local_bp.route("/select-user-api/") diff --git a/website/bp/main.py b/website/bp/main.py index 17e20c7..1e22e74 100644 --- a/website/bp/main.py +++ b/website/bp/main.py @@ -19,6 +19,7 @@ check_filters_contract, check_filters_faction, quick_weight, ) +from website.session import get_session, anonymous_session from website.wrappers import token_required, minify_html main_bp = Blueprint("main", __name__) @@ -106,8 +107,10 @@ def automation(i): @main_bp.route("/agents/") @minify_html -@token_required -def agents(session): +def agents(): + session = get_session() + if session is None: + session = anonymous_session() page = int(request.args.get("page", default=1)) agents_list = Agent.all(session, page) li = {1} @@ -144,8 +147,10 @@ def agents(session): @main_bp.route("/agent//") @minify_html -@token_required -def agent(symbol, session): +def agent(symbol): + session = get_session() + if session is None: + session = anonymous_session() return render_template("agent/agent.html", agent=Agent(session, symbol)) diff --git a/website/session.py b/website/session.py index 2ca8b3e..f70d6bf 100644 --- a/website/session.py +++ b/website/session.py @@ -6,24 +6,24 @@ def get_session(): if "username" in session: - user = db.session.query(Token).filter_by(active=True, username=session["username"]).first() + user = db.session.query(Token).filter_by(active=True, user=session["username"]).first() if user is None: - user = db.session.query(Token, username=session["username"]).first() + user = db.session.query(Token).filter_by(user=session["username"]).first() if user is None: - raise ValueError("No token not found") + return None user.active = True db.session.commit() return asession.AutoTradersSession(user.token) else: - raise ValueError("User not logged in") + return None def get_user(): if "username" in session: - user = db.session.query(Token).filter_by(username=session["username"]).first() + user = db.session.query(Token).filter_by(user=session["username"]).first() return user else: - raise ValueError("User not logged in") + return None def login_session(username): @@ -34,3 +34,7 @@ def login_session(username): def logout_session(): del session["username"] session["logged_in"] = False + + +def anonymous_session(): + return asession.AutoTradersSession() diff --git a/website/templates/auth/login.html b/website/templates/auth/login.html index fc0f5d6..ea8b83a 100644 --- a/website/templates/auth/login.html +++ b/website/templates/auth/login.html @@ -3,19 +3,30 @@ Login {% endblock %} {% block body %} -
-

Login

+
+
+

Login

+
+
+
+ + +
+
+ + +
+ +
-
- - - - - -