From 7597f0953b7481bbce56bfaa0bef9209e3d86a39 Mon Sep 17 00:00:00 2001 From: Shane Tomlinson <stomlinson@mozilla.com> Date: Tue, 3 Jul 2012 11:29:32 +0100 Subject: [PATCH] Use the correct reset_password and confirm landing page names. Update network.js for password_reset_status cleanup user.js for for better cohesion. --- resources/static/common/js/network.js | 8 +- resources/static/common/js/user.js | 128 +++++++++--------- resources/static/pages/js/start.js | 4 +- resources/static/test/cases/common/js/user.js | 5 +- resources/static/test/mocks/xhr.js | 6 + 5 files changed, 75 insertions(+), 76 deletions(-) diff --git a/resources/static/common/js/network.js b/resources/static/common/js/network.js index 5864671e9..bf30d93ff 100644 --- a/resources/static/common/js/network.js +++ b/resources/static/common/js/network.js @@ -179,7 +179,7 @@ BrowserID.Network = (function() { }, withContext: function(onComplete, onFailure) { - withContext(onComplete, onFailure) + withContext(onComplete, onFailure); }, /** @@ -379,8 +379,7 @@ BrowserID.Network = (function() { */ checkPasswordReset: function(email, onComplete, onFailure) { get({ - // XXX the URL is going to have to change - url: "/wsapi/email_addition_status?email=" + encodeURIComponent(email), + url: "/wsapi/password_reset_status?email=" + encodeURIComponent(email), success: function(status, textStatus, jqXHR) { complete(onComplete, status.status); }, @@ -754,6 +753,7 @@ BrowserID.Network = (function() { // Make sure we get context first or else we will needlessly send // a cookie to the server. withContext(function() { + var enabled; try { // set a test cookie with a duration of 1 second. // NOTE - The Android 3.3 and 4.0 default browsers will still pass @@ -762,7 +762,7 @@ BrowserID.Network = (function() { // submitted input. // http://stackoverflow.com/questions/8509387/android-browser-not-respecting-cookies-disabled/9264996#9264996 document.cookie = "test=true; max-age=1"; - var enabled = document.cookie.indexOf("test") > -1; + enabled = document.cookie.indexOf("test") > -1; } catch(e) { enabled = false; } diff --git a/resources/static/common/js/user.js b/resources/static/common/js/user.js index 25cef9804..e0783c165 100644 --- a/resources/static/common/js/user.js +++ b/resources/static/common/js/user.js @@ -11,7 +11,8 @@ BrowserID.User = (function() { bid = BrowserID, network = bid.Network, storage = bid.Storage, - User, pollTimeout, + User, + pollTimeout, provisioning = bid.Provisioning, addressCache = {}, primaryAuthCache = {}, @@ -100,7 +101,53 @@ BrowserID.User = (function() { } } - function registrationPoll(checkFunc, email, onSuccess, onFailure) { + function stageAddressVerification(onComplete, staged) { + var status = { success: staged }; + + if (!staged) status.reason = "throttle"; + // Used on the main site when the user verifies - once + // verification is complete, the user is redirected back to the + // RP and logged in. + var site = User.getReturnTo(); + if (staged && site) storage.setReturnTo(site); + + complete(onComplete, status); + } + + function completeAddressVerification(completeFunc, token, password, onComplete, onFailure) { + User.tokenInfo(token, function(info) { + var invalidInfo = { valid: false }; + if (info) { + completeFunc(token, password, function (valid) { + var result = invalidInfo; + + if (valid) { + result = _.extend({ valid: valid }, info); + var email = info.email, + idInfo = storage.getEmail(email); + + // Now that the address is verified, its verified bit has to be + // updated as well or else the user will be forced to verify the + // address again. + if (idInfo) { + idInfo.verified = true; + storage.addEmail(email, idInfo); + } + + storage.setReturnTo(""); + } + + complete(onComplete, result); + }, onFailure); + } else if (onComplete) { + onComplete(invalidInfo); + } + }, onFailure); + + } + + + function addressVerificationPoll(checkFunc, email, onSuccess, onFailure) { function poll() { checkFunc(email, function(status) { // registration status checks the status of the last initiated registration, @@ -117,7 +164,7 @@ BrowserID.User = (function() { // To avoid too many address_info requests, returns from each // address_info request are cached. If the user is doing - // a registrationPoll, it means the user was registering the address + // a addressVerificationPoll, it means the user was registering the address // and the registration has completed. Because the status is // "complete" or "known", we know that the address is known, so we // toggle the field to be up to date. If the known field remains @@ -220,51 +267,6 @@ BrowserID.User = (function() { }); } - function stageAddressVerificationResponse(onComplete, staged) { - var status = { success: staged }; - - if (!staged) status.reason = "throttle"; - // Used on the main site when the user verifies - once - // verification is complete, the user is redirected back to the - // RP and logged in. - var site = User.getReturnTo(); - if (staged && site) storage.setReturnTo(site); - - complete(onComplete, status); - } - - function completeAddressVerification(token, password, networkFuncName, onComplete, onFailure) { - User.tokenInfo(token, function(info) { - var invalidInfo = { valid: false }; - if (info) { - network[networkFuncName](token, password, function (valid) { - var result = invalidInfo; - - if (valid) { - result = _.extend({ valid: valid }, info); - var email = info.email, - idInfo = storage.getEmail(email); - - // Now that the address is verified, its verified bit has to be - // updated as well or else the user will be forced to verify the - // address again. - if (idInfo) { - idInfo.verified = true; - storage.addEmail(email, idInfo); - } - - storage.setReturnTo(""); - } - - complete(onComplete, result); - }, onFailure); - } else if (onComplete) { - onComplete(invalidInfo); - } - }, onFailure); - - } - User = { init: function(config) { @@ -347,7 +349,7 @@ BrowserID.User = (function() { * @param {function} [onFailure] - Called on error. */ createSecondaryUser: function(email, password, onComplete, onFailure) { - // set - XXX Use stageAddressVerificationResponse to handle the response. + // set - XXX Use stageAddressVerification to handle the response. network.createUser(email, password, origin, function(created) { // Used on the main site when the user verifies - once verification // is complete, the user is redirected back to the RP and logged in. @@ -523,7 +525,7 @@ BrowserID.User = (function() { * @param {function} [onSuccess] - Called to give status updates. * @param {function} [onFailure] - Called on error. */ - waitForUserValidation: registrationPoll.curry(network.checkUserRegistration), + waitForUserValidation: addressVerificationPoll.curry(network.checkUserRegistration), /** * Cancel the waitForUserValidation poll @@ -561,9 +563,7 @@ BrowserID.User = (function() { * with valid=false otw. * @param {function} [onFailure] - Called on error. */ - verifyUser: function(token, password, onComplete, onFailure) { - completeAddressVerification(token, password, "completeUserRegistration", onComplete, onFailure); - }, + verifyUser: completeAddressVerification.curry(network.completeUserRegistration), /** * Check if the user can set their password. Only returns true for users @@ -617,7 +617,7 @@ BrowserID.User = (function() { User.isEmailRegistered(email, function(registered) { if (registered) { network.requestPasswordReset(email, password, origin, - stageAddressVerificationResponse.curry(onComplete), onFailure); + stageAddressVerification.curry(onComplete), onFailure); } else if (onComplete) { onComplete({ success: false, reason: "invalid_user" }); @@ -635,9 +635,7 @@ BrowserID.User = (function() { * with valid=false otw. * @param {function} [onFailure] - Called on error. */ - completePasswordReset: function(token, password, onComplete, onFailure) { - completeAddressVerification(token, password, "completePasswordReset", onComplete, onFailure); - }, + completePasswordReset: completeAddressVerification.curry(network.completePasswordReset), /** * Wait for the password reset to complete @@ -646,7 +644,7 @@ BrowserID.User = (function() { * @param {function} [onSuccess] - Called to give status updates. * @param {function} [onFailure] - Called on error. */ - waitForPasswordResetComplete: registrationPoll.curry(network.checkPasswordReset), + waitForPasswordResetComplete: addressVerificationPoll.curry(network.checkPasswordReset), /** * Cancel the waitForPasswordResetComplete poll @@ -674,13 +672,11 @@ BrowserID.User = (function() { else if (!idInfo.verified) { // this address is unverified, try to reverify it. network.requestEmailReverify(email, origin, - stageAddressVerificationResponse.curry(onComplete), onFailure); + stageAddressVerification.curry(onComplete), onFailure); } }, - completeEmailReverify: function(token, password, onComplete, onFailure) { - completeAddressVerification(token, password, "completeEmailReverify", onComplete, onFailure); - }, + completeEmailReverify: completeAddressVerification.curry(network.completeEmailReverify), /** * Wait for the email reverification to complete @@ -689,7 +685,7 @@ BrowserID.User = (function() { * @param {function} [onSuccess] - Called to give status updates. * @param {function} [onFailure] - Called on error. */ - waitForEmailReverifyComplete: registrationPoll.curry(network.checkEmailReverify), + waitForEmailReverifyComplete: addressVerificationPoll.curry(network.checkEmailReverify), /** * Cancel the waitForEmailReverifyComplete poll @@ -991,7 +987,7 @@ BrowserID.User = (function() { * @param {function} [onSuccess] - Called to give status updates. * @param {function} [onFailure] - Called on error. */ - waitForEmailValidation: registrationPoll.curry(network.checkEmailRegistration), + waitForEmailValidation: addressVerificationPoll.curry(network.checkEmailRegistration), /** * Cancel the waitForEmailValidation poll @@ -1011,9 +1007,7 @@ BrowserID.User = (function() { * with valid=false otw. * @param {function} [onFailure] - Called on error. */ - verifyEmail: function(token, password, onComplete, onFailure) { - completeAddressVerification(token, password, "completeEmailRegistration", onComplete, onFailure); - }, + verifyEmail: completeAddressVerification.curry(network.completeEmailRegistration), /** * Remove an email address. diff --git a/resources/static/pages/js/start.js b/resources/static/pages/js/start.js index ca4d024ac..ed1f480e3 100644 --- a/resources/static/pages/js/start.js +++ b/resources/static/pages/js/start.js @@ -151,10 +151,10 @@ $(function() { else if (path === "/verify_email_address") { verifySecondaryAddress("verifyUser"); } - else if (path === "/complete_password_reset") { + else if (path === "/reset_password") { verifySecondaryAddress("completePasswordReset"); } - else if (path === "/complete_email_reverify") { + else if (path === "/confirm") { verifySecondaryAddress("completeEmailReverify"); } else if (path === "/about") { diff --git a/resources/static/test/cases/common/js/user.js b/resources/static/test/cases/common/js/user.js index b196fe70b..7dad1b8d5 100644 --- a/resources/static/test/cases/common/js/user.js +++ b/resources/static/test/cases/common/js/user.js @@ -7,9 +7,8 @@ (function() { "use strict"; - var jwcrypto = require("./lib/jwcrypto"); - - var bid = BrowserID, + var jwcrypto = require("./lib/jwcrypto"), + bid = BrowserID, lib = bid.User, storage = bid.Storage, network = bid.Network, diff --git a/resources/static/test/mocks/xhr.js b/resources/static/test/mocks/xhr.js index 5b458fbeb..e91e378ed 100644 --- a/resources/static/test/mocks/xhr.js +++ b/resources/static/test/mocks/xhr.js @@ -74,6 +74,12 @@ BrowserID.Mocks.xhr = (function() { "post /wsapi/complete_reset invalid": { success: false }, "post /wsapi/complete_reset ajaxError": undefined, + "get /wsapi/password_reset_status?email=registered%40testuser.com pending": { status: "pending" }, + "get /wsapi/password_reset_status?email=registered%40testuser.com complete": { status: "complete", userid: 4 }, + "get /wsapi/password_reset_status?email=registered%40testuser.com mustAuth": { status: "mustAuth" }, + "get /wsapi/password_reset_status?email=registered%40testuser.com noRegistration": { status: "noRegistration" }, + "get /wsapi/password_reset_status?email=registered%40testuser.com ajaxError": undefined, + "post /wsapi/stage_reverify unknown_secondary": { success: true }, "post /wsapi/stage_reverify valid": { success: true }, "post /wsapi/stage_reverify invalid": { success: false }, -- GitLab