Update config and dependencies for auth
Refactor configuration loading to separate user credentials from server settings. Update dependencies to their latest compatible versions to leverage new features and security patches. Key changes include: - Moving user credentials (`api_key`, `web_password`, `role`) from `.env` to a dedicated `users.toml` file for better manageability and security. - Introducing an `AuthenticatedUser` struct and extractor for API key-based authentication. - Updating `axum` and related crates to leverage Rust edition 2024 improvements. - Adjusting configuration values and tests to reflect these changes.
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
|
||||
use axum::extract::FromRequestParts;
|
||||
use axum::http::request::Parts;
|
||||
|
||||
use crate::config::Config;
|
||||
use crate::error::AppError;
|
||||
|
||||
/// Extracted from the X-API-Key header on every authenticated request.
|
||||
pub struct AuthenticatedUser {
|
||||
pub slug: String,
|
||||
pub role: String,
|
||||
pub data_dir: PathBuf,
|
||||
}
|
||||
|
||||
impl FromRequestParts<Arc<Config>> for AuthenticatedUser {
|
||||
type Rejection = AppError;
|
||||
|
||||
async fn from_request_parts(
|
||||
parts: &mut Parts,
|
||||
state: &Arc<Config>,
|
||||
) -> Result<Self, Self::Rejection> {
|
||||
let api_key = parts
|
||||
.headers
|
||||
.get("X-API-Key")
|
||||
.and_then(|v| v.to_str().ok())
|
||||
.ok_or(AppError::Unauthorized)?;
|
||||
|
||||
let slug = state
|
||||
.api_keys
|
||||
.get(api_key)
|
||||
.ok_or(AppError::Unauthorized)?;
|
||||
|
||||
let user = state
|
||||
.users
|
||||
.iter()
|
||||
.find(|u| u.slug == *slug)
|
||||
.ok_or(AppError::Unauthorized)?;
|
||||
|
||||
let data_dir = state.data_path.join(slug);
|
||||
tokio::fs::create_dir_all(data_dir.join("open")).await?;
|
||||
tokio::fs::create_dir_all(data_dir.join("done")).await?;
|
||||
|
||||
Ok(Self {
|
||||
slug: slug.clone(),
|
||||
role: user.role.clone(),
|
||||
data_dir,
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user