src/client/http_client.rs: request password at creation time
This commit is contained in:
parent
e45afdff9c
commit
45cdce069e
|
@ -48,7 +48,7 @@ fn run() -> Result<(), Error> {
|
||||||
|
|
||||||
let username = "root@pam";
|
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");
|
let h2client = client.h2upgrade("/api2/json/admin/datastore/store2/h2upload");
|
||||||
|
|
||||||
|
|
|
@ -194,7 +194,7 @@ fn list_backups(
|
||||||
let repo_url = tools::required_string_param(¶m, "repository")?;
|
let repo_url = tools::required_string_param(¶m, "repository")?;
|
||||||
let repo: BackupRepository = repo_url.parse()?;
|
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());
|
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(¶m, "repository")?;
|
let repo_url = tools::required_string_param(¶m, "repository")?;
|
||||||
let repo: BackupRepository = repo_url.parse()?;
|
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());
|
let path = format!("api2/json/admin/datastore/{}/groups", repo.store());
|
||||||
|
|
||||||
|
@ -301,7 +301,7 @@ fn list_snapshots(
|
||||||
"backup-id": group.backup_id(),
|
"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);
|
let path = format!("api2/json/admin/datastore/{}/snapshots?{}", repo.store(), query);
|
||||||
|
|
||||||
|
@ -350,7 +350,7 @@ fn forget_snapshots(
|
||||||
"backup-time": snapshot.backup_time().timestamp(),
|
"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);
|
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(¶m, "repository")?;
|
let repo_url = tools::required_string_param(¶m, "repository")?;
|
||||||
let repo: BackupRepository = repo_url.parse()?;
|
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());
|
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 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);
|
record_repository(&repo);
|
||||||
|
|
||||||
|
@ -500,7 +500,7 @@ fn restore(
|
||||||
|
|
||||||
let archive_name = tools::required_string_param(¶m, "archive-name")?;
|
let archive_name = tools::required_string_param(¶m, "archive-name")?;
|
||||||
|
|
||||||
let mut client = HttpClient::new(repo.host(), repo.user());
|
let mut client = HttpClient::new(repo.host(), repo.user())?;
|
||||||
|
|
||||||
record_repository(&repo);
|
record_repository(&repo);
|
||||||
|
|
||||||
|
@ -567,7 +567,7 @@ fn prune(
|
||||||
let repo_url = tools::required_string_param(¶m, "repository")?;
|
let repo_url = tools::required_string_param(¶m, "repository")?;
|
||||||
let repo: BackupRepository = repo_url.parse()?;
|
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());
|
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
|
// like get, but simply ignore errors and return Null instead
|
||||||
fn try_get(repo: &BackupRepository, url: &str) -> Value {
|
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() {
|
let mut resp = match client.get(url).wait() {
|
||||||
Ok(v) => v,
|
Ok(v) => v,
|
||||||
|
|
|
@ -110,15 +110,22 @@ fn load_ticket_info(server: &str, username: &str) -> Option<(String, String)> {
|
||||||
|
|
||||||
impl HttpClient {
|
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 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,
|
client,
|
||||||
server: String::from(server),
|
server: String::from(server),
|
||||||
auth: BroadcastFuture::new(login),
|
auth: BroadcastFuture::new(login),
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_password(_username: &str) -> Result<String, Error> {
|
fn get_password(_username: &str) -> Result<String, Error> {
|
||||||
|
@ -278,33 +285,17 @@ impl HttpClient {
|
||||||
|
|
||||||
fn credentials(
|
fn credentials(
|
||||||
client: Client<hyper_tls::HttpsConnector<hyper::client::HttpConnector>>,
|
client: Client<hyper_tls::HttpsConnector<hyper::client::HttpConnector>>,
|
||||||
server: &str,
|
server: String,
|
||||||
username: &str,
|
username: String,
|
||||||
|
password: String,
|
||||||
) -> Box<Future<Item=AuthInfo, Error=Error> + Send> {
|
) -> Box<Future<Item=AuthInfo, Error=Error> + Send> {
|
||||||
|
|
||||||
let server = server.to_owned();
|
let server2 = server.clone();
|
||||||
let server2 = server.to_owned();
|
|
||||||
let username = username.to_owned();
|
|
||||||
|
|
||||||
let create_request = futures::future::lazy(move || {
|
let create_request = futures::future::lazy(move || {
|
||||||
|
let data = json!({ "username": username, "password": password });
|
||||||
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 req = Self::request_builder(&server, "POST", "/api2/json/access/ticket", Some(data)).unwrap();
|
let req = Self::request_builder(&server, "POST", "/api2/json/access/ticket", Some(data)).unwrap();
|
||||||
|
Self::api_request(client, req)
|
||||||
futures::future::Either::B(Self::api_request(client, req))
|
|
||||||
});
|
});
|
||||||
|
|
||||||
let login_future = create_request
|
let login_future = create_request
|
||||||
|
|
Loading…
Reference in New Issue