diff --git a/projects/tentacles/src/python/tentacles/__main__.py b/projects/tentacles/src/python/tentacles/__main__.py index 0eaa5ef..6a69e0c 100644 --- a/projects/tentacles/src/python/tentacles/__main__.py +++ b/projects/tentacles/src/python/tentacles/__main__.py @@ -6,7 +6,7 @@ from pathlib import Path import click -from flask import Flask, request, session, current_app +from flask import Flask, request, session, current_app, Authorization import tomllib from tentacles.blueprints import ui, api diff --git a/projects/tentacles/src/python/tentacles/blueprints/ui.py b/projects/tentacles/src/python/tentacles/blueprints/ui.py index a73190a..25d078b 100644 --- a/projects/tentacles/src/python/tentacles/blueprints/ui.py +++ b/projects/tentacles/src/python/tentacles/blueprints/ui.py @@ -20,8 +20,8 @@ from flask import ( BLUEPRINT = Blueprint("ui", __name__) -def is_logged_in(authorization): - return False +def is_logged_in(request: Request) -> bool: + return request.uid is not None @BLUEPRINT.route("/") @@ -37,7 +37,7 @@ def root(): @BLUEPRINT.route("/login", methods=["GET", "POST"]) def login(): - if is_logged_in(request.authorization): + if is_logged_in(request): return redirect("/") elif request.method == "POST": @@ -61,12 +61,15 @@ def login(): @BLUEPRINT.route("/register", methods=["GET", "POST"]) def register(): - if is_logged_in(request.authorization): + if is_logged_in(request): return redirect("/") + elif request.method == "POST": try: if uid := current_app.db.try_create_user( - request.form["username"], request.form["password"] + request.form["username"], + request.form["email"], + request.form["password"], ): flash( "Please check your email for a verification request", diff --git a/projects/tentacles/src/python/tentacles/schema.sql b/projects/tentacles/src/python/tentacles/schema.sql index cc818aa..d077e9f 100644 --- a/projects/tentacles/src/python/tentacles/schema.sql +++ b/projects/tentacles/src/python/tentacles/schema.sql @@ -8,12 +8,26 @@ INSERT OR IGNORE INTO groups (id, name, priority) VALUES (0, 'root', 20); INSERT OR IGNORE INTO groups (id, name, priority) VALUES (0, 'users', 10); INSERT OR IGNORE INTO groups (id, name, priority) VALUES (0, 'guests', 0); +CREATE TABLE IF NOT EXISTS user_statuses ( + id INTEGER PRIMARY KEY AUTOINCREMENT + , name TEXT + , UNIQUE(name) +); + +INSERT OR IGNORE INTO user_statuses (id, name) values (-1, 'disabled'); +INSERT OR IGNORE INTO user_statuses (id, name) values (-2, 'unverified'); +INSERT OR IGNORE INTO user_statuses (id, name) values (1, 'enabled'); + CREATE TABLE IF NOT EXISTS users ( id INTEGER PRIMARY KEY AUTOINCREMENT , group_id INTEGER , name TEXT + , email TEXT , hash TEXT + , status_id INTEGER + , verification_id INTEGER , FOREIGN KEY(group_id) REFERENCES groups(id) + , FOREIGN KEY(status_id) REFERENCES user_statuses(id) , UNIQUE(name) ); diff --git a/projects/tentacles/src/python/tentacles/store.py b/projects/tentacles/src/python/tentacles/store.py index e95616d..17b0b41 100644 --- a/projects/tentacles/src/python/tentacles/store.py +++ b/projects/tentacles/src/python/tentacles/store.py @@ -73,14 +73,14 @@ class Store(object): @fmap(one) @requires_conn - def try_create_user(self, username, password): + def try_create_user(self, username, email, password): """Attempt to create a new user.""" digest = sha3_256() digest.update(password.encode("utf-8")) return self._conn.execute( - "INSERT INTO users (name, hash) VALUES (?, ?) RETURNING (id)", - [username, digest.hexdigest()], + "INSERT INTO users (name, email, hash) VALUES (?, ?, ?) RETURNING (id)", + [username, email, digest.hexdigest()], ).fetchone() @requires_conn diff --git a/projects/tentacles/src/python/tentacles/templates/register.html.j2 b/projects/tentacles/src/python/tentacles/templates/register.html.j2 index dd51e75..7652c71 100644 --- a/projects/tentacles/src/python/tentacles/templates/register.html.j2 +++ b/projects/tentacles/src/python/tentacles/templates/register.html.j2 @@ -3,7 +3,8 @@

Register

Username: +

Email address:

Password: -

+

{% endblock %}