1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
use base64urlsafedata::Base64UrlSafeData;
use serde::{Deserialize, Serialize};
use crate::extensions::{RegistrationExtensionsClientOutputs, RequestRegistrationExtensions};
use crate::options::*;
#[derive(Debug, Serialize, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PublicKeyCredentialCreationOptions {
pub rp: RelyingParty,
pub user: User,
pub challenge: Base64UrlSafeData,
pub pub_key_cred_params: Vec<PubKeyCredParams>,
#[serde(skip_serializing_if = "Option::is_none")]
pub timeout: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub attestation: Option<AttestationConveyancePreference>,
#[serde(skip_serializing_if = "Option::is_none")]
pub exclude_credentials: Option<Vec<PublicKeyCredentialDescriptor>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub authenticator_selection: Option<AuthenticatorSelectionCriteria>,
#[serde(skip_serializing_if = "Option::is_none")]
pub extensions: Option<RequestRegistrationExtensions>,
}
#[derive(Debug, Serialize, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CreationChallengeResponse {
pub public_key: PublicKeyCredentialCreationOptions,
}
#[cfg(feature = "wasm")]
impl From<CreationChallengeResponse> for web_sys::CredentialCreationOptions {
fn from(ccr: CreationChallengeResponse) -> Self {
use js_sys::{Array, Object, Uint8Array};
use wasm_bindgen::JsValue;
let chal = Uint8Array::from(ccr.public_key.challenge.0.as_slice());
let userid = Uint8Array::from(ccr.public_key.user.id.0.as_slice());
let jsv = serde_wasm_bindgen::to_value(&ccr).unwrap();
let pkcco = js_sys::Reflect::get(&jsv, &"publicKey".into()).unwrap();
js_sys::Reflect::set(&pkcco, &"challenge".into(), &chal).unwrap();
let user = js_sys::Reflect::get(&pkcco, &"user".into()).unwrap();
js_sys::Reflect::set(&user, &"id".into(), &userid).unwrap();
if let Some(extensions) = ccr.public_key.extensions {
if let Some(cred_blob) = extensions.cred_blob {
let exts = js_sys::Reflect::get(&pkcco, &"extensions".into()).unwrap();
let cred_blob = Uint8Array::from(cred_blob.0.as_ref());
js_sys::Reflect::set(&exts, &"credBlob".into(), &cred_blob).unwrap();
}
}
if let Some(exclude_credentials) = ccr.public_key.exclude_credentials {
let exclude_creds: Array = exclude_credentials
.iter()
.map(|ac| {
let obj = Object::new();
js_sys::Reflect::set(
&obj,
&"type".into(),
&JsValue::from_str(ac.type_.as_str()),
)
.unwrap();
js_sys::Reflect::set(&obj, &"id".into(), &Uint8Array::from(ac.id.0.as_slice()))
.unwrap();
if let Some(transports) = &ac.transports {
let tarray: Array = transports
.iter()
.map(|trs| serde_wasm_bindgen::to_value(trs).unwrap())
.collect();
js_sys::Reflect::set(&obj, &"transports".into(), &tarray).unwrap();
}
obj
})
.collect();
js_sys::Reflect::set(&pkcco, &"excludeCredentials".into(), &exclude_creds).unwrap();
}
web_sys::CredentialCreationOptions::from(jsv)
}
}
#[derive(Debug, Serialize, Clone, Deserialize)]
pub struct AuthenticatorAttestationResponseRaw {
#[serde(rename = "attestationObject")]
pub attestation_object: Base64UrlSafeData,
#[serde(rename = "clientDataJSON")]
pub client_data_json: Base64UrlSafeData,
#[serde(default)]
pub transports: Option<Vec<AuthenticatorTransport>>,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct RegisterPublicKeyCredential {
pub id: String,
#[serde(rename = "rawId")]
pub raw_id: Base64UrlSafeData,
pub response: AuthenticatorAttestationResponseRaw,
#[serde(rename = "type")]
pub type_: String,
#[serde(default)]
pub extensions: RegistrationExtensionsClientOutputs,
}
#[cfg(feature = "wasm")]
impl From<web_sys::PublicKeyCredential> for RegisterPublicKeyCredential {
fn from(data: web_sys::PublicKeyCredential) -> RegisterPublicKeyCredential {
use js_sys::Uint8Array;
let transports = None;
let data_raw_id =
Uint8Array::new(&js_sys::Reflect::get(&data, &"rawId".into()).unwrap()).to_vec();
let data_response = js_sys::Reflect::get(&data, &"response".into()).unwrap();
let data_response_attestation_object = Uint8Array::new(
&js_sys::Reflect::get(&data_response, &"attestationObject".into()).unwrap(),
)
.to_vec();
let data_response_client_data_json = Uint8Array::new(
&js_sys::Reflect::get(&data_response, &"clientDataJSON".into()).unwrap(),
)
.to_vec();
let data_extensions = data.get_client_extension_results();
let data_raw_id_b64 = Base64UrlSafeData(data_raw_id);
let data_response_attestation_object_b64 =
Base64UrlSafeData(data_response_attestation_object);
let data_response_client_data_json_b64 = Base64UrlSafeData(data_response_client_data_json);
RegisterPublicKeyCredential {
id: format!("{}", data_raw_id_b64),
raw_id: data_raw_id_b64,
response: AuthenticatorAttestationResponseRaw {
attestation_object: data_response_attestation_object_b64,
client_data_json: data_response_client_data_json_b64,
transports,
},
type_: "public-key".to_string(),
extensions: data_extensions.into(),
}
}
}