Skip to content

Instantly share code, notes, and snippets.

@smessmer
Last active September 28, 2023 23:28
Show Gist options
  • Save smessmer/5a022c8d09c764cf60e5fab3ad470141 to your computer and use it in GitHub Desktop.
Save smessmer/5a022c8d09c764cf60e5fab3ad470141 to your computer and use it in GitHub Desktop.
/// This is a copy of [webauthn_rs_core::interface::Credential],
/// but with a more compact serialization implementation.
/// See <https://github.com/kanidm/webauthn-rs/issues/352>
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct SerializableCredential {
cred_id: SerializableCredentialID,
cred: SerializableCOSEKey,
counter: Counter,
transports: Option<Vec<AuthenticatorTransport>>,
user_verified: bool,
backup_eligible: bool,
backup_state: bool,
registration_policy: UserVerificationPolicy,
extensions: RegisteredExtensions,
attestation: ParsedAttestation,
attestation_format: AttestationFormat,
}
impl From<SerializableCredential> for Credential {
fn from(credential: SerializableCredential) -> Self {
Self {
cred_id: credential.cred_id.into(),
cred: credential.cred.into(),
counter: credential.counter,
transports: credential.transports,
user_verified: credential.user_verified,
backup_eligible: credential.backup_eligible,
backup_state: credential.backup_state,
registration_policy: credential.registration_policy,
extensions: credential.extensions,
attestation: credential.attestation,
attestation_format: credential.attestation_format,
}
}
}
impl From<Credential> for SerializableCredential {
fn from(credential: Credential) -> Self {
Self {
cred_id: credential.cred_id.into(),
cred: credential.cred.into(),
counter: credential.counter,
transports: credential.transports,
user_verified: credential.user_verified,
backup_eligible: credential.backup_eligible,
backup_state: credential.backup_state,
registration_policy: credential.registration_policy,
extensions: credential.extensions,
attestation: credential.attestation,
attestation_format: credential.attestation_format,
}
}
}
/// This is a copy of [webauthn_rs_core::proto::CredentialID],
/// but with a more compact serialization implementation.
/// See <https://github.com/kanidm/webauthn-rs/issues/352>
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct SerializableCredentialID(Vec<u8>);
impl From<SerializableCredentialID> for CredentialID {
fn from(credential_id: SerializableCredentialID) -> Self {
Self(credential_id.0)
}
}
impl From<CredentialID> for SerializableCredentialID {
fn from(credential_id: CredentialID) -> Self {
Self(credential_id.0)
}
}
/// This is a copy of [webauthn_rs_core::interface::COSEKey],
/// but with a more compact serialization implementation.
/// See <https://github.com/kanidm/webauthn-rs/issues/352>
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct SerializableCOSEKey {
type_: COSEAlgorithm,
key: SerializableCOSEKeyType,
}
impl From<COSEKey> for SerializableCOSEKey {
fn from(cose_key: COSEKey) -> Self {
Self {
type_: cose_key.type_,
key: cose_key.key.into(),
}
}
}
impl From<SerializableCOSEKey> for COSEKey {
fn from(cose_key: SerializableCOSEKey) -> Self {
Self {
type_: cose_key.type_,
key: cose_key.key.into(),
}
}
}
/// This is a copy of [webauthn_rs_core::interface::COSEKeyType],
/// but with a more compact serialization implementation.
/// See <https://github.com/kanidm/webauthn-rs/issues/352>
#[allow(non_camel_case_types, clippy::upper_case_acronyms)]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum SerializableCOSEKeyType {
EC_OKP(COSEOKPKey),
EC_EC2(SerializableCOSEEC2Key),
RSA(SerializableCOSERSAKey),
}
impl From<COSEKeyType> for SerializableCOSEKeyType {
fn from(cose_key_type: COSEKeyType) -> Self {
match cose_key_type {
COSEKeyType::EC_OKP(okp) => Self::EC_OKP(okp),
COSEKeyType::EC_EC2(ec2) => Self::EC_EC2(ec2.into()),
COSEKeyType::RSA(rsa) => Self::RSA(rsa.into()),
}
}
}
impl From<SerializableCOSEKeyType> for COSEKeyType {
fn from(serializable_cose_key_type: SerializableCOSEKeyType) -> Self {
match serializable_cose_key_type {
SerializableCOSEKeyType::EC_OKP(okp) => COSEKeyType::EC_OKP(okp),
SerializableCOSEKeyType::EC_EC2(ec2) => COSEKeyType::EC_EC2(ec2.into()),
SerializableCOSEKeyType::RSA(rsa) => COSEKeyType::RSA(rsa.into()),
}
}
}
/// This is a copy of [webauthn_rs_core::interface::COSEEC2Key],
/// but with a more compact serialization implementation.
/// See <https://github.com/kanidm/webauthn-rs/issues/352>
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct SerializableCOSEEC2Key {
curve: ECDSACurve,
x: Vec<u8>,
y: Vec<u8>,
}
impl From<COSEEC2Key> for SerializableCOSEEC2Key {
fn from(cose_ec2_key: COSEEC2Key) -> Self {
Self {
curve: cose_ec2_key.curve,
x: cose_ec2_key.x.into(),
y: cose_ec2_key.y.into(),
}
}
}
impl From<SerializableCOSEEC2Key> for COSEEC2Key {
fn from(cose_ec2_key: SerializableCOSEEC2Key) -> Self {
Self {
curve: cose_ec2_key.curve,
x: cose_ec2_key.x.into(),
y: cose_ec2_key.y.into(),
}
}
}
/// This is a copy of [webauthn_rs_core::interface::COSEEC2Key],
/// but with a more compact serialization implementation.
/// See <https://github.com/kanidm/webauthn-rs/issues/352>
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct SerializableCOSERSAKey {
n: Vec<u8>,
e: [u8; 3],
}
impl From<COSERSAKey> for SerializableCOSERSAKey {
fn from(cose_rsa_key: COSERSAKey) -> Self {
Self {
n: cose_rsa_key.n.into(),
e: cose_rsa_key.e,
}
}
}
impl From<SerializableCOSERSAKey> for COSERSAKey {
fn from(cose_rsa_key: SerializableCOSERSAKey) -> Self {
Self {
n: cose_rsa_key.n.into(),
e: cose_rsa_key.e,
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment