From 10d5f30e5a66224a6ac5dfe04b6651dff6f40be2 Mon Sep 17 00:00:00 2001
From: projectmoon
Date: Sat, 5 Dec 2020 13:07:33 +0000
Subject: [PATCH] User accounts, DAO trait, registration.
Not very thorough, but it does work.
---
Cargo.lock | 61 +++++++++++
Cargo.toml | 3 +
migrations/2020-12-04-202734_users/down.sql | 1 +
migrations/2020-12-04-202734_users/up.sql | 5 +
src/db.rs | 95 +++++++++++------
src/errors.rs | 24 +++++
src/models.rs | 44 +-------
src/models/users.rs | 56 ++++++++++
src/routes/auth.rs | 109 ++++++++++++++++++--
src/routes/characters.rs | 28 ++---
src/routes/root.rs | 9 +-
src/schema.rs | 13 +++
templates/index.html.tera | 6 ++
templates/login.html.tera | 2 +-
templates/registration.html.tera | 25 +++++
15 files changed, 377 insertions(+), 104 deletions(-)
create mode 100644 migrations/2020-12-04-202734_users/down.sql
create mode 100644 migrations/2020-12-04-202734_users/up.sql
create mode 100644 src/models/users.rs
create mode 100644 templates/registration.html.tera
diff --git a/Cargo.lock b/Cargo.lock
index b0cdb30..dc3b214 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -79,6 +79,18 @@ dependencies = [
"memchr",
]
+[[package]]
+name = "arrayref"
+version = "0.3.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a4c527152e37cf757a3f78aae5a06fbeefdb07ccc535c980a3208ee3060dd544"
+
+[[package]]
+name = "arrayvec"
+version = "0.5.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "23b62fc65de8e4e7f52534fb52b0f3ed04746ae267519eef2a83941e8085068b"
+
[[package]]
name = "atty"
version = "0.2.14"
@@ -126,12 +138,29 @@ version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3441f0f7b02788e948e47f457ca01f1d7e6d92c693bc132c22b087d3141c03ff"
+[[package]]
+name = "base64"
+version = "0.13.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "904dfeac50f3cdaba28fc6f57fdcddb75f49ed61346676a78c4ffe55877802fd"
+
[[package]]
name = "bitflags"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
+[[package]]
+name = "blake2b_simd"
+version = "0.5.11"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "afa748e348ad3be8263be728124b24a24f268266f6f5d58af9d75f6a40b5c587"
+dependencies = [
+ "arrayref",
+ "arrayvec",
+ "constant_time_eq",
+]
+
[[package]]
name = "block-buffer"
version = "0.7.3"
@@ -208,6 +237,12 @@ dependencies = [
"bitflags",
]
+[[package]]
+name = "constant_time_eq"
+version = "0.1.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "245097e9a4535ee1e3e3931fcfcd55a796a44c643e8596ff6566d68f09b87bbc"
+
[[package]]
name = "cookie"
version = "0.11.3"
@@ -224,6 +259,17 @@ dependencies = [
"time",
]
+[[package]]
+name = "crossbeam-utils"
+version = "0.8.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "02d96d1e189ef58269ebe5b97953da3274d83a93af647c2ddd6f9dab28cedb8d"
+dependencies = [
+ "autocfg",
+ "cfg-if 1.0.0",
+ "lazy_static",
+]
+
[[package]]
name = "crypto-mac"
version = "0.7.0"
@@ -1086,6 +1132,18 @@ dependencies = [
"unicode-xid 0.1.0",
]
+[[package]]
+name = "rust-argon2"
+version = "0.8.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4b18820d944b33caa75a71378964ac46f58517c92b6ae5f762636247c09e78fb"
+dependencies = [
+ "base64 0.13.0",
+ "blake2b_simd",
+ "constant_time_eq",
+ "crossbeam-utils",
+]
+
[[package]]
name = "rustc-demangle"
version = "0.1.18"
@@ -1246,8 +1304,11 @@ name = "tenebrous-sheets"
version = "0.1.0"
dependencies = [
"diesel",
+ "log 0.4.11",
+ "rand",
"rocket",
"rocket_contrib",
+ "rust-argon2",
"serde",
"serde_derive",
"serde_json",
diff --git a/Cargo.toml b/Cargo.toml
index 5153d3e..e5b30ad 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -12,6 +12,9 @@ serde_derive = "1.0"
serde_json = "1.0"
diesel = "1.4"
thiserror = "1.0"
+rust-argon2 = "0.8"
+log = "0.4"
+rand = "0.7"
rocket = { version= "0.4.6", features = ["private-cookies"] }
[dependencies.rocket_contrib]
diff --git a/migrations/2020-12-04-202734_users/down.sql b/migrations/2020-12-04-202734_users/down.sql
new file mode 100644
index 0000000..cc1f647
--- /dev/null
+++ b/migrations/2020-12-04-202734_users/down.sql
@@ -0,0 +1 @@
+DROP TABLE users;
diff --git a/migrations/2020-12-04-202734_users/up.sql b/migrations/2020-12-04-202734_users/up.sql
new file mode 100644
index 0000000..109d18a
--- /dev/null
+++ b/migrations/2020-12-04-202734_users/up.sql
@@ -0,0 +1,5 @@
+CREATE TABLE users (
+ id INTEGER NOT NULL PRIMARY KEY,
+ username TEXT NOT NULL,
+ password TEXT NOT NULL
+);
diff --git a/src/db.rs b/src/db.rs
index 8dc4678..eeb7489 100644
--- a/src/db.rs
+++ b/src/db.rs
@@ -1,39 +1,74 @@
use crate::models::characters::{CharacterEntry, NewCharacter};
+use crate::models::users::{NewUser, User};
use diesel::prelude::*;
+use diesel::SqliteConnection;
#[database("tenebrous_db")]
-pub(crate) struct TenebrousDbConn(diesel::SqliteConnection);
+pub(crate) struct TenebrousDbConn(SqliteConnection);
-pub(crate) fn load_character_list(
- conn: TenebrousDbConn,
- for_user_id: i32,
-) -> QueryResult> {
- use crate::schema::characters::dsl::*;
- characters.filter(user_id.eq(for_user_id)).load(&*conn)
+pub(crate) trait Dao {
+ fn load_user_by_id(&self, id: i32) -> QueryResult
{% endblock content %}
diff --git a/templates/login.html.tera b/templates/login.html.tera
index b7653d3..ca2f2fb 100644
--- a/templates/login.html.tera
+++ b/templates/login.html.tera
@@ -15,7 +15,7 @@
-
+
{% endblock content %}
diff --git a/templates/registration.html.tera b/templates/registration.html.tera
new file mode 100644
index 0000000..d55400e
--- /dev/null
+++ b/templates/registration.html.tera
@@ -0,0 +1,25 @@
+{% extends "base" %}
+
+{% block content %}
+
+
Registration
+
+ {% if flash %}
+
Error: {{ flash }}
+ {% endif %}
+
+
Please register with a username and password.
+
+
+{% endblock content %}