| <!DOCTYPE html> |
| <title>Credential Manager: create() basics.</title> |
| <script src="../resources/testharness.js"></script> |
| <script src="../resources/testharnessreport.js"></script> |
| <script src="/serviceworker/resources/interfaces.js"></script> |
| <script src="/gen/layout_test_data/mojo/public/js/mojo_bindings.js"></script> |
| <script src="/gen/third_party/WebKit/public/platform/modules/webauth/authenticator.mojom.js"></script> |
| <script src="resources/authenticator-helpers.js"></script> |
| <script> |
| |
| // Common mock values for the mockAuthenticator. |
| var challenge = new TextEncoder().encode("climb a mountain"); |
| |
| var public_key_rp = { |
| id: "1098237235409872", |
| name: "Acme" |
| }; |
| |
| var public_key_user = { |
| id: "1098237235409872", |
| name: "avery.a.jones@example.com", |
| displayName: "Avery A. Jones", |
| icon: "https://pics.acme.com/00/p/aBjjjpqPb.png" |
| }; |
| |
| var public_key_parameters = [{ |
| type: "public-key", |
| algorithm: -7, |
| },]; |
| |
| var publicKey = { |
| challenge, |
| rp: public_key_rp, |
| user: public_key_user, |
| parameters: public_key_parameters, |
| excludeList: [], |
| }; |
| |
| var raw_id = new TextEncoder("utf-8").encode("rawId"); |
| var id = btoa("rawId"); |
| var client_data_json = new TextEncoder("utf-8").encode("clientDataJSON"); |
| var attestation_object = new TextEncoder("utf-8").encode("attestationObject"); |
| |
| promise_test(function(t) { |
| var credential_data = { |
| id: 'id', |
| password: 'pencil', |
| }; |
| |
| return navigator.credentials.create({password: credential_data}) |
| .then(function(credential) { |
| assert_equals(credential.idName, 'username'); |
| assert_equals(credential.passwordName, 'password'); |
| assert_equals(credential.additionalData, null); |
| }); |
| }, "navigator.credentials.create() with valid PasswordCredentialData"); |
| |
| promise_test(function(t) { |
| var f = document.createElement('form'); |
| f.innerHTML = "<input type='text' name='theId' value='musterman' autocomplete='username'>" |
| + "<input type='text' name='thePassword' value='sekrit' autocomplete='current-password'>" |
| + "<input type='text' name='theIcon' value='https://example.com/photo' autocomplete='photo'>" |
| + "<input type='text' name='theExtraField' value='extra'>" |
| + "<input type='text' name='theName' value='friendly name' autocomplete='name'>"; |
| |
| return navigator.credentials.create({password: f}) |
| .then(function(credential) { |
| assert_equals(credential.idName, 'theId'); |
| assert_equals(credential.passwordName, 'thePassword'); |
| |
| assert_equals(credential.additionalData.get('theId'), 'musterman'); |
| assert_equals(credential.additionalData.get('thePassword'), 'sekrit'); |
| assert_equals(credential.additionalData.get('theIcon'), |
| 'https://example.com/photo'); |
| assert_equals(credential.additionalData.get('theName'), 'friendly name'); |
| assert_equals(credential.additionalData.get('theExtraField'), 'extra'); |
| }); |
| }, "navigator.credentials.create() with valid HTMLFormElement"); |
| |
| promise_test(function(t) { |
| mockAuthenticator.setRawId(raw_id); |
| mockAuthenticator.setId(id); |
| mockAuthenticator.setClientDataJson(client_data_json); |
| mockAuthenticator.setAttestationObject(attestation_object); |
| |
| mockAuthenticator.setAuthenticatorStatus( |
| webauth.mojom.AuthenticatorStatus.SUCCESS); |
| |
| return navigator.credentials.create({publicKey}).then(r => { |
| assert_equals(r.id, id, "id"); |
| assert_true(r.rawId instanceof ArrayBuffer); |
| assert_array_equals(new Uint8Array(r.rawId), |
| raw_id, "rawId returned is the same"); |
| assert_true(r.response instanceof AuthenticatorAttestationResponse); |
| assert_true(r.response.clientDataJSON instanceof ArrayBuffer); |
| assert_array_equals(new Uint8Array(r.response.clientDataJSON), |
| client_data_json, "clientDataJSON returned is the same"); |
| assert_true(r.response.attestationObject instanceof ArrayBuffer); |
| assert_array_equals(new Uint8Array(r.response.attestationObject), |
| attestation_object, "attestationObject returned is the same"); |
| assert_not_exists(r.response, 'authenticatorData'); |
| assert_not_exists(r.response, 'signature'); |
| }); |
| }, "Verify that the mock returns the values we give it."); |
| |
| promise_test(function (t) { |
| return promise_rejects(t, "NotSupportedError", |
| navigator.credentials.create()); |
| }, "navigator.credentials.create() with no argument."); |
| |
| promise_test(function (t) { |
| mockAuthenticator.setAuthenticatorStatus( |
| webauth.mojom.AuthenticatorStatus.CANCELLED); |
| return promise_rejects(t, "NotAllowedError", |
| navigator.credentials.create({ publicKey })); |
| }, "Verify that cancelled error returned by mock is properly handled."); |
| |
| promise_test(function (t) { |
| mockAuthenticator.setAuthenticatorStatus( |
| webauth.mojom.AuthenticatorStatus.UNKNOWN_ERROR); |
| return promise_rejects(t, "NotReadableError", |
| navigator.credentials.create({ publicKey })); |
| }, "Verify that unknown error returned by mock is properly handled."); |
| |
| promise_test(function (t) { |
| mockAuthenticator.setAuthenticatorStatus( |
| webauth.mojom.AuthenticatorStatus.NOT_ALLOWED_ERROR); |
| return promise_rejects(t, "NotAllowedError", |
| navigator.credentials.create({ publicKey })); |
| }, "Verify that not allowed error returned by mock is properly handled."); |
| |
| promise_test(function (t) { |
| mockAuthenticator.setAuthenticatorStatus( |
| webauth.mojom.AuthenticatorStatus.NOT_SUPPORTED_ERROR); |
| return promise_rejects(t, "NotSupportedError", |
| navigator.credentials.create({ publicKey })); |
| }, "Verify that not supported error returned by mock is properly handled."); |
| |
| promise_test(function (t) { |
| mockAuthenticator.setAuthenticatorStatus( |
| webauth.mojom.AuthenticatorStatus.SECURITY_ERROR); |
| return promise_rejects(t, "SecurityError", |
| navigator.credentials.create({ publicKey })); |
| }, "Verify that security error returned by mock is properly handled."); |
| |
| promise_test(function (t) { |
| mockAuthenticator.setAuthenticatorStatus(webauth.mojom.AuthenticatorStatus.NOT_IMPLEMENTED); |
| return promise_rejects(t, "NotAllowedError", |
| navigator.credentials.create({ publicKey })); |
| }, 'Verify that not implemented error returned by mock is properly handled.'); |
| |
| promise_test(function(t) { |
| var publicKey = { |
| // No challenge. |
| rp: public_key_rp, |
| user: public_key_user, |
| parameters: public_key_parameters, |
| }; |
| |
| return promise_rejects(t, new TypeError(), |
| navigator.credentials.create({publicKey})); |
| }, "navigator.credentials.create() with missing challenge"); |
| |
| promise_test(function(t) { |
| var publicKey = { |
| challenge, |
| rp: public_key_rp, |
| user: public_key_user, |
| // No parameters. |
| }; |
| |
| return promise_rejects(t, new TypeError(), |
| navigator.credentials.create({publicKey})); |
| }, "navigator.credentials.create() with missing parameters"); |
| |
| promise_test(function(t) { |
| var custom_public_key = { |
| challenge, |
| // No rp. |
| user: public_key_user, |
| parameters: public_key_parameters, |
| }; |
| |
| return promise_rejects(t, new TypeError(), |
| navigator.credentials.create({publicKey: custom_public_key})); |
| }, "navigator.credentials.create() with missing rp"); |
| |
| promise_test(function(t) { |
| var custom_public_key = { |
| challenge, |
| rp: public_key_rp, |
| // No user. |
| parameters: public_key_parameters, |
| }; |
| |
| return promise_rejects(t, new TypeError(), |
| navigator.credentials.create({publicKey: custom_public_key})); |
| }, "navigator.credentials.create() with missing user"); |
| |
| promise_test(function(t) { |
| mockAuthenticator.reset(); |
| mockAuthenticator.setRawId(raw_id); |
| mockAuthenticator.setId(id); |
| mockAuthenticator.setClientDataJson(client_data_json); |
| mockAuthenticator.setAttestationObject(attestation_object); |
| mockAuthenticator.setAuthenticatorStatus( |
| webauth.mojom.AuthenticatorStatus.SUCCESS); |
| |
| var publicKey = { |
| challenge, |
| rp: { name: "Acme" }, |
| user: public_key_user, |
| parameters: public_key_parameters, |
| }; |
| |
| return navigator.credentials.create({publicKey: publicKey}).then(r => { |
| assert_equals(r.id, id, 'id'); |
| assert_true(r.rawId instanceof ArrayBuffer); |
| assert_array_equals(new Uint8Array(r.rawId), |
| raw_id, "rawId returned is the same"); |
| assert_true(r.response instanceof AuthenticatorAttestationResponse); |
| assert_true(r.response.clientDataJSON instanceof ArrayBuffer); |
| assert_array_equals(new Uint8Array(r.response.clientDataJSON), |
| client_data_json, "clientDataJSON returned is the same"); |
| assert_true(r.response.attestationObject instanceof ArrayBuffer); |
| assert_array_equals(new Uint8Array(r.response.attestationObject), |
| attestation_object, "attestationObject returned is the same"); |
| assert_not_exists(r.response, 'authenticatorData'); |
| assert_not_exists(r.response, 'signature'); |
| }); |
| }, "navigator.credentials.create() with missing rp.id"); |
| |
| |
| promise_test(function(t) { |
| var custom_public_key = { |
| challenge, |
| rp: { id: "1098237235409872" }, |
| user: public_key_user, |
| parameters: public_key_parameters, |
| }; |
| |
| return promise_rejects(t, "NotSupportedError", |
| navigator.credentials.create({publicKey: custom_public_key})); |
| }, "navigator.credentials.create() with missing rp.name"); |
| |
| promise_test(function(t) { |
| var custom_public_key = { |
| challenge, |
| rp: public_key_rp, |
| user: { |
| name: "avery.a.jones@example.com", |
| displayName: "Avery A. Jones", |
| icon: "https://pics.acme.com/00/p/aBjjjpqPb.png" |
| }, |
| parameters: public_key_parameters, |
| }; |
| |
| return promise_rejects(t, "NotSupportedError", |
| navigator.credentials.create({publicKey: custom_public_key})); |
| }, "navigator.credentials.create() with missing user.id"); |
| |
| promise_test(function(t) { |
| var custom_public_key = { |
| challenge, |
| rp: public_key_rp, |
| user: { |
| id: "1098237235409872", |
| displayName: "Avery A. Jones", |
| icon: "https://pics.acme.com/00/p/aBjjjpqPb.png" |
| }, |
| parameters: public_key_parameters, |
| timeout: 60000, // 1 minute |
| excludeList: [], // No excludeList |
| }; |
| |
| return promise_rejects(t, "NotSupportedError", |
| navigator.credentials.create({publicKey: custom_public_key})); |
| }, "navigator.credentials.create() with missing user.name"); |
| |
| promise_test(function(t) { |
| var custom_public_key = { |
| challenge, |
| rp: public_key_rp, |
| user: { |
| id: "1098237235409872", |
| name: "avery.a.jones@example.com", |
| icon: "https://pics.acme.com/00/p/aBjjjpqPb.png" |
| }, |
| parameters: public_key_parameters, |
| }; |
| |
| return promise_rejects(t, "NotSupportedError", |
| navigator.credentials.create({publicKey: custom_public_key})); |
| }, "navigator.credentials.create() with missing user.displayName"); |
| |
| promise_test(function(t) { |
| mockAuthenticator.reset(); |
| mockAuthenticator.setRawId(raw_id); |
| mockAuthenticator.setId(id); |
| mockAuthenticator.setClientDataJson(client_data_json); |
| mockAuthenticator.setAttestationObject(attestation_object); |
| mockAuthenticator.setAuthenticatorStatus( |
| webauth.mojom.AuthenticatorStatus.SUCCESS); |
| |
| var public_key = { |
| challenge, |
| rp: public_key_rp, |
| user: { |
| id: "1098237235409872", |
| name: "avery.a.jones@example.com", |
| displayName: "Avery A. Jones", |
| }, |
| parameters: public_key_parameters, |
| }; |
| |
| return navigator.credentials.create({publicKey: public_key}).then(r => { |
| assert_equals(r.id, id, 'id'); |
| assert_true(r.rawId instanceof ArrayBuffer); |
| assert_array_equals(new Uint8Array(r.rawId), |
| raw_id, "rawId returned is the same"); |
| assert_true(r.response instanceof AuthenticatorAttestationResponse); |
| assert_true(r.response.clientDataJSON instanceof ArrayBuffer); |
| assert_array_equals(new Uint8Array(r.response.clientDataJSON), |
| client_data_json, "clientDataJSON returned is the same"); |
| assert_true(r.response.attestationObject instanceof ArrayBuffer); |
| assert_array_equals(new Uint8Array(r.response.attestationObject), |
| attestation_object, "attestationObject returned is the same"); |
| assert_not_exists(r.response, 'authenticatorData'); |
| assert_not_exists(r.response, 'signature'); |
| }); |
| }, "navigator.credentials.create() with missing user.icon"); |
| |
| </script> |