[NO TESTS] WIP

This commit is contained in:
Reid 'arrdem' McKenzie 2023-05-21 23:16:53 -06:00
parent ae6044c76e
commit 4413df7132
5 changed files with 28 additions and 10 deletions

View file

@ -6,7 +6,7 @@
from pathlib import Path from pathlib import Path
import click import click
from flask import Flask, request, session, current_app from flask import Flask, request, session, current_app, Authorization
import tomllib import tomllib
from tentacles.blueprints import ui, api from tentacles.blueprints import ui, api

View file

@ -20,8 +20,8 @@ from flask import (
BLUEPRINT = Blueprint("ui", __name__) BLUEPRINT = Blueprint("ui", __name__)
def is_logged_in(authorization): def is_logged_in(request: Request) -> bool:
return False return request.uid is not None
@BLUEPRINT.route("/") @BLUEPRINT.route("/")
@ -37,7 +37,7 @@ def root():
@BLUEPRINT.route("/login", methods=["GET", "POST"]) @BLUEPRINT.route("/login", methods=["GET", "POST"])
def login(): def login():
if is_logged_in(request.authorization): if is_logged_in(request):
return redirect("/") return redirect("/")
elif request.method == "POST": elif request.method == "POST":
@ -61,12 +61,15 @@ def login():
@BLUEPRINT.route("/register", methods=["GET", "POST"]) @BLUEPRINT.route("/register", methods=["GET", "POST"])
def register(): def register():
if is_logged_in(request.authorization): if is_logged_in(request):
return redirect("/") return redirect("/")
elif request.method == "POST": elif request.method == "POST":
try: try:
if uid := current_app.db.try_create_user( if uid := current_app.db.try_create_user(
request.form["username"], request.form["password"] request.form["username"],
request.form["email"],
request.form["password"],
): ):
flash( flash(
"Please check your email for a verification request", "Please check your email for a verification request",

View file

@ -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, 'users', 10);
INSERT OR IGNORE INTO groups (id, name, priority) VALUES (0, 'guests', 0); 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 ( CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT id INTEGER PRIMARY KEY AUTOINCREMENT
, group_id INTEGER , group_id INTEGER
, name TEXT , name TEXT
, email TEXT
, hash TEXT , hash TEXT
, status_id INTEGER
, verification_id INTEGER
, FOREIGN KEY(group_id) REFERENCES groups(id) , FOREIGN KEY(group_id) REFERENCES groups(id)
, FOREIGN KEY(status_id) REFERENCES user_statuses(id)
, UNIQUE(name) , UNIQUE(name)
); );

View file

@ -73,14 +73,14 @@ class Store(object):
@fmap(one) @fmap(one)
@requires_conn @requires_conn
def try_create_user(self, username, password): def try_create_user(self, username, email, password):
"""Attempt to create a new user.""" """Attempt to create a new user."""
digest = sha3_256() digest = sha3_256()
digest.update(password.encode("utf-8")) digest.update(password.encode("utf-8"))
return self._conn.execute( return self._conn.execute(
"INSERT INTO users (name, hash) VALUES (?, ?) RETURNING (id)", "INSERT INTO users (name, email, hash) VALUES (?, ?, ?) RETURNING (id)",
[username, digest.hexdigest()], [username, email, digest.hexdigest()],
).fetchone() ).fetchone()
@requires_conn @requires_conn

View file

@ -3,7 +3,8 @@
<h1>Register</h1> <h1>Register</h1>
<form method="post"> <form method="post">
<p>Username: <input type="text" name="username"> <p>Username: <input type="text" name="username">
<p>Email address: <input type="text" name="email">
<p>Password: <input type="password" name="password"> <p>Password: <input type="password" name="password">
<p><input type="submit" value=Login> <p><input type="submit" value=Register>
</form> </form>
{% endblock %} {% endblock %}