Refactor and clean up code across multiple modules
- Simplified token type assignment in OAuth service. - Removed unused session_lock module and re-exported Session from zesdex_entities. - Cleaned up session entity by removing unnecessary comments and code. - Consolidated session handling in HTTP handlers for better readability. - Improved formatting and readability in OAuth repository tests. - Enhanced session lock repository with clearer match statements. - Streamlined session repository error handling. - Refined RNG tests for better clarity. - Adjusted module visibility and organization in lib.rs. - Updated IPC client and connection code for better error handling and clarity. - Improved frame handling in IPC for better readability. - Organized module imports and added test utilities for IPC. - Enhanced database connection error handling. - Simplified JWT token creation error handling. - Improved password verification error handling. - Cleaned up state management code for better readability. - Refactored middleware for session authentication and rate limiting. - Simplified clipboard utility for better error handling. - Enhanced logging initialization for better error reporting. - Improved pagination utility with clearer method annotations. - Cleaned up sanitization functions for filenames and paths. - Enhanced slug generation functions for better clarity and usability.
This commit is contained in:
@@ -28,7 +28,10 @@ impl DbConn {
|
||||
where
|
||||
F: FnOnce(&rusqlite::Connection) -> Result<T>,
|
||||
{
|
||||
let conn = self.conn.lock().map_err(|e| anyhow::anyhow!("db lock poisoned: {e}"))?;
|
||||
let conn = self
|
||||
.conn
|
||||
.lock()
|
||||
.map_err(|e| anyhow::anyhow!("db lock poisoned: {e}"))?;
|
||||
f(&conn)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,8 +58,7 @@ impl JwtClaims {
|
||||
pub fn create_token(secret: &str, claims: JwtClaims) -> Result<String> {
|
||||
let header = jsonwebtoken::Header::new(jsonwebtoken::Algorithm::HS256);
|
||||
let key = jsonwebtoken::EncodingKey::from_secret(secret.as_bytes());
|
||||
jsonwebtoken::encode(&header, &claims, &key)
|
||||
.context("failed to encode JWT")
|
||||
jsonwebtoken::encode(&header, &claims, &key).context("failed to encode JWT")
|
||||
}
|
||||
|
||||
/// Verify a JWT string and return its claims.
|
||||
|
||||
@@ -45,8 +45,8 @@ pub fn hash_password(password: &str) -> Result<String> {
|
||||
/// Returns an error if the hash string is not a valid PHC string or if
|
||||
/// the argon2 library encounters an internal failure.
|
||||
pub fn verify_password(password: &str, hash: &str) -> Result<bool> {
|
||||
let parsed_hash =
|
||||
PasswordHash::new(hash).map_err(|e| anyhow::anyhow!("failed to parse password hash: {e}"))?;
|
||||
let parsed_hash = PasswordHash::new(hash)
|
||||
.map_err(|e| anyhow::anyhow!("failed to parse password hash: {e}"))?;
|
||||
let argon2 = Argon2::default();
|
||||
Ok(argon2
|
||||
.verify_password(password.as_bytes(), &parsed_hash)
|
||||
|
||||
@@ -23,15 +23,15 @@ use uuid::Uuid;
|
||||
use zesdex_cms::domain::app_config::ProviderConfig;
|
||||
use zesdex_cms::domain::conversation::Conversation;
|
||||
use zesdex_cms::domain::memory::Memory;
|
||||
use zesdex_cms::domain::repository::{
|
||||
AppConfigRepository, ConversationRepository, MemoryRepository, SettingsRepository,
|
||||
};
|
||||
use zesdex_cms::domain::settings::Settings;
|
||||
use zesdex_cms::infrastructure::persistence::{
|
||||
JsonAppConfigRepository, JsonConversationRepository, JsonSettingsRepository,
|
||||
MarkdownMemoryRepository,
|
||||
};
|
||||
use zesdex_entities::seaorm::common::store::Store;
|
||||
use zesdex_cms::domain::repository::{
|
||||
AppConfigRepository, ConversationRepository, MemoryRepository, SettingsRepository,
|
||||
};
|
||||
use zesdex_iam::domain::repository::SessionRepository;
|
||||
use zesdex_iam::domain::session::Session;
|
||||
use zesdex_iam::infrastructure::persistence::session_repo::FileSystemSessionRepository;
|
||||
@@ -213,14 +213,12 @@ impl CmsServiceProvider for DefaultCmsServiceProvider {
|
||||
|
||||
fn save_conversation(&self, conv: &Conversation) -> Result<()> {
|
||||
let dir = self.session_dir(&conv.session_id);
|
||||
self.conversation_repo
|
||||
.save(&dir, conv)
|
||||
.with_context(|| {
|
||||
format!(
|
||||
"failed to save conversation for session '{}'",
|
||||
conv.session_id
|
||||
)
|
||||
})
|
||||
self.conversation_repo.save(&dir, conv).with_context(|| {
|
||||
format!(
|
||||
"failed to save conversation for session '{}'",
|
||||
conv.session_id
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
// -- Memories --
|
||||
@@ -299,17 +297,14 @@ pub fn initialize_app_context() -> Result<AppContext> {
|
||||
.to_str()
|
||||
.ok_or_else(|| anyhow::anyhow!("invalid db path: {}", db_path.display()))?;
|
||||
|
||||
let db = database::init_db(db_path_str)
|
||||
.context("failed to initialise database")?;
|
||||
let db = database::init_db(db_path_str).context("failed to initialise database")?;
|
||||
|
||||
database::run_migrations(&db)
|
||||
.context("failed to run database migrations")?;
|
||||
database::run_migrations(&db).context("failed to run database migrations")?;
|
||||
|
||||
// -- Services --
|
||||
let iam_service: Box<dyn IamServiceProvider> =
|
||||
Box::new(DefaultIamServiceProvider::new(store.base_dir.clone()));
|
||||
let cms_service: Box<dyn CmsServiceProvider> =
|
||||
Box::new(DefaultCmsServiceProvider::new(&store));
|
||||
let cms_service: Box<dyn CmsServiceProvider> = Box::new(DefaultCmsServiceProvider::new(&store));
|
||||
|
||||
// -- JWT secret --
|
||||
let jwt_secret = std::env::var("ZESDEX_JWT_SECRET")
|
||||
|
||||
Reference in New Issue
Block a user