From 42044f2a46f98539f72d4984c7c643388d286006 Mon Sep 17 00:00:00 2001 From: Nick Zana Date: Tue, 16 May 2023 16:19:58 -0400 Subject: [PATCH] fido-common: Implement serde for credential::public_key::Descriptor Adds serde_with as a dependency in order to serialize Descriptor::id as bytes instead of as a sequence. Enables cfg_eval feature to enable conditional usage of serde_with behind the "serde" feature flag for the fido-common crate. --- crates/fido-common/Cargo.toml | 3 ++- crates/fido-common/src/credential/public_key.rs | 14 ++++++++++++-- crates/fido-common/src/lib.rs | 2 +- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/crates/fido-common/Cargo.toml b/crates/fido-common/Cargo.toml index 5d1f529..916ee10 100644 --- a/crates/fido-common/Cargo.toml +++ b/crates/fido-common/Cargo.toml @@ -9,7 +9,8 @@ edition = "2021" ciborium = { version = "0.2.1", default-features = false, optional = true } coset = { version = "0.3.4", default-features = false } serde = { version = "1", features = ["derive"], optional = true } +serde_with = { path = "../../../serde_with/serde_with", optional = true } bitflags = { version = "2.2.1", default-features = false, optional = true } [features] -serde = ["dep:serde", "dep:bitflags", "dep:ciborium"] +serde = ["dep:serde", "dep:serde_with", "dep:bitflags", "dep:ciborium"] diff --git a/crates/fido-common/src/credential/public_key.rs b/crates/fido-common/src/credential/public_key.rs index 8bc6c4c..06f73f8 100644 --- a/crates/fido-common/src/credential/public_key.rs +++ b/crates/fido-common/src/credential/public_key.rs @@ -3,6 +3,8 @@ use std::collections::BTreeSet; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; +#[cfg(feature = "serde")] +use serde_with::{serde_as, skip_serializing_none, Bytes}; #[cfg(feature = "serde")] pub(crate) mod algorithm { @@ -48,8 +50,14 @@ pub struct Parameters { } /// > This dictionary identifies a specific public key credential. -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -#[derive(Debug)] +#[cfg_eval] +#[derive(Debug, Clone)] +#[cfg_attr( + feature = "serde", + serde_as, + skip_serializing_none, + derive(Serialize, Deserialize) +)] pub struct Descriptor { /// > This member contains the type of the public key credential /// > the caller is referring to. @@ -58,6 +66,8 @@ pub struct Descriptor { /// > A probabilistically-unique byte sequence identifying a /// > public key credential source and its authentication /// > assertions. + // Bounds: [16, 1023] bytes + #[cfg_attr(feature = "serde", serde_as(as = "Bytes"))] pub id: Vec, /// > This... member contains a hint as to how the client might /// > communicate with the managing authenticator of the public diff --git a/crates/fido-common/src/lib.rs b/crates/fido-common/src/lib.rs index 2bcce8d..9f605e7 100644 --- a/crates/fido-common/src/lib.rs +++ b/crates/fido-common/src/lib.rs @@ -1,4 +1,4 @@ -#![feature(split_array, slice_take)] +#![feature(cfg_eval, split_array, slice_take)] pub mod attestation; pub mod authenticator;