use serde::{Deserialize, Serialize};
use crate::cbor::Value;
use crate::ctap2::Info;
use crate::webauthn::extensions::{
Ctap2Extension, ExtensionContext, OutputContext, RegistrationExtensionOutputs,
RegistrationProcessor,
};
use crate::webauthn::types::{
PublicKeyCredentialCreationOptions, PublicKeyCredentialRequestOptions,
};
pub const EXTENSION_ID: &str = "minPinLength";
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RegistrationOutput {
#[serde(rename = "minPinLength")]
pub length: u32,
}
pub(crate) fn to_cbor() -> (String, Value) {
(EXTENSION_ID.into(), Value::Bool(true))
}
pub(crate) fn from_cbor(value: &Value) -> Option<u32> {
value.as_int().map(|v| v as u32)
}
pub struct MinPinLengthExtension;
impl Ctap2Extension for MinPinLengthExtension {
fn make_credential(
&self,
info: &Info,
options: &PublicKeyCredentialCreationOptions,
) -> Result<Option<Box<dyn RegistrationProcessor>>, String> {
let ext = match &options.extensions {
Some(e) => e,
None => return Ok(None),
};
if ext.min_pin_length != Some(true) {
return Ok(None);
}
if !info.extensions.iter().any(|e| e == EXTENSION_ID) {
return Ok(None);
}
Ok(Some(Box::new(MinPinLengthRegistrationProcessor)))
}
fn get_assertion(
&self,
_info: &Info,
_options: &PublicKeyCredentialRequestOptions,
) -> Result<Option<Box<dyn super::AuthenticationProcessor>>, String> {
Ok(None)
}
}
struct MinPinLengthRegistrationProcessor;
impl RegistrationProcessor for MinPinLengthRegistrationProcessor {
fn prepare_inputs(&self, _ctx: &mut ExtensionContext) -> Result<Vec<(String, Value)>, String> {
Ok(vec![to_cbor()])
}
fn prepare_outputs(&self, ctx: &OutputContext<'_>, outputs: &mut RegistrationExtensionOutputs) {
if let Some((_, val)) = ctx
.auth_data_extensions
.and_then(|exts| exts.iter().find(|(k, _)| k == EXTENSION_ID))
&& let Some(length) = from_cbor(val)
{
outputs.min_pin_length = Some(RegistrationOutput { length });
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_cbor() {
let (key, val) = to_cbor();
assert_eq!(key, "minPinLength");
assert_eq!(val.as_bool(), Some(true));
let out = from_cbor(&Value::Int(8));
assert_eq!(out, Some(8));
}
}