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:
@@ -91,9 +91,10 @@ impl SessionAuthLayer {
|
||||
store: Arc::new(store),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Convenience constructor using `Store::new()`.
|
||||
pub fn default() -> Self {
|
||||
impl Default for SessionAuthLayer {
|
||||
fn default() -> Self {
|
||||
Self::new(Store::new())
|
||||
}
|
||||
}
|
||||
@@ -148,8 +149,8 @@ where
|
||||
let session_id = match session_id {
|
||||
Some(id) if !id.is_empty() => id,
|
||||
_ => {
|
||||
let resp = (StatusCode::UNAUTHORIZED, "missing X-Session-Id header")
|
||||
.into_response();
|
||||
let resp =
|
||||
(StatusCode::UNAUTHORIZED, "missing X-Session-Id header").into_response();
|
||||
return Box::pin(async move { Ok(resp) });
|
||||
}
|
||||
};
|
||||
@@ -173,7 +174,7 @@ where
|
||||
req.extensions_mut().insert(identity);
|
||||
|
||||
let fut = self.inner.call(req);
|
||||
Box::pin(async move { fut.await })
|
||||
Box::pin(fut)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -205,7 +206,11 @@ pub async fn require_session(
|
||||
};
|
||||
|
||||
if let Err(e) = validate_session(&session_id, &store) {
|
||||
return (StatusCode::UNAUTHORIZED, format!("session validation failed: {e}")).into_response();
|
||||
return (
|
||||
StatusCode::UNAUTHORIZED,
|
||||
format!("session validation failed: {e}"),
|
||||
)
|
||||
.into_response();
|
||||
}
|
||||
let user_agent = req
|
||||
.headers()
|
||||
|
||||
@@ -1,9 +1,3 @@
|
||||
#![allow(
|
||||
clippy::cast_possible_truncation,
|
||||
clippy::cast_sign_loss,
|
||||
clippy::cast_precision_loss,
|
||||
clippy::cast_possible_wrap
|
||||
)]
|
||||
//! Simple in-memory rate limiter for Axum.
|
||||
//!
|
||||
//! Uses a sliding-window approach: each client has a rolling list of
|
||||
@@ -80,7 +74,9 @@ impl RateLimiter {
|
||||
.lock()
|
||||
.map_err(|e| anyhow::anyhow!("rate limiter lock poisoned: {e}"))?;
|
||||
|
||||
let timestamps = windows.entry(client_id.to_string()).or_insert_with(Vec::new);
|
||||
let timestamps = windows
|
||||
.entry(client_id.to_string())
|
||||
.or_insert_with(Vec::new);
|
||||
|
||||
// Discard entries older than the window.
|
||||
timestamps.retain(|&ts| ts >= cutoff);
|
||||
@@ -100,15 +96,19 @@ impl RateLimiter {
|
||||
client_id: &str,
|
||||
max_requests: u32,
|
||||
window_secs: u64,
|
||||
) -> Result<(), Response> {
|
||||
) -> Result<(), Box<Response>> {
|
||||
match self.check_rate_limit(client_id, max_requests, window_secs) {
|
||||
Ok(true) => Ok(()),
|
||||
Ok(false) => Err((
|
||||
StatusCode::TOO_MANY_REQUESTS,
|
||||
"rate limit exceeded, try again later",
|
||||
)
|
||||
.into_response()),
|
||||
Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string()).into_response()),
|
||||
Ok(false) => Err(Box::new(
|
||||
(
|
||||
StatusCode::TOO_MANY_REQUESTS,
|
||||
"rate limit exceeded, try again later",
|
||||
)
|
||||
.into_response(),
|
||||
)),
|
||||
Err(e) => Err(Box::new(
|
||||
(StatusCode::INTERNAL_SERVER_ERROR, e.to_string()).into_response(),
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -164,9 +164,7 @@ impl RateLimitLayer {
|
||||
trust_proxy_headers: bool,
|
||||
) -> Self {
|
||||
Self {
|
||||
limiter: std::sync::Arc::new(RateLimiter::with_proxy_trust(
|
||||
trust_proxy_headers,
|
||||
)),
|
||||
limiter: std::sync::Arc::new(RateLimiter::with_proxy_trust(trust_proxy_headers)),
|
||||
max_requests,
|
||||
window_secs,
|
||||
}
|
||||
@@ -273,7 +271,7 @@ where
|
||||
match limiter.check_or_429(&client_id, max_requests, window_secs) {
|
||||
Ok(()) => {}
|
||||
Err(resp) => {
|
||||
return Box::pin(async move { Ok(resp) });
|
||||
return Box::pin(async move { Ok(*resp) });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user