From 0d23307f861f4b5052dd9a5a6098e81d8734f1dc Mon Sep 17 00:00:00 2001 From: notplants Date: Mon, 8 Nov 2021 17:01:38 +0100 Subject: [PATCH] Use empty struct for Authenticated --- peach-lib/src/config_manager.rs | 3 +-- peach-lib/src/password_utils.rs | 5 +---- peach-web/src/routes/authentication.rs | 10 ++++++---- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/peach-lib/src/config_manager.rs b/peach-lib/src/config_manager.rs index 5940706..2581408 100644 --- a/peach-lib/src/config_manager.rs +++ b/peach-lib/src/config_manager.rs @@ -4,13 +4,12 @@ //! //! The configuration file is located at: "/var/lib/peachcloud/config.yml" -use fslock::{LockFile, IntoOsString}; +use fslock::{LockFile}; use serde::{Deserialize, Serialize}; use std::fs; use crate::error::PeachError; use crate::error::*; -use crate::password_utils::hash_password; // main configuration file pub const YAML_PATH: &str = "/var/lib/peachcloud/config.yml"; diff --git a/peach-lib/src/password_utils.rs b/peach-lib/src/password_utils.rs index 8612329..28e5e18 100644 --- a/peach-lib/src/password_utils.rs +++ b/peach-lib/src/password_utils.rs @@ -2,14 +2,11 @@ use crate::config_manager::{get_peachcloud_domain, load_peach_config, set_admin_password_hash, get_admin_password_hash, get_temporary_password_hash, set_temporary_password_hash}; use crate::error::PeachError; -use crate::error::StdIoError; use crate::sbot_client; use log::info; use rand::distributions::Alphanumeric; use rand::{thread_rng, Rng}; -use snafu::ResultExt; use std::iter; -use std::process::Command; use crypto::digest::Digest; use crypto::sha3::Sha3; @@ -52,7 +49,7 @@ pub fn set_new_password(new_password: &str) -> Result<(), PeachError> { } /// Creates a hash from a password string -pub fn hash_password(password: &String) -> String { +pub fn hash_password(password: &str) -> String { let mut hasher = Sha3::sha3_256(); hasher.input_str(password); hasher.result_str() diff --git a/peach-web/src/routes/authentication.rs b/peach-web/src/routes/authentication.rs index 3e0670a..18639d9 100644 --- a/peach-web/src/routes/authentication.rs +++ b/peach-web/src/routes/authentication.rs @@ -24,9 +24,11 @@ use rocket::http::{Cookie, CookieJar, Status}; pub const AUTH_COOKIE_KEY: &str = "peachweb_auth"; pub const ADMIN_USERNAME: &str = "admin"; -pub struct Authenticated { - is_authenticated: bool, -} +/// Note: Currently we use an empty struct for the Authenticated request guard +/// because there is only one user to be authenticated, and no data needs to be stored here. +/// In a multi-user authentication scheme, we would store the user_id in this struct, +/// and retrieve the correct user via the user_id stored in the cookie. +pub struct Authenticated; #[derive(Debug)] pub enum LoginError { @@ -47,7 +49,7 @@ impl<'r> FromRequest<'r> for Authenticated { .cookies() .get_private(AUTH_COOKIE_KEY) .and_then(|cookie| cookie.value().parse().ok()) - .map(|_value: String| { Authenticated { is_authenticated: true } }); + .map(|_value: String| { Authenticated { } }); match authenticated { Some(auth) => { request::Outcome::Success(auth)