fix auth errors and add primigenial paper trash

This commit is contained in:
DioCrafts
2025-03-24 16:47:42 +01:00
parent 838ae6e0d4
commit 38b0e9594b
58 changed files with 3701 additions and 135 deletions
+43 -4
View File
@@ -1,11 +1,10 @@
use std::sync::Arc;
use axum::{
extract::{State, Request, FromRequestParts},
http::{StatusCode, request::Parts, HeaderMap, header},
extract::{State, Request},
http::{StatusCode, HeaderMap, header},
middleware::Next,
response::{Response, IntoResponse},
body::Body,
RequestPartsExt,
};
use async_trait::async_trait;
use futures::future::BoxFuture;
@@ -23,6 +22,13 @@ pub struct CurrentUser {
pub role: String,
}
// Estructura para usar en extractores de Axum
#[derive(Clone, Debug)]
pub struct AuthUser {
pub id: String,
pub username: String,
}
// Error para las operaciones de autenticación
#[derive(Debug, thiserror::Error)]
pub enum AuthError {
@@ -60,6 +66,22 @@ impl IntoResponse for AuthError {
}
}
// Implementamos el extractor para AuthUser
// Use a function instead of an extractor for now
// We'll use this directly in handlers until we solve the extractor lifetime issues
pub async fn get_auth_user(req: &Request<Body>) -> Result<AuthUser, AuthError> {
// Get the current user from extensions
if let Some(current_user) = req.extensions().get::<CurrentUser>() {
return Ok(AuthUser {
id: current_user.id.clone(),
username: current_user.username.clone(),
});
}
// Return error if user not found
Err(AuthError::UserNotFound)
}
// Middleware de autenticación simplificado - solo valida si existe un token
pub async fn auth_middleware(
State(state): State<Arc<AppState>>,
@@ -73,7 +95,24 @@ pub async fn auth_middleware(
.and_then(|value| value.to_str().ok())
.and_then(|value| value.strip_prefix("Bearer ")) {
// Crear un usuario ficticio para pruebas (esto se reemplazará con la validación real)
// EMERGENCY BYPASS for torrefacto user
if token_str == "torrefacto-emergency-access-token" || token_str == "torrefacto-emergency-access-token-new" {
tracing::info!("Using EMERGENCY BYPASS in auth middleware for torrefacto token");
// Create a user with the actual registered user info
let current_user = CurrentUser {
id: "b2f7d91b-6b44-4601-8472-f4e520879f20".to_string(),
username: "torrefacto".to_string(),
email: "dionisio@gmail.com".to_string(),
role: "user".to_string(),
};
// Add user to the request
request.extensions_mut().insert(current_user);
return Ok(next.run(request).await);
}
// For regular tokens, create a test user (this will be replaced with real validation)
let current_user = CurrentUser {
id: "test-user-id".to_string(),
username: "test-user".to_string(),