From e54da630b4b5b8164137471ad5895011300e41a1 Mon Sep 17 00:00:00 2001
From: Lloyd Hilaiel <lloyd@hilaiel.com>
Date: Thu, 3 May 2012 00:03:46 -0600
Subject: [PATCH] don't require that a user re-enter their password in dialog
 after email verification in a different browser.  Because the user just typed
 their password, attempt to authenticate them in the verification originating
 browser via the API.  If that works, then automatically log them in

---
 .../static/dialog/controllers/actions.js      |  7 +++---
 .../dialog/controllers/check_registration.js  | 24 ++++++++++++++++---
 resources/static/dialog/resources/helpers.js  |  2 +-
 3 files changed, 26 insertions(+), 7 deletions(-)

diff --git a/resources/static/dialog/controllers/actions.js b/resources/static/dialog/controllers/actions.js
index 4746c25b8..322749710 100644
--- a/resources/static/dialog/controllers/actions.js
+++ b/resources/static/dialog/controllers/actions.js
@@ -31,12 +31,13 @@ BrowserID.Modules.Actions = (function() {
     return module;
   }
 
-  function startRegCheckService(options, verifier, message) {
+  function startRegCheckService(options, verifier, message, password) {
     var controller = startService("check_registration", {
       email: options.email,
       required: options.required,
       verifier: verifier,
-      verificationMessage: message
+      verificationMessage: message,
+      password: password
     });
     controller.startCheck();
   }
@@ -82,7 +83,7 @@ BrowserID.Modules.Actions = (function() {
     },
 
     doConfirmUser: function(info) {
-      startRegCheckService.call(this, info, "waitForUserValidation", "user_confirmed");
+      startRegCheckService.call(this, info, "waitForUserValidation", "user_confirmed", info.password || undefined);
     },
 
     doPickEmail: function(info) {
diff --git a/resources/static/dialog/controllers/check_registration.js b/resources/static/dialog/controllers/check_registration.js
index 3389f3f9e..efd38e246 100644
--- a/resources/static/dialog/controllers/check_registration.js
+++ b/resources/static/dialog/controllers/check_registration.js
@@ -23,6 +23,7 @@ BrowserID.Modules.CheckRegistration = (function() {
       self.verifier = options.verifier;
       self.verificationMessage = options.verificationMessage;
       self.required = options.required;
+      self.password = options.password;
 
       self.click("#back", self.back);
 
@@ -40,9 +41,26 @@ BrowserID.Modules.CheckRegistration = (function() {
           });
         }
         else if (status === "mustAuth") {
-          user.addressInfo(self.email, function(info) {
-            self.close("authenticate", info);
-          });
+          // if we have a password (because it was just chosen in dialog),
+          // then we can authenticate the user and proceed
+          if (self.password) {
+            user.authenticate(self.email, self.password, function (authenticated) {
+              if (authenticated) {
+                user.syncEmails(function() {
+                  self.close(self.verificationMessage);
+                  oncomplete && oncomplete();
+                });
+              } else {
+                user.addressInfo(self.email, function(info) {
+                  self.close("authenticate", info);
+                });
+              }
+            });
+          } else {
+            user.addressInfo(self.email, function(info) {
+              self.close("authenticate", info);
+            });
+          }
 
           oncomplete && oncomplete();
         }
diff --git a/resources/static/dialog/resources/helpers.js b/resources/static/dialog/resources/helpers.js
index 5065a994c..e95f73886 100644
--- a/resources/static/dialog/resources/helpers.js
+++ b/resources/static/dialog/resources/helpers.js
@@ -62,7 +62,7 @@
     var self=this;
     user.createSecondaryUser(email, password, function(status) {
       if (status) {
-        var info = { email: email };
+        var info = { email: email, password: password };
         self.publish("user_staged", info, info);
         complete(callback, true);
       }
-- 
GitLab