From 2a0ccc0d1d3c2d5d914aa1bea3fcd62987d04ed8 Mon Sep 17 00:00:00 2001 From: Paul Lietar Date: Wed, 18 Jan 2017 17:07:20 +0000 Subject: [PATCH] Move device id into config --- src/authentication/mod.rs | 19 +++++++++---------- src/main.rs | 27 ++++++++++++++++++--------- src/session.rs | 17 ++++++++--------- 3 files changed, 35 insertions(+), 28 deletions(-) diff --git a/src/authentication/mod.rs b/src/authentication/mod.rs index 8d69f610..693487db 100644 --- a/src/authentication/mod.rs +++ b/src/authentication/mod.rs @@ -14,8 +14,6 @@ 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)] @@ -174,16 +172,18 @@ 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) { +pub fn get_credentials(device_name: &str, device_id: &str, + username: Option, password: Option, + cached_credentials: Option) + -> Credentials +{ + match (username, password, cached_credentials) { (Some(username), Some(password), _) => Credentials::with_password(username, password), - (Some(ref username), _, Some(ref credentials)) if *username == credentials.username - => credentials.clone(), + (Some(ref username), _, Some(ref credentials)) + if *username == credentials.username => credentials.clone(), (Some(username), None, _) => { write!(stderr(), "Password for {}: ", username).unwrap(); @@ -197,8 +197,7 @@ pub fn get_credentials(session: &Session, username: Option, password: Op (None, _, None) => { info!("No username provided and no stored credentials, starting discovery ..."); - discovery_login(session.config().device_name.clone(), - session.device_id()).unwrap() + discovery_login(device_name.clone(), device_id.clone()).unwrap() } } } diff --git a/src/main.rs b/src/main.rs index cfe1b9e8..d0ccf007 100644 --- a/src/main.rs +++ b/src/main.rs @@ -101,23 +101,32 @@ fn setup(args: &[String]) -> (Session, Player) { .map(|bitrate| Bitrate::from_str(bitrate).expect("Invalid bitrate")) .unwrap_or(Bitrate::Bitrate160); - let config = Config { - user_agent: version::version_string(), - device_name: matches.opt_str("name").unwrap(), - bitrate: bitrate, - onstart: matches.opt_str("onstart"), - onstop: matches.opt_str("onstop"), - }; + let device_name = matches.opt_str("name").unwrap(); + let device_id = librespot::session::device_id(&device_name); let cache = matches.opt_str("c").map(|cache_location| { Box::new(DefaultCache::new(PathBuf::from(cache_location)).unwrap()) as Box }).unwrap_or_else(|| Box::new(NoCache)); + let cached_credentials = cache.get_credentials(); + + let credentials = get_credentials(&device_name, &device_id, + matches.opt_str("username"), + matches.opt_str("password"), + cached_credentials); + + let config = Config { + user_agent: version::version_string(), + device_name: device_name, + device_id: device_id, + bitrate: bitrate, + onstart: matches.opt_str("onstart"), + onstop: matches.opt_str("onstop"), + }; + let session = Session::new(config, cache); - let credentials = get_credentials(&session, matches.opt_str("username"), - matches.opt_str("password")); session.login(credentials).unwrap(); let device_name = matches.opt_str("device"); diff --git a/src/session.rs b/src/session.rs index c913e489..2d3f838e 100644 --- a/src/session.rs +++ b/src/session.rs @@ -46,6 +46,7 @@ impl FromStr for Bitrate { pub struct Config { pub user_agent: String, pub device_name: String, + pub device_id: String, pub bitrate: Bitrate, pub onstart: Option, pub onstop: Option, @@ -58,7 +59,6 @@ pub struct SessionData { pub struct SessionInternal { config: Config, - device_id: String, data: RwLock, cache: Box, @@ -73,17 +73,16 @@ pub struct SessionInternal { #[derive(Clone)] pub struct Session(pub Arc); +pub fn device_id(device_name: &str) -> String { + let mut h = Sha1::new(); + h.input_str(&device_name); + h.result_str() +} + impl Session { pub fn new(config: Config, cache: Box) -> Session { - let device_id = { - let mut h = Sha1::new(); - h.input_str(&config.device_name); - h.result_str() - }; - Session(Arc::new(SessionInternal { config: config, - device_id: device_id, data: RwLock::new(SessionData { country: String::new(), canonical_username: String::new(), @@ -241,7 +240,7 @@ impl Session { } pub fn device_id(&self) -> &str { - &self.0.device_id + &self.config().device_id } }