#[cfg(test)]
mod tests;
#[cfg(doc)]
use super::super::{Challenge, CredentialId};
use super::{
super::{
super::request::register::{USER_HANDLE_MAX_LEN, UserHandle},
auth::ser::{
AUTH_ASSERT_FIELDS, AuthData, AuthenticatorAssertionVisitor, ClientExtensionsOutputs,
ClientExtensionsOutputsVisitor, EXT_FIELDS,
},
ser::{
AuthenticationExtensionsPrfOutputsHelper, Base64DecodedVal, ClientExtensions,
PublicKeyCredential, Type,
},
ser_relaxed::AuthenticationExtensionsPrfValuesRelaxed,
},
Authentication, AuthenticatorAssertion, AuthenticatorAttachment,
};
use core::{
fmt::{self, Formatter},
marker::PhantomData,
};
use serde::de::{Deserialize, Deserializer, Error, MapAccess, Visitor};
struct ClientExtensionsOutputsRelaxed(pub ClientExtensionsOutputs);
impl ClientExtensions for ClientExtensionsOutputsRelaxed {
fn empty() -> Self {
Self(ClientExtensionsOutputs::empty())
}
}
impl<'de> Deserialize<'de> for ClientExtensionsOutputsRelaxed {
#[inline]
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
deserializer
.deserialize_struct(
"ClientExtensionsOutputsRelaxed",
EXT_FIELDS,
ClientExtensionsOutputsVisitor::<
true,
AuthenticationExtensionsPrfOutputsHelper<
true,
false,
AuthenticationExtensionsPrfValuesRelaxed,
>,
>(PhantomData),
)
.map(Self)
}
}
#[derive(Debug)]
pub struct AuthenticatorAssertionRelaxed<const USER_LEN: usize, const DISCOVERABLE: bool>(
pub AuthenticatorAssertion<USER_LEN, DISCOVERABLE>,
);
impl<'de, const USER_LEN: usize, const DISCOVERABLE: bool> Deserialize<'de>
for AuthenticatorAssertionRelaxed<USER_LEN, DISCOVERABLE>
where
UserHandle<USER_LEN>: Deserialize<'de>,
{
#[inline]
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
deserializer
.deserialize_struct(
"AuthenticatorAssertionRelaxed",
AUTH_ASSERT_FIELDS,
AuthenticatorAssertionVisitor::<true, USER_LEN, DISCOVERABLE>,
)
.map(Self)
}
}
#[derive(Debug)]
pub struct AuthenticationRelaxed<const USER_LEN: usize, const DISCOVERABLE: bool>(
pub Authentication<USER_LEN, DISCOVERABLE>,
);
impl<'de, const USER_LEN: usize, const DISCOVERABLE: bool> Deserialize<'de>
for AuthenticationRelaxed<USER_LEN, DISCOVERABLE>
where
UserHandle<USER_LEN>: Deserialize<'de>,
{
#[expect(clippy::unreachable, reason = "when there is a bug, we want to crash")]
#[inline]
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
PublicKeyCredential::<
true,
false,
AuthenticatorAssertionRelaxed<USER_LEN, DISCOVERABLE>,
ClientExtensionsOutputsRelaxed,
>::deserialize(deserializer)
.map(|cred| {
Self(Authentication {
raw_id: cred.id.unwrap_or_else(|| {
unreachable!("there is a bug in PublicKeyCredential::deserialize")
}),
response: cred.response.0,
authenticator_attachment: cred.authenticator_attachment,
})
})
}
}
pub type DiscoverableAuthenticationRelaxed<const USER_LEN: usize> =
AuthenticationRelaxed<USER_LEN, true>;
pub type DiscoverableAuthenticationRelaxed64 = AuthenticationRelaxed<USER_HANDLE_MAX_LEN, true>;
pub type DiscoverableAuthenticationRelaxed16 = AuthenticationRelaxed<16, true>;
pub type NonDiscoverableAuthenticationRelaxed<const USER_LEN: usize> =
AuthenticationRelaxed<USER_LEN, false>;
pub type NonDiscoverableAuthenticationRelaxed64 = AuthenticationRelaxed<USER_HANDLE_MAX_LEN, false>;
pub type NonDiscoverableAuthenticationRelaxed16 = AuthenticationRelaxed<16, false>;
#[derive(Debug)]
pub struct CustomAuthentication<const USER_LEN: usize, const DISCOVERABLE: bool>(
pub Authentication<USER_LEN, DISCOVERABLE>,
);
impl<'de, const USER_LEN: usize, const DISCOVERABLE: bool> Deserialize<'de>
for CustomAuthentication<USER_LEN, DISCOVERABLE>
where
UserHandle<USER_LEN>: Deserialize<'de>,
{
#[expect(
clippy::too_many_lines,
reason = "want to hide; thus don't put in outer scope"
)]
#[inline]
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct CustomAuthenticationVisitor<const LEN: usize, const DISC: bool>;
impl<'d, const LEN: usize, const DISC: bool> Visitor<'d> for CustomAuthenticationVisitor<LEN, DISC>
where
UserHandle<LEN>: Deserialize<'d>,
{
type Value = CustomAuthentication<LEN, DISC>;
fn expecting(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
formatter.write_str("CustomAuthentication")
}
#[expect(
clippy::too_many_lines,
reason = "want to hide; thus don't put in outer scope"
)]
fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
where
A: MapAccess<'d>,
{
enum Field {
AuthenticatorAttachment,
AuthenticatorData,
ClientDataJson,
ClientExtensionResults,
Id,
Signature,
Type,
UserHandle,
}
impl<'e> Deserialize<'e> for Field {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'e>,
{
struct FieldVisitor;
impl Visitor<'_> for FieldVisitor {
type Value = Field;
fn expecting(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
write!(
formatter,
"'{AUTHENTICATOR_ATTACHMENT}', '{AUTHENTICATOR_DATA}', '{CLIENT_DATA_JSON}', '{CLIENT_EXTENSION_RESULTS}', '{ID}', '{SIGNATURE}', '{TYPE}', or '{USER_HANDLE}'"
)
}
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: Error,
{
match v {
AUTHENTICATOR_ATTACHMENT => Ok(Field::AuthenticatorAttachment),
AUTHENTICATOR_DATA => Ok(Field::AuthenticatorData),
CLIENT_DATA_JSON => Ok(Field::ClientDataJson),
CLIENT_EXTENSION_RESULTS => Ok(Field::ClientExtensionResults),
ID => Ok(Field::Id),
SIGNATURE => Ok(Field::Signature),
TYPE => Ok(Field::Type),
USER_HANDLE => Ok(Field::UserHandle),
_ => Err(E::unknown_field(v, FIELDS)),
}
}
}
deserializer.deserialize_identifier(FieldVisitor)
}
}
let mut authenticator_attachment = None;
let mut authenticator_data = None;
let mut client_data_json = None;
let mut ext = false;
let mut id = None;
let mut signature = None;
let mut typ = false;
let mut user_handle = None;
while let Some(key) = map.next_key()? {
match key {
Field::AuthenticatorAttachment => {
if authenticator_attachment.is_some() {
return Err(Error::duplicate_field(AUTHENTICATOR_ATTACHMENT));
}
authenticator_attachment = map.next_value::<Option<_>>().map(Some)?;
}
Field::AuthenticatorData => {
if authenticator_data.is_some() {
return Err(Error::duplicate_field(AUTHENTICATOR_DATA));
}
authenticator_data =
map.next_value::<AuthData>().map(|val| Some(val.0))?;
}
Field::ClientDataJson => {
if client_data_json.is_some() {
return Err(Error::duplicate_field(CLIENT_DATA_JSON));
}
client_data_json = map
.next_value::<Base64DecodedVal>()
.map(|val| Some(val.0))?;
}
Field::ClientExtensionResults => {
if ext {
return Err(Error::duplicate_field(CLIENT_EXTENSION_RESULTS));
}
ext = map.next_value::<ClientExtensionsOutputs>().map(|_| true)?;
}
Field::Id => {
if id.is_some() {
return Err(Error::duplicate_field(ID));
}
id = map.next_value().map(Some)?;
}
Field::Signature => {
if signature.is_some() {
return Err(Error::duplicate_field(SIGNATURE));
}
signature = map
.next_value::<Base64DecodedVal>()
.map(|val| Some(val.0))?;
}
Field::Type => {
if typ {
return Err(Error::duplicate_field(TYPE));
}
typ = map.next_value::<Type>().map(|_| true)?;
}
Field::UserHandle => {
if user_handle.is_some() {
return Err(Error::duplicate_field(USER_HANDLE));
}
user_handle = map.next_value().map(Some)?;
}
}
}
authenticator_data
.ok_or_else(|| Error::missing_field(AUTHENTICATOR_DATA))
.and_then(|auth_data| {
client_data_json
.ok_or_else(|| Error::missing_field(CLIENT_DATA_JSON))
.and_then(|c_data| {
id.ok_or_else(|| Error::missing_field(ID))
.and_then(|raw_id| {
signature
.ok_or_else(|| Error::missing_field(SIGNATURE))
.and_then(|sig| {
if ext {
if DISC {
user_handle.ok_or_else(|| Error::missing_field(USER_HANDLE))
} else {
user_handle.map_or_else(|| Ok(None), Ok)
}.map(|user| {
CustomAuthentication(Authentication {
response: AuthenticatorAssertion::new_inner(
c_data,
auth_data,
sig,
user,
),
authenticator_attachment:
authenticator_attachment.map_or(
AuthenticatorAttachment::None,
|auth_attach| {
auth_attach.unwrap_or(
AuthenticatorAttachment::None,
)
},
),
raw_id,
})
})
} else {
Err(Error::missing_field(
CLIENT_EXTENSION_RESULTS,
))
}
})
})
})
})
}
}
const AUTHENTICATOR_ATTACHMENT: &str = "authenticatorAttachment";
const AUTHENTICATOR_DATA: &str = "authenticatorData";
const CLIENT_DATA_JSON: &str = "clientDataJSON";
const CLIENT_EXTENSION_RESULTS: &str = "clientExtensionResults";
const ID: &str = "id";
const SIGNATURE: &str = "signature";
const TYPE: &str = "type";
const USER_HANDLE: &str = "userHandle";
const FIELDS: &[&str; 8] = &[
AUTHENTICATOR_ATTACHMENT,
AUTHENTICATOR_DATA,
CLIENT_DATA_JSON,
CLIENT_EXTENSION_RESULTS,
ID,
SIGNATURE,
TYPE,
USER_HANDLE,
];
deserializer.deserialize_struct("CustomAuthentication", FIELDS, CustomAuthenticationVisitor)
}
}
pub type DiscoverableCustomAuthentication<const USER_LEN: usize> =
CustomAuthentication<USER_LEN, true>;
pub type DiscoverableCustomAuthentication64 = CustomAuthentication<USER_HANDLE_MAX_LEN, true>;
pub type DiscoverableCustomAuthentication16 = CustomAuthentication<16, true>;
pub type NonDiscoverableCustomAuthentication<const USER_LEN: usize> =
CustomAuthentication<USER_LEN, false>;
pub type NonDiscoverableCustomAuthentication64 = CustomAuthentication<USER_HANDLE_MAX_LEN, false>;
pub type NonDiscoverableCustomAuthentication16 = CustomAuthentication<16, false>;