diff --git a/.gitignore b/.gitignore index 26d6c24..d10cef4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,11 @@ # Sqlite database database.db +# Vim temporary files and swap +*~ +*.swp +*.swo + # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] diff --git a/main.py b/main.py index 7b93cce..2db1fd2 100755 --- a/main.py +++ b/main.py @@ -11,7 +11,7 @@ from flask_qrcode import QRcode # refresh the qr code and update the list of users # https://flask-socketio.readthedocs.io -app = Flask(__name__, static_folder='html') +app = Flask(__name__) FlaskUUID(app) QRcode(app) @@ -34,27 +34,29 @@ def show_qr_and_list(): @app.route('/') def catch_uuids(id): - ua = request.headers.get('User-Agent') - # TODO: use the Remote-User header that Authelia should set after authentication - # (check if "Authorization" in request.headers, or request.authorization) + ua = request.headers.get('Remote-User') + # TODO: Check directly with Authelia using https://auth.agofer.net/api/verify time = datetime.now().strftime("%A %Y-%m-%d %H:%M:%S") error = None + data = [] conn = get_db_connection() existing = conn.execute( 'SELECT * FROM hits WHERE uuid = ?', (str(id),)).fetchone() # TODO: verify that the uuid was generated by us (otherwise any uuid, # like one generated by the user, would be accepted) - if existing: + if not ua: + error = 'NO_USERNAME' + elif existing: error = 'ALREADY_USED' else: + conn.execute("INSERT INTO hits (uuid, user) VALUES (?, ?)", (str(id), ua)) + conn.commit() data = conn.execute( 'SELECT * FROM hits WHERE user = ? ORDER BY id DESC LIMIT 10', (ua,) ).fetchall() - conn.execute("INSERT INTO hits (uuid, user) VALUES (?, ?)", (str(id), ua)) - conn.commit() conn.close() return render_template('thanks.html', user=ua, time=time, error=error, hits=data) if __name__ == '__main__': - app.run() + app.run(host='0.0.0.0') diff --git a/templates/thanks.html b/templates/thanks.html index d7edfbf..1e7dad3 100644 --- a/templates/thanks.html +++ b/templates/thanks.html @@ -4,7 +4,10 @@ Example - {% if error %} + {% if error == 'NO_USERNAME' %} +

Error

+

No username received. Login here.

+ {% elif error == 'ALREADY_USED' %}

Error

Code has been used already.

{% else %}