src/bin/proxmox-backup-client.rs: implement login/logout

This commit is contained in:
Dietmar Maurer
2019-08-10 09:12:17 +02:00
parent f71e8cc96f
commit e240d8be0b
2 changed files with 77 additions and 1 deletions

View File

@ -34,7 +34,7 @@ use super::merge_known_chunks::*;
use crate::backup::*;
#[derive(Clone)]
struct AuthInfo {
pub struct AuthInfo {
username: String,
ticket: String,
token: String,
@ -47,6 +47,27 @@ pub struct HttpClient {
auth: BroadcastFuture<AuthInfo>,
}
/// Delete stored ticket data (logout)
pub fn delete_ticket_info(server: &str, username: &str) -> Result<(), Error> {
let base = BaseDirectories::with_prefix("proxmox-backup")?;
// usually /run/user/<uid>/...
let path = base.place_runtime_file("tickets")?;
let mode = nix::sys::stat::Mode::from_bits_truncate(0o0600);
let mut data = file_get_json(&path, Some(json!({})))?;
if let Some(map) = data[server].as_object_mut() {
map.remove(username);
}
file_set_contents(path, data.to_string().as_bytes(), Some(mode))?;
Ok(())
}
fn store_ticket_info(server: &str, username: &str, ticket: &str, token: &str) -> Result<(), Error> {
let base = BaseDirectories::with_prefix("proxmox-backup")?;
@ -144,6 +165,14 @@ impl HttpClient {
})
}
/// Login future
///
/// Login is done on demand, so this is onyl required if you need
/// access to authentication data in 'AuthInfo'.
pub fn login(&self) -> impl Future<Item=AuthInfo, Error=Error> {
self.auth.listen()
}
fn get_password(_username: &str) -> Result<String, Error> {
use std::env::VarError::*;
match std::env::var("PBS_PASSWORD") {