diff options
Diffstat (limited to 'logic')
-rw-r--r-- | logic/src/lib.rs | 30 | ||||
-rw-r--r-- | logic/src/login.rs | 18 | ||||
-rw-r--r-- | logic/src/session.rs | 19 |
3 files changed, 57 insertions, 10 deletions
diff --git a/logic/src/lib.rs b/logic/src/lib.rs index 54c9c40..64656f5 100644 --- a/logic/src/lib.rs +++ b/logic/src/lib.rs @@ -3,14 +3,36 @@ which is licensed under the GNU Affero General Public License (version 3); see /COPYING. Copyright (C) 2025 metamuffin <metamuffin.org> */ -#![feature(duration_constructors)] +#![feature(duration_constructors, let_chains)] +pub mod admin; pub mod filter_sort; pub mod home; +pub mod items; +pub mod login; pub mod node; pub mod search; pub mod session; pub mod stats; -pub mod items; -pub mod admin; -pub mod login; + +use serde::{Deserialize, Serialize}; +use std::sync::LazyLock; +use std::sync::Mutex; + +#[rustfmt::skip] +#[derive(Debug, Deserialize, Serialize, Default)] +pub struct Config { + login_expire: i64, + session_key: Option<String>, + admin_username:Option<String>, + admin_password:Option<String>, +} + +pub static CONF_PRELOAD: Mutex<Option<Config>> = Mutex::new(None); +static CONF: LazyLock<Config> = LazyLock::new(|| { + CONF_PRELOAD + .lock() + .unwrap() + .take() + .expect("logic config not preloaded. logic error") +}); diff --git a/logic/src/login.rs b/logic/src/login.rs index e9c2f93..26a6b7f 100644 --- a/logic/src/login.rs +++ b/logic/src/login.rs @@ -3,13 +3,27 @@ which is licensed under the GNU Affero General Public License (version 3); see /COPYING. Copyright (C) 2025 metamuffin <metamuffin.org> */ -use crate::session::create; +use crate::{CONF, session::create}; use anyhow::{Result, anyhow}; use argon2::{Argon2, PasswordHasher, password_hash::Salt}; -use jellybase::{CONF, database::Database}; +use jellybase::database::Database; use jellycommon::user::UserPermission; +use log::info; use std::{collections::HashSet, time::Duration}; +pub fn create_admin_account(database: &Database) -> Result<()> { + if let Some(username) = &CONF.admin_username + && let Some(password) = &CONF.admin_password + { + database + .create_admin_user(username, hash_password(username, password)) + .unwrap(); + } else { + info!("admin account disabled") + } + Ok(()) +} + pub fn login_logic( database: &Database, username: &str, diff --git a/logic/src/session.rs b/logic/src/session.rs index bc7f137..72a1089 100644 --- a/logic/src/session.rs +++ b/logic/src/session.rs @@ -3,13 +3,13 @@ which is licensed under the GNU Affero General Public License (version 3); see /COPYING. Copyright (C) 2025 metamuffin <metamuffin.org> */ +use crate::CONF; use aes_gcm_siv::{ KeyInit, aead::{Aead, generic_array::GenericArray}, }; use anyhow::anyhow; use base64::Engine; -use jellybase::SECRETS; use jellycommon::{ chrono::{DateTime, Utc}, user::{PermissionSet, User}, @@ -32,7 +32,7 @@ pub struct SessionData { } static SESSION_KEY: LazyLock<[u8; 32]> = LazyLock::new(|| { - if let Some(sk) = &SECRETS.session_key { + if let Some(sk) = &CONF.session_key { let r = base64::engine::general_purpose::STANDARD .decode(sk) .expect("key invalid; should be valid base64"); @@ -85,9 +85,20 @@ pub fn validate(token: &str) -> anyhow::Result<String> { Ok(session_data.username) } +#[cfg(test)] +fn load_test_config() { + use crate::{CONF_PRELOAD, Config}; + *CONF_PRELOAD.lock().unwrap() = Some(Config { + login_expire: 10, + session_key: None, + admin_password: None, + admin_username: None, + }); +} + #[test] fn test() { - jellybase::use_test_config(); + load_test_config(); let tok = create( "blub".to_string(), jellycommon::user::PermissionSet::default(), @@ -98,7 +109,7 @@ fn test() { #[test] fn test_crypto() { - jellybase::use_test_config(); + load_test_config(); let nonce = [(); 12].map(|_| rand::random()); let cipher = aes_gcm_siv::Aes256GcmSiv::new_from_slice(&*SESSION_KEY).unwrap(); let plaintext = b"testing stuff---"; |