diff --git a/app/auth.py b/app/auth.py index 343cfec..e7b06b1 100644 --- a/app/auth.py +++ b/app/auth.py @@ -1,11 +1,12 @@ import sqlite3 +from hashlib import sha256 def init_db(path: str) -> None: with sqlite3.connect(path) as connection: cursor = connection.cursor() cursor.execute(''' CREATE TABLE IF NOT EXISTS Tokens ( - TOKEN TEXT PRIMARY KEY NOT NULL, + TKHASH TEXT PRIMARY KEY NOT NULL, USERID INTEGER NOT NULL, NAME TEXT NOT NULL ) @@ -18,17 +19,55 @@ def init_db(path: str) -> None: ) ''') + cursor.execute(''' + CREATE TABLE IF NOT EXISTS Users ( + USERID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, + USERNAME TEXT NOT NULL UNIQUE, + PWHASH TEXT NOT NULL, + PWSALT TEXT NOT NULL, + EMAIL TEXT + IS_ADMIN INTEGER NOT NULL DEFAULT 0 + ) + ''') + connection.commit() cursor.close() def check_token_domain(db_path: str, token: str, domain: str) -> bool: with sqlite3.connect(db_path) as connection: cursor = connection.cursor() + + token_hash = sha256(token.encode('utf-8')).hexdigest() + cursor.execute(''' SELECT COUNT(*) FROM Tokens JOIN Domains ON Tokens.userid = Domains.userid - WHERE token = ? + WHERE tkhash = ? AND domain = ? - ''', (token,domain)) + ''', (token_hash, domain)) return cursor.fetchone()[0] == 1 + +def user_login(db_path: str, username: str, password: str) -> bool: + with sqlite3.connect(db_path) as connection: + cursor = connection.cursor() + + cursor.execute(''' + SELECT pwsalt FROM Users + WHERE username = ? + ''', (username,)) + + password_salt = cursor.fetchone() + if password_salt is None: + return False + password_salt = password_salt[0] + + password_hash = sha256( (password_salt + password).encode('utf-8') ).hexdigest() + + cursor.execute(''' + SELECT COUNT(*) FROM Users + WHERE username = ? + AND pwhash = ? + ''', (username, password_hash)) + + return cursor.fetchone()[0] == 1 diff --git a/app/main.py b/app/main.py index f54200d..e446192 100644 --- a/app/main.py +++ b/app/main.py @@ -1,4 +1,4 @@ -from flask import Flask, jsonify, request +from flask import Flask, jsonify, request, render_template, redirect import auth import re @@ -8,6 +8,7 @@ HOSTS_PATH = "/app/hosts.d/izbi" DB_PATH = "/app/sqlite.db" + app = Flask(__name__) hosts = {} @@ -36,6 +37,27 @@ def validate_ip(ip: str) -> bool: @app.route('/', methods=["GET"]) +def homepage(): + return render_template("index.html") + + + +@app.route('/login', methods=["POST"]) +def login(): + if "user" not in request.form or "pass" not in request.form: + return redirect("/"), 400 + + if request.form["user"].strip() == "" or request.form["pass"].strip() == "": + return redirect("/"), 400 + + if not auth.user_login(DB_PATH, request.form["user"], request.form["pass"]): + return redirect("/") + + return redirect("/dashboard", code=302) + + + +@app.route('/health', methods=["GET"]) def health(): return jsonify({"status": "healthy", "comment": ""}) diff --git a/app/templates/index.html b/app/templates/index.html new file mode 100644 index 0000000..0cc30fe --- /dev/null +++ b/app/templates/index.html @@ -0,0 +1,15 @@ + + + + IZBI DNS + + +

Dynamic DNS service for .izbi domains :3

+ +
+
+
+ +
+ +