diff --git a/.gitignore b/.gitignore index 04e6ea2..da1c839 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ .env *~ db.sqlite3 + +Caddyfile diff --git a/Pipfile b/Pipfile index f379dae..10a6309 100644 --- a/Pipfile +++ b/Pipfile @@ -10,6 +10,8 @@ scheduler = "*" pytz = "*" peewee = "*" peewee-migrate = "*" +flask = "*" +flask-login = "*" [dev-packages] diff --git a/flask/app.py b/flask/app.py new file mode 100644 index 0000000..43081b2 --- /dev/null +++ b/flask/app.py @@ -0,0 +1,62 @@ +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 +import hashlib +import hmac +import base64 + +from settings import FLASK_SECRET_KEY, TELEGRAM_API_TOKEN, TELEGRAM_BOT_NAME, TELEGRAM_BOT_DOMAIN + +app = Flask(__name__) + +app.secret_key = FLASK_SECRET_KEY + +@app.route('/') +def index(): + data = {'bot_name': TELEGRAM_BOT_NAME, 'bot_damin': TELEGRAM_BOT_DOMAIN} + return render_template('index.html', data = data) + +@app.route('/dashboard') +def dashboard(): + return render_template('dashboard.html') + +def string_generator(data_incoming): + data = data_incoming.copy() + del data['hash'] + keys = sorted(data.keys()) + string_arr = [] + for key in keys: + string_arr.append(key + '=' + data[key]) + string_cat = '\n'.join(string_arr) + return string_cat + +@app.route('/login') +def login(): + tg_data = { + "id": request.args.get("id", None), + "first_name": request.args.get('first_name', None), + "last_name": request.args.get('last_name', None), + "username": request.args.get("username", None), + "photo_url": request.args.get("photo_url", None), + "auth_date": request.args.get('auth_date', None), + "hash": request.args.get("hash", None) + } + data_check_string = string_generator(tg_data) + secret_key = hashlib.sha256(TELEGRAM_API_TOKEN.encode('utf-8')).digest() + secret_key_bytes = secret_key + 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 + }) + +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 new file mode 100644 index 0000000..8466013 --- /dev/null +++ b/flask/templates/dashboard.html @@ -0,0 +1,34 @@ + + + + + + + + + + + Dashboard + + + +
+
+
+
+

Dashboard

+

Successfuly logged in and the login verified

+
+
+
+
+ + + + + + + + + + diff --git a/flask/templates/index.html b/flask/templates/index.html new file mode 100644 index 0000000..094c11c --- /dev/null +++ b/flask/templates/index.html @@ -0,0 +1,33 @@ + + + + + + + + + + + Hello, world! + +
+
+
+
+

Telegram Login

+

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

+ +
+
+
+
+ + + + + + + + + diff --git a/settings.py b/settings.py index ed343d6..d1570a5 100644 --- a/settings.py +++ b/settings.py @@ -16,6 +16,8 @@ MASTODON_VISIBILITY = os.environ.get('MASTODON_VISIBILITY', 'direct') TELEGRAM_API_TOKEN = os.environ.get('TELEGRAM_API_TOKEN') TELEGRAM_CHAT_ID = int(os.environ.get('TELEGRAM_CHAT_ID')) TELEGRAM_COMMAND_TIMEOUT = int(os.environ.get('TELEGRAM_COMMAND_TIMEOUT', 120)) +TELEGRAM_BOT_NAME = os.environ.get('TELEGRAM_BOT_NAME') +TELEGRAM_BOT_DOMAIN = os.environ.get('TELEGRAM_BOT_DOMAIN') SQLITE_DB = os.environ.get('SQLITE_DB', 'db.sqlite3') @@ -24,3 +26,4 @@ ORDERS_YML = os.environ.get('ORDERS_YML', 'orders.yml') TIMEZONE = os.environ.get('TIMEZONE', 'America/Chicago') FLASK_URL = os.environ.get("FLASK_URL") +FLASK_SECRET_KEY = os.environ.get("FLASK_SECRET_KEY")