Skip to content

Commit

Permalink
adding cbor::Value::from for i64-like enums
Browse files Browse the repository at this point in the history
  • Loading branch information
kaczmarczyck committed Jun 5, 2020
1 parent 6a44d33 commit b4003e3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
24 changes: 18 additions & 6 deletions src/ctap/data_formats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ impl From<PublicKeyCredentialParameter> for cbor::Value {
fn from(cred_param: PublicKeyCredentialParameter) -> Self {
cbor_map_options! {
"type" => cred_param.cred_type,
"alg" => cred_param.alg as i64,
"alg" => cred_param.alg,
}
}
}
Expand Down Expand Up @@ -429,6 +429,12 @@ pub enum SignatureAlgorithm {
Unknown = 0,
}

impl From<SignatureAlgorithm> for cbor::Value {
fn from(alg: SignatureAlgorithm) -> Self {
(alg as i64).into()
}
}

impl TryFrom<&cbor::Value> for SignatureAlgorithm {
type Error = Ctap2StatusCode;

Expand All @@ -448,6 +454,12 @@ pub enum CredentialProtectionPolicy {
UserVerificationRequired = 0x03,
}

impl From<CredentialProtectionPolicy> for cbor::Value {
fn from(policy: CredentialProtectionPolicy) -> Self {
(policy as i64).into()
}
}

impl TryFrom<cbor::Value> for CredentialProtectionPolicy {
type Error = Ctap2StatusCode;

Expand Down Expand Up @@ -526,7 +538,7 @@ impl From<PublicKeyCredentialSource> for cbor::Value {
UserHandle => Some(credential.user_handle),
OtherUi => credential.other_ui,
CredRandom => credential.cred_random,
CredProtectPolicy => credential.cred_protect_policy.map(|p| p as i64),
CredProtectPolicy => credential.cred_protect_policy,
}
}
}
Expand Down Expand Up @@ -1137,7 +1149,7 @@ mod test {
let signature_algorithm = SignatureAlgorithm::try_from(&cbor_signature_algorithm);
let expected_signature_algorithm = SignatureAlgorithm::ES256;
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);

let cbor_unknown_algorithm = cbor_int!(-1);
Expand All @@ -1164,11 +1176,11 @@ mod test {

#[test]
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 expected_policy = CredentialProtectionPolicy::UserVerificationOptional;
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);

let cbor_policy_error = cbor_int!(-1);
Expand Down Expand Up @@ -1290,7 +1302,7 @@ mod test {
#[test]
fn test_cred_protect_extension() {
let cbor_extensions = cbor_map! {
"credProtect" => CredentialProtectionPolicy::UserVerificationRequired as i64,
"credProtect" => CredentialProtectionPolicy::UserVerificationRequired,
};
let extensions = Extensions::try_from(&cbor_extensions).unwrap();
assert_eq!(
Expand Down
6 changes: 3 additions & 3 deletions src/ctap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ where
.user_display_name
.map(|s| truncate_to_char_boundary(&s, 64).to_string()),
cred_random,
cred_protect_policy: cred_protect_policy.clone(),
cred_protect_policy,
};
self.persistent_store.store_credential(credential_source)?;
random_id
Expand All @@ -547,7 +547,7 @@ where
let hmac_secret_output = if use_hmac_extension { Some(true) } else { None };
let extensions = cbor_map_options! {
"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) {
return Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_RESPONSE_CANNOT_WRITE_CBOR);
Expand Down Expand Up @@ -1220,7 +1220,7 @@ mod test {
policy: CredentialProtectionPolicy,
) -> AuthenticatorMakeCredentialParameters {
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 mut make_credential_params = create_minimal_make_credential_parameters();
make_credential_params.extensions = extensions;
Expand Down

0 comments on commit b4003e3

Please sign in to comment.