src/client/http_client.rs: request password at creation time

This commit is contained in:
Dietmar Maurer 2019-04-30 11:44:35 +02:00
parent e45afdff9c
commit 45cdce069e
3 changed files with 30 additions and 36 deletions

View File

@ -48,7 +48,7 @@ fn run() -> Result<(), Error> {
let username = "root@pam";
let mut client = HttpClient::new(host, username);
let mut client = HttpClient::new(host, username)?;
let h2client = client.h2upgrade("/api2/json/admin/datastore/store2/h2upload");

View File

@ -194,7 +194,7 @@ fn list_backups(
let repo_url = tools::required_string_param(&param, "repository")?;
let repo: BackupRepository = repo_url.parse()?;
let mut client = HttpClient::new(repo.host(), repo.user());
let mut client = HttpClient::new(repo.host(), repo.user())?;
let path = format!("api2/json/admin/datastore/{}/backups", repo.store());
@ -236,7 +236,7 @@ fn list_backup_groups(
let repo_url = tools::required_string_param(&param, "repository")?;
let repo: BackupRepository = repo_url.parse()?;
let client = HttpClient::new(repo.host(), repo.user());
let client = HttpClient::new(repo.host(), repo.user())?;
let path = format!("api2/json/admin/datastore/{}/groups", repo.store());
@ -301,7 +301,7 @@ fn list_snapshots(
"backup-id": group.backup_id(),
}))?;
let client = HttpClient::new(repo.host(), repo.user());
let client = HttpClient::new(repo.host(), repo.user())?;
let path = format!("api2/json/admin/datastore/{}/snapshots?{}", repo.store(), query);
@ -350,7 +350,7 @@ fn forget_snapshots(
"backup-time": snapshot.backup_time().timestamp(),
}))?;
let mut client = HttpClient::new(repo.host(), repo.user());
let mut client = HttpClient::new(repo.host(), repo.user())?;
let path = format!("api2/json/admin/datastore/{}/snapshots?{}", repo.store(), query);
@ -370,7 +370,7 @@ fn start_garbage_collection(
let repo_url = tools::required_string_param(&param, "repository")?;
let repo: BackupRepository = repo_url.parse()?;
let mut client = HttpClient::new(repo.host(), repo.user());
let mut client = HttpClient::new(repo.host(), repo.user())?;
let path = format!("api2/json/admin/datastore/{}/gc", repo.store());
@ -445,7 +445,7 @@ fn create_backup(
let backup_time = Local.timestamp(Local::now().timestamp(), 0);
let mut client = HttpClient::new(repo.host(), repo.user());
let mut client = HttpClient::new(repo.host(), repo.user())?;
record_repository(&repo);
@ -500,7 +500,7 @@ fn restore(
let archive_name = tools::required_string_param(&param, "archive-name")?;
let mut client = HttpClient::new(repo.host(), repo.user());
let mut client = HttpClient::new(repo.host(), repo.user())?;
record_repository(&repo);
@ -567,7 +567,7 @@ fn prune(
let repo_url = tools::required_string_param(&param, "repository")?;
let repo: BackupRepository = repo_url.parse()?;
let mut client = HttpClient::new(repo.host(), repo.user());
let mut client = HttpClient::new(repo.host(), repo.user())?;
let path = format!("api2/json/admin/datastore/{}/prune", repo.store());
@ -583,7 +583,10 @@ fn prune(
// like get, but simply ignore errors and return Null instead
fn try_get(repo: &BackupRepository, url: &str) -> Value {
let client = HttpClient::new(repo.host(), repo.user());
let client = match HttpClient::new(repo.host(), repo.user()) {
Ok(v) => v,
_ => return Value::Null,
};
let mut resp = match client.get(url).wait() {
Ok(v) => v,

View File

@ -110,15 +110,22 @@ fn load_ticket_info(server: &str, username: &str) -> Option<(String, String)> {
impl HttpClient {
pub fn new(server: &str, username: &str) -> Self {
pub fn new(server: &str, username: &str) -> Result<Self, Error> {
let client = Self::build_client();
let login = Self::credentials(client.clone(), server, username);
Self {
let password = if let Some((ticket, _token)) = load_ticket_info(server, username) {
ticket
} else {
Self::get_password(&username)?
};
let login = Self::credentials(client.clone(), server.to_owned(), username.to_owned(), password);
Ok(Self {
client,
server: String::from(server),
auth: BroadcastFuture::new(login),
}
})
}
fn get_password(_username: &str) -> Result<String, Error> {
@ -278,33 +285,17 @@ impl HttpClient {
fn credentials(
client: Client<hyper_tls::HttpsConnector<hyper::client::HttpConnector>>,
server: &str,
username: &str,
server: String,
username: String,
password: String,
) -> Box<Future<Item=AuthInfo, Error=Error> + Send> {
let server = server.to_owned();
let server2 = server.to_owned();
let username = username.to_owned();
let server2 = server.clone();
let create_request = futures::future::lazy(move || {
let data = if let Some((ticket, _token)) = load_ticket_info(&server, &username) {
json!({ "username": username, "password": ticket })
} else {
let password = match Self::get_password(&username) {
Ok(p) => p,
Err(err) => {
return futures::future::Either::A(futures::future::err(err));
}
};
json!({ "username": username, "password": password })
};
let data = json!({ "username": username, "password": password });
let req = Self::request_builder(&server, "POST", "/api2/json/access/ticket", Some(data)).unwrap();
futures::future::Either::B(Self::api_request(client, req))
Self::api_request(client, req)
});
let login_future = create_request