From ab131350aee6740cc7f634f8ca89c8b419c6ec0e Mon Sep 17 00:00:00 2001
From: Lloyd Hilaiel <lloyd@hilaiel.com>
Date: Mon, 12 Mar 2012 13:04:54 -0600
Subject: [PATCH] add to storage.js functions related to managing our knowledge
 of users that have used this computer and users that have confirmed ownership
 of this computer - for the purposes of setting optimal authentication
 durations

---
 resources/static/shared/storage.js | 47 +++++++++++++++++++++++++++++-
 1 file changed, 46 insertions(+), 1 deletion(-)

diff --git a/resources/static/shared/storage.js b/resources/static/shared/storage.js
index b21fbbcbe..f2297080f 100644
--- a/resources/static/shared/storage.js
+++ b/resources/static/shared/storage.js
@@ -197,7 +197,7 @@ BrowserID.Storage = (function() {
       };
     }
 
-    // does IE8 not have addEventListener, nor does it support storage events.
+    // IE8 does not have addEventListener, nor does it support storage events.
     if (window.addEventListener) window.addEventListener('storage', checkState, false);
     else window.setInterval(checkState, 2000);
   }
@@ -205,6 +205,31 @@ BrowserID.Storage = (function() {
     storage.loggedIn = "{}";
   }
 
+  // tools to manage knowledge of whether this is the user's computer, which helps
+  // us set appropriate authentication duration.
+  function userConfirmedOnComputer(userid) {
+    var allInfo = JSON.parse(storage.usersComputer || "{}");
+    return allInfo[userid] === 'confirmed';
+  }
+  function userSeenOnComputer(userid) {
+    var allInfo = JSON.parse(storage.usersComputer || "{}");
+    return !!(allInfo[userid]); // "seen" or "confirmed"
+  }
+  function setUserSeenOnComputer(userid) {
+    var allInfo = JSON.parse(storage.usersComputer || "{}");
+    if (!allInfo[userid]) {
+      allInfo[userid] = "seen";
+      storage.usersComputer = JSON.stringify(allInfo);
+    }
+  }
+  function setUserConfirmedOnComputer(userid) {
+    var allInfo = JSON.parse(storage.usersComputer || "{}");
+    if (allInfo[userid] !== 'confirmed') {
+      allInfo[userid] = 'confirmed';
+      storage.usersComputer = JSON.stringify(allInfo);
+    }
+  }
+
   return {
     /**
      * Add an email address and optional key pair.
@@ -271,6 +296,26 @@ BrowserID.Storage = (function() {
       remove: managePageRemove
     },
 
+    usersComputer: {
+      /** Query whether the user has confirmed that this is their computer
+       * @param {string} userid - the user's numeric id, returned from session_context when authed.
+       * @method usersComputer.confirmed */
+      confirmed: userConfirmedOnComputer,
+      /** Save the fact that a user confirmed that this is their computer
+       * @param {string} userid - the user's numeric id, returned from session_context when authed.
+       * @method usersComputer.setConfirmed */
+      setConfirmed: setUserConfirmedOnComputer,
+      /** Query whether a user has been "seen" on this computer before
+       * @param {string} userid - the user's numeric id, returned from session_context when authed.
+       * @method usersComputer.seen */
+      seen: userSeenOnComputer,
+      /** Save the fact that a user has been seen on this computer before, but do not overwrite
+       *  existing state
+       * @param {string} userid - the user's numeric id, returned from session_context when authed.
+       * @method usersComputer.setSeen */
+      setSeen: setUserSeenOnComputer
+    },
+
     /** set logged in state for a site
      * @param {string} origin - the site to set logged in state for
      * @param {string} email - the email that the user is logged in with or falsey if login state should be cleared
-- 
GitLab