Split account verification out from login(), make wizard work for using an existing account, related code cleanup/simplification
authorJustin Dolske <dolske@mozilla.com>
Wed, 25 Jun 2008 00:22:53 -0700
changeset 44733 3d03b79e0ba7609f2da3ed9824959c82ed5bd588
parent 44732 adaaa4e6751c71279186b704220481a36befdfc6
child 44735 83a955a47d0404e8f5a43e0f11f91a39361efc15
push id1
push userroot
push dateTue, 26 Apr 2011 22:38:44 +0000
treeherdermozilla-beta@bfdb6e623a36 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
Split account verification out from login(), make wizard work for using an existing account, related code cleanup/simplification
services/sync/modules/dav.js
services/sync/modules/service.js
--- a/services/sync/modules/dav.js
+++ b/services/sync/modules/dav.js
@@ -296,31 +296,42 @@ DAVCollection.prototype = {
         ret.push(elt.textContent.replace(root, ''));
     } catch (e) {}
 
     self.done(ret);
   },
 
   // Login / Logout
 
-  checkLogin: function DC_checkLogin() {
+  checkLogin: function DC_checkLogin(username, password) {
     let self = yield;
 
-    this._log.debug("Checking login");
+    this._log.debug("checkLogin called for user " + username);
+
+    let headers = {
+                    'Content-type'  : 'text/plain',
+                    'Authorization' : 'Basic ' + btoa(username + ":" + password)
+                  };
+    let lock = DAVLocks['default'];
+    if (lock)
+      headers['If'] = "<" + lock.URL + "> (<" + lock.token + ">)";
 
     // Make a call to make sure it's working
-    this.GET("", self.cb);
+    this._makeRequest.async(this, self.cb, "GET", "", headers);
     let resp = yield;
 
-    if (resp.status < 200 || resp.status >= 300) {
-      self.done(false);
-      return;
-    }
+    this._log.debug("checkLogin got response status " + resp.status);
+    // XXX would be nice if 404 == invalid username, 401 == invalid password.
+    let retmsg = "";
+    if (resp.status == 401)
+      retmsg = "invalid username or password";
+    else if (resp.status < 200 || resp.status >= 300)
+      retmsg = "server error";
 
-    self.done(true);
+    self.done(retmsg);
   },
 
   // Locking
 
   _getActiveLock: function DC__getActiveLock() {
     let self = yield;
     let ret = null;
 
--- a/services/sync/modules/service.js
+++ b/services/sync/modules/service.js
@@ -191,20 +191,18 @@ WeaveSvc.prototype = {
   get password() { return ID.get('WeaveID').password; },
   set password(value) { ID.get('WeaveID').password = value; },
 
   get passphrase() { return ID.get('WeaveCryptoID').password; },
   set passphrase(value) { ID.get('WeaveCryptoID').password = value; },
 
   get userPath() { return ID.get('WeaveID').username; },
 
-  get currentUser() {
-    if (this._loggedIn)
-      return this.username;
-    return null;
+  get isLoggedIn() {
+    return this._loggedIn;
   },
 
   get enabled() {
     return Utils.prefs.getBoolPref("enabled");
   },
 
   get schedule() {
     if (!this.enabled)
@@ -494,64 +492,55 @@ WeaveSvc.prototype = {
                                "chrome://weave/content/status.xul",
                                "Weave:status",
                                "chrome,centerscreen,modal",
                                null);
   },
 
   // These are global (for all engines)
 
-  login: function WeaveSync_login(onComplete, password, passphrase, verifyonly) {
-    this._localLock(this._notify("login", this._login,
-                                 password, passphrase, verifyonly)).async(this, onComplete);
+  verifyLogin: function WeaveSync_verifyLogin(username, password) {
+    this._localLock(this._notify("verify-login", this._verifyLogin, username, password)).async(this, null);
   },
-  _login: function WeaveSync__login(password, passphrase, verifyonly) {
+
+  _verifyLogin: function WeaveSync__verifyLogin(username, password) {
+    let self = yield;
+    this._log.debug("Verifying login for user " + username);
+
+    DAV.baseURL = Utils.prefs.getCharPref("serverURL");
+    DAV.defaultPrefix = "user/" + username;
+
+    DAV.checkLogin.async(DAV, self.cb, username, password);
+    let resultMsg = yield;
+
+    // If we got an error message, throw it. [need to throw to cause the
+    // _notify() wrapper to generate an error notification for observers].
+    if (resultMsg) {
+      this._log.debug("Login verification: " + resultMsg);
+      throw resultMsg;
+    }
+
+  },
+
+  login: function WeaveSync_login(onComplete) {
+    this._localLock(this._notify("login", this._login)).async(this, onComplete);
+  },
+  _login: function WeaveSync__login() {
     let self = yield;
 
-    // cache password & passphrase
-    // if null, we'll try to get them from the pw manager below
-    ID.get('WeaveID').setTempPassword(password);
-    ID.get('WeaveCryptoID').setTempPassword(passphrase);
-
-    if(verifyonly)
-       this._log.debug("Verifying login");
-    else
-       this._log.debug("Logging in");
+    this._log.debug("Logging in user " + this.username);
 
     if (!this.username)
       throw "No username set, login failed";
     if (!this.password)
       throw "No password given or found in password manager";
 
     DAV.baseURL = Utils.prefs.getCharPref("serverURL");
     DAV.defaultPrefix = "user/" + this.userPath;
 
-    DAV.checkLogin.async(DAV, self.cb, this.username, this.password);
-    let success = yield;
-    if (!success) {
-      try {
-        // FIXME: This code may not be needed any more, due to the way
-        // that the server is expected to create the user dir for us.
-        this._checkUserDir.async(this, self.cb);
-        yield;
-      } catch (e) { /* FIXME: tmp workaround for services.m.c */ }
-      DAV.checkLogin.async(DAV, self.cb, this.username, this.password);
-      let success = yield;
-      if (!success)
-        throw "Login failed";
-    }
-
-    // If being called from the Wizard to verify credentials, stop here.
-    if (verifyonly) {
-      this._log.debug("Login verified");
-      self.done(true);
-      return;
-    }
-    // Otherwise, setup the user session.
-
     this._log.info("Using server URL: " + DAV.baseURL + DAV.defaultPrefix);
 
     this._versionCheck.async(this, self.cb);
     yield;
     this._getKeypair.async(this, self.cb);
     yield;
 
     this._loggedIn = true;