fix: log in with the password on servers that refuse tokens

Nextcloud Music answers a token login with Subsonic error 41, so remus
could not log in to it at all. Login now retries with the password,
hex-encoded as the spec allows, when the server returns 41, and saves
which method worked. Servers that accept tokens are unaffected, and
existing login files still load.

Server errors now keep their code. A wrong password on Nextcloud adds a
line about the API password from the Music app's settings, which is not
the Nextcloud login password.
This commit is contained in:
Ansho Rei
2026-11-04 03:44:34 +01:00
parent a74fd1bd5c
commit c12ed3e12d
5 changed files with 358 additions and 59 deletions
+49 -21
View File
@@ -18,7 +18,7 @@ use tokio::sync::Semaphore;
use crate::config::{self, Config};
use crate::player::{Player, PlayerEvent};
use crate::subsonic::{Album, Artist, Client, Playlist, SearchResult, Song};
use crate::subsonic::{self, Album, Artist, Client, Playlist, SearchResult, Song};
use crate::{Action, AppWindow, Area, Header, Hint, Item, Screen, Theme};
thread_local! {
@@ -257,7 +257,7 @@ impl App {
log::info!("using saved login for {} on {}", cfg.username, cfg.url);
self.ui.set_login_server(cfg.url.clone().into());
self.ui.set_login_username(cfg.username.clone().into());
self.enter_browse(Client::new(&cfg.url, &cfg.username, &cfg.salt, &cfg.token));
self.enter_browse(cfg.client());
}
Ok(None) => {
log::info!("no saved login, showing the login screen");
@@ -330,27 +330,22 @@ impl App {
}
self.ui.set_login_error("".into());
self.ui.set_login_busy(true);
let client = Client::from_password(&url, &user, &password);
let probe = client.clone();
self.spawn(async move { probe.ping().await }, move |app, r| {
app.ui.set_login_busy(false);
match r {
Ok(()) => {
app.ui.set_login_password("".into());
let cfg = Config {
url: client.base().to_string(),
username: client.user().to_string(),
salt: client.salt().to_string(),
token: client.token().to_string(),
};
if let Err(e) = config::save(&cfg) {
app.toast(format!("Could not save settings: {e:#}"));
self.spawn(
async move { Client::login(&url, &user, &password).await },
move |app, r| {
app.ui.set_login_busy(false);
match r {
Ok(client) => {
app.ui.set_login_password("".into());
if let Err(e) = config::save(&Config::from_client(&client)) {
app.toast(format!("Could not save settings: {e:#}"));
}
app.enter_browse(client);
}
app.enter_browse(client);
Err(e) => app.ui.set_login_error(login_error(&e).into()),
}
Err(e) => app.ui.set_login_error(format!("{e:#}").into()),
}
});
},
);
}
fn enter_browse(&mut self, client: Client) {
@@ -1734,6 +1729,26 @@ fn tint(seed: &str) -> Color {
Color::from_rgb_u8(to(r), to(g), to(b))
}
/// The message for a failed login, with a pointer to the fix where the
/// server's own message does not give one.
fn login_error(e: &anyhow::Error) -> String {
let msg = format!("{e:#}");
match subsonic::api_error(e) {
Some(a)
if a.code == subsonic::WRONG_CREDENTIALS
&& a.server
.as_deref()
.is_some_and(|s| s.to_lowercase().contains("nextcloud")) =>
{
format!(
"{msg}. Nextcloud Music needs the API password generated in its own \
settings, not your Nextcloud login password."
)
}
_ => msg,
}
}
fn fmt_time(secs: f64) -> String {
let t = secs.max(0.0) as u64;
let (h, m, s) = (t / 3600, (t / 60) % 60, t % 60);
@@ -1794,6 +1809,19 @@ mod tests {
assert_eq!(bucket(5000.0), 1536);
}
#[test]
fn nextcloud_wrong_password_points_at_the_api_password() {
let err = |server: &str| {
anyhow::Error::from(subsonic::ApiError {
code: subsonic::WRONG_CREDENTIALS,
message: "Wrong username or password".into(),
server: Some(server.into()),
})
};
assert!(login_error(&err("nextcloud music")).contains("API password"));
assert_eq!(login_error(&err("navidrome")), "Wrong username or password");
}
#[test]
fn formatting() {
assert_eq!(fmt_time(61.9), "1:01");