Use empty struct for Authenticated

This commit is contained in:
notplants 2021-11-08 17:01:38 +01:00
parent 6605af7bed
commit 0d23307f86
3 changed files with 8 additions and 10 deletions

View File

@ -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";

View File

@ -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()

View File

@ -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)