mirror of
https://github.com/librespot-org/librespot.git
synced 2025-01-17 17:34:04 +00:00
Move device id into config
This commit is contained in:
parent
9de55bb8cd
commit
2a0ccc0d1d
3 changed files with 35 additions and 28 deletions
|
@ -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<D>(de: &mut D) -> Result<Vec<u8>, D::Error>
|
|||
mod discovery;
|
||||
pub use self::discovery::discovery_login;
|
||||
|
||||
pub fn get_credentials(session: &Session, username: Option<String>, password: Option<String>) -> Credentials {
|
||||
let credentials = session.cache().get_credentials();
|
||||
|
||||
match (username, password, credentials) {
|
||||
pub fn get_credentials(device_name: &str, device_id: &str,
|
||||
username: Option<String>, password: Option<String>,
|
||||
cached_credentials: Option<Credentials>)
|
||||
-> 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<String>, 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()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
27
src/main.rs
27
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<Cache + Send + Sync>
|
||||
}).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");
|
||||
|
|
|
@ -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<String>,
|
||||
pub onstop: Option<String>,
|
||||
|
@ -58,7 +59,6 @@ pub struct SessionData {
|
|||
|
||||
pub struct SessionInternal {
|
||||
config: Config,
|
||||
device_id: String,
|
||||
data: RwLock<SessionData>,
|
||||
|
||||
cache: Box<Cache + Send + Sync>,
|
||||
|
@ -73,17 +73,16 @@ pub struct SessionInternal {
|
|||
#[derive(Clone)]
|
||||
pub struct Session(pub Arc<SessionInternal>);
|
||||
|
||||
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<Cache + Send + Sync>) -> 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
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue