passkey-client 0.4.0

Webauthn client in Rust.
Documentation
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
use super::*;
use coset::iana;
use passkey_authenticator::{MemoryStore, MockUserValidationMethod, UserCheck};
use passkey_types::{
    ctap2, encoding::try_from_base64url, rand::random_vec, webauthn::CollectedClientData, Bytes,
};
use serde::Deserialize;
use url::{ParseError, Url};

mod ext_prf;

fn good_credential_creation_options() -> webauthn::PublicKeyCredentialCreationOptions {
    webauthn::PublicKeyCredentialCreationOptions {
        rp: webauthn::PublicKeyCredentialRpEntity {
            id: Some("future.1password.com".into()),
            name: "future.1password.com".into(),
        },
        user: webauthn::PublicKeyCredentialUserEntity {
            id: random_vec(16).into(),
            display_name: "wendy".into(),
            name: "wendy".into(),
        },
        challenge: random_vec(32).into(),
        pub_key_cred_params: vec![webauthn::PublicKeyCredentialParameters {
            ty: webauthn::PublicKeyCredentialType::PublicKey,
            alg: iana::Algorithm::ES256,
        }],
        timeout: None,
        exclude_credentials: Default::default(),
        authenticator_selection: Default::default(),
        hints: Some(vec![webauthn::PublicKeyCredentialHints::ClientDevice]),
        attestation: Default::default(),
        attestation_formats: Default::default(),
        extensions: Default::default(),
    }
}

fn good_credential_request_options(
    credential_id: impl Into<Bytes>,
) -> webauthn::PublicKeyCredentialRequestOptions {
    webauthn::PublicKeyCredentialRequestOptions {
        challenge: random_vec(32).into(),
        timeout: None,
        rp_id: Some("future.1password.com".into()),
        allow_credentials: Some(vec![webauthn::PublicKeyCredentialDescriptor {
            ty: webauthn::PublicKeyCredentialType::PublicKey,
            id: credential_id.into(),
            transports: None,
        }]),
        user_verification: Default::default(),
        hints: Some(vec![webauthn::PublicKeyCredentialHints::ClientDevice]),
        attestation: Default::default(),
        attestation_formats: Default::default(),
        extensions: Default::default(),
    }
}

fn uv_mock_with_creation(times: usize) -> MockUserValidationMethod {
    let mut user_mock = MockUserValidationMethod::new();
    user_mock
        .expect_is_verification_enabled()
        .returning(|| Some(true));
    user_mock
        .expect_check_user()
        .with(
            mockall::predicate::always(),
            mockall::predicate::eq(true),
            mockall::predicate::eq(true),
        )
        .returning(|_, _, _| {
            Ok(UserCheck {
                presence: true,
                verification: true,
            })
        })
        .times(times);
    user_mock.expect_is_presence_enabled().returning(|| true);
    user_mock
}

#[tokio::test]
async fn create_and_authenticate() {
    let auth = Authenticator::new(
        ctap2::Aaguid::new_empty(),
        MemoryStore::new(),
        uv_mock_with_creation(2),
    );
    let mut client = Client::new(auth);

    let origin = Url::parse("https://future.1password.com").unwrap();
    let options = webauthn::CredentialCreationOptions {
        public_key: good_credential_creation_options(),
    };
    let cred = client
        .register(&origin, options, DefaultClientData)
        .await
        .expect("failed to register with options");

    let credential_id = cred.raw_id;

    let auth_options = webauthn::CredentialRequestOptions {
        public_key: good_credential_request_options(credential_id),
    };
    client
        .authenticate(&origin, auth_options, DefaultClientData)
        .await
        .expect("failed to authenticate with freshly created credential");
}

#[tokio::test]
async fn create_and_authenticate_with_extra_client_data() {
    #[derive(Clone, Serialize, Deserialize)]
    struct AndroidClientData {
        android_package_name: String,
    }
    let auth = Authenticator::new(
        ctap2::Aaguid::new_empty(),
        MemoryStore::new(),
        uv_mock_with_creation(2),
    );
    let mut client = Client::new(auth);

    let origin = Url::parse("https://future.1password.com").unwrap();
    let options = webauthn::CredentialCreationOptions {
        public_key: good_credential_creation_options(),
    };
    let extra_data = AndroidClientData {
        android_package_name: "com.example.app".to_owned(),
    };
    let cred = client
        .register(
            &origin,
            options,
            DefaultClientDataWithExtra(extra_data.clone()),
        )
        .await
        .expect("failed to register with options");

    let returned_base64url_client_data_json: String = cred.response.client_data_json.into();
    let returned_client_data_json =
        try_from_base64url(returned_base64url_client_data_json.as_str())
            .expect("could not base64url decode client data");
    let returned_client_data: CollectedClientData<AndroidClientData> =
        serde_json::from_slice(&returned_client_data_json)
            .expect("could not json deserialize client data");
    assert_eq!(
        returned_client_data.extra_data.android_package_name,
        "com.example.app"
    );

    let credential_id = cred.raw_id;

    let auth_options = webauthn::CredentialRequestOptions {
        public_key: good_credential_request_options(credential_id),
    };
    let result = client
        .authenticate(
            &origin,
            auth_options,
            DefaultClientDataWithExtra(extra_data),
        )
        .await
        .expect("failed to authenticate with freshly created credential");

    let returned_base64url_client_data_json: String = result.response.client_data_json.into();
    let returned_client_data_json =
        try_from_base64url(returned_base64url_client_data_json.as_str())
            .expect("could not base64url decode client data");
    let returned_client_data: CollectedClientData<AndroidClientData> =
        serde_json::from_slice(&returned_client_data_json)
            .expect("could not json deserialize client data");
    assert_eq!(
        returned_client_data.extra_data.android_package_name,
        "com.example.app"
    );
}

#[tokio::test]
async fn create_and_authenticate_with_origin_subdomain() {
    let auth = Authenticator::new(
        ctap2::Aaguid::new_empty(),
        MemoryStore::new(),
        uv_mock_with_creation(2),
    );
    let mut client = Client::new(auth);

    let origin = Url::parse("https://www.future.1password.com").unwrap();
    let options = webauthn::CredentialCreationOptions {
        public_key: good_credential_creation_options(),
    };
    let cred = client
        .register(&origin, options, DefaultClientData)
        .await
        .expect("failed to register with options");

    let att_obj: ctap2::make_credential::Response =
        ciborium::de::from_reader(cred.response.attestation_object.as_slice())
            .expect("could not deserialize response");
    assert_eq!(
        att_obj.auth_data.rp_id_hash(),
        &sha256(b"future.1password.com")
    );

    let auth_options = webauthn::CredentialRequestOptions {
        public_key: good_credential_request_options(cred.raw_id),
    };
    let res = client
        .authenticate(&origin, auth_options, DefaultClientData)
        .await
        .expect("failed to authenticate with freshly created credential");
    let att_obj = ctap2::AuthenticatorData::from_slice(&res.response.authenticator_data)
        .expect("could not deserialize response");
    assert_eq!(att_obj.rp_id_hash(), &sha256(b"future.1password.com"));
}

#[tokio::test]
async fn create_and_authenticate_without_rp_id() {
    let auth = Authenticator::new(
        ctap2::Aaguid::new_empty(),
        MemoryStore::new(),
        uv_mock_with_creation(2),
    );
    let mut client = Client::new(auth);

    let origin = Url::parse("https://www.future.1password.com").unwrap();
    let options = webauthn::CredentialCreationOptions {
        public_key: webauthn::PublicKeyCredentialCreationOptions {
            rp: webauthn::PublicKeyCredentialRpEntity {
                id: None,
                name: "future.1password.com".into(),
            },
            ..good_credential_creation_options()
        },
    };
    let cred = client
        .register(&origin, options, DefaultClientData)
        .await
        .expect("failed to register with options");

    let att_obj: ctap2::make_credential::Response =
        ciborium::de::from_reader(cred.response.attestation_object.as_slice())
            .expect("could not deserialize response");
    assert_eq!(
        att_obj.auth_data.rp_id_hash(),
        &sha256(b"www.future.1password.com")
    );

    let auth_options = webauthn::CredentialRequestOptions {
        public_key: webauthn::PublicKeyCredentialRequestOptions {
            rp_id: None,
            ..good_credential_request_options(cred.raw_id)
        },
    };
    let res = client
        .authenticate(&origin, auth_options, DefaultClientData)
        .await
        .expect("failed to authenticate with freshly created credential");
    let att_obj = ctap2::AuthenticatorData::from_slice(&res.response.authenticator_data)
        .expect("could not deserialize response");
    assert_eq!(att_obj.rp_id_hash(), &sha256(b"www.future.1password.com"));
}

#[tokio::test]
async fn create_and_authenticate_without_cred_params() {
    let auth = Authenticator::new(
        ctap2::Aaguid::new_empty(),
        MemoryStore::new(),
        uv_mock_with_creation(2),
    );
    let mut client = Client::new(auth);

    let origin = Url::parse("https://future.1password.com").unwrap();
    let options = webauthn::CredentialCreationOptions {
        public_key: webauthn::PublicKeyCredentialCreationOptions {
            pub_key_cred_params: Vec::new(),
            ..good_credential_creation_options()
        },
    };
    let cred = client
        .register(&origin, options, DefaultClientData)
        .await
        .expect("failed to register with options");

    let credential_id = cred.raw_id;

    let auth_options = webauthn::CredentialRequestOptions {
        public_key: good_credential_request_options(credential_id),
    };
    client
        .authenticate(&origin, auth_options, DefaultClientData)
        .await
        .expect("failed to authenticate with freshly created credential");
}

#[test]
fn validate_rp_id() -> Result<(), ParseError> {
    let client = RpIdVerifier::new(public_suffix::DEFAULT_PROVIDER);

    let example = Url::parse("https://example.com")?.into();
    let com_tld = client.assert_domain(&example, Some("com"));
    assert_eq!(com_tld, Err(WebauthnError::InvalidRpId));

    let example_dots = Url::parse("https://example...com")?.into();
    let bunch_of_dots = client.assert_domain(&example_dots, Some("...com"));
    assert_eq!(bunch_of_dots, Err(WebauthnError::InvalidRpId));

    let future = Url::parse("https://www.future.1password.com")?.into();
    let sub_domain_ignored = client.assert_domain(&future, Some("future.1password.com"));
    assert_eq!(sub_domain_ignored, Ok("future.1password.com"));

    let use_effective_domain = client.assert_domain(&future, None);
    assert_eq!(use_effective_domain, Ok("www.future.1password.com"));

    let not_protected = Url::parse("http://example.com")?.into();
    let not_https = client.assert_domain(&not_protected, Some("example.com"));
    assert_eq!(not_https, Err(WebauthnError::UnprotectedOrigin));

    let localhost = Url::parse("http://localhost:8080")?.into();
    let should_still_match = client.assert_domain(&localhost, Some("example.com"));
    assert_eq!(should_still_match, Err(WebauthnError::OriginRpMissmatch));

    let localhost_not_allowed = client.assert_domain(&localhost, Some("localhost"));
    assert_eq!(
        localhost_not_allowed,
        Err(WebauthnError::InsecureLocalhostNotAllowed)
    );
    let localhost_not_allowed = client.assert_domain(&localhost, None);
    assert_eq!(
        localhost_not_allowed,
        Err(WebauthnError::InsecureLocalhostNotAllowed)
    );

    let client = client.allows_insecure_localhost(true);
    let skips_http_and_tld_check = client.assert_domain(&localhost, Some("localhost"));
    assert_eq!(skips_http_and_tld_check, Ok("localhost"));
    let skips_http_and_tld_check = client.assert_domain(&localhost, None);
    assert_eq!(skips_http_and_tld_check, Ok("localhost"));

    Ok(())
}

struct BrokenTLDProvider {}
impl public_suffix::EffectiveTLDProvider for BrokenTLDProvider {
    // Notice that this just returns Err() for every domain regardless.
    // This is only done to allow the test's assertion to prove that we
    // are actually using this verifier and not the default one.
    fn effective_tld_plus_one<'a>(
        &self,
        _domain: &'a str,
    ) -> Result<&'a str, public_suffix::Error> {
        Err(public_suffix::Error::CannotDeriveETldPlus1)
    }
}
#[test]
fn validate_domain_with_private_list_provider() -> Result<(), ParseError> {
    let my_custom_provider = BrokenTLDProvider {};
    let client = RpIdVerifier::new(my_custom_provider);

    // Notice that, in this test, this is a legitimate origin/rp_id combination
    // We assert that this produces an error to prove that we are indeed using our
    // BrokenTLDProvider which always returns Err() regardless of the TLD.
    let origin = Url::parse("https://www.future.1password.com")?.into();
    let rp_id = "future.1password.com";
    let result = client.assert_domain(&origin, Some(rp_id));
    assert_eq!(result, Err(WebauthnError::InvalidRpId));

    Ok(())
}

fn user_mock_with_uv() -> MockUserValidationMethod {
    let mut user_mock = MockUserValidationMethod::new();
    user_mock
        .expect_is_verification_enabled()
        .returning(|| Some(true));
    user_mock.expect_check_user().returning(|_, _, _| {
        Ok(UserCheck {
            presence: true,
            verification: true,
        })
    });
    // Always called by `get_info`
    user_mock
        .expect_is_verification_enabled()
        .returning(|| Some(true));
    user_mock.expect_is_presence_enabled().returning(|| true);
    user_mock
}

fn user_mock_without_uv() -> MockUserValidationMethod {
    let mut user_mock = MockUserValidationMethod::new();
    user_mock.expect_check_user().returning(|_, _, _| {
        Ok(UserCheck {
            presence: true,
            verification: false,
        })
    });
    // Always called by `get_info`
    user_mock
        .expect_is_verification_enabled()
        .returning(|| Some(true));
    user_mock.expect_is_presence_enabled().returning(|| true);
    user_mock
}

#[tokio::test]
async fn client_register_triggers_uv_when_uv_is_required() {
    // Arrange
    let auth = Authenticator::new(
        ctap2::Aaguid::new_empty(),
        MemoryStore::new(),
        user_mock_with_uv(),
    );
    let mut client = Client::new(auth);
    let origin = Url::parse("https://future.1password.com").unwrap();
    let mut options = webauthn::CredentialCreationOptions {
        public_key: good_credential_creation_options(),
    };
    options.public_key.authenticator_selection = Some(AuthenticatorSelectionCriteria {
        user_verification: UserVerificationRequirement::Required,
        authenticator_attachment: Default::default(),
        resident_key: Default::default(),
        require_resident_key: Default::default(),
    });

    // Act & Assert
    client
        .register(&origin, options, DefaultClientData)
        .await
        .expect("failed to register with options");
}

#[tokio::test]
async fn client_register_does_not_trigger_uv_when_uv_is_discouraged() {
    // Arrange
    let auth = Authenticator::new(
        ctap2::Aaguid::new_empty(),
        MemoryStore::new(),
        user_mock_without_uv(),
    );
    let mut client = Client::new(auth);
    let origin = Url::parse("https://future.1password.com").unwrap();
    let mut options = webauthn::CredentialCreationOptions {
        public_key: good_credential_creation_options(),
    };
    options.public_key.authenticator_selection = Some(AuthenticatorSelectionCriteria {
        user_verification: UserVerificationRequirement::Discouraged,
        authenticator_attachment: Default::default(),
        resident_key: Default::default(),
        require_resident_key: Default::default(),
    });

    // Act & Assert
    client
        .register(&origin, options, DefaultClientData)
        .await
        .expect("failed to register with options");
}