feat: register outputs json

This commit is contained in:
2021-07-03 11:29:23 +08:00
parent 9c2cc1d3f3
commit a528985ffd
3 changed files with 106 additions and 19 deletions

View File

@@ -1,9 +1,81 @@
use std::fmt;
use std::thread;
use std::sync::mpsc::{channel, Sender};
use serde::{Deserialize, Serialize};
use authenticator::StatusUpdate;
use authenticator::{StatusUpdate, RegisterResult};
use base64::URL_SAFE_NO_PAD;
use rand::Rng;
use rust_util::XResult;
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct U2FDeviceInfo {
pub vendor_name: String,
pub device_name: String,
pub version_interface: u8,
pub version_major: u8,
pub version_minor: u8,
pub version_build: u8,
pub cap_flags: u8,
}
impl U2FDeviceInfo {
fn from(register_result: &authenticator::RegisterResult) -> Self {
let i = &register_result.1;
Self {
vendor_name: String::from_utf8_lossy(&i.vendor_name).to_string(),
device_name: String::from_utf8_lossy(&i.device_name).to_string(),
version_interface: i.version_interface,
version_major: i.version_major,
version_minor: i.version_minor,
version_build: i.version_build,
cap_flags: i.cap_flags,
}
}
}
impl fmt::Display for U2FDeviceInfo {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"Vendor: {}, Device: {}, Interface: {}, Firmware: v{}.{}.{}, Capabilities: {}",
self.vendor_name,
self.device_name,
&self.version_interface,
&self.version_major,
&self.version_minor,
&self.version_build,
to_hex(&[self.cap_flags], ":"),
)
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct U2fRegistrationData {
pub u2f_device_info: U2FDeviceInfo,
pub device_name: Option<String>,
pub attestation_cert_pem: Option<String>,
pub pub_key: String,
pub key_handle: String,
}
impl U2fRegistrationData {
pub fn from(app_id: &str, client_data: &str, register_result: RegisterResult) -> XResult<Self> {
let rr = match u2f::register::parse_registration(app_id.to_string(), client_data.as_bytes().to_vec(), register_result.0.to_vec()) {
Ok(rr) => rr,
Err(e) => return simple_error!("Parse registration data failed: {}", e),
};
Ok(Self {
// register_result,
u2f_device_info: U2FDeviceInfo::from(&register_result),
device_name: rr.device_name,
attestation_cert_pem: rr.attestation_cert.map(|c| {
to_pem(&c, "CERTIFICATE", 64)
}),
pub_key: hex::encode(rr.pub_key),
key_handle: hex::encode(rr.key_handle),
})
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct U2fV2Challenge {
@@ -82,4 +154,9 @@ pub fn to_pem(bs: &[u8], sub: &str, w: usize) -> String {
}
s.push_str(&format!("\n-----END {}-----", sub));
s
}
}
pub fn to_hex(data: &[u8], joiner: &str) -> String {
let parts: Vec<String> = data.iter().map(|byte| format!("{:02x}", byte)).collect();
parts.join(joiner)
}