Remove plain-text credential store
librespot stores a more secure token that is valid for a while, rely on this instead. On the flip side this requires users to re-enter their login data when their token has expired. If the token validity is too short we will have to come up with another approach, e.g. OS keyrings. fixes #447
This commit is contained in:
@@ -1,5 +1,3 @@
|
|||||||
use std::path::Path;
|
|
||||||
|
|
||||||
use cursive::traits::Boxable;
|
use cursive::traits::Boxable;
|
||||||
use cursive::view::Identifiable;
|
use cursive::view::Identifiable;
|
||||||
use cursive::views::*;
|
use cursive::views::*;
|
||||||
@@ -8,13 +6,9 @@ use cursive::{CbSink, Cursive, CursiveExt};
|
|||||||
use librespot_core::authentication::Credentials as RespotCredentials;
|
use librespot_core::authentication::Credentials as RespotCredentials;
|
||||||
use librespot_protocol::authentication::AuthenticationType;
|
use librespot_protocol::authentication::AuthenticationType;
|
||||||
|
|
||||||
pub fn create_credentials(path: &Path) -> Result<RespotCredentials, String> {
|
pub fn create_credentials() -> Result<RespotCredentials, String> {
|
||||||
let mut login_cursive = Cursive::default();
|
let mut login_cursive = Cursive::default();
|
||||||
let info_buf = TextContent::new("Failed to authenticate\n");
|
let info_buf = TextContent::new("Please login to Spotify\n");
|
||||||
info_buf.append(format!(
|
|
||||||
"Cannot read config file from {}\n",
|
|
||||||
path.to_str().unwrap()
|
|
||||||
));
|
|
||||||
let info_view = Dialog::around(TextView::new_with_content(info_buf))
|
let info_view = Dialog::around(TextView::new_with_content(info_buf))
|
||||||
.button("Login", move |s| {
|
.button("Login", move |s| {
|
||||||
let login_view = Dialog::new()
|
let login_view = Dialog::new()
|
||||||
|
|||||||
32
src/main.rs
32
src/main.rs
@@ -37,7 +37,6 @@ extern crate regex;
|
|||||||
|
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::process;
|
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
@@ -104,12 +103,7 @@ fn setup_logging(filename: &str) -> Result<(), fern::InitError> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn credentials_prompt(reset: bool, error_message: Option<String>) -> Credentials {
|
fn credentials_prompt(error_message: Option<String>) -> Credentials {
|
||||||
let path = config::config_path("credentials.toml");
|
|
||||||
if reset && fs::remove_file(&path).is_err() {
|
|
||||||
error!("could not delete credential file");
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Some(message) = error_message {
|
if let Some(message) = error_message {
|
||||||
let mut siv = cursive::default();
|
let mut siv = cursive::default();
|
||||||
let dialog = cursive::views::Dialog::around(cursive::views::TextView::new(format!(
|
let dialog = cursive::views::Dialog::around(cursive::views::TextView::new(format!(
|
||||||
@@ -121,21 +115,7 @@ fn credentials_prompt(reset: bool, error_message: Option<String>) -> Credentials
|
|||||||
siv.run();
|
siv.run();
|
||||||
}
|
}
|
||||||
|
|
||||||
let creds =
|
authentication::create_credentials().expect("Could not create credentials")
|
||||||
crate::config::load_or_generate_default(&path, authentication::create_credentials, true)
|
|
||||||
.unwrap_or_else(|e| {
|
|
||||||
eprintln!("{}", e);
|
|
||||||
process::exit(1);
|
|
||||||
});
|
|
||||||
|
|
||||||
#[cfg(target_family = "unix")]
|
|
||||||
std::fs::set_permissions(path, std::os::unix::fs::PermissionsExt::from_mode(0o600))
|
|
||||||
.unwrap_or_else(|e| {
|
|
||||||
eprintln!("{}", e);
|
|
||||||
process::exit(1);
|
|
||||||
});
|
|
||||||
|
|
||||||
creds
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type UserData = Arc<UserDataInner>;
|
type UserData = Arc<UserDataInner>;
|
||||||
@@ -198,20 +178,16 @@ fn main() {
|
|||||||
info!("Using cached credentials");
|
info!("Using cached credentials");
|
||||||
c
|
c
|
||||||
}
|
}
|
||||||
None => credentials_prompt(false, None),
|
None => credentials_prompt(None),
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
while let Err(error) = spotify::Spotify::test_credentials(credentials.clone()) {
|
while let Err(error) = spotify::Spotify::test_credentials(credentials.clone()) {
|
||||||
let reset = error
|
|
||||||
.get_ref()
|
|
||||||
.map_or(false, |err| err.to_string().contains("BadCredentials"));
|
|
||||||
debug!("credential reset: {:?}", reset);
|
|
||||||
let error_msg = match error.get_ref() {
|
let error_msg = match error.get_ref() {
|
||||||
Some(inner) => inner.to_string(),
|
Some(inner) => inner.to_string(),
|
||||||
None => error.to_string(),
|
None => error.to_string(),
|
||||||
};
|
};
|
||||||
credentials = credentials_prompt(reset, Some(error_msg));
|
credentials = credentials_prompt(Some(error_msg));
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut cursive = cursive::default().into_runner();
|
let mut cursive = cursive::default().into_runner();
|
||||||
|
|||||||
Reference in New Issue
Block a user