| 1 |
use super::Pin; |
| 2 |
use crate::{ |
| 3 |
ctap2::{ |
| 4 |
commands::{ |
| 5 |
authenticator_config::{AuthConfigCommand, AuthConfigResult}, |
| 6 |
bio_enrollment::BioTemplateId, |
| 7 |
get_info::AuthenticatorInfo, |
| 8 |
PinUvAuthResult, |
| 9 |
}, |
| 10 |
server::{PublicKeyCredentialDescriptor, PublicKeyCredentialUserEntity}, |
| 11 |
}, |
| 12 |
BioEnrollmentResult, CredentialManagementResult, |
| 13 |
}; |
| 14 |
use serde::{Deserialize, Serialize as DeriveSer, Serializer}; |
| 15 |
use std::sync::mpsc::Sender; |
| 16 |
|
| 17 |
#[derive(Debug, Deserialize, DeriveSer)] |
| 18 |
pub enum CredManagementCmd { |
| 19 |
GetCredentials, |
| 20 |
DeleteCredential(PublicKeyCredentialDescriptor), |
| 21 |
UpdateUserInformation(PublicKeyCredentialDescriptor, PublicKeyCredentialUserEntity), |
| 22 |
} |
| 23 |
|
| 24 |
#[derive(Debug, Deserialize, DeriveSer)] |
| 25 |
pub enum BioEnrollmentCmd { |
| 26 |
GetFingerprintSensorInfo, |
| 27 |
GetEnrollments, |
| 28 |
StartNewEnrollment(Option<String>), |
| 29 |
DeleteEnrollment(BioTemplateId), |
| 30 |
ChangeName(BioTemplateId, String), |
| 31 |
} |
| 32 |
|
| 33 |
#[derive(Debug)] |
| 34 |
pub enum InteractiveRequest { |
| 35 |
Quit, |
| 36 |
Reset, |
| 37 |
ChangePIN(Pin, Pin), |
| 38 |
SetPIN(Pin), |
| 39 |
ChangeConfig(AuthConfigCommand, Option<PinUvAuthResult>), |
| 40 |
CredentialManagement(CredManagementCmd, Option<PinUvAuthResult>), |
| 41 |
BioEnrollment(BioEnrollmentCmd, Option<PinUvAuthResult>), |
| 42 |
} |
| 43 |
|
| 44 |
// Simply ignoring the Sender when serializing |
| 45 |
pub(crate) fn serialize_pin_required<S>(_: &Sender<Pin>, s: S) -> Result<S::Ok, S::Error> |
| 46 |
where |
| 47 |
S: Serializer, |
| 48 |
{ |
| 49 |
s.serialize_none() |
| 50 |
} |
| 51 |
|
| 52 |
// Simply ignoring the Sender when serializing |
| 53 |
pub(crate) fn serialize_pin_invalid<S>( |
| 54 |
_: &Sender<Pin>, |
| 55 |
retries: &Option<u8>, |
| 56 |
s: S, |
| 57 |
) -> Result<S::Ok, S::Error> |
| 58 |
where |
| 59 |
S: Serializer, |
| 60 |
{ |
| 61 |
if let Some(r) = retries { |
| 62 |
s.serialize_u8(*r) |
| 63 |
} else { |
| 64 |
s.serialize_none() |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
#[derive(Debug, DeriveSer)] |
| 69 |
pub enum StatusPinUv { |
| 70 |
#[serde(serialize_with = "serialize_pin_required")] |
| 71 |
PinRequired(Sender<Pin>), |
| 72 |
#[serde(serialize_with = "serialize_pin_invalid")] |
| 73 |
InvalidPin(Sender<Pin>, Option<u8>), |
| 74 |
PinIsTooShort, |
| 75 |
PinIsTooLong(usize), |
| 76 |
InvalidUv(Option<u8>), |
| 77 |
// This SHOULD ever only happen for CTAP2.0 devices that |
| 78 |
// use internal UV (e.g. fingerprint sensors) and failed (e.g. wrong |
| 79 |
// finger used). |
| 80 |
// PinAuthInvalid, // Folded into InvalidUv |
| 81 |
PinAuthBlocked, |
| 82 |
PinBlocked, |
| 83 |
PinNotSet, |
| 84 |
UvBlocked, |
| 85 |
} |
| 86 |
|
| 87 |
#[derive(Debug)] |
| 88 |
pub enum InteractiveUpdate { |
| 89 |
StartManagement((Sender<InteractiveRequest>, Option<AuthenticatorInfo>)), |
| 90 |
// We provide the already determined PUAT to be able to issue more requests without |
| 91 |
// forcing the user to enter another PIN. |
| 92 |
BioEnrollmentUpdate((BioEnrollmentResult, Option<PinUvAuthResult>)), |
| 93 |
CredentialManagementUpdate((CredentialManagementResult, Option<PinUvAuthResult>)), |
| 94 |
AuthConfigUpdate((AuthConfigResult, Option<PinUvAuthResult>)), |
| 95 |
} |
| 96 |
|
| 97 |
#[derive(Debug)] |
| 98 |
pub enum StatusUpdate { |
| 99 |
/// We're waiting for the user to touch their token |
| 100 |
PresenceRequired, |
| 101 |
/// Sent if a PIN is needed (or was wrong), or some other kind of PIN-related |
| 102 |
/// error occurred. The Sender is for sending back a PIN (if needed). |
| 103 |
PinUvError(StatusPinUv), |
| 104 |
/// Sent, if multiple devices are found and the user has to select one |
| 105 |
SelectDeviceNotice, |
| 106 |
/// Sent when a token was selected for interactive management |
| 107 |
InteractiveManagement(InteractiveUpdate), |
| 108 |
/// Sent when a token returns multiple results for a getAssertion request |
| 109 |
SelectResultNotice(Sender<Option<usize>>, Vec<PublicKeyCredentialUserEntity>), |
| 110 |
} |
| 111 |
|
| 112 |
pub(crate) fn send_status(status: &Sender<StatusUpdate>, msg: StatusUpdate) { |
| 113 |
match status.send(msg) { |
| 114 |
Ok(_) => {} |
| 115 |
Err(e) => error!("Couldn't send status: {:?}", e), |
| 116 |
}; |
| 117 |
} |