From 7645bf8c7a074d382e492cd76d9d65963a502da0 Mon Sep 17 00:00:00 2001
From: Shane Tomlinson <stomlinson@mozilla.com>
Date: Tue, 24 Apr 2012 10:49:19 +0100
Subject: [PATCH] Add a cookie check to most main site pages.

* signin, signup, forgot, add_email_address, verify_email_address.
* to user.js->checkAuthentication, call the callback if cookies are disabled.

issue #1418
issue #1484
---
 resources/static/pages/start.js | 124 +++++++++++++++++---------------
 resources/static/shared/user.js |  20 +++---
 2 files changed, 78 insertions(+), 66 deletions(-)

diff --git a/resources/static/pages/start.js b/resources/static/pages/start.js
index 0fdebc814..e781afe91 100644
--- a/resources/static/pages/start.js
+++ b/resources/static/pages/start.js
@@ -20,9 +20,10 @@ $(function() {
       path = document.location.pathname,
       moduleManager = bid.module,
       modules = bid.Modules,
-      CodeCheck = modules.CodeCheck,
+      CookieCheck = modules.CookieCheck,
       XHRDelay = modules.XHRDelay,
-      XHRDisableForm = modules.XHRDisableForm;
+      XHRDisableForm = modules.XHRDisableForm,
+      ANIMATION_TIME = 500;
 
 
   xhr.init({ time_until_delay: 10 * 1000 });
@@ -33,76 +34,83 @@ $(function() {
     $(window).bind('resize', function() { $('#vAlign').css({'height' : $(window).height() }); }).trigger('resize');
   }
 
-  dom.addClass("body", "ready");
-
   moduleManager.register("xhr_delay", XHRDelay);
   moduleManager.start("xhr_delay");
 
   moduleManager.register("xhr_disable_form", XHRDisableForm);
   moduleManager.start("xhr_disable_form");
 
-  if (!path || path === "/") {
-    bid.index();
-  }
-  else if (path === "/signin") {
-    var module = bid.signIn.create();
-    module.start({});
-  }
-  else if (path === "/signup") {
-    bid.signUp();
-  }
-  else if (path === "/forgot") {
-    bid.forgot();
-  }
-  else if (path === "/add_email_address") {
-    var module = bid.addEmailAddress.create();
-    module.start({
-      token: token
-    });
+  if(path && path !== "/") {
+    // do a cookie check on every page except the main page.
+    moduleManager.register("cookie_check", CookieCheck);
+    moduleManager.start("cookie_check", { ready: start });
   }
-  else if(token && path === "/verify_email_address") {
-    bid.verifyEmailAddress(token);
+  else {
+    // the main page makes it through without checking for cookies.
+    start(true);
   }
 
-  $("a.signOut").click(function(event) {
-    event.preventDefault();
-    event.stopPropagation();
-
-    user.logoutUser(function() {
-      document.location = "/";
-    }, pageHelpers.getFailure(bid.Errors.logout));
-  });
-
-  var ANIMATION_TIME = 500;
-  network.cookiesEnabled(function(cookiesEnabled) {
-    if(cookiesEnabled) {
-      user.checkAuthentication(function(authenticated) {
-        if (authenticated) {
-          displayAuthenticated();
-        }
-        else {
-          displayNonAuthenticated();
-        }
+  function start(status) {
+    if(!status) return;
+
+    dom.addClass("body", "ready");
+
+    if (!path || path === "/") {
+      bid.index();
+    }
+    else if (path === "/signin") {
+      var module = bid.signIn.create();
+      module.start({});
+    }
+    else if (path === "/signup") {
+      bid.signUp();
+    }
+    else if (path === "/forgot") {
+      bid.forgot();
+    }
+    else if (path === "/add_email_address") {
+      var module = bid.addEmailAddress.create();
+      module.start({
+        token: token
       });
     }
-    else {
-      displayNonAuthenticated();
+    else if(token && path === "/verify_email_address") {
+      bid.verifyEmailAddress(token);
     }
-  });
-
-  function displayAuthenticated() {
-    $(".display_always").fadeIn(ANIMATION_TIME);
-    dom.addClass("body", "authenticated");
-    $(".display_auth").fadeIn(ANIMATION_TIME);
-    if ($('#emailList').length) {
-      bid.manageAccount();
+
+    user.checkAuthentication(function(authenticated) {
+      if (authenticated) {
+        displayAuthenticated();
+      }
+      else {
+        displayNonAuthenticated();
+      }
+    });
+
+    function displayAuthenticated() {
+      $(".display_always,.display_auth").fadeIn(ANIMATION_TIME);
+      dom.addClass("body", "authenticated");
+
+      if ($('#emailList').length) {
+        bid.manageAccount();
+      }
+
+      $("a.signOut").click(function(event) {
+        event.preventDefault();
+        event.stopPropagation();
+
+        user.logoutUser(function() {
+          document.location = "/";
+        }, pageHelpers.getFailure(bid.Errors.logout));
+      });
     }
-  }
 
-  function displayNonAuthenticated() {
-    $(".display_always").fadeIn(ANIMATION_TIME);
-    dom.addClass("body", "not_authenticated");
-    $(".display_nonauth").fadeIn(ANIMATION_TIME);
+    function displayNonAuthenticated() {
+      $(".display_always").fadeIn(ANIMATION_TIME);
+      dom.addClass("body", "not_authenticated");
+      $(".display_nonauth").fadeIn(ANIMATION_TIME);
+    }
   }
+
 });
 
diff --git a/resources/static/shared/user.js b/resources/static/shared/user.js
index 48778cd31..aee3065b9 100644
--- a/resources/static/shared/user.js
+++ b/resources/static/shared/user.js
@@ -648,9 +648,7 @@ BrowserID.User = (function() {
       // log out of browserid
       network.logout(function() {
         setAuthenticationStatus(false);
-        if (onComplete) {
-          onComplete();
-        }
+        complete(onComplete);
       }, onFailure);
     },
 
@@ -700,7 +698,8 @@ BrowserID.User = (function() {
     },
 
     /**
-     * Check whether the current user is authenticated.
+     * Check whether the current user is authenticated.  Calls the callback
+     * with false if cookies are disabled.
      * @method checkAuthentication
      * @param {function} [onComplete] - Called when check is complete with one
      * boolean parameter, authenticated.  authenticated will be true if user is
@@ -708,10 +707,15 @@ BrowserID.User = (function() {
      * @param {function} [onFailure] - Called on error.
      */
     checkAuthentication: function(onComplete, onFailure) {
-      network.checkAuth(function(authenticated) {
-        setAuthenticationStatus(authenticated);
-        if (onComplete) {
-          onComplete(authenticated);
+      network.cookiesEnabled(function(cookiesEnabled) {
+        if(cookiesEnabled) {
+          network.checkAuth(function(authenticated) {
+            setAuthenticationStatus(authenticated);
+            complete(onComplete, authenticated);
+          }, onFailure);
+        }
+        else {
+          complete(onComplete, cookiesEnabled);
         }
       }, onFailure);
     },
-- 
GitLab