adding cbor::Value::from for i64-like enums

This commit is contained in:
Fabian Kaczmarczyck
2020-06-05 09:48:03 +02:00
parent 6a44d3349c
commit b4003e36bf
2 changed files with 21 additions and 9 deletions

View File

@@ -155,7 +155,7 @@ impl From<PublicKeyCredentialParameter> for cbor::Value {
fn from(cred_param: PublicKeyCredentialParameter) -> Self { fn from(cred_param: PublicKeyCredentialParameter) -> Self {
cbor_map_options! { cbor_map_options! {
"type" => cred_param.cred_type, "type" => cred_param.cred_type,
"alg" => cred_param.alg as i64, "alg" => cred_param.alg,
} }
} }
} }
@@ -429,6 +429,12 @@ pub enum SignatureAlgorithm {
Unknown = 0, Unknown = 0,
} }
impl From<SignatureAlgorithm> for cbor::Value {
fn from(alg: SignatureAlgorithm) -> Self {
(alg as i64).into()
}
}
impl TryFrom<&cbor::Value> for SignatureAlgorithm { impl TryFrom<&cbor::Value> for SignatureAlgorithm {
type Error = Ctap2StatusCode; type Error = Ctap2StatusCode;
@@ -448,6 +454,12 @@ pub enum CredentialProtectionPolicy {
UserVerificationRequired = 0x03, UserVerificationRequired = 0x03,
} }
impl From<CredentialProtectionPolicy> for cbor::Value {
fn from(policy: CredentialProtectionPolicy) -> Self {
(policy as i64).into()
}
}
impl TryFrom<cbor::Value> for CredentialProtectionPolicy { impl TryFrom<cbor::Value> for CredentialProtectionPolicy {
type Error = Ctap2StatusCode; type Error = Ctap2StatusCode;
@@ -526,7 +538,7 @@ impl From<PublicKeyCredentialSource> for cbor::Value {
UserHandle => Some(credential.user_handle), UserHandle => Some(credential.user_handle),
OtherUi => credential.other_ui, OtherUi => credential.other_ui,
CredRandom => credential.cred_random, CredRandom => credential.cred_random,
CredProtectPolicy => credential.cred_protect_policy.map(|p| p as i64), CredProtectPolicy => credential.cred_protect_policy,
} }
} }
} }
@@ -1137,7 +1149,7 @@ mod test {
let signature_algorithm = SignatureAlgorithm::try_from(&cbor_signature_algorithm); let signature_algorithm = SignatureAlgorithm::try_from(&cbor_signature_algorithm);
let expected_signature_algorithm = SignatureAlgorithm::ES256; let expected_signature_algorithm = SignatureAlgorithm::ES256;
assert_eq!(signature_algorithm, Ok(expected_signature_algorithm)); assert_eq!(signature_algorithm, Ok(expected_signature_algorithm));
let created_cbor: cbor::Value = cbor_int!(signature_algorithm.unwrap() as i64); let created_cbor = cbor::Value::from(signature_algorithm.unwrap());
assert_eq!(created_cbor, cbor_signature_algorithm); assert_eq!(created_cbor, cbor_signature_algorithm);
let cbor_unknown_algorithm = cbor_int!(-1); let cbor_unknown_algorithm = cbor_int!(-1);
@@ -1164,11 +1176,11 @@ mod test {
#[test] #[test]
fn test_from_into_cred_protection_policy() { fn test_from_into_cred_protection_policy() {
let cbor_policy = cbor_int!(CredentialProtectionPolicy::UserVerificationOptional as i64); let cbor_policy = cbor::Value::from(CredentialProtectionPolicy::UserVerificationOptional);
let policy = CredentialProtectionPolicy::try_from(&cbor_policy); let policy = CredentialProtectionPolicy::try_from(&cbor_policy);
let expected_policy = CredentialProtectionPolicy::UserVerificationOptional; let expected_policy = CredentialProtectionPolicy::UserVerificationOptional;
assert_eq!(policy, Ok(expected_policy)); assert_eq!(policy, Ok(expected_policy));
let created_cbor: cbor::Value = cbor_int!(policy.unwrap() as i64); let created_cbor = cbor::Value::from(policy.unwrap());
assert_eq!(created_cbor, cbor_policy); assert_eq!(created_cbor, cbor_policy);
let cbor_policy_error = cbor_int!(-1); let cbor_policy_error = cbor_int!(-1);
@@ -1290,7 +1302,7 @@ mod test {
#[test] #[test]
fn test_cred_protect_extension() { fn test_cred_protect_extension() {
let cbor_extensions = cbor_map! { let cbor_extensions = cbor_map! {
"credProtect" => CredentialProtectionPolicy::UserVerificationRequired as i64, "credProtect" => CredentialProtectionPolicy::UserVerificationRequired,
}; };
let extensions = Extensions::try_from(&cbor_extensions).unwrap(); let extensions = Extensions::try_from(&cbor_extensions).unwrap();
assert_eq!( assert_eq!(

View File

@@ -522,7 +522,7 @@ where
.user_display_name .user_display_name
.map(|s| truncate_to_char_boundary(&s, 64).to_string()), .map(|s| truncate_to_char_boundary(&s, 64).to_string()),
cred_random, cred_random,
cred_protect_policy: cred_protect_policy.clone(), cred_protect_policy,
}; };
self.persistent_store.store_credential(credential_source)?; self.persistent_store.store_credential(credential_source)?;
random_id random_id
@@ -547,7 +547,7 @@ where
let hmac_secret_output = if use_hmac_extension { Some(true) } else { None }; let hmac_secret_output = if use_hmac_extension { Some(true) } else { None };
let extensions = cbor_map_options! { let extensions = cbor_map_options! {
"hmac-secret" => hmac_secret_output, "hmac-secret" => hmac_secret_output,
"credProtect" => cred_protect_policy.map(|policy| policy as i64), "credProtect" => cred_protect_policy,
}; };
if !cbor::write(extensions, &mut auth_data) { if !cbor::write(extensions, &mut auth_data) {
return Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_RESPONSE_CANNOT_WRITE_CBOR); return Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_RESPONSE_CANNOT_WRITE_CBOR);
@@ -1220,7 +1220,7 @@ mod test {
policy: CredentialProtectionPolicy, policy: CredentialProtectionPolicy,
) -> AuthenticatorMakeCredentialParameters { ) -> AuthenticatorMakeCredentialParameters {
let mut extension_map = BTreeMap::new(); let mut extension_map = BTreeMap::new();
extension_map.insert("credProtect".to_string(), cbor_int!(policy as i64)); extension_map.insert("credProtect".to_string(), cbor::Value::from(policy));
let extensions = Some(Extensions::new(extension_map)); let extensions = Some(Extensions::new(extension_map));
let mut make_credential_params = create_minimal_make_credential_parameters(); let mut make_credential_params = create_minimal_make_credential_parameters();
make_credential_params.extensions = extensions; make_credential_params.extensions = extensions;