Skip to content
Snippets Groups Projects
Commit 75e3f4a6 authored by Shane Tomlinson's avatar Shane Tomlinson
Browse files

Fixing Fx throwing an exception when accessing localStorage if cookies are disabled.

* In the example RP, instead of accessing localStorage directly, access it through a variable that is guaranteed to exist.
* In page_helpers.js, do all localStorage access through new functions in storage.js
* In storage.js, if localStorage is inaccessible, create a standin.

issue #1414
parent fdb2a3c7
No related branches found
No related tags found
No related merge requests found
......@@ -105,6 +105,15 @@ pre {
<script src="https://browserid.org/include.js"></script>
<script>
try {
var storage = localStorage;
}
catch(e) {
// Fx with cookies disabled with blow up when trying to access localStorage.
storage = {};
}
function loggit() {
try {
console.log.apply(console, arguments);
......@@ -135,7 +144,7 @@ function checkAssertion(assertion) {
};
navigator.id.experimental.watch({
email: (localStorage.loggedInUser === 'null') ? null : localStorage.loggedInUser,
email: (storage.loggedInUser === 'null') ? null : storage.loggedInUser,
onready: function () {
loggit("onready");
var txt = serial++ + ' navigator.id ready at ' + (new Date).toString();
......@@ -179,10 +188,10 @@ $(document).ready(function() {
$(".specify button.logout").click(navigator.id.logout);
$(".session button.update_session").click(function() {
localStorage.loggedInUser = $.trim($('#loggedInUser').val());
storage.loggedInUser = $.trim($('#loggedInUser').val());
$(".session input").fadeOut(100).fadeIn(350);
});
$('#loggedInUser').val(localStorage.loggedInUser ? localStorage.loggedInUser : "");
$('#loggedInUser').val(storage.loggedInUser ? storage.loggedInUser : "");
});
</script>
......
......@@ -8,8 +8,8 @@ BrowserID.PageHelpers = (function() {
var win = window,
doc = win.document,
locStorage = win.localStorage,
bid = BrowserID,
storage = bid.Storage,
user = bid.User,
helpers = bid.Helpers,
dom = bid.DOM,
......@@ -18,7 +18,15 @@ BrowserID.PageHelpers = (function() {
origStoredEmail;
function setStoredEmail(email) {
locStorage.signInEmail = email;
storage.signInEmail.set(email);
}
function clearStoredEmail() {
storage.signInEmail.remove();
}
function getStoredEmail() {
return storage.signInEmail.get() || "";
}
function onEmailChange(event) {
......@@ -30,7 +38,7 @@ BrowserID.PageHelpers = (function() {
// If the user tried to sign in on the sign up page with an existing email,
// place that email in the email field, then focus the password.
var el = $("#email"),
email = locStorage.signInEmail;
email = getStoredEmail();
if (email) {
el.val(email);
......@@ -41,14 +49,6 @@ BrowserID.PageHelpers = (function() {
dom.bindEvent("#email", "keyup", onEmailChange);
}
function clearStoredEmail() {
locStorage.removeItem("signInEmail");
}
function getStoredEmail() {
return locStorage.signInEmail || "";
}
function getParameterByName( name ) {
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
......
......@@ -5,9 +5,23 @@
BrowserID.Storage = (function() {
var jwk,
storage = localStorage,
ONE_DAY_IN_MS = (1000 * 60 * 60 * 24);
try {
var storage = localStorage;
}
catch(e) {
// Fx with cookies disabled will except while trying to access
// localStorage. Because of this, and because the new API requires access
// to localStorage
storage = {
removeItem: function(key) {
this[key] = null;
delete this[key];
}
};
}
function prepareDeps() {
if (!jwk) {
jwk = require("./jwk");
......@@ -160,21 +174,21 @@ BrowserID.Storage = (function() {
}
}
function managePageGet(key) {
var allInfo = JSON.parse(storage.managePage || "{}");
return allInfo[key];
function generic2KeySet(namespace, key, value) {
var allInfo = JSON.parse(storage[namespace] || "{}");
allInfo[key] = value;
storage[namespace] = JSON.stringify(allInfo);
}
function managePageSet(key, value) {
var allInfo = JSON.parse(storage.managePage || "{}");
allInfo[key] = value;
storage.managePage = JSON.stringify(allInfo);
function generic2KeyGet(namespace, key) {
var allInfo = JSON.parse(storage[namespace] || "{}");
return allInfo[key];
}
function managePageRemove(key) {
var allInfo = JSON.parse(storage.managePage || "{}");
function generic2KeyRemove(namespace, key) {
var allInfo = JSON.parse(storage[namespace] || "{}");
delete allInfo[key];
storage.managePage = JSON.stringify(allInfo);
storage[namespace] = JSON.stringify(allInfo);
}
function setLoggedIn(origin, email) {
......@@ -421,9 +435,15 @@ BrowserID.Storage = (function() {
* Set a data field for the manage page
* @method managePage.set
*/
set: managePageSet,
get: managePageGet,
remove: managePageRemove
set: generic2KeySet.curry("managePage"),
get: generic2KeyGet.curry("managePage"),
remove: generic2KeyRemove.curry("managePage")
},
signInEmail: {
set: generic2KeySet.curry("main_site", "signInEmail"),
get: generic2KeyGet.curry("main_site", "signInEmail"),
remove: generic2KeyRemove.curry("main_site", "signInEmail")
},
usersComputer: {
......
......@@ -157,5 +157,12 @@
test("getStagedOnBehalfOf", function() {
// XXX needs a test
});
test("signInEmail.set/.get/.remove - set, get, and remove the signInEmail", function() {
storage.signInEmail.set("testuser@testuser.com");
equal(storage.signInEmail.get(), "testuser@testuser.com", "correct email gotten");
storage.signInEmail.remove();
equal(typeof storage.signInEmail.get(), "undefined", "after remove, signInEmail is empty");
});
}());
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment