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,
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
pub mod auth;
|
||||
pub mod config;
|
||||
pub mod error;
|
||||
pub mod routes;
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
use axum::Json;
|
||||
use serde_json::{json, Value};
|
||||
|
||||
use crate::auth::AuthenticatedUser;
|
||||
use crate::error::AppError;
|
||||
|
||||
pub async fn handle_whoami(user: AuthenticatedUser) -> Result<Json<Value>, AppError> {
|
||||
Ok(Json(json!({
|
||||
"slug": user.slug,
|
||||
"role": user.role,
|
||||
"data_dir": user.data_dir.to_string_lossy(),
|
||||
})))
|
||||
}
|
||||
@@ -1,11 +1,15 @@
|
||||
mod debug;
|
||||
mod health;
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use axum::routing::get;
|
||||
use axum::Router;
|
||||
|
||||
use crate::config::Config;
|
||||
|
||||
pub fn api_router() -> Router<Arc<Config>> {
|
||||
Router::new().route("/api/health", axum::routing::get(health::handle_health))
|
||||
Router::new()
|
||||
.route("/api/health", get(health::handle_health))
|
||||
.route("/api/debug/whoami", get(debug::handle_whoami))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user