src/client/http_client.rs: Refactor handling Option and Result types
Signed-off-by: Christian Ebner <c.ebner@proxmox.com> Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
parent
40f8680e37
commit
66c8eb9383
@ -101,44 +101,24 @@ fn store_ticket_info(server: &str, username: &str, ticket: &str, token: &str) ->
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn load_ticket_info(server: &str, username: &str) -> Option<(String, String)> {
|
fn load_ticket_info(server: &str, username: &str) -> Option<(String, String)> {
|
||||||
let base = match BaseDirectories::with_prefix("proxmox-backup") {
|
let base = BaseDirectories::with_prefix("proxmox-backup").ok()?;
|
||||||
Ok(b) => b,
|
|
||||||
_ => return None,
|
|
||||||
};
|
|
||||||
|
|
||||||
// usually /run/user/<uid>/...
|
// usually /run/user/<uid>/...
|
||||||
let path = match base.place_runtime_file("tickets") {
|
let path = base.place_runtime_file("tickets").ok()?;
|
||||||
Ok(p) => p,
|
let data = file_get_json(&path, None).ok()?;
|
||||||
_ => return None,
|
|
||||||
};
|
|
||||||
|
|
||||||
let data = match file_get_json(&path, None) {
|
|
||||||
Ok(v) => v,
|
|
||||||
_ => return None,
|
|
||||||
};
|
|
||||||
|
|
||||||
let now = Utc::now().timestamp();
|
let now = Utc::now().timestamp();
|
||||||
|
|
||||||
let ticket_lifetime = tools::ticket::TICKET_LIFETIME - 60;
|
let ticket_lifetime = tools::ticket::TICKET_LIFETIME - 60;
|
||||||
|
let uinfo = data[server][username].as_object()?;
|
||||||
|
let timestamp = uinfo["timestamp"].as_i64()?;
|
||||||
|
let age = now - timestamp;
|
||||||
|
|
||||||
if let Some(uinfo) = data[server][username].as_object() {
|
if age < ticket_lifetime {
|
||||||
if let Some(timestamp) = uinfo["timestamp"].as_i64() {
|
let ticket = uinfo["ticket"].as_str()?;
|
||||||
let age = now - timestamp;
|
let token = uinfo["token"].as_str()?;
|
||||||
if age < ticket_lifetime {
|
Some((ticket.to_owned(), token.to_owned()))
|
||||||
let ticket = match uinfo["ticket"].as_str() {
|
} else {
|
||||||
Some(t) => t,
|
None
|
||||||
None => return None,
|
|
||||||
};
|
|
||||||
let token = match uinfo["token"].as_str() {
|
|
||||||
Some(t) => t,
|
|
||||||
None => return None,
|
|
||||||
};
|
|
||||||
return Some((ticket.to_owned(), token.to_owned()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
None
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl HttpClient {
|
impl HttpClient {
|
||||||
|
Loading…
Reference in New Issue
Block a user