use axum::{
body::Body,
extract::State,
http::Request,
middleware::Next,
response::{IntoResponse, Redirect, Response},
};
use axum_extra::extract::cookie::CookieJar;
use std::env;
use crate::{DbPool, models};
const SESSION_COOKIE: &str = "miniapm_session";
pub async fn web_auth_middleware(
State(pool): State<DbPool>,
jar: CookieJar,
request: Request<Body>,
next: Next,
) -> Response {
let enabled = env::var("ENABLE_USER_ACCOUNTS")
.map(|v| v == "1" || v.to_lowercase() == "true")
.unwrap_or(false);
if !enabled {
return next.run(request).await;
}
let token = match jar.get(SESSION_COOKIE) {
Some(cookie) => cookie.value().to_string(),
None => return Redirect::to("/auth/login").into_response(),
};
match models::user::get_user_from_session(&pool, &token) {
Ok(Some(user)) => {
if user.must_change_password {
let path = request.uri().path();
if path == "/auth/change-password" || path.starts_with("/static") {
return next.run(request).await;
}
return Redirect::to("/auth/change-password").into_response();
}
next.run(request).await
}
_ => Redirect::to("/auth/login").into_response(),
}
}