2022-11-28 16:16:33 -08:00
|
|
|
use std::collections::HashMap;
|
2022-11-27 22:03:15 -08:00
|
|
|
use std::sync::RwLock;
|
|
|
|
|
|
|
|
use serde::{Serialize, Deserialize};
|
|
|
|
use tokio::sync::oneshot::Sender;
|
|
|
|
|
|
|
|
use crate::ipc;
|
2022-11-28 16:16:33 -08:00
|
|
|
use crate::errors::*;
|
2022-11-27 22:03:15 -08:00
|
|
|
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
2022-11-28 16:16:33 -08:00
|
|
|
#[serde(rename_all = "PascalCase")]
|
|
|
|
#[serde(untagged)]
|
2022-11-27 22:03:15 -08:00
|
|
|
pub enum Credentials {
|
|
|
|
LongLived {
|
2022-11-28 16:16:33 -08:00
|
|
|
access_key_id: String,
|
|
|
|
secret_access_key: String,
|
2022-11-27 22:03:15 -08:00
|
|
|
},
|
|
|
|
ShortLived {
|
2022-11-28 16:16:33 -08:00
|
|
|
access_key_id: String,
|
|
|
|
secret_access_key: String,
|
|
|
|
token: String,
|
|
|
|
expiration: String,
|
2022-11-27 22:03:15 -08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
2022-11-28 16:16:33 -08:00
|
|
|
pub enum SessionStatus {
|
|
|
|
Unlocked,
|
2022-11-27 22:03:15 -08:00
|
|
|
Locked,
|
|
|
|
Empty,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub struct AppState {
|
2022-11-28 16:16:33 -08:00
|
|
|
status: RwLock<SessionStatus>,
|
|
|
|
credentials: RwLock<Option<Credentials>>,
|
2022-11-27 22:03:15 -08:00
|
|
|
request_count: RwLock<u64>,
|
2022-11-28 16:16:33 -08:00
|
|
|
open_requests: RwLock<HashMap<u64, Sender<ipc::Approval>>>,
|
2022-11-27 22:03:15 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AppState {
|
2022-11-28 16:16:33 -08:00
|
|
|
pub fn new(status: SessionStatus, creds: Option<Credentials>) -> Self {
|
2022-11-27 22:03:15 -08:00
|
|
|
AppState {
|
2022-11-28 16:16:33 -08:00
|
|
|
status: RwLock::new(status),
|
|
|
|
credentials: RwLock::new(creds),
|
|
|
|
request_count: RwLock::new(0),
|
|
|
|
open_requests: RwLock::new(HashMap::new()),
|
2022-11-27 22:03:15 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-28 16:16:33 -08:00
|
|
|
pub fn register_request(&self, chan: Sender<ipc::Approval>) -> u64 {
|
2022-11-27 22:03:15 -08:00
|
|
|
let count = {
|
|
|
|
let c = self.request_count.write().unwrap();
|
|
|
|
*c += 1;
|
|
|
|
c
|
|
|
|
};
|
|
|
|
|
|
|
|
let open_requests = self.open_requests.write().unwrap();
|
2022-11-28 16:16:33 -08:00
|
|
|
open_requests.insert(*count, chan);
|
|
|
|
*count
|
2022-11-27 22:03:15 -08:00
|
|
|
}
|
|
|
|
|
2022-11-28 16:16:33 -08:00
|
|
|
pub fn send_response(&self, response: ipc::RequestResponse) -> Result<(), SendResponseError> {
|
2022-11-27 22:03:15 -08:00
|
|
|
let mut open_requests = self.open_requests.write().unwrap();
|
2022-11-28 16:16:33 -08:00
|
|
|
let chan = open_requests
|
|
|
|
.remove(&response.id)
|
2022-11-27 22:03:15 -08:00
|
|
|
.ok_or(SendResponseError::NotFound)
|
|
|
|
?;
|
|
|
|
|
2022-11-28 16:16:33 -08:00
|
|
|
chan.send(response.approval)
|
2022-11-27 22:03:15 -08:00
|
|
|
.map_err(|_e| SendResponseError::Abandoned)
|
|
|
|
}
|
|
|
|
|
2022-11-28 16:16:33 -08:00
|
|
|
pub fn get_creds_serialized(&self) -> String {
|
|
|
|
let creds = self.credentials.read().unwrap();
|
|
|
|
// fix this at some point
|
|
|
|
serde_json::to_string(&creds.unwrap()).unwrap()
|
|
|
|
}
|
2022-11-27 22:03:15 -08:00
|
|
|
}
|