diff --git a/browserid/static/dialog/controllers/dialog_controller.js b/browserid/static/dialog/controllers/dialog_controller.js index 1d8224aecde9d56e9017f40344da48ac0a83cb3b..924f77cbb3e9e8e5fe6e879f1ed48f85200af6a1 100644 --- a/browserid/static/dialog/controllers/dialog_controller.js +++ b/browserid/static/dialog/controllers/dialog_controller.js @@ -97,7 +97,12 @@ }); hub.subscribe("assertion_generated", function(msg, info) { - self.doAssertionGenerated(info.assertion); + if(info.assertion !== null) { + self.doAssertionGenerated(info.assertion); + } + else { + self.doPickEmail(); + } }); hub.subscribe("email_staged", function(msg, info) { @@ -147,7 +152,7 @@ } }, - doSignIn: function() { + doPickEmail: function() { this.element.pickemail(); }, @@ -192,7 +197,7 @@ syncEmails: function() { var self = this; - user.syncEmails(self.doSignIn.bind(self), + user.syncEmails(self.doPickEmail.bind(self), self.getErrorDialog(BrowserID.Errors.signIn)); }, @@ -202,7 +207,7 @@ user.checkAuthenticationAndSync(function onSuccess() {}, function onComplete(authenticated) { if (authenticated) { - self.doSignIn(); + self.doPickEmail(); } else { self.doAuthenticate(); } diff --git a/browserid/static/dialog/controllers/pickemail_controller.js b/browserid/static/dialog/controllers/pickemail_controller.js index 49e6a018c5268c041dcafbedc97d7c123dc9e52c..86358f9e95b308c1c9224f2324b8fef6d5ccfebe 100644 --- a/browserid/static/dialog/controllers/pickemail_controller.js +++ b/browserid/static/dialog/controllers/pickemail_controller.js @@ -39,7 +39,10 @@ var ANIMATION_TIME = 250, bid = BrowserID, - user = bid.User; + user = bid.User, + body = $("body"), + animationComplete = body.innerWidth() < 640, + assertion; function animateSwap(fadeOutSelector, fadeInSelector, callback) { // XXX instead of using jQuery here, think about using CSS animations. @@ -80,41 +83,60 @@ }); } - function signIn(element, event) { - var self=this, - body = $("body"), - animationComplete = body.innerWidth() < 640, - assertion, - email = $("input[type=radio]:checked").val(); - + function checkEmail(email) { + var identity = user.getStoredEmailKeypair(email); + if(!identity) { + alert("The selected email is invalid or has been deleted."); + this.close("assertion_generated", { + assertion: null + }); + } - cancelEvent(event); + return !!identity; + } - function onComplete() { - if(assertion && animationComplete) { - self.close("assertion_generated", { - assertion: assertion - }); - } + function tryClose() { + if(typeof assertion !== "undefined" && animationComplete) { + this.close("assertion_generated", { + assertion: assertion + }); } + } + function getAssertion(email) { // Kick of the assertion fetching/keypair generating while we are showing // the animation, hopefully this minimizes the delay the user notices. + var self=this; user.getAssertion(email, function(assert) { - assertion = assert; - onComplete(); + assertion = assert || null; + tryClose.call(self); }); + } - - if(body.innerWidth() > 640) { + function startAnimation() { + if(!animationComplete) { + var self=this; $("#signIn").animate({"width" : "685px"}, "slow", function () { // post animation body.delay(500).animate({ "opacity" : "0.5"}, "fast", function () { animationComplete = true; - onComplete(); + tryClose.call(self); }); }); } + + } + + function signIn(element, event) { + cancelEvent(event); + var self=this, + email = $("input[type=radio]:checked").val(); + + var valid = checkEmail.call(self, email); + if (valid) { + getAssertion.call(self, email); + startAnimation.call(self); + } } function addEmail(element, event) { @@ -160,6 +182,8 @@ } }); + $("body").css("opacity", "1"); + pickEmailState.call(this); }, diff --git a/browserid/static/dialog/resources/user.js b/browserid/static/dialog/resources/user.js index 53e4a69fc96f7eeaec5cfc8388911de7271b7661..75a3d51a8bb7810dac8f5f3e2efc2f88095eb845 100644 --- a/browserid/static/dialog/resources/user.js +++ b/browserid/static/dialog/resources/user.js @@ -571,6 +571,16 @@ BrowserID.User = (function() { return storage.getEmails(); }, + /** + * Get an individual stored identity. + * @method getStoredEmailKeypair + * @return {object} identity information for email, if exists, undefined + * otw. + */ + getStoredEmailKeypair: function(email) { + return storage.getEmail(email); + }, + /** * Clear the list of identities stored locally. * @method clearStoredEmailKeypairs diff --git a/browserid/static/dialog/test/qunit/user_unit_test.js b/browserid/static/dialog/test/qunit/user_unit_test.js index d9516191bbdaf3356bf018ef9a494c91337342df..96a0c2db0641569d7cf60f808044a94536290bff 100644 --- a/browserid/static/dialog/test/qunit/user_unit_test.js +++ b/browserid/static/dialog/test/qunit/user_unit_test.js @@ -188,6 +188,23 @@ steal.plugins("jquery", "funcunit/qunit").then("/dialog/resources/user", functio equal("object", typeof identities, "we have some identities"); }); + test("getStoredEmailKeypair with known key", function() { + lib.syncEmailKeypair("testuser@testuser.com", function() { + var identity = lib.getStoredEmailKeypair("testuser@testuser.com"); + + ok(identity, "we have an identity"); + start(); + }, failure("syncEmailKeypair failure")); + + stop(); + }); + + test("getStoredEmailKeypair with unknown key", function() { + var identity = lib.getStoredEmailKeypair("testuser@testuser.com"); + + equal(typeof identity, "undefined", "identity is undefined for unknown key"); + }); + test("clearStoredEmailKeypairs", function() { lib.clearStoredEmailKeypairs(); var identities = lib.getStoredEmailKeypairs(); @@ -413,8 +430,11 @@ steal.plugins("jquery", "funcunit/qunit").then("/dialog/resources/user", functio test("syncEmailKeypair with successful sync", function() { syncValid = true; lib.syncEmailKeypair("testemail@testemail.com", function(keypair) { - var identities = lib.getStoredEmailKeypairs(); - ok("testemail@testemail.com" in identities, "Valid email is synced"); + var identity = lib.getStoredEmailKeypair("testuser@testuser.com"); + + ok(identity, "we have an identity"); + ok(identity.priv, "a private key is on the identity"); + ok(identity.pub, "a private key is on the identity"); start(); }, failure("syncEmailKeypair failure")); @@ -429,8 +449,8 @@ steal.plugins("jquery", "funcunit/qunit").then("/dialog/resources/user", functio ok(false, "sync was invalid, this should have failed"); start(); }, function() { - var identities = lib.getStoredEmailKeypairs(); - equal("testemail@testemail.com" in identities, false, "Invalid email is not synced"); + var identity = lib.getStoredEmailKeypair("testemail@testemail.com"); + equal(typeof identity, "undefined", "Invalid email is not synced"); start(); });