-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathserver.rs
More file actions
636 lines (586 loc) · 23.2 KB
/
Copy pathserver.rs
File metadata and controls
636 lines (586 loc) · 23.2 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
//! Types for serializing across D-Bus instances
use std::collections::HashMap;
use serde::{Deserialize, Serialize};
use zvariant::{self, DeserializeDict, LE, Optional, OwnedValue, SerializeDict, Type, Value};
use crate::model::Operation;
#[derive(Clone, Debug, Serialize, Deserialize, Type)]
pub enum BackgroundEvent {
UsbStateChanged(OwnedValue),
HybridStateChanged(OwnedValue),
}
impl TryFrom<BackgroundEvent> for crate::model::BackgroundEvent {
type Error = zvariant::Error;
fn try_from(value: BackgroundEvent) -> Result<Self, Self::Error> {
let ret = match value {
BackgroundEvent::HybridStateChanged(hybrid_state_val) => {
HybridState::try_from(Value::<'_>::from(hybrid_state_val))
.and_then(crate::model::HybridState::try_from)
.map(crate::model::BackgroundEvent::HybridQrStateChanged)
}
BackgroundEvent::UsbStateChanged(usb_state_val) => {
UsbState::try_from(Value::<'_>::from(usb_state_val))
.and_then(crate::model::UsbState::try_from)
.map(crate::model::BackgroundEvent::UsbStateChanged)
}
}?;
Ok(ret)
}
}
impl From<crate::model::BackgroundEvent> for BackgroundEvent {
fn from(value: crate::model::BackgroundEvent) -> Self {
match value {
crate::model::BackgroundEvent::HybridQrStateChanged(state) => {
let state: HybridState = state.into();
let value = Value::new(state)
.try_to_owned()
.expect("non-file descriptor value to succeed");
BackgroundEvent::HybridStateChanged(value)
}
crate::model::BackgroundEvent::UsbStateChanged(state) => {
let state: UsbState = state.into();
let value = Value::new(state)
.try_to_owned()
.expect("non-file descriptor value to succeed");
BackgroundEvent::UsbStateChanged(value)
}
}
}
}
#[derive(Clone, Debug, DeserializeDict, Type)]
#[zvariant(signature = "dict")]
pub struct CreateCredentialRequest {
pub origin: Option<String>,
pub is_same_origin: Option<bool>,
#[zvariant(rename = "type")]
pub r#type: String,
#[zvariant(rename = "publicKey")]
pub public_key: Option<CreatePublicKeyCredentialRequest>,
}
#[derive(SerializeDict, Type)]
#[zvariant(signature = "dict")]
pub struct CreateCredentialResponse {
#[zvariant(rename = "type")]
r#type: String,
public_key: Option<CreatePublicKeyCredentialResponse>,
}
#[derive(Clone, Debug, DeserializeDict, Type)]
#[zvariant(signature = "dict")]
pub struct CreatePublicKeyCredentialRequest {
pub request_json: String,
}
#[derive(SerializeDict, Type)]
#[zvariant(signature = "dict")]
pub struct CreatePublicKeyCredentialResponse {
pub registration_response_json: String,
}
impl From<CreatePublicKeyCredentialResponse> for CreateCredentialResponse {
fn from(response: CreatePublicKeyCredentialResponse) -> Self {
CreateCredentialResponse {
// TODO: Decide on camelCase or kebab-case for cred types
r#type: "public-key".to_string(),
public_key: Some(response),
}
}
}
#[derive(SerializeDict, DeserializeDict, Type, Value)]
#[zvariant(signature = "dict")]
pub struct Credential {
id: String,
name: String,
username: Optional<String>,
}
impl From<Credential> for crate::model::Credential {
fn from(value: Credential) -> Self {
Self {
id: value.id,
name: value.name,
username: value.username.into(),
}
}
}
impl From<crate::model::Credential> for Credential {
fn from(value: crate::model::Credential) -> Self {
Self {
id: value.id,
name: value.name,
username: value.username.into(),
}
}
}
#[derive(SerializeDict, DeserializeDict, Type)]
#[zvariant(signature = "a{sv}")]
pub struct Device {
pub id: String,
pub transport: String,
}
impl TryFrom<Value<'_>> for Device {
type Error = zvariant::Error;
fn try_from(value: Value<'_>) -> std::result::Result<Self, Self::Error> {
let ctx = zvariant::serialized::Context::new_dbus(LE, 0);
let encoded = zvariant::to_bytes(ctx, &value)?;
let device: Device = encoded.deserialize()?.0;
Ok(device)
}
}
impl From<crate::model::Device> for Device {
fn from(value: crate::model::Device) -> Self {
Device {
id: value.id,
transport: value.transport.as_str().to_owned(),
}
}
}
impl TryFrom<Device> for crate::model::Device {
type Error = ();
fn try_from(value: Device) -> std::result::Result<Self, Self::Error> {
let transport = value.transport.try_into().map_err(|_| ())?;
Ok(Self {
id: value.id,
transport,
})
}
}
#[derive(Clone, Debug, DeserializeDict, Type)]
#[zvariant(signature = "dict")]
pub struct GetCredentialRequest {
pub origin: Option<String>,
pub is_same_origin: Option<bool>,
#[zvariant(rename = "type")]
pub r#type: String,
#[zvariant(rename = "publicKey")]
pub public_key: Option<GetPublicKeyCredentialRequest>,
}
#[derive(Clone, Debug, DeserializeDict, Type)]
#[zvariant(signature = "dict")]
pub struct GetPublicKeyCredentialRequest {
pub request_json: String,
}
#[derive(SerializeDict, Type)]
#[zvariant(signature = "dict")]
pub struct GetCredentialResponse {
#[zvariant(rename = "type")]
r#type: String,
public_key: Option<GetPublicKeyCredentialResponse>,
}
#[derive(SerializeDict, Type)]
#[zvariant(signature = "dict")]
pub struct GetPublicKeyCredentialResponse {
pub authentication_response_json: String,
}
impl From<GetPublicKeyCredentialResponse> for GetCredentialResponse {
fn from(response: GetPublicKeyCredentialResponse) -> Self {
GetCredentialResponse {
// TODO: Decide on camelCase or kebab-case for cred types
r#type: "public-key".to_string(),
public_key: Some(response),
}
}
}
#[derive(Clone, Debug, Serialize, Deserialize, Type)]
pub enum HybridState {
/// Default state, not listening for hybrid transport.
Idle(OwnedValue),
/// QR code flow is starting, awaiting QR code scan and BLE advert from phone.
Started(OwnedValue),
/// BLE advert received, connecting to caBLE tunnel with shared secret.
Connecting(OwnedValue),
/// Connected to device via caBLE tunnel.
Connected(OwnedValue),
/// Credential received over tunnel.
Completed(OwnedValue),
// This isn't actually sent from the server.
UserCancelled(OwnedValue),
/// Failed to receive a credential
Failed(OwnedValue),
}
impl From<crate::model::HybridState> for HybridState {
fn from(value: crate::model::HybridState) -> Self {
match value {
crate::model::HybridState::Idle => HybridState::Idle(OwnedValue::from(false)),
crate::model::HybridState::Started(qr_code) => {
HybridState::Started(value_to_owned(&Value::from(qr_code)))
}
crate::model::HybridState::Connecting => {
HybridState::Connecting(OwnedValue::from(false))
}
crate::model::HybridState::Connected => HybridState::Connected(OwnedValue::from(false)),
crate::model::HybridState::Completed => HybridState::Completed(OwnedValue::from(false)),
crate::model::HybridState::UserCancelled => {
HybridState::UserCancelled(OwnedValue::from(false))
}
crate::model::HybridState::Failed => HybridState::Failed(OwnedValue::from(false)),
}
}
}
impl TryFrom<HybridState> for crate::model::HybridState {
type Error = zvariant::Error;
fn try_from(value: HybridState) -> std::result::Result<Self, Self::Error> {
match value {
HybridState::Idle(_) => Ok(Self::Idle),
HybridState::Started(value) => value.try_into().map(Self::Started),
HybridState::Connecting(_) => Ok(Self::Connecting),
HybridState::Connected(_) => Ok(Self::Connected),
HybridState::Completed(_) => Ok(Self::Completed),
HybridState::UserCancelled(_) => Ok(Self::UserCancelled),
HybridState::Failed(_) => Ok(Self::Failed),
}
}
}
impl TryFrom<Value<'_>> for HybridState {
type Error = zvariant::Error;
fn try_from(value: Value<'_>) -> std::result::Result<Self, Self::Error> {
let fields: HashMap<String, Value<'_>> = value.try_into()?;
let tag = fields
.get("type")
.ok_or(zvariant::Error::Message(
"Expected a dictionary with `type` key".to_string(),
))
.and_then(|t| t.try_into())?;
let value = fields.get("value").ok_or(zvariant::Error::Message(
"Expected a dictionary with `value` key".to_string(),
))?;
match tag {
"IDLE" => Ok(Self::Idle(value_to_owned(value))),
"STARTED" => Ok(Self::Started(value_to_owned(value))),
"CONNECTING" => Ok(Self::Connecting(value_to_owned(value))),
"CONNECTED" => Ok(Self::Connected(value_to_owned(value))),
"COMPLETED" => Ok(Self::Completed(value_to_owned(value))),
"USER_CANCELLED" => Ok(Self::Completed(value_to_owned(value))),
"FAILED" => Ok(Self::Failed(value_to_owned(value))),
_ => Err(zvariant::Error::Message(format!(
"Invalid HybridState type passed: {tag}"
))),
}
}
}
impl From<HybridState> for Value<'_> {
fn from(value: HybridState) -> Self {
let mut fields = HashMap::new();
match value {
HybridState::Idle(owned_value) => {
fields.insert("type", value_to_owned(&Value::from("IDLE")));
fields.insert("value", owned_value);
}
HybridState::Started(owned_value) => {
fields.insert("type", value_to_owned(&Value::from("STARTED")));
fields.insert("value", owned_value);
}
HybridState::Connecting(owned_value) => {
fields.insert("type", value_to_owned(&Value::from("CONNECTING")));
fields.insert("value", owned_value);
}
HybridState::Connected(owned_value) => {
fields.insert("type", value_to_owned(&Value::from("CONNECTED")));
fields.insert("value", owned_value);
}
HybridState::Completed(owned_value) => {
fields.insert("type", value_to_owned(&Value::from("COMPLETED")));
fields.insert("value", owned_value);
}
HybridState::UserCancelled(owned_value) => {
fields.insert("type", value_to_owned(&Value::from("USER_CANCELLED")));
fields.insert("value", owned_value);
}
HybridState::Failed(owned_value) => {
fields.insert("type", value_to_owned(&Value::from("FAILED")));
fields.insert("value", owned_value);
}
}
Value::from(fields)
}
}
/// Identifier for a request to be used for cancellation.
pub type RequestId = u32;
#[derive(Serialize, Deserialize, Type)]
pub enum ServiceError {
/// Some unknown error with the authenticator occurred.
AuthenticatorError,
/// No matching credentials were found on the device.
NoCredentials,
/// Too many incorrect PIN attempts, and authenticator must be removed and
/// reinserted to continue any more PIN attempts.
///
/// Note that this is different than exhausting the PIN count that fully
/// locks out the device.
PinAttemptsExhausted,
// TODO: We may want to hide the details on this variant from the public API.
/// Something went wrong with the credential service itself, not the authenticator.
Internal,
}
impl TryFrom<Value<'_>> for ServiceError {
type Error = zvariant::Error;
fn try_from(value: Value<'_>) -> std::result::Result<Self, Self::Error> {
let ctx = zvariant::serialized::Context::new_dbus(LE, 0);
let encoded = zvariant::to_bytes(ctx, &value)?;
let obj: Self = encoded.deserialize()?.0;
Ok(obj)
}
}
impl From<ServiceError> for crate::model::Error {
fn from(value: ServiceError) -> Self {
match value {
ServiceError::AuthenticatorError => Self::AuthenticatorError,
ServiceError::NoCredentials => Self::NoCredentials,
ServiceError::PinAttemptsExhausted => Self::PinAttemptsExhausted,
// TODO: this is bogus, we should refactor to remove the tuple field
// and let the client decide how to render the error.
ServiceError::Internal => {
Self::Internal("Something went wrong. Please try again later.".to_string())
}
}
}
}
/// Used to de-/serialize state D-Bus and model::UsbState.
#[derive(Serialize, Deserialize, Type)]
pub enum UsbState {
Idle(OwnedValue),
Waiting(OwnedValue),
SelectingDevice(OwnedValue),
Connected(OwnedValue),
NeedsPin(OwnedValue), /* {
attempts_left: Option<u32>,
},
*/
NeedsUserVerification(OwnedValue), /* {
attempts_left: Option<u32>,
},*/
NeedsUserPresence(OwnedValue),
//UserCancelled,
SelectCredential(OwnedValue), /* {
creds: Vec<Credential>,
},*/
Completed(OwnedValue),
// Failed(crate::credential_service::Error),
Failed(OwnedValue),
}
impl TryFrom<UsbState> for crate::model::UsbState {
type Error = zvariant::Error;
fn try_from(value: UsbState) -> std::result::Result<Self, Self::Error> {
let ret = match value {
UsbState::Idle(_) => Ok(Self::Idle),
UsbState::Waiting(_) => Ok(Self::Waiting),
UsbState::SelectingDevice(_) => Ok(Self::SelectingDevice),
UsbState::Connected(_) => Ok(Self::Connected),
UsbState::NeedsPin(value) => value.try_into().map(|attempts_left: i32| {
let attempts_left = if attempts_left < 0 {
None
} else {
Some(u32::try_from(attempts_left).unwrap())
};
Self::NeedsPin { attempts_left }
}),
UsbState::NeedsUserVerification(value) => value.try_into().map(|attempts_left: i32| {
let attempts_left = if attempts_left < 0 {
None
} else {
Some(u32::try_from(attempts_left).unwrap())
};
Self::NeedsUserVerification { attempts_left }
}),
UsbState::NeedsUserPresence(_) => Ok(Self::NeedsUserPresence),
UsbState::SelectCredential(value) => value
.try_into()
.map(|creds: Vec<Credential>| {
creds
.into_iter()
.map(crate::model::Credential::from)
.collect()
})
.map(|creds| Self::SelectCredential { creds }),
UsbState::Completed(_) => Ok(Self::Completed),
UsbState::Failed(value) => {
let error_code: String = Value::<'_>::from(value).try_into()?;
Ok(Self::Failed(
match error_code.as_ref() {
"AuthenticatorError" => ServiceError::AuthenticatorError,
"NoCredentials" => ServiceError::NoCredentials,
"PinAttemptsExhausted" => ServiceError::PinAttemptsExhausted,
_ => ServiceError::Internal,
}
.into(),
))
}
}?;
Ok(ret)
}
}
impl From<crate::model::UsbState> for UsbState {
fn from(value: crate::model::UsbState) -> Self {
match value {
crate::model::UsbState::Idle => UsbState::Idle(OwnedValue::from(false)),
crate::model::UsbState::Waiting => UsbState::Waiting(OwnedValue::from(false)),
crate::model::UsbState::SelectingDevice => {
UsbState::SelectingDevice(OwnedValue::from(false))
}
crate::model::UsbState::Connected => UsbState::Connected(OwnedValue::from(false)),
crate::model::UsbState::NeedsPin { attempts_left } => {
let num = match attempts_left {
Some(num) => num as i32,
None => -1,
};
UsbState::NeedsPin(OwnedValue::from(num))
}
crate::model::UsbState::NeedsUserVerification { attempts_left } => {
let num = match attempts_left {
Some(num) => num as i32,
None => -1,
};
UsbState::NeedsPin(OwnedValue::from(num))
}
crate::model::UsbState::NeedsUserPresence => {
UsbState::NeedsUserPresence(OwnedValue::from(false))
}
crate::model::UsbState::SelectCredential { creds } => {
let creds: Vec<Credential> = creds.into_iter().map(Credential::from).collect();
let value = Value::new(creds)
.try_to_owned()
.expect("All non-file descriptors to convert to OwnedValue successfully");
UsbState::SelectCredential(value)
}
crate::model::UsbState::Completed => UsbState::Completed(OwnedValue::from(false)),
crate::model::UsbState::Failed(error) => UsbState::Failed(
Value::<'_>::from(error.to_string())
.try_to_owned()
.expect("non-file descriptor value to convert"),
),
}
}
}
impl TryFrom<Value<'_>> for UsbState {
type Error = zvariant::Error;
fn try_from(value: Value<'_>) -> std::result::Result<Self, Self::Error> {
let fields: HashMap<String, Value<'_>> = value.try_into()?;
let tag = fields
.get("type")
.ok_or(zvariant::Error::Message(
"Expected a dictionary with `type` key".to_string(),
))
.and_then(|t| t.try_into())?;
let value = fields.get("value").ok_or(zvariant::Error::Message(
"Expected a dictionary with `value` key".to_string(),
))?;
match tag {
"IDLE" => Ok(Self::Idle(value_to_owned(value))),
"WAITING" => Ok(Self::Waiting(value_to_owned(value))),
"SELECTING_DEVICE" => Ok(Self::SelectingDevice(value_to_owned(value))),
"CONNECTED" => Ok(Self::SelectingDevice(value_to_owned(value))),
"NEEDS_PIN" => Ok(Self::NeedsPin(value_to_owned(value))),
"NEEDS_USER_VERIFICATION" => Ok(Self::NeedsUserVerification(value_to_owned(value))),
"NEEDS_USER_PRESENCE" => Ok(Self::NeedsUserPresence(value_to_owned(value))),
"SELECT_CREDENTIAL" => Ok(Self::SelectCredential(value_to_owned(value))),
"COMPLETED" => Ok(Self::Completed(value_to_owned(value))),
"FAILED" => Ok(Self::Failed(value_to_owned(value))),
_ => Err(zvariant::Error::Message(format!(
"Invalid UsbState type passed: {tag}"
))),
}
}
}
impl From<UsbState> for Value<'_> {
fn from(value: UsbState) -> Self {
let mut fields = HashMap::new();
match value {
UsbState::Idle(owned_value) => {
fields.insert(
"type",
Value::from("IDLE")
.try_to_owned()
.expect("non-file descriptor fields to succeed"),
);
fields.insert("value", owned_value);
}
UsbState::Waiting(owned_value) => {
fields.insert(
"type",
Value::from("WAITING")
.try_to_owned()
.expect("non-file descriptor fields to succeed"),
);
fields.insert("value", owned_value);
}
UsbState::SelectingDevice(owned_value) => {
fields.insert(
"type",
Value::from("SELECTING_DEVICE")
.try_to_owned()
.expect("non-file descriptor fields to succeed"),
);
fields.insert("value", owned_value);
}
UsbState::Connected(owned_value) => {
fields.insert(
"type",
Value::from("CONNECTED")
.try_to_owned()
.expect("non-file descriptor fields to succeed"),
);
fields.insert("value", owned_value);
}
UsbState::NeedsPin(owned_value) => {
fields.insert(
"type",
Value::from("NEEDS_PIN")
.try_to_owned()
.expect("non-file descriptor fields to succeed"),
);
fields.insert("value", owned_value);
}
UsbState::NeedsUserVerification(owned_value) => {
fields.insert(
"type",
Value::from("NEEDS_USER_VERIFICATION")
.try_to_owned()
.expect("non-file descriptor fields to succeed"),
);
fields.insert("value", owned_value);
}
UsbState::NeedsUserPresence(owned_value) => {
fields.insert(
"type",
Value::from("NEEDS_USER_PRESENCE")
.try_to_owned()
.expect("non-file descriptor fields to succeed"),
);
fields.insert("value", owned_value);
}
UsbState::SelectCredential(owned_value) => {
fields.insert(
"type",
Value::from("SELECT_CREDENTIAL")
.try_to_owned()
.expect("non-file descriptor fields to succeed"),
);
fields.insert("value", owned_value);
}
UsbState::Completed(owned_value) => {
fields.insert(
"type",
Value::from("COMPLETED")
.try_to_owned()
.expect("non-file descriptor fields to succeed"),
);
fields.insert("value", owned_value);
}
UsbState::Failed(owned_value) => {
fields.insert(
"type",
Value::from("FAILED")
.try_to_owned()
.expect("non-file descriptor fields to succeed"),
);
fields.insert("value", owned_value);
}
};
Value::from(fields)
}
}
#[derive(Serialize, Deserialize, Type)]
pub struct ViewRequest {
pub operation: Operation,
pub id: RequestId,
}
fn value_to_owned(value: &Value<'_>) -> OwnedValue {
value
.try_to_owned()
.expect("non-file descriptor values to succeed")
}