Bug 753797 - Make YouSendIt prompt for password when remembered password is no longer valid. r=bienvenu.
authorMike Conley <mconley@mozilla.com>
Tue, 15 May 2012 10:31:08 -0400
changeset 10191 b4c56e6d36b2e70346430f9a9349cb22a419267d
parent 10190 2d68a2e7b53f7a2bc4618ef0a2a31e4c62a5bf2f
child 10192 d87e0df241fc504d91321ec9c896ac9f14ee5a8b
push id7721
push usermconley@mozilla.com
push dateTue, 15 May 2012 14:34:10 +0000
treeherdercomm-central@b4c56e6d36b2 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbienvenu
bugs753797
Bug 753797 - Make YouSendIt prompt for password when remembered password is no longer valid. r=bienvenu.
mail/components/cloudfile/nsYouSendIt.js
--- a/mail/components/cloudfile/nsYouSendIt.js
+++ b/mail/components/cloudfile/nsYouSendIt.js
@@ -258,26 +258,26 @@ nsYouSendIt.prototype = {
     this.log.info("getting user info");
     let args = "?email=" + this._userName;
 
     let req = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"]
                 .createInstance(Ci.nsIXMLHttpRequest);
     req.open("GET", gServerUrl + kUserInfoPath + args, true);
 
     req.onload = function() {
-      if (req.status >= 200 && req.status < 400) {
+      if (req.status >= 200 && req.status <= 400) {
         this.log.info("request status = " + req.status +
                       " response = " + req.responseText);
         let docResponse = JSON.parse(req.responseText);
         this.log.info("user info response parsed = " + docResponse);
         if (docResponse.errorStatus)
           this.log.info("error status = " + docResponse.errorStatus.code);
 
         if (docResponse.errorStatus && docResponse.errorStatus.code > 200) {
-          if (docResponse.errorStatus.code > 400) {
+          if (docResponse.errorStatus.code >= 400) {
             // Our token has gone stale
             this.log.info("Our token has gone stale - requesting a new one.");
 
             let retryGetUserInfo = function() {
               this._getUserInfo(successCallback, failureCallback);
             }.bind(this);
 
             this._handleStaleToken(retryGetUserInfo, failureCallback);
@@ -602,16 +602,26 @@ nsYouSendIt.prototype = {
                                     authPrompter.SAVE_PASSWORD_PERMANENTLY,
                                     password))
       return password.value;
 
     return "";
   },
 
   /**
+   * Clears any saved YouSendIt passwords for this instance's account.
+   */
+  clearPassword: function nsYouSendIt_clearPassword() {
+    let logins = Services.logins.findLogins({}, gServerUrl, null, gServerUrl);
+    for each (let loginInfo in logins)
+      if (loginInfo.username == this._userName)
+        Services.logins.removeLogin(loginInfo);
+  },
+
+  /**
    * Attempt to log on and get the auth token for this YouSendIt account.
    *
    * @param successCallback the callback to be fired if logging on is successful
    * @param failureCallback the callback to be fired if loggong on fails
    * @aparam aWithUI a boolean for whether or not we should prompt for a password
    *                 if no auth token is currently stored.
    */
   logon: function nsYouSendIt_login(successCallback, failureCallback, aWithUI) {
@@ -637,23 +647,25 @@ nsYouSendIt.prototype = {
         this.log.info("login response parsed = " + docResponse);
         this._cachedAuthToken = docResponse.authToken;
         this.log.info("authToken = " + this._cachedAuthToken);
         if (this._cachedAuthToken) {
           this._loggedIn = true;
           successCallback();
         }
         else {
+          this.clearPassword();
           this._loggedIn = false;
           this._lastErrorText = docResponse.errorStatus.message;
           this._lastErrorStatus = docResponse.errorStatus.code;
           failureCallback();
         }
       }
       else {
+        this.clearPassword();
         failureCallback();
       }
     }.bind(this);
     req.setRequestHeader("X-Api-Key", kApiKey);
     req.setRequestHeader("Date", curDate);
     req.setRequestHeader("Accept", "application/json");
     req.send();
     this.log.info("Login information sent!");