Browse Source

Use prompt for password in main.rs

Sasha Hilton 7 years ago
parent
commit
084646e21b
2 changed files with 14 additions and 8 deletions
  1. 5 7
      core/src/authentication.rs
  2. 9 1
      src/main.rs

+ 5 - 7
core/src/authentication.rs

@@ -7,11 +7,11 @@ use crypto::hmac::Hmac;
 use crypto::pbkdf2::pbkdf2;
 use crypto::sha1::Sha1;
 use protobuf::ProtobufEnum;
-use rpassword;
 use serde;
 use serde_json;
 use std::fs::File;
-use std::io::{self, stderr, Read, Write};
+use std::io::{self, Read, Write};
+use std::ops::FnOnce;
 use std::path::Path;
 
 use protocol::authentication::AuthenticationType;
@@ -180,10 +180,11 @@ where
     base64::decode(&v).map_err(|e| serde::de::Error::custom(e.to_string()))
 }
 
-pub fn get_credentials(
+pub fn get_credentials<F: FnOnce(&String) -> String>(
     username: Option<String>,
     password: Option<String>,
     cached_credentials: Option<Credentials>,
+    prompt: F,
 ) -> Option<Credentials> {
     match (username, password, cached_credentials) {
         (Some(username), Some(password), _) => Some(Credentials::with_password(username, password)),
@@ -193,10 +194,7 @@ pub fn get_credentials(
         }
 
         (Some(username), None, _) => {
-            write!(stderr(), "Password for {}: ", username).unwrap();
-            stderr().flush().unwrap();
-            let password = rpassword::read_password().unwrap();
-            Some(Credentials::with_password(username.clone(), password))
+            Some(Credentials::with_password(username.clone(), prompt(&username)))
         }
 
         (None, _, Some(credentials)) => Some(credentials),

+ 9 - 1
src/main.rs

@@ -3,6 +3,7 @@ extern crate env_logger;
 extern crate futures;
 extern crate getopts;
 extern crate librespot;
+extern crate rpassword;
 extern crate tokio_core;
 extern crate tokio_io;
 extern crate tokio_signal;
@@ -177,10 +178,17 @@ 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()
+        };
+
         get_credentials(
             matches.opt_str("username"),
             matches.opt_str("password"),
-            cached_credentials
+            cached_credentials,
+            password
         )
     };