diff --git a/src/authentication/mod.rs b/src/authentication/mod.rs index fbb47a00..7fec380f 100644 --- a/src/authentication/mod.rs +++ b/src/authentication/mod.rs @@ -6,13 +6,16 @@ use crypto::hmac::Hmac; use crypto::pbkdf2::pbkdf2; use crypto::sha1::Sha1; use protobuf::ProtobufEnum; +use rpassword; use serde; use serde_json; -use std::io::{self, Read, Write}; +use std::io::{self, stderr, Read, Write}; use std::fs::File; use std::path::Path; use rustc_serialize::base64::{self, FromBase64, ToBase64}; +use session::Session; + use protocol::authentication::AuthenticationType; #[derive(Debug, Clone)] @@ -170,3 +173,31 @@ fn deserialize_base64(de: &mut D) -> Result, D::Error> mod discovery; pub use self::discovery::discovery_login; + +pub fn get_credentials(session: &Session, username: Option, password: Option) -> Credentials { + let credentials = session.cache().get_credentials(); + + match (username, password, credentials) { + + (Some(username), Some(password), _) + => Credentials::with_password(username, password), + + (Some(ref username), _, Some(ref credentials)) if *username == credentials.username + => credentials.clone(), + + (Some(username), None, _) => { + write!(stderr(), "Password for {}: ", username).unwrap(); + stderr().flush().unwrap(); + let password = rpassword::read_password().unwrap(); + Credentials::with_password(username.clone(), password) + } + + (None, _, Some(credentials)) + => credentials, + + (None, _, None) => { + info!("No username provided and no stored credentials, starting discovery ..."); + discovery_login(&session.config().device_name, session.device_id()).unwrap() + } + } +} diff --git a/src/main.rs b/src/main.rs index 69fbace4..b9c41654 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,7 @@ use std::thread; use librespot::spirc::SpircManager; use librespot::main_helper; +use librespot::authentication::get_credentials; fn usage(program: &str, opts: &getopts::Options) -> String { let brief = format!("Usage: {} [options]", program); @@ -33,7 +34,8 @@ fn main() { main_helper::setup_logging(&matches); let session = main_helper::create_session(&matches); - let credentials = main_helper::get_credentials(&session, &matches); + let credentials = get_credentials(&session, matches.opt_str("username"), + matches.opt_str("password")); session.login(credentials).unwrap(); let player = main_helper::create_player(&session, &matches); diff --git a/src/main_helper.rs b/src/main_helper.rs index 67e8b2a2..e42660b7 100644 --- a/src/main_helper.rs +++ b/src/main_helper.rs @@ -1,13 +1,10 @@ use env_logger::LogBuilder; use getopts; -use rpassword; use std::env; -use std::io::{stderr, Write}; use std::path::PathBuf; use std::process::exit; use audio_backend::{BACKENDS, Sink}; -use authentication::{Credentials, discovery_login}; use cache::{Cache, DefaultCache, NoCache}; use player::Player; use session::{Bitrate, Config, Session}; @@ -92,36 +89,6 @@ pub fn create_session(matches: &getopts::Matches) -> Session { Session::new(config, cache) } -pub fn get_credentials(session: &Session, matches: &getopts::Matches) -> Credentials { - let credentials = session.cache().get_credentials(); - - match (matches.opt_str("username"), - matches.opt_str("password"), - credentials) { - - (Some(username), Some(password), _) - => Credentials::with_password(username, password), - - (Some(ref username), _, Some(ref credentials)) if *username == credentials.username - => credentials.clone(), - - (Some(username), None, _) => { - write!(stderr(), "Password for {}: ", username).unwrap(); - stderr().flush().unwrap(); - let password = rpassword::read_password().unwrap(); - Credentials::with_password(username.clone(), password) - } - - (None, _, Some(credentials)) - => credentials, - - (None, _, None) => { - info!("No username provided and no stored credentials, starting discovery ..."); - discovery_login(&session.config().device_name, session.device_id()).unwrap() - } - } -} - pub fn create_player(session: &Session, matches: &getopts::Matches) -> Player { let backend_name = matches.opt_str("backend"); let device_name = matches.opt_str("device");