diff --git a/Cargo.lock b/Cargo.lock
index 3ec633a..6417100 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -101,6 +101,12 @@ dependencies = [
"winapi",
]
+[[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"
@@ -198,6 +204,17 @@ dependencies = [
"wyz",
]
+[[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.9.0"
@@ -313,6 +330,12 @@ version = "0.4.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f92cfa0fd5690b3cf8c1ef2cabbd9b7ef22fa53cf5e1f92b05103f6d5d1cf6e7"
+[[package]]
+name = "constant_time_eq"
+version = "0.1.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "245097e9a4535ee1e3e3931fcfcd55a796a44c643e8596ff6566d68f09b87bbc"
+
[[package]]
name = "core-foundation"
version = "0.9.1"
@@ -2025,6 +2048,18 @@ dependencies = [
"smallvec",
]
+[[package]]
+name = "rust-argon2"
+version = "0.8.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4b18820d944b33caa75a71378964ac46f58517c92b6ae5f762636247c09e78fb"
+dependencies = [
+ "base64",
+ "blake2b_simd",
+ "constant_time_eq",
+ "crossbeam-utils",
+]
+
[[package]]
name = "rustc_version"
version = "0.2.3"
@@ -2510,6 +2545,7 @@ dependencies = [
"phf",
"rand 0.8.3",
"refinery",
+ "rust-argon2",
"serde",
"sqlx",
"tempfile",
diff --git a/Cargo.toml b/Cargo.toml
index 7d5bacc..ff312b4 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -16,6 +16,7 @@ tracing-subscriber = "0.2"
toml = "0.5"
nom = "5"
rand = "0.8"
+rust-argon2 = "0.8"
thiserror = "1.0"
itertools = "0.10"
async-trait = "0.1"
diff --git a/src/commands/management.rs b/src/commands/management.rs
index f4396d2..aaf8dc5 100644
--- a/src/commands/management.rs
+++ b/src/commands/management.rs
@@ -1,6 +1,9 @@
use super::{Command, Execution, ExecutionResult};
use crate::context::Context;
-use crate::logic::record_room_information;
+use crate::db::Users;
+use crate::error::BotError::PasswordCreationError;
+use crate::logic::{hash_password, record_room_information};
+use crate::models::User;
use async_trait::async_trait;
use matrix_sdk::identifiers::UserId;
@@ -47,6 +50,13 @@ impl Command for RegisterCommand {
}
async fn execute(&self, ctx: &Context<'_>) -> ExecutionResult {
- Execution::success("User account registered".to_string())
+ let pw_hash = hash_password(&self.0).map_err(|e| PasswordCreationError(e))?;
+ let user = User {
+ username: ctx.username.to_owned(),
+ password: pw_hash,
+ };
+
+ ctx.db.upsert_user(&user).await?;
+ Execution::success("User account registered/updated".to_string())
}
}
diff --git a/src/db/mod.rs b/src/db/mod.rs
index c2a107e..6e916f1 100644
--- a/src/db/mod.rs
+++ b/src/db/mod.rs
@@ -1,3 +1,5 @@
+use crate::error::BotError;
+use crate::models::User;
use async_trait::async_trait;
use errors::DataError;
use std::collections::{HashMap, HashSet};
@@ -14,6 +16,19 @@ pub(crate) trait DbState {
async fn set_device_id(&self, device_id: &str) -> Result<(), DataError>;
}
+#[async_trait]
+pub(crate) trait Users {
+ async fn upsert_user(&self, user: &User) -> Result<(), DataError>;
+
+ async fn get_user(&self, username: &str) -> Result