diff --git a/flask/app.py b/flask/app.py index 43081b2..c1bad62 100644 --- a/flask/app.py +++ b/flask/app.py @@ -2,27 +2,56 @@ import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parent.parent)) -from flask import Flask, render_template, request, jsonify, redirect -from flask_login import LoginManager, UserMixin +from flask import Flask, render_template, request, redirect, flash +from flask_login import LoginManager, UserMixin, login_user, logout_user, login_required, current_user import hashlib import hmac import base64 from settings import FLASK_SECRET_KEY, TELEGRAM_API_TOKEN, TELEGRAM_BOT_NAME, TELEGRAM_BOT_DOMAIN +from db.queries import user_get app = Flask(__name__) app.secret_key = FLASK_SECRET_KEY +login_manager = LoginManager() +login_manager.init_app(app) +login_manager.login_view = "index" + +class FlaskUser(UserMixin): + def __init__(self, db_user): + self.db_user = db_user + + def get_id(self): + return self.db_user.telegram_username + +@login_manager.user_loader +def load_user(user_id): + try: + db_user = user_get(user_id) + return FlaskUser(db_user) + except: + return None + @app.route('/') def index(): - data = {'bot_name': TELEGRAM_BOT_NAME, 'bot_damin': TELEGRAM_BOT_DOMAIN} - return render_template('index.html', data = data) + if not current_user.is_authenticated: + data = {'bot_name': TELEGRAM_BOT_NAME, 'bot_damin': TELEGRAM_BOT_DOMAIN} + return render_template('index.html', data = data) + else: + return redirect('/dashboard') @app.route('/dashboard') +@login_required def dashboard(): return render_template('dashboard.html') +@app.route('/logout') +def logout(): + logout_user() + return redirect('/') + def string_generator(data_incoming): data = data_incoming.copy() del data['hash'] @@ -50,13 +79,18 @@ def login(): data_check_string_bytes = bytes(data_check_string, 'utf-8') hmac_string = hmac.new(secret_key_bytes, data_check_string_bytes, hashlib.sha256).hexdigest() if hmac_string == tg_data['hash']: - return redirect('/dashboard') - - return jsonify({ - 'hmac_string': hmac_string, - 'tg_hash': tg_data['hash'], - 'tg_data': tg_data - }) + try: + db_user = user_get(tg_data['username']) + login_user(FlaskUser(db_user)) + except: + flash("Login failed. Please try again.") + return redirect('/') + else: + flash("Login failed. Please try again.") + return redirect('/') + + + return redirect('/dashboard') if __name__ == '__main__': app.run(host='0.0.0.0', debug=True, port=8080) \ No newline at end of file diff --git a/flask/templates/dashboard.html b/flask/templates/dashboard.html index 8466013..ea976cc 100644 --- a/flask/templates/dashboard.html +++ b/flask/templates/dashboard.html @@ -5,30 +5,31 @@ - - + Gear Orders Bot - Dashboard + - -
-
-
-
-

Dashboard

-

Successfuly logged in and the login verified

-
-
-
+ +
+

Dashboard

+

Successfuly logged in and the login verified

- - - - - - - diff --git a/flask/templates/index.html b/flask/templates/index.html index 094c11c..aeb52fa 100644 --- a/flask/templates/index.html +++ b/flask/templates/index.html @@ -5,29 +5,51 @@ - - + Gear Orders Bot - Hello, world! + -
-
-
-
-

Telegram Login

-

A simple telegram login using flask. The instructions can be found -
mithun.co

- -
-
+ +
+

Gear Orders Bot

+ + {% with messages = get_flashed_messages() %} + {% if messages %} +
+ {% for message in messages %} +

{{ message }}

+ {% endfor %} +
+ {% endif %} + {% endwith %}
-
- - - - - - -