diff --git a/browserid/static/dialog/resources/user.js b/browserid/static/dialog/resources/user.js index 8bf2da890435d79a0e47d6537f5bad6f9b9b71ff..93169a2501ad528582b27873f6320a13653c818c 100644 --- a/browserid/static/dialog/resources/user.js +++ b/browserid/static/dialog/resources/user.js @@ -40,7 +40,8 @@ BrowserID.User = (function() { var jwk, jwt, vep, jwcert, origin, network = BrowserID.Network, - storage = BrowserID.Storage; + storage = BrowserID.Storage, + User; function prepareDeps() { if (!jwk) { @@ -127,7 +128,64 @@ BrowserID.User = (function() { poll(); } - var User = { + + /** + * Certify an identity with the server, persist it to storage if the server + * says the identity is good + * @method certifyEmailKeypair + */ + function certifyEmailKeypair(email, keypair, onSuccess, onFailure) { + network.certKey(email, keypair.publicKey, function(cert) { + persistEmailKeypair(email, keypair, cert, onSuccess, onFailure); + }, onFailure); + } + + /** + * Persist an email address without a keypair + * @method persistEmail + * @param {string} email - Email address to persist. + * @param {function} [onSuccess] - Called on successful completion. + * @param {function} [onFailure] - Called on error. + */ + function persistEmail(email, onSuccess, onFailure) { + storage.addEmail(email, { + created: new Date() + }); + + if (onSuccess) { + onSuccess(); + } + } + + /** + * Persist an address and key pair locally. + * @method persistEmailKeypair + * @param {string} email - Email address to persist. + * @param {object} keypair - Key pair to save + * @param {function} [onSuccess] - Called on successful completion. + * @param {function} [onFailure] - Called on error. + */ + function persistEmailKeypair(email, keypair, cert, onSuccess, onFailure) { + var now = new Date(); + var email_obj = storage.getEmails()[email] || { + created: now + }; + + _.extend(email_obj, { + updated: now, + pub: keypair.publicKey.toSimpleObject(), + priv: keypair.secretKey.toSimpleObject(), + cert: cert + }); + + storage.addEmail(email, email_obj); + + if (onSuccess) { + onSuccess(); + } + } + + User = { /** * Set the interface to use for networking. Used for unit testing. * @method setNetwork @@ -171,10 +229,6 @@ BrowserID.User = (function() { network.createUser(email, origin, function(created) { if (onSuccess) { - if(created) { - self.stagedEmail = email; - } - onSuccess(created); } }, onFailure); @@ -293,36 +347,13 @@ BrowserID.User = (function() { var email = emails_to_add.shift(); - self.persistEmail(email, addNextEmail, onFailure); + persistEmail(email, addNextEmail, onFailure); } addNextEmail(); }); }, - /** - * Signifies that an identity has been confirmed. - * @method confirmEmail - * @param {string} email - Email address. - * @param {function} [onSuccess] - Called on successful completion. - * @param {function} [onFailure] - Called on error. - */ - confirmEmail: function(email, onSuccess, onFailure) { - var self = this; - if (email === self.stagedEmail) { - self.stagedEmail = null; - - // certify - this.persistEmail(email, function() { - self.syncEmails(onSuccess, onFailure); - }); - - } - else if (onFailure) { - onFailure(); - } - }, - /** * Check whether the current user is authenticated. * @method checkAuthentication @@ -431,8 +462,6 @@ BrowserID.User = (function() { var self = this; network.addEmail(email, origin, function(added) { if (added) { - self.stagedEmail = email; - // we no longer send the keypair, since we will certify it later. if (onSuccess) { onSuccess(added); @@ -485,63 +514,9 @@ BrowserID.User = (function() { // FIXME use true key sizes prepareDeps(); var keypair = jwk.KeyPair.generate(vep.params.algorithm, 64); - this.certifyEmailKeypair(email, keypair, onSuccess, onFailure); - }, - - /** - * Certify an identity. - * @method certifyEmailKeypair - */ - certifyEmailKeypair: function(email, keypair, onSuccess, onFailure) { - network.certKey(email, keypair.publicKey, function(cert) { - User.persistEmailKeypair(email, keypair, cert, onSuccess, onFailure); - }, onFailure); - }, - - /** - * Persist an email address without a keypair - * @method persistEmail - * @param {string} email - Email address to persist. - * @param {function} [onSuccess] - Called on successful completion. - * @param {function} [onFailure] - Called on error. - */ - persistEmail: function(email, onSuccess, onFailure) { - storage.addEmail(email, { - created: new Date() - }); - - if (onSuccess) { - onSuccess(); - } + certifyEmailKeypair(email, keypair, onSuccess, onFailure); }, - /** - * Persist an address and key pair locally. - * @method persistEmailKeypair - * @param {string} email - Email address to persist. - * @param {object} keypair - Key pair to save - * @param {function} [onSuccess] - Called on successful completion. - * @param {function} [onFailure] - Called on error. - */ - persistEmailKeypair: function(email, keypair, cert, onSuccess, onFailure) { - var now = new Date(); - var email_obj = storage.getEmails()[email] || { - created: now - }; - - _.extend(email_obj, { - updated: now, - pub: keypair.publicKey.toSimpleObject(), - priv: keypair.secretKey.toSimpleObject(), - cert: cert - }); - - storage.addEmail(email, email_obj); - - if (onSuccess) { - onSuccess(); - } - }, /** * Get an assertion for an identity diff --git a/browserid/static/dialog/test/qunit/user_unit_test.js b/browserid/static/dialog/test/qunit/user_unit_test.js index eeb4d71bbd2325c4ee4ddeed17019e5dfcbf9aea..d9516191bbdaf3356bf018ef9a494c91337342df 100644 --- a/browserid/static/dialog/test/qunit/user_unit_test.js +++ b/browserid/static/dialog/test/qunit/user_unit_test.js @@ -250,50 +250,6 @@ steal.plugins("jquery", "funcunit/qunit").then("/dialog/resources/user", functio stop(); }); - test("confirmEmail on staged identity", function() { - lib.createUser("testuser@testuser.com", function(created) { - lib.confirmEmail("testuser@testuser.com", function() { - ok(true, "confirming staged identity"); - start(); - }); - }, failure("createUser failure")); - - stop(); - }); - - - test("confirmEmail on non staged identity", function() { - lib.createUser("testuser@testuser.com", function(created) { - lib.confirmEmail("testuser2@testuser.com", function onSuccess() { - ok(false, "confirming unstaged identity"); - start(); - }, function onFailure() { - ok(true, "confirming unstaged identity should fail"); - start(); - }); - }, failure("createUser failure")); - - stop(); - }); - - - test("confirmEmail on previously confirmed identity", function() { - lib.createUser("testuser@testuser.com", function(created) { - lib.confirmEmail("testuser@testuser.com", function() { - lib.confirmEmail("testuser@testuser.com", function() { - ok(false, "confirming previously confirmed identity should fail"); - start(); - }, function onFailure() { - ok(true, "confirming previously confirmed identity should fail"); - start(); - }); - }); - }, failure("createUser failure")); - - stop(); - }); - - test("authenticateAndSync with valid credentials", function() { lib.authenticateAndSync("testuser@testuser.com", "testuser", function() { @@ -483,65 +439,6 @@ steal.plugins("jquery", "funcunit/qunit").then("/dialog/resources/user", functio }); - test("persistEmail", function() { - lib.persistEmail("testemail@testemail.com", function onSuccess() { - var identities = lib.getStoredEmailKeypairs(); - ok("testemail@testemail.com" in identities, "Our new email is added"); - start(); - }); - - stop(); - }); - - - test("persistEmailKeypair with new email", function() { - var user_kp = jwk.KeyPair.generate("RS",64); - lib.persistEmailKeypair("testemail@testemail.com", user_kp, "cert", function onSuccess() { - var id = lib.getStoredEmailKeypairs()["testemail@testemail.com"]; - - ok(id, "Email is added"); - ok(id.created, "A create date is generated"); - ok(id.updated, "An updated date is generated"); - equal(id.created, id.updated, "Create and update dates are the same"); - - ok(id.pub, "A public key is generated"); - ok(id.priv, "A private key is generated"); - ok(id.cert, "A certificate is generated"); - - start(); - }); - - stop(); - }); - - test("persistEmailKeypair with already saved email", function() { - var user_kp = jwk.KeyPair.generate("RS",64); - lib.persistEmailKeypair("testemail@testemail.com", user_kp, "cert", function onSuccess() { - setTimeout(function() { - lib.persistEmailKeypair("testemail@testemail.com", user_kp, "cert", function onSuccess() { - - var id = lib.getStoredEmailKeypairs()["testemail@testemail.com"]; - - ok(id, "Email is added"); - ok(id.created, "A create date is generated"); - ok(id.updated, "An updated date is generated"); - notEqual(id.created, id.updated, "Create and update dates are NOT the same when an address is updated"); - - ok(id.pub, "A public key is generated"); - ok(id.priv, "A private key is generated"); - ok(id.cert, "A certificate is generated"); - - start(); - }); - }, 500); - }); - - stop(); - - }); - - - test("removeEmail that is added", function() { storage.addEmail("testemail@testemail.com", {pub: "pub", priv: "priv"}); @@ -643,33 +540,30 @@ steal.plugins("jquery", "funcunit/qunit").then("/dialog/resources/user", functio test("getAssertion with known email that has key", function() { - var keypair = jwk.KeyPair.generate("RS",64); - lib.certifyEmailKeypair("testuser@testuser.com", keypair, function() { + lib.syncEmailKeypair("testuser@testuser.com", function() { lib.getAssertion("testuser@testuser.com", function onSuccess(assertion) { equal("string", typeof assertion, "we have an assertion!"); start(); }, failure("getAssertion failure")); - }, failure("certifyEmailKeypair failure")); + }, failure("syncEmailKeypair failure")); stop(); }); test("getAssertion with known email that does not have a key", function() { - lib.persistEmail("testuser@testuser.com", function() { - lib.getAssertion("testuser@testuser.com", function onSuccess(assertion) { - equal("string", typeof assertion, "we have an assertion!"); - start(); - }, failure("getAssertion failure")); - }, failure("persistEmail failure")); + storage.addEmail("testuser@testuser.com", {}); + lib.getAssertion("testuser@testuser.com", function onSuccess(assertion) { + equal("string", typeof assertion, "we have an assertion!"); + start(); + }, failure("getAssertion failure")); stop(); }); test("getAssertion with unknown email", function() { - var keypair = jwk.KeyPair.generate("RS",64); - lib.certifyEmailKeypair("testuser@testuser.com", keypair, function() { + lib.syncEmailKeypair("testuser@testuser.com", function() { lib.getAssertion("testuser2@testuser.com", function onSuccess(assertion) { equal("undefined", typeof assertion, "email was unknown, we do not have an assertion"); start();