feat: allow password protection to be disabled (#1201)

Thanks @Odonno!
This commit is contained in:
David Bottiau 2026-06-15 11:01:01 +02:00 committed by GitHub
parent e0d481cc2e
commit 921dff5ffe
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 19 additions and 2 deletions

View File

@ -85,6 +85,10 @@ pub async fn login(State(state): State<Arc<WebState>>, Json(body): Json<LoginReq
}
pub async fn setup(State(state): State<Arc<WebState>>, Json(body): Json<LoginRequest>) -> Result<Response, StatusCode> {
if state.password_disabled {
return Err(StatusCode::FORBIDDEN);
}
// Only allow setup when no password is configured
if state.password_hash.read().await.is_some() {
return Err(StatusCode::FORBIDDEN);
@ -115,6 +119,9 @@ pub async fn setup(State(state): State<Arc<WebState>>, Json(body): Json<LoginReq
}
pub async fn check(State(state): State<Arc<WebState>>, req: Request<axum::body::Body>) -> Json<AuthCheckResponse> {
if state.password_disabled {
return Json(AuthCheckResponse { authenticated: true, required: false, setup_required: false });
}
let has_password = state.password_hash.read().await.is_some();
if !has_password {
return Json(AuthCheckResponse { authenticated: false, required: false, setup_required: true });

View File

@ -62,7 +62,13 @@ async fn main() {
};
// Password hash: env var takes priority, then database
let password_hash = if let Ok(pw) = std::env::var("DBX_PASSWORD") {
let password_disabled = std::env::var("DBX_DISABLE_PASSWORD")
.map(|v| matches!(v.trim().to_lowercase().as_str(), "1" | "true" | "yes" | "on"))
.unwrap_or(false);
let password_hash = if password_disabled {
None
} else if let Ok(pw) = std::env::var("DBX_PASSWORD") {
let salt = SaltString::generate(&mut OsRng);
Some(Argon2::default().hash_password(pw.as_bytes(), &salt).expect("Failed to hash password").to_string())
} else {
@ -72,6 +78,7 @@ async fn main() {
let web_state = Arc::new(WebState {
app: app_state,
data_dir,
password_disabled,
password_hash: RwLock::new(password_hash),
sessions: RwLock::new(HashSet::new()),
sse_channels: RwLock::new(HashMap::new()),
@ -358,7 +365,9 @@ async fn main() {
let addr = SocketAddr::from(([0, 0, 0, 0], port));
tracing::info!("DBX Web server starting on http://{}", addr);
if std::env::var("DBX_PASSWORD").is_ok() {
if password_disabled {
tracing::info!("Password protection is disabled");
} else if std::env::var("DBX_PASSWORD").is_ok() {
tracing::info!("Password protection is enabled");
}

View File

@ -13,6 +13,7 @@ pub struct LoginRateLimit {
pub struct WebState {
pub app: Arc<AppState>,
pub data_dir: PathBuf,
pub password_disabled: bool,
pub password_hash: RwLock<Option<String>>,
pub sessions: RwLock<HashSet<String>>,
pub sse_channels: RwLock<HashMap<String, broadcast::Sender<String>>>,