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:
+76
-3
@@ -7,14 +7,48 @@ use anyhow::{Context, Result};
|
||||
use directories::ProjectDirs;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// Server credentials. `token` is md5(password + salt): not the password,
|
||||
/// but enough to log in to this server, so the file is written 0600.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
use crate::subsonic::{Auth, Client};
|
||||
|
||||
/// Server credentials, written 0600. Either a token, md5(password + salt),
|
||||
/// which is not the password but is enough to log in to this server; or, for
|
||||
/// a server that refuses tokens, the password itself.
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct Config {
|
||||
pub url: String,
|
||||
pub username: String,
|
||||
#[serde(default, skip_serializing_if = "String::is_empty")]
|
||||
pub salt: String,
|
||||
#[serde(default, skip_serializing_if = "String::is_empty")]
|
||||
pub token: String,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub password: Option<String>,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
pub fn from_client(client: &Client) -> Config {
|
||||
let (salt, token, password) = match client.auth() {
|
||||
Auth::Token { salt, token } => (salt.clone(), token.clone(), None),
|
||||
Auth::Password(p) => (String::new(), String::new(), Some(p.clone())),
|
||||
};
|
||||
Config {
|
||||
url: client.base().to_string(),
|
||||
username: client.user().to_string(),
|
||||
salt,
|
||||
token,
|
||||
password,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn client(&self) -> Client {
|
||||
let auth = match &self.password {
|
||||
Some(p) => Auth::Password(p.clone()),
|
||||
None => Auth::Token {
|
||||
salt: self.salt.clone(),
|
||||
token: self.token.clone(),
|
||||
},
|
||||
};
|
||||
Client::new(&self.url, &self.username, auth)
|
||||
}
|
||||
}
|
||||
|
||||
fn dirs() -> Option<ProjectDirs> {
|
||||
@@ -67,3 +101,42 @@ pub fn save(cfg: &Config) -> Result<()> {
|
||||
log::info!("saved login to {}", path.display());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn token_login_from_before_password_auth_still_loads() {
|
||||
let old = "url = \"https://h\"\nusername = \"u\"\nsalt = \"s\"\ntoken = \"t\"\n";
|
||||
let cfg: Config = toml::from_str(old).unwrap();
|
||||
assert_eq!(cfg.password, None);
|
||||
let auth = Auth::Token {
|
||||
salt: "s".into(),
|
||||
token: "t".into(),
|
||||
};
|
||||
assert_eq!(*cfg.client().auth(), auth);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn each_auth_method_round_trips() {
|
||||
let token = Auth::Token {
|
||||
salt: "s".into(),
|
||||
token: "t".into(),
|
||||
};
|
||||
for auth in [token, Auth::Password("sesame".into())] {
|
||||
let cfg = Config::from_client(&Client::new("https://h", "u", auth.clone()));
|
||||
let text = toml::to_string(&cfg).unwrap();
|
||||
let back: Config = toml::from_str(&text).unwrap();
|
||||
assert_eq!(back, cfg);
|
||||
assert_eq!(*back.client().auth(), auth);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn password_login_stores_no_token_fields() {
|
||||
let cfg = Config::from_client(&Client::new("https://h", "u", Auth::Password("p".into())));
|
||||
let text = toml::to_string(&cfg).unwrap();
|
||||
assert!(!text.contains("salt") && !text.contains("token"), "{text}");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user