From 9253be7bc9675c266000a06fac218f444b63d66c Mon Sep 17 00:00:00 2001 From: johannesd3 Date: Wed, 10 Feb 2021 22:40:33 +0100 Subject: [PATCH] Small refactor of librespot-core * Remove default impl for `SessionConfig` * Move util mod to single file * Restore privacy of mods * Move `fn get_credentials` to application * Remove `extern crate` statements --- Cargo.lock | 10 ---------- core/Cargo.toml | 1 - core/build.rs | 3 --- core/src/authentication.rs | 24 ---------------------- core/src/config.rs | 15 -------------- core/src/lib.rs | 30 +++------------------------- core/src/{util/mod.rs => util.rs} | 0 src/main.rs | 33 ++++++++++++++++++++++++++----- 8 files changed, 31 insertions(+), 85 deletions(-) rename core/src/{util/mod.rs => util.rs} (100%) diff --git a/Cargo.lock b/Cargo.lock index 1e8176f6..c9bcef5b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1475,7 +1475,6 @@ dependencies = [ "tokio-util", "tower-service", "url 1.7.2", - "uuid", "vergen", ] @@ -2990,15 +2989,6 @@ dependencies = [ "percent-encoding 2.1.0", ] -[[package]] -name = "uuid" -version = "0.8.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bc5cf98d8186244414c848017f0e2676b3fcb46807f6668a97dfe67359a3c4b7" -dependencies = [ - "getrandom 0.2.2", -] - [[package]] name = "vergen" version = "3.2.0" diff --git a/core/Cargo.toml b/core/Cargo.toml index 36d79988..673637a0 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -40,7 +40,6 @@ tokio = { version = "1.0", features = ["io-util", "rt-multi-thread"] } tokio-util = { version = "0.6", features = ["codec"] } tower-service = "0.3" url = "1.7" -uuid = { version = "0.8", features = ["v4"] } [build-dependencies] rand = "0.7" diff --git a/core/build.rs b/core/build.rs index e8c71e4a..83f50472 100644 --- a/core/build.rs +++ b/core/build.rs @@ -1,6 +1,3 @@ -extern crate rand; -extern crate vergen; - use rand::distributions::Alphanumeric; use rand::Rng; use vergen::{generate_cargo_keys, ConstantsFlags}; diff --git a/core/src/authentication.rs b/core/src/authentication.rs index 5394ff35..fa570409 100644 --- a/core/src/authentication.rs +++ b/core/src/authentication.rs @@ -142,30 +142,6 @@ where base64::decode(&v).map_err(|e| serde::de::Error::custom(e.to_string())) } -pub fn get_credentials String>( - username: Option, - password: Option, - cached_credentials: Option, - prompt: F, -) -> Option { - match (username, password, cached_credentials) { - (Some(username), Some(password), _) => Some(Credentials::with_password(username, password)), - - (Some(ref username), _, Some(ref credentials)) if *username == credentials.username => { - Some(credentials.clone()) - } - - (Some(username), None, _) => Some(Credentials::with_password( - username.clone(), - prompt(&username), - )), - - (None, _, Some(credentials)) => Some(credentials), - - (None, _, None) => None, - } -} - error_chain! { types { AuthenticationError, AuthenticationErrorKind, AuthenticationResultExt, AuthenticationResult; diff --git a/core/src/config.rs b/core/src/config.rs index 60cb66e0..469b935a 100644 --- a/core/src/config.rs +++ b/core/src/config.rs @@ -1,9 +1,6 @@ use std::fmt; use std::str::FromStr; use url::Url; -use uuid::Uuid; - -use crate::version; #[derive(Clone, Debug)] pub struct SessionConfig { @@ -13,18 +10,6 @@ pub struct SessionConfig { pub ap_port: Option, } -impl Default for SessionConfig { - fn default() -> SessionConfig { - let device_id = Uuid::new_v4().to_hyphenated().to_string(); - SessionConfig { - user_agent: version::version_string(), - device_id: device_id, - proxy: None, - ap_port: None, - } - } -} - #[derive(Clone, Copy, Debug, Hash, PartialOrd, Ord, PartialEq, Eq)] pub enum DeviceType { Unknown = 0, diff --git a/core/src/lib.rs b/core/src/lib.rs index 25ce5413..6c180c2e 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -8,43 +8,19 @@ extern crate serde_derive; extern crate pin_project_lite; #[macro_use] extern crate error_chain; -extern crate aes; -extern crate base64; -extern crate byteorder; -extern crate bytes; -extern crate futures; -extern crate hmac; -extern crate httparse; -extern crate hyper; -extern crate num_bigint; -extern crate num_integer; -extern crate num_traits; -extern crate once_cell; -extern crate pbkdf2; -extern crate protobuf; -extern crate rand; -extern crate serde; -extern crate serde_json; -extern crate sha1; -extern crate shannon; -pub extern crate tokio; -extern crate tokio_util; -extern crate tower_service; -extern crate url; -extern crate uuid; -extern crate librespot_protocol as protocol; +use librespot_protocol as protocol; #[macro_use] mod component; -pub mod apresolve; +mod apresolve; pub mod audio_key; pub mod authentication; pub mod cache; pub mod channel; pub mod config; -pub mod connection; +mod connection; pub mod diffie_hellman; pub mod keymaster; pub mod mercury; diff --git a/core/src/util/mod.rs b/core/src/util.rs similarity index 100% rename from core/src/util/mod.rs rename to core/src/util.rs diff --git a/src/main.rs b/src/main.rs index 71041f55..31ef3001 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,7 +12,7 @@ use std::{ }; use url::Url; -use librespot::core::authentication::{get_credentials, Credentials}; +use librespot::core::authentication::Credentials; use librespot::core::cache::Cache; use librespot::core::config::{ConnectConfig, DeviceType, SessionConfig, VolumeCtrl}; use librespot::core::session::Session; @@ -70,6 +70,29 @@ fn list_backends() { } } +pub fn get_credentials Option>( + username: Option, + password: Option, + cached_credentials: Option, + prompt: F, +) -> Option { + if let Some(username) = username { + if let Some(password) = password { + return Some(Credentials::with_password(username, password)); + } + + match cached_credentials { + Some(credentials) if username == credentials.username => Some(credentials), + _ => { + let password = prompt(&username)?; + Some(Credentials::with_password(username, password)) + } + } + } else { + cached_credentials + } +} + #[derive(Clone)] struct Setup { backend: fn(Option) -> Box, @@ -317,10 +340,10 @@ fn setup(args: &[String]) -> Setup { let credentials = { let cached_credentials = cache.as_ref().and_then(Cache::credentials); - let password = |username: &String| -> String { - write!(stderr(), "Password for {}: ", username).unwrap(); - stderr().flush().unwrap(); - rpassword::read_password().unwrap() + let password = |username: &String| -> Option { + write!(stderr(), "Password for {}: ", username).ok()?; + stderr().flush().ok()?; + rpassword::read_password().ok() }; get_credentials(