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 std::path::Path;
|
||||||
use rustc_serialize::base64::{self, FromBase64, ToBase64};
|
use rustc_serialize::base64::{self, FromBase64, ToBase64};
|
||||||
|
|
||||||
use session::Session;
|
|
||||||
|
|
||||||
use protocol::authentication::AuthenticationType;
|
use protocol::authentication::AuthenticationType;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
|
@ -174,16 +172,18 @@ fn deserialize_base64<D>(de: &mut D) -> Result<Vec<u8>, D::Error>
|
||||||
mod discovery;
|
mod discovery;
|
||||||
pub use self::discovery::discovery_login;
|
pub use self::discovery::discovery_login;
|
||||||
|
|
||||||
pub fn get_credentials(session: &Session, username: Option<String>, password: Option<String>) -> Credentials {
|
pub fn get_credentials(device_name: &str, device_id: &str,
|
||||||
let credentials = session.cache().get_credentials();
|
username: Option<String>, password: Option<String>,
|
||||||
|
cached_credentials: Option<Credentials>)
|
||||||
match (username, password, credentials) {
|
-> Credentials
|
||||||
|
{
|
||||||
|
match (username, password, cached_credentials) {
|
||||||
|
|
||||||
(Some(username), Some(password), _)
|
(Some(username), Some(password), _)
|
||||||
=> Credentials::with_password(username, password),
|
=> Credentials::with_password(username, password),
|
||||||
|
|
||||||
(Some(ref username), _, Some(ref credentials)) if *username == credentials.username
|
(Some(ref username), _, Some(ref credentials))
|
||||||
=> credentials.clone(),
|
if *username == credentials.username => credentials.clone(),
|
||||||
|
|
||||||
(Some(username), None, _) => {
|
(Some(username), None, _) => {
|
||||||
write!(stderr(), "Password for {}: ", username).unwrap();
|
write!(stderr(), "Password for {}: ", username).unwrap();
|
||||||
|
@ -197,8 +197,7 @@ pub fn get_credentials(session: &Session, username: Option<String>, password: Op
|
||||||
|
|
||||||
(None, _, None) => {
|
(None, _, None) => {
|
||||||
info!("No username provided and no stored credentials, starting discovery ...");
|
info!("No username provided and no stored credentials, starting discovery ...");
|
||||||
discovery_login(session.config().device_name.clone(),
|
discovery_login(device_name.clone(), device_id.clone()).unwrap()
|
||||||
session.device_id()).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"))
|
.map(|bitrate| Bitrate::from_str(bitrate).expect("Invalid bitrate"))
|
||||||
.unwrap_or(Bitrate::Bitrate160);
|
.unwrap_or(Bitrate::Bitrate160);
|
||||||
|
|
||||||
let config = Config {
|
let device_name = matches.opt_str("name").unwrap();
|
||||||
user_agent: version::version_string(),
|
let device_id = librespot::session::device_id(&device_name);
|
||||||
device_name: matches.opt_str("name").unwrap(),
|
|
||||||
bitrate: bitrate,
|
|
||||||
onstart: matches.opt_str("onstart"),
|
|
||||||
onstop: matches.opt_str("onstop"),
|
|
||||||
};
|
|
||||||
|
|
||||||
let cache = matches.opt_str("c").map(|cache_location| {
|
let cache = matches.opt_str("c").map(|cache_location| {
|
||||||
Box::new(DefaultCache::new(PathBuf::from(cache_location)).unwrap())
|
Box::new(DefaultCache::new(PathBuf::from(cache_location)).unwrap())
|
||||||
as Box<Cache + Send + Sync>
|
as Box<Cache + Send + Sync>
|
||||||
}).unwrap_or_else(|| Box::new(NoCache));
|
}).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 session = Session::new(config, cache);
|
||||||
|
|
||||||
let credentials = get_credentials(&session, matches.opt_str("username"),
|
|
||||||
matches.opt_str("password"));
|
|
||||||
session.login(credentials).unwrap();
|
session.login(credentials).unwrap();
|
||||||
|
|
||||||
let device_name = matches.opt_str("device");
|
let device_name = matches.opt_str("device");
|
||||||
|
|
|
@ -46,6 +46,7 @@ impl FromStr for Bitrate {
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
pub user_agent: String,
|
pub user_agent: String,
|
||||||
pub device_name: String,
|
pub device_name: String,
|
||||||
|
pub device_id: String,
|
||||||
pub bitrate: Bitrate,
|
pub bitrate: Bitrate,
|
||||||
pub onstart: Option<String>,
|
pub onstart: Option<String>,
|
||||||
pub onstop: Option<String>,
|
pub onstop: Option<String>,
|
||||||
|
@ -58,7 +59,6 @@ pub struct SessionData {
|
||||||
|
|
||||||
pub struct SessionInternal {
|
pub struct SessionInternal {
|
||||||
config: Config,
|
config: Config,
|
||||||
device_id: String,
|
|
||||||
data: RwLock<SessionData>,
|
data: RwLock<SessionData>,
|
||||||
|
|
||||||
cache: Box<Cache + Send + Sync>,
|
cache: Box<Cache + Send + Sync>,
|
||||||
|
@ -73,17 +73,16 @@ pub struct SessionInternal {
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Session(pub Arc<SessionInternal>);
|
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 {
|
impl Session {
|
||||||
pub fn new(config: Config, cache: Box<Cache + Send + Sync>) -> 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 {
|
Session(Arc::new(SessionInternal {
|
||||||
config: config,
|
config: config,
|
||||||
device_id: device_id,
|
|
||||||
data: RwLock::new(SessionData {
|
data: RwLock::new(SessionData {
|
||||||
country: String::new(),
|
country: String::new(),
|
||||||
canonical_username: String::new(),
|
canonical_username: String::new(),
|
||||||
|
@ -241,7 +240,7 @@ impl Session {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn device_id(&self) -> &str {
|
pub fn device_id(&self) -> &str {
|
||||||
&self.0.device_id
|
&self.config().device_id
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue