use axum::{ extract::{Request, State}, http::StatusCode, middleware::{self, Next}, response::{IntoResponse, Response}, routing::{get, post}, Router, }; // use axum_macros::debug_middleware; use tracing::{info, warn}; use view::view; use crate::SharedState; use self::upload::upload; mod upload; mod view; const BAD_AUTH: (StatusCode, &str) = (StatusCode::BAD_REQUEST, "No auth, no service :3"); pub fn add_routes(state: SharedState) -> Router { Router::new() .route("/upload", post(upload)) .route("/view", get(view)) .layer(middleware::from_fn_with_state( state.clone(), key_check_auth, )) } // #[debug_middleware] pub async fn key_check_auth( State(state): State, req: Request, next: Next, ) -> Response { let headers = req.headers(); let bits = match headers.get("magic_bits") { Some(bits) => bits.to_str().unwrap_or_default(), None => { warn!("Somebody hit auth with no magic_bits"); return BAD_AUTH.into_response(); } }; let user = { let auth = &state.read().auth; let user = auth.users.iter().find(|user| user.magic_bit == bits); if user.is_none() { warn!( "Somebody attempted to use the following magic bits: {}", bits ); return BAD_AUTH.into_response(); } user.unwrap().clone() }; info!("{} has authed", user.name); next.run(req).await }