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,45 +101,25 @@ 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()?;
|
||||||
if let Some(uinfo) = data[server][username].as_object() {
|
let timestamp = uinfo["timestamp"].as_i64()?;
|
||||||
if let Some(timestamp) = uinfo["timestamp"].as_i64() {
|
|
||||||
let age = now - timestamp;
|
let age = now - timestamp;
|
||||||
if age < ticket_lifetime {
|
|
||||||
let ticket = match uinfo["ticket"].as_str() {
|
|
||||||
Some(t) => t,
|
|
||||||
None => return None,
|
|
||||||
};
|
|
||||||
let token = match uinfo["token"].as_str() {
|
|
||||||
Some(t) => t,
|
|
||||||
None => return None,
|
|
||||||
};
|
|
||||||
return Some((ticket.to_owned(), token.to_owned()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if age < ticket_lifetime {
|
||||||
|
let ticket = uinfo["ticket"].as_str()?;
|
||||||
|
let token = uinfo["token"].as_str()?;
|
||||||
|
Some((ticket.to_owned(), token.to_owned()))
|
||||||
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl HttpClient {
|
impl HttpClient {
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user