[Pkg-mozext-commits] [gcontactsync] 17/31: Issue #58 - Handle 401 errors

David Prévot taffit at moszumanska.debian.org
Sun Feb 22 21:34:34 UTC 2015


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch master
in repository gcontactsync.

commit 4205e5f5f907c176de246954e56a83e2e8630fb3
Author: Josh Geenen <joshgeenen at gmail.com>
Date:   Mon Dec 22 18:16:58 2014 -0600

    Issue #58 - Handle 401 errors
---
 src/content/GHttpRequest.js              | 97 ++++++--------------------------
 src/locale/en-US/gcontactsync.properties |  6 +-
 2 files changed, 17 insertions(+), 86 deletions(-)

diff --git a/src/content/GHttpRequest.js b/src/content/GHttpRequest.js
index 20e3f5d..43b8884 100644
--- a/src/content/GHttpRequest.js
+++ b/src/content/GHttpRequest.js
@@ -185,102 +185,37 @@ com.gContactSync.GHttpRequest = function gCS_GHttpRequest(aType, aAuth, aUrl, aB
 // get the superclass' prototype
 com.gContactSync.GHttpRequest.prototype = new com.gContactSync.HttpRequest();
 
-// TODO - rewrite, need to request a refresh_token
 /**
  * Handles 'Token Expired' errors.
  * If a sync is in progress:
- *  - Get the username
  *  - Alert the user
- *  - Prompt for the password
- *  - Get a new auth token to replace the old one
+ *  - Show the OAuth dialog
+ *  - Save the new refresh token
  *  - Restart the sync
  */
-com.gContactSync.handle401 = function gCS_handle401(httpRequest) {
+com.gContactSync.handle401 = function gCS_handle401() {
   com.gContactSync.LOGGER.LOG("***Found an expired token***");
-  /*
-  // If there is a synchronization in process
-  if (com.gContactSync.Preferences.mSyncPrefs.synchronizing.value) {
-    // Get the current username
-    var username = com.gContactSync.Sync.mCurrentUsername;
-    com.gContactSync.alertWarning(com.gContactSync.StringBundle.getStr("tokenExpiredMsg"));
-    // Prompt for the username and password
-    var prompt   = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
-                             .getService(Components.interfaces.nsIPromptService)
-                             .promptUsernameAndPassword,
-        password = {};
-    // set the username
-    username = { value: username };
-    com.gContactSync.LOGGER.VERBOSE_LOG(" * Showing a username/password prompt");
-    // opens a username/password prompt
-    var ok = prompt(window, com.gContactSync.StringBundle.getStr("loginTitle"),
-                    com.gContactSync.StringBundle.getStr("loginText"), username,
-                    password, null, {value: false});
-    if (!ok) {
-      com.gContactSync.LOGGER.VERBOSE_LOG(" * User canceled the prompt");
-      com.gContactSync.Sync.finish(com.gContactSync.StringBundle.getStr("tokenExpired"), false);
-      return false;
-    }
-    // This is a primitive way of validating an e-mail address, but Google takes
-    // care of the rest.  It seems to allow getting an auth token w/ only the
-    // username, but returns an error when trying to do anything w/ that token
-    // so this makes sure it is a full e-mail address.
-    if (username.value.indexOf("@") < 1) {
-      com.gContactSync.alertError(com.gContactSync.StringBundle.getStr("invalidEmail"));
-      return com.gContactSync.handle401();
-    }
-    // fix the username before authenticating
-    username.value = com.gContactSync.fixUsername(username.value);
-    var body    = com.gContactSync.gdata.makeAuthBody(username.value, password.value);
-    var httpReq = new com.gContactSync.GHttpRequest("authenticate", null, null, body);
-    // if it succeeds and Google returns the auth token, store it and then start
-    // a new sync
-    httpReq.mOnSuccess = function fix401Success(httpReq) {
-      com.gContactSync.LOGGER.VERBOSE_LOG(com.gContactSync
-                                             .serializeFromText(httpReq.responseText));
-      com.gContactSync.finish401(username.value,
-                                 httpReq.responseText.split("\n")[2]);
-    };
-    // if it fails, alert the user and prompt them to try again
-    httpReq.mOnError   = function fix401Error(httpReq) {
-      com.gContactSync.alertError(com.gContactSync.StringBundle.getStr('authErr'));
-      com.gContactSync.LOGGER.LOG_ERROR('Authentication Error - ' +
-                                         httpReq.status,
-                                         httpReq.responseText);
-      com.gContactSync.handle401();
-    };
-    // if the user is offline, alert them and quit
-    httpReq.mOnOffline = function fix401Offline(httpReq) {
-      com.gContactSync.alertError(com.gContactSync.StringBundle.getStr('offlineErr'));
-      com.gContactSync.LOGGER.LOG_ERROR(com.gContactSync.StringBundle.getStr('offlineErr'));
-    };
-    httpReq.send();
+  if (!com.gContactSync.Preferences.mSyncPrefs.synchronizing.value || !com.gContactSync.Sync.mCurrentUsername) {
+    return;
   }
-  */
+  com.gContactSync.alertWarning(com.gContactSync.StringBundle.getStr("tokenExpiredMsg"));
+  com.gContactSync.gdata.requestNewRefreshToken(com.gContactSync.Sync.mCurrentUsername, com.gContactSync.finish401);
 };
 
 /**
  * Called after the re-authentication HTTP request is sent after a 401 error
- * @param aUsername {string}  The account's username.
- * @param aAuthToken {string} An authentication token for the account.
+ * @param aResponse {object} The JSON response to the OAuth2 request.
  */
-com.gContactSync.finish401 = function gCS_finish401(aUsername, aAuthToken) {
-  com.gContactSync.LOGGER.VERBOSE_LOG(" * finish401 called: " + aUsername +
-                                      " - " + aAuthToken);
-  /*
-  if (aUsername && aAuthToken) {
+com.gContactSync.finish401 = function gCS_finish401(aResponse) {
+  var username = com.gContactSync.Sync.mCurrentUsername;
+  if (username && aResponse) {
     // Remove the auth token if it wasn't already
-    if (com.gContactSync.LoginManager.mAuthTokens[aUsername]) {
+    if (com.gContactSync.LoginManager.mAuthTokens[username]) {
       com.gContactSync.LOGGER.VERBOSE_LOG(" * Removing old auth token");
-      com.gContactSync.LoginManager.removeAuthToken(aUsername);
+      com.gContactSync.LoginManager.removeAuthToken(username);
     }
-    var token = 'GoogleLogin ' + aAuthToken;
-    com.gContactSync.LoginManager.addAuthToken(aUsername, token);
-    com.gContactSync.Sync.mCurrentAuthToken = token;
-    if (com.gContactSync.Preferences.mSyncPrefs.syncGroups.value ||
-        com.gContactSync.Preferences.mSyncPrefs.myContacts)
-      com.gContactSync.Sync.getGroups();
-    else
-      com.gContactSync.Sync.getContacts();
+    com.gContactSync.LoginManager.addAuthToken(username, aResponse.refresh_token);
+    com.gContactSync.Sync.mIndex--;
+    com.gContactSync.Sync.syncNextUser();
   }
-  */
 };
diff --git a/src/locale/en-US/gcontactsync.properties b/src/locale/en-US/gcontactsync.properties
index e57a2c9..fc2c46e 100644
--- a/src/locale/en-US/gcontactsync.properties
+++ b/src/locale/en-US/gcontactsync.properties
@@ -12,10 +12,6 @@ dummy1=nobody
 # e-mail address
 dummyEmailAdded=The following fake (dummy) e-mail address was added to this contact because it is in a mailing list.\nThis is necessary to prevent breaking that mailing list and address book.
 
-# The following is used in a login prompt for a Google (Gmail) account
-loginTitle=Google Account Login
-loginText=Welcome to gContactSync!  Please log into your Google Account below using your full e-mail address\nYour password won't be saved. Click Cancel to login later.
-
 # Status bar (lower right corner) updates
 syncFinishedString=Synchronization finished at
 notAuth=Please login to begin sychronizing
@@ -38,7 +34,7 @@ authErr=Error encountered while authenticating.\nPlease make sure you typed your
 invalidEmail=Please enter your full e-mail address, not just your username.
 loggingDisabled=Logging is disabled.  Go in the Preferences and enable logging if you want to view the log file.
 tokenExpired=Login expired.
-tokenExpiredMsg=Your authentication token from Google has expired.  Please click OK then type in your password to get a new token and Synchronize your contacts.
+tokenExpiredMsg=Your authentication token from Google has expired.  Please click OK then enter your credentials to get a new token and Synchronize your contacts.
 noTokenFound=Warning: gContactSync was unable to find a token for one of your accounts.\nThis can happen if you created the account in one version of Thunderbird and you are in another, or if you copy only a portion of your profile over, among other things.\n\nClick OK to sign in and get a token or Cancel to skip synchronizating this address book.\nUsername
 ab=Address Book
 err503Short=Server returned a 503 (Service Unavailable) error.

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-mozext/gcontactsync.git



More information about the Pkg-mozext-commits mailing list