1
1
use crate :: webauthn:: FilteredPublicKeyCredentialParameters ;
2
- use crate :: { Bytes , String , Vec } ;
2
+ use crate :: { Bytes , TryFromStrError , Vec } ;
3
3
use serde:: { Deserialize , Serialize } ;
4
4
use serde_indexed:: { DeserializeIndexed , SerializeIndexed } ;
5
5
@@ -10,11 +10,11 @@ pub type AuthenticatorInfo = Response;
10
10
#[ serde_indexed( offset = 1 ) ]
11
11
pub struct Response {
12
12
// 0x01
13
- pub versions : Vec < String < 12 > , 4 > ,
13
+ pub versions : Vec < Version , 4 > ,
14
14
15
15
// 0x02
16
16
#[ serde( skip_serializing_if = "Option::is_none" ) ]
17
- pub extensions : Option < Vec < String < 13 > , 4 > > ,
17
+ pub extensions : Option < Vec < Extension , 4 > > ,
18
18
19
19
// 0x03
20
20
pub aaguid : Bytes < 16 > ,
@@ -44,7 +44,7 @@ pub struct Response {
44
44
// 0x09
45
45
// FIDO_2_1
46
46
#[ serde( skip_serializing_if = "Option::is_none" ) ]
47
- pub transports : Option < Vec < String < 8 > , 4 > > ,
47
+ pub transports : Option < Vec < Transport , 4 > > ,
48
48
49
49
// 0x0A
50
50
// FIDO_2_1
@@ -135,7 +135,7 @@ impl Default for Response {
135
135
136
136
#[ derive( Debug ) ]
137
137
pub struct ResponseBuilder {
138
- pub versions : Vec < String < 12 > , 4 > ,
138
+ pub versions : Vec < Version , 4 > ,
139
139
pub aaguid : Bytes < 16 > ,
140
140
}
141
141
@@ -178,6 +178,120 @@ impl ResponseBuilder {
178
178
}
179
179
}
180
180
181
+ #[ derive( Copy , Clone , Debug , Eq , PartialEq , Serialize , Deserialize ) ]
182
+ #[ non_exhaustive]
183
+ #[ serde( into = "&str" , try_from = "&str" ) ]
184
+ pub enum Version {
185
+ Fido2_0 ,
186
+ Fido2_1 ,
187
+ Fido2_1Pre ,
188
+ U2fV2 ,
189
+ }
190
+
191
+ impl Version {
192
+ const FIDO_2_0 : & ' static str = "FIDO_2_0" ;
193
+ const FIDO_2_1 : & ' static str = "FIDO_2_1" ;
194
+ const FIDO_2_1_PRE : & ' static str = "FIDO_2_1_PRE" ;
195
+ const U2F_V2 : & ' static str = "U2F_V2" ;
196
+ }
197
+
198
+ impl From < Version > for & str {
199
+ fn from ( version : Version ) -> Self {
200
+ match version {
201
+ Version :: Fido2_0 => Version :: FIDO_2_0 ,
202
+ Version :: Fido2_1 => Version :: FIDO_2_1 ,
203
+ Version :: Fido2_1Pre => Version :: FIDO_2_1_PRE ,
204
+ Version :: U2fV2 => Version :: U2F_V2 ,
205
+ }
206
+ }
207
+ }
208
+
209
+ impl TryFrom < & str > for Version {
210
+ type Error = TryFromStrError ;
211
+
212
+ fn try_from ( s : & str ) -> Result < Self , Self :: Error > {
213
+ match s {
214
+ Self :: FIDO_2_0 => Ok ( Self :: Fido2_0 ) ,
215
+ Self :: FIDO_2_1 => Ok ( Self :: Fido2_1 ) ,
216
+ Self :: FIDO_2_1_PRE => Ok ( Self :: Fido2_1Pre ) ,
217
+ Self :: U2F_V2 => Ok ( Self :: U2fV2 ) ,
218
+ _ => Err ( TryFromStrError ) ,
219
+ }
220
+ }
221
+ }
222
+
223
+ #[ derive( Copy , Clone , Debug , Eq , PartialEq , Serialize , Deserialize ) ]
224
+ #[ non_exhaustive]
225
+ #[ serde( into = "&str" , try_from = "&str" ) ]
226
+ pub enum Extension {
227
+ CredProtect ,
228
+ HmacSecret ,
229
+ LargeBlobKey ,
230
+ }
231
+
232
+ impl Extension {
233
+ const CRED_PROTECT : & ' static str = "credProtect" ;
234
+ const HMAC_SECRET : & ' static str = "hmac-secret" ;
235
+ const LARGE_BLOB_KEY : & ' static str = "largeBlobKey" ;
236
+ }
237
+
238
+ impl From < Extension > for & str {
239
+ fn from ( extension : Extension ) -> Self {
240
+ match extension {
241
+ Extension :: CredProtect => Extension :: CRED_PROTECT ,
242
+ Extension :: HmacSecret => Extension :: HMAC_SECRET ,
243
+ Extension :: LargeBlobKey => Extension :: LARGE_BLOB_KEY ,
244
+ }
245
+ }
246
+ }
247
+
248
+ impl TryFrom < & str > for Extension {
249
+ type Error = TryFromStrError ;
250
+
251
+ fn try_from ( s : & str ) -> Result < Self , Self :: Error > {
252
+ match s {
253
+ Self :: CRED_PROTECT => Ok ( Self :: CredProtect ) ,
254
+ Self :: HMAC_SECRET => Ok ( Self :: HmacSecret ) ,
255
+ Self :: LARGE_BLOB_KEY => Ok ( Self :: LargeBlobKey ) ,
256
+ _ => Err ( TryFromStrError ) ,
257
+ }
258
+ }
259
+ }
260
+
261
+ #[ derive( Copy , Clone , Debug , Eq , PartialEq , Serialize , Deserialize ) ]
262
+ #[ non_exhaustive]
263
+ #[ serde( into = "&str" , try_from = "&str" ) ]
264
+ pub enum Transport {
265
+ Nfc ,
266
+ Usb ,
267
+ }
268
+
269
+ impl Transport {
270
+ const NFC : & ' static str = "nfc" ;
271
+ const USB : & ' static str = "usb" ;
272
+ }
273
+
274
+ impl From < Transport > for & str {
275
+ fn from ( transport : Transport ) -> Self {
276
+ match transport {
277
+ Transport :: Nfc => Transport :: NFC ,
278
+ Transport :: Usb => Transport :: USB ,
279
+ }
280
+ }
281
+ }
282
+
283
+ impl TryFrom < & str > for Transport {
284
+ type Error = TryFromStrError ;
285
+
286
+ fn try_from ( s : & str ) -> Result < Self , Self :: Error > {
287
+ match s {
288
+ Self :: NFC => Ok ( Self :: Nfc ) ,
289
+ Self :: USB => Ok ( Self :: Usb ) ,
290
+ _ => Err ( TryFromStrError ) ,
291
+ }
292
+ }
293
+ }
294
+
181
295
#[ derive( Copy , Clone , Debug , Eq , PartialEq , Serialize , Deserialize ) ]
182
296
#[ non_exhaustive]
183
297
#[ serde( rename_all = "camelCase" ) ]
0 commit comments