passkey_client/client_data.rs
1use serde::Serialize;
2
3/// A trait describing how client data should be generated during a WebAuthn operation.
4pub trait ClientData<E: Serialize> {
5 /// Extra client data to be appended to the automatically generated client data.
6 fn extra_client_data(&self) -> E;
7
8 /// The hash of the client data to be used in the WebAuthn operation.
9 fn client_data_hash(&self) -> Option<Vec<u8>>;
10}
11
12/// The client data and its hash will be automatically generated from the request
13/// according to the WebAuthn specification.
14pub struct DefaultClientData;
15impl ClientData<()> for DefaultClientData {
16 fn extra_client_data(&self) {}
17
18 fn client_data_hash(&self) -> Option<Vec<u8>> {
19 None
20 }
21}
22
23/// The extra client data will be appended to the automatically generated client data.
24/// The hash will be automatically generated from the result client data according to the WebAuthn specification.
25pub struct DefaultClientDataWithExtra<E: Serialize>(pub E);
26impl<E: Serialize + Clone> ClientData<E> for DefaultClientDataWithExtra<E> {
27 fn extra_client_data(&self) -> E {
28 self.0.clone()
29 }
30 fn client_data_hash(&self) -> Option<Vec<u8>> {
31 None
32 }
33}
34
35/// The client data will be automatically generated from the request according to the WebAuthn specification
36/// but it will not be used as a base for the hash. The client data hash will instead be provided by the caller.
37pub struct DefaultClientDataWithCustomHash(pub Vec<u8>);
38impl ClientData<()> for DefaultClientDataWithCustomHash {
39 fn extra_client_data(&self) {}
40
41 fn client_data_hash(&self) -> Option<Vec<u8>> {
42 Some(self.0.clone())
43 }
44}
45
46/// Backwards compatibility with the previous `register` and `authenticate` functions
47/// which only took `Option<Vec<u8>>` as a client data hash.
48impl ClientData<()> for Option<Vec<u8>> {
49 fn extra_client_data(&self) {}
50
51 fn client_data_hash(&self) -> Option<Vec<u8>> {
52 self.clone()
53 }
54}