-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathclient.rs
More file actions
117 lines (107 loc) · 3.67 KB
/
client.rs
File metadata and controls
117 lines (107 loc) · 3.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
use async_std::stream::Stream;
use credentialsd_common::{client::FlowController, server::RequestId};
use futures_lite::StreamExt;
use zbus::{Connection, zvariant};
use crate::dbus::FlowControlServiceProxy;
pub struct DbusCredentialClient {
conn: Connection,
}
impl DbusCredentialClient {
pub fn new(conn: Connection) -> Self {
Self { conn }
}
async fn proxy(&self) -> std::result::Result<FlowControlServiceProxy, ()> {
FlowControlServiceProxy::new(&self.conn)
.await
.map_err(|err| tracing::error!("Failed to communicate with D-Bus service: {err}"))
}
}
impl FlowController for DbusCredentialClient {
async fn get_available_public_key_devices(
&self,
) -> std::result::Result<Vec<credentialsd_common::model::Device>, ()> {
let dbus_devices = self
.proxy()
.await?
.get_available_public_key_devices()
.await
.map_err(|err| {
tracing::error!("Failed to retrieve available devices/transports: {err}")
})?;
dbus_devices.into_iter().map(|d| d.try_into()).collect()
}
async fn get_hybrid_credential(&mut self) -> std::result::Result<(), ()> {
self.proxy()
.await?
.get_hybrid_credential()
.await
.inspect_err(|err| tracing::error!("Failed to start hybrid credential flow: {err}"))
.map_err(|_| ())
}
async fn get_usb_credential(&mut self) -> std::result::Result<(), ()> {
self.proxy()
.await?
.get_usb_credential()
.await
.inspect_err(|err| tracing::error!("Failed to start USB credential flow: {err}"))
.map_err(|_| ())
}
async fn initiate_event_stream(
&mut self,
) -> std::result::Result<
std::pin::Pin<
Box<dyn Stream<Item = credentialsd_common::model::BackgroundEvent> + Send + 'static>,
>,
(),
> {
let stream = self
.proxy()
.await?
.receive_state_changed()
.await
.map_err(|err| tracing::error!("Failed to initalize event stream: {err}"))?
.filter_map(|msg| {
msg.args()
.and_then(|args| {
args.update
.try_into()
.map_err(|err: zvariant::Error| err.into())
})
.inspect_err(|err| tracing::warn!("Failed to parse StateChanged signal: {err}"))
.ok()
})
.boxed();
self.proxy()
.await?
.initiate_event_stream()
.await
.map_err(|err| tracing::error!("Failed to initialize event stream: {err}"))
.map(|_| stream)
}
async fn enter_client_pin(&mut self, pin: String) -> std::result::Result<(), ()> {
self.proxy()
.await?
.enter_client_pin(pin)
.await
.map_err(|err| tracing::error!("Failed to send PIN to authenticator: {err}"))
}
async fn select_credential(&self, credential_id: String) -> std::result::Result<(), ()> {
self.proxy()
.await?
.select_credential(credential_id)
.await
.map_err(|err| tracing::error!("Failed to select credential: {err}"))
}
async fn cancel_request(&self, request_id: RequestId) -> Result<(), ()> {
if self
.proxy()
.await?
.cancel_request(request_id)
.await
.is_err()
{
tracing::warn!("Failed to cancel request {request_id}");
}
Ok(())
}
}