@@ -1,11 +1,28 @@ |
| 1 | 1 | //! IPC adapter for garcard (authentication agent) |
| 2 | 2 | |
| 3 | | -use crate::ipc::client::IpcClient; |
| 3 | +use crate::ipc::client::{IpcClient, StandardResponse}; |
| 4 | 4 | use crate::ipc::discovery::socket_path_for; |
| 5 | 5 | use crate::panels::Component; |
| 6 | 6 | use anyhow::Result; |
| 7 | +use serde::Serialize; |
| 8 | +use serde_json::Value; |
| 7 | 9 | use std::path::PathBuf; |
| 8 | 10 | |
| 11 | +/// Commands for garcard daemon. |
| 12 | +#[derive(Debug, Serialize)] |
| 13 | +#[serde(tag = "command", rename_all = "snake_case")] |
| 14 | +enum GarcardCommand { |
| 15 | + Ping, |
| 16 | + Status, |
| 17 | + Diagnose, |
| 18 | + Version, |
| 19 | + AuthSummary, |
| 20 | + TempList, |
| 21 | + TempRevoke { authorization_id: String }, |
| 22 | + TempRevokeAll, |
| 23 | + Quit, |
| 24 | +} |
| 25 | + |
| 9 | 26 | /// Adapter for garcard authentication agent |
| 10 | 27 | pub struct GarcardAdapter { |
| 11 | 28 | client: IpcClient, |
@@ -49,6 +66,49 @@ impl GarcardAdapter { |
| 49 | 66 | pub fn is_connected(&self) -> bool { |
| 50 | 67 | self.client.is_connected() |
| 51 | 68 | } |
| 69 | + |
| 70 | + fn send_command(&mut self, command: GarcardCommand) -> Result<Option<Value>> { |
| 71 | + let response: StandardResponse = self.client.send_receive(&command)?; |
| 72 | + response.into_result() |
| 73 | + } |
| 74 | + |
| 75 | + pub fn ping(&mut self) -> Result<Option<Value>> { |
| 76 | + self.send_command(GarcardCommand::Ping) |
| 77 | + } |
| 78 | + |
| 79 | + pub fn status(&mut self) -> Result<Option<Value>> { |
| 80 | + self.send_command(GarcardCommand::Status) |
| 81 | + } |
| 82 | + |
| 83 | + pub fn diagnose(&mut self) -> Result<Option<Value>> { |
| 84 | + self.send_command(GarcardCommand::Diagnose) |
| 85 | + } |
| 86 | + |
| 87 | + pub fn version(&mut self) -> Result<Option<Value>> { |
| 88 | + self.send_command(GarcardCommand::Version) |
| 89 | + } |
| 90 | + |
| 91 | + pub fn auth_summary(&mut self) -> Result<Option<Value>> { |
| 92 | + self.send_command(GarcardCommand::AuthSummary) |
| 93 | + } |
| 94 | + |
| 95 | + pub fn temp_list(&mut self) -> Result<Option<Value>> { |
| 96 | + self.send_command(GarcardCommand::TempList) |
| 97 | + } |
| 98 | + |
| 99 | + pub fn temp_revoke(&mut self, authorization_id: impl Into<String>) -> Result<Option<Value>> { |
| 100 | + self.send_command(GarcardCommand::TempRevoke { |
| 101 | + authorization_id: authorization_id.into(), |
| 102 | + }) |
| 103 | + } |
| 104 | + |
| 105 | + pub fn temp_revoke_all(&mut self) -> Result<Option<Value>> { |
| 106 | + self.send_command(GarcardCommand::TempRevokeAll) |
| 107 | + } |
| 108 | + |
| 109 | + pub fn quit(&mut self) -> Result<Option<Value>> { |
| 110 | + self.send_command(GarcardCommand::Quit) |
| 111 | + } |
| 52 | 112 | } |
| 53 | 113 | |
| 54 | 114 | impl Default for GarcardAdapter { |