feat: log out of or forget a saved server, and pick one on the login screen
The queue button on a server row in Settings opens a prompt with Cancel, Log out and Forget, replacing the double-press Remove. Log out keeps the address and username without credentials; Forget also deletes the server's saved queue and cover cache. The login screen lists the saved servers beside the form, and a saved login the server turns down at startup is logged out and opens the login screen prefilled. Bumps the version to 0.10.0.
This commit is contained in:
+138
-6
@@ -11,7 +11,8 @@ 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.
|
||||
/// a server that refuses tokens, the password itself. Neither means logged
|
||||
/// out: the server and username are kept for logging in again.
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct Config {
|
||||
pub url: String,
|
||||
@@ -39,6 +40,17 @@ impl Config {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn logged_in(&self) -> bool {
|
||||
self.password.is_some() || !self.token.is_empty()
|
||||
}
|
||||
|
||||
/// Drops the credentials, keeping the server and username.
|
||||
pub fn log_out(&mut self) {
|
||||
self.salt.clear();
|
||||
self.token.clear();
|
||||
self.password = None;
|
||||
}
|
||||
|
||||
pub fn client(&self) -> Client {
|
||||
let auth = match &self.password {
|
||||
Some(p) => Auth::Password(p.clone()),
|
||||
@@ -72,8 +84,7 @@ pub fn state_dir() -> Option<PathBuf> {
|
||||
impl Config {
|
||||
/// Names the server and user, as the sidebar shows them.
|
||||
pub fn label(&self) -> String {
|
||||
let host = self.url.split("://").nth(1).unwrap_or(&self.url);
|
||||
format!("{} on {}", self.username, host.trim_end_matches('/'))
|
||||
server_label(&self.url, &self.username)
|
||||
}
|
||||
|
||||
fn same_login(&self, other: &Config) -> bool {
|
||||
@@ -81,10 +92,21 @@ impl Config {
|
||||
}
|
||||
}
|
||||
|
||||
/// "user on host". The scheme is left out for https only, so an http and an
|
||||
/// https login to the same host can be told apart.
|
||||
pub fn server_label(url: &str, username: &str) -> String {
|
||||
let host = match url.split_once("://") {
|
||||
Some(("https", rest)) => rest,
|
||||
_ => url,
|
||||
};
|
||||
format!("{} on {}", username, host.trim_end_matches('/'))
|
||||
}
|
||||
|
||||
/// Every saved login, and which one is browsed.
|
||||
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
|
||||
pub struct Logins {
|
||||
/// Index into `servers`.
|
||||
/// Index into `servers`. Points at a logged-in server whenever there is
|
||||
/// one.
|
||||
#[serde(default)]
|
||||
pub active: usize,
|
||||
/// `[[server]]` tables in the file.
|
||||
@@ -93,8 +115,22 @@ pub struct Logins {
|
||||
}
|
||||
|
||||
impl Logins {
|
||||
/// The server being browsed, or none when every saved one is logged out.
|
||||
pub fn active(&self) -> Option<&Config> {
|
||||
self.servers.get(self.active)
|
||||
self.servers.get(self.active).filter(|c| c.logged_in())
|
||||
}
|
||||
|
||||
/// Moves `active` off a server that is gone or logged out, to the first
|
||||
/// logged-in one.
|
||||
fn settle_active(&mut self) {
|
||||
if self.active().is_none() {
|
||||
self.active = self.servers.iter().position(Config::logged_in).unwrap_or(0);
|
||||
}
|
||||
}
|
||||
|
||||
/// The saved entry for this server and user, logged in or not.
|
||||
pub fn find(&self, url: &str, username: &str) -> Option<usize> {
|
||||
self.servers.iter().position(|s| s.url == url && s.username == username)
|
||||
}
|
||||
|
||||
/// Adds a login, or refreshes the saved one for the same server and user,
|
||||
@@ -113,7 +149,7 @@ impl Logins {
|
||||
}
|
||||
|
||||
/// Removes a login. The active one stays the same server where it can;
|
||||
/// removing it makes the first remaining login active.
|
||||
/// removing it makes the first remaining logged-in server active.
|
||||
pub fn remove(&mut self, i: usize) {
|
||||
if i >= self.servers.len() {
|
||||
return;
|
||||
@@ -124,6 +160,16 @@ impl Logins {
|
||||
} else if i == self.active {
|
||||
self.active = 0;
|
||||
}
|
||||
self.settle_active();
|
||||
}
|
||||
|
||||
/// Logs out of a server, keeping its entry. Logging out of the active one
|
||||
/// makes the first logged-in server active.
|
||||
pub fn log_out(&mut self, i: usize) {
|
||||
if let Some(s) = self.servers.get_mut(i) {
|
||||
s.log_out();
|
||||
self.settle_active();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -143,6 +189,7 @@ fn parse_logins(text: &str) -> Result<Logins> {
|
||||
if l.active >= l.servers.len() {
|
||||
l.active = 0;
|
||||
}
|
||||
l.settle_active();
|
||||
l
|
||||
}
|
||||
Stored::One(c) => Logins {
|
||||
@@ -362,6 +409,91 @@ mod tests {
|
||||
assert_eq!(parse_logins(text).unwrap().active, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_logged_out_login_keeps_its_server_and_round_trips() {
|
||||
let mut l = Logins::default();
|
||||
l.add(login(
|
||||
"https://a",
|
||||
"u",
|
||||
Auth::Token {
|
||||
salt: "s".into(),
|
||||
token: "t".into(),
|
||||
},
|
||||
));
|
||||
l.add(login("https://b", "v", Auth::Password("p".into())));
|
||||
l.log_out(0);
|
||||
l.log_out(1);
|
||||
let text = toml::to_string(&l).unwrap();
|
||||
assert!(
|
||||
!text.contains("salt") && !text.contains("token") && !text.contains("password"),
|
||||
"{text}"
|
||||
);
|
||||
let back = parse_logins(&text).unwrap();
|
||||
assert_eq!(back, l);
|
||||
assert_eq!(back.servers.len(), 2);
|
||||
assert!(back.servers.iter().all(|s| !s.logged_in()));
|
||||
assert_eq!(back.servers[1].url, "https://b");
|
||||
assert_eq!(back.servers[1].username, "v");
|
||||
assert!(back.active().is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_active_login_skips_logged_out_servers() {
|
||||
let mut l = Logins::default();
|
||||
for host in ["https://a", "https://b", "https://c"] {
|
||||
l.add(login(host, "u", Auth::Password("p".into())));
|
||||
}
|
||||
l.active = 0;
|
||||
l.log_out(1);
|
||||
assert_eq!(l.active().unwrap().url, "https://a");
|
||||
l.log_out(0);
|
||||
assert_eq!(l.active().unwrap().url, "https://c");
|
||||
l.log_out(2);
|
||||
assert!(l.active().is_none());
|
||||
// Logging in again reuses the entry and makes it active.
|
||||
l.add(login("https://b", "u", Auth::Password("p".into())));
|
||||
assert_eq!(l.servers.len(), 3);
|
||||
assert_eq!(l.active().unwrap().url, "https://b");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_saved_active_index_on_a_logged_out_server_moves_to_a_logged_in_one() {
|
||||
let text = "active = 0\n[[server]]\nurl = \"https://a\"\nusername = \"u\"\n\
|
||||
[[server]]\nurl = \"https://b\"\nusername = \"u\"\npassword = \"p\"\n";
|
||||
let l = parse_logins(text).unwrap();
|
||||
assert_eq!(l.active, 1);
|
||||
assert_eq!(l.active().unwrap().url, "https://b");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn removing_the_active_login_skips_logged_out_servers() {
|
||||
let mut l = Logins::default();
|
||||
for host in ["https://a", "https://b", "https://c"] {
|
||||
l.add(login(host, "u", Auth::Password("p".into())));
|
||||
}
|
||||
l.log_out(0);
|
||||
l.active = 1;
|
||||
l.remove(1);
|
||||
assert_eq!(l.active().unwrap().url, "https://c");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_login_list_from_v0_8_0_loads_logged_in() {
|
||||
let text = "active = 1\n\
|
||||
[[server]]\nurl = \"https://a\"\nusername = \"u\"\nsalt = \"s\"\ntoken = \"t\"\n\
|
||||
[[server]]\nurl = \"https://b\"\nusername = \"v\"\npassword = \"p\"\n";
|
||||
let l = parse_logins(text).unwrap();
|
||||
assert_eq!(l.servers.len(), 2);
|
||||
assert!(l.servers.iter().all(Config::logged_in));
|
||||
assert_eq!(l.active().unwrap().url, "https://b");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn labels_tell_http_and_https_apart() {
|
||||
assert_eq!(server_label("https://h/", "u"), "u on h");
|
||||
assert_eq!(server_label("http://h", "u"), "u on http://h");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn settings_default_when_fields_are_missing() {
|
||||
let s: Settings = toml::from_str("").unwrap();
|
||||
|
||||
Reference in New Issue
Block a user