Bug 1229023 - Fetch and expose Firefox Accounts profile data. r=markh
authorFernando Jimenez <ferjmoreno@gmail.com>
Wed, 02 Dec 2015 19:41:27 +0100
changeset 275357 b7dd3ffbf52b18e9d58c27abb7ed67c4a2208686
parent 275356 b6eea9eddc4aeba17ec35572ce45a3a565f4ec2a
child 275358 df1a6fe663132fd8f6089a4413749263604783f1
push id68832
push userferjmoreno@gmail.com
push dateThu, 03 Dec 2015 08:41:27 +0000
treeherdermozilla-inbound@b7dd3ffbf52b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmarkh
bugs1229023
milestone45.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1229023 - Fetch and expose Firefox Accounts profile data. r=markh
b2g/app/b2g.js
services/fxaccounts/FxAccountsManager.jsm
services/fxaccounts/tests/xpcshell/test_manager.js
--- a/b2g/app/b2g.js
+++ b/b2g/app/b2g.js
@@ -1082,16 +1082,19 @@ pref("layout.accessiblecaret.use_long_ta
 
 // Enable sync and mozId with Firefox Accounts.
 pref("services.sync.fxaccounts.enabled", true);
 pref("identity.fxaccounts.enabled", true);
 
 // Mobile Identity API.
 pref("services.mobileid.server.uri", "https://msisdn.services.mozilla.com");
 
+pref("identity.fxaccounts.remote.oauth.uri", "https://oauth.accounts.firefox.com/v1");
+pref("identity.fxaccounts.remote.profile.uri", "https://profile.accounts.firefox.com/v1");
+
 // Enable mapped array buffer.
 #ifndef XP_WIN
 pref("dom.mapped_arraybuffer.enabled", true);
 #endif
 
 // SystemUpdate API
 pref("dom.system_update.enabled", true);
 
--- a/services/fxaccounts/FxAccountsManager.jsm
+++ b/services/fxaccounts/FxAccountsManager.jsm
@@ -57,17 +57,18 @@ this.FxAccountsManager = {
   // We only expose the email and the verified status so far.
   get _user() {
     if (!this._activeSession || !this._activeSession.email) {
       return null;
     }
 
     return {
       email: this._activeSession.email,
-      verified: this._activeSession.verified
+      verified: this._activeSession.verified,
+      profile: this._activeSession.profile,
     }
   },
 
   _error: function(aError, aDetails) {
     log.error(aError);
     let reason = {
       error: aError
     };
@@ -160,22 +161,32 @@ this.FxAccountsManager = {
             // There is no way to obtain the key fetch token afterwards
             // without login out the user and asking her to log in again.
             // Also, key fetch tokens are designed to be short-lived, so
             // we need to fetch kB as soon as we have the key fetch token.
             if (aFetchKeys) {
               this._fxAccounts.getKeys();
             }
 
-            return Promise.resolve({
-              accountCreated: aMethod === "signUp",
-              user: this._user
+            return this._fxAccounts.getSignedInUserProfile().catch(error => {
+              // Not fetching the profile is sad but the FxA logs will already
+              // have noise.
+              return null;
             });
           }
-        );
+        ).then(profile => {
+          if (profile) {
+            this._activeSession.profile = profile;
+          }
+
+          return Promise.resolve({
+            accountCreated: aMethod === "signUp",
+            user: this._user
+          });
+        });
       },
       reason => { return this._serverError(reason); }
     );
   },
 
   /**
    * Determine whether the incoming error means that the current account
    * has new server-side state via deletion or password change, and if so,
@@ -415,20 +426,33 @@ this.FxAccountsManager = {
           return Promise.resolve(null);
         }
 
         this._activeSession = user;
         // If we get a stored information of a not yet verified account,
         // we kick off verification before returning what we have.
         if (!user.verified) {
           this.verificationStatus(user);
+          // Trying to get the profile for unverified users will fail, so we
+          // don't even try in that case.
+          log.debug("Account ", this._user);
+          return Promise.resolve(this._user);
         }
 
-        log.debug("Account " + JSON.stringify(this._user));
-        return Promise.resolve(this._user);
+        return this._fxAccounts.getSignedInUserProfile().then(profile => {
+          if (profile) {
+            this._activeSession.profile = profile;
+          }
+          log.debug("Account ", this._user);
+          return Promise.resolve(this._user);
+        }).catch(error => {
+          // FxAccounts logs already inform about the error.
+          log.debug("Account ", this._user);
+          return Promise.resolve(this._user);
+        });
       }
     );
   },
 
   queryAccount: function(aEmail) {
     log.debug("queryAccount " + aEmail);
     if (Services.io.offline) {
       return this._error(ERROR_OFFLINE);
@@ -481,16 +505,23 @@ this.FxAccountsManager = {
         let error = this._getError(data);
         if (error) {
           this._error(error, data);
         }
         // If the verification status has changed, update state.
         if (this._activeSession.verified != data.verified) {
           this._activeSession.verified = data.verified;
           this._fxAccounts.setSignedInUser(this._activeSession);
+          this._fxAccounts.getSignedInUserProfile().then(profile => {
+            if (profile) {
+              this._activeSession.profile = profile;
+            }
+          }).catch(error => {
+            // FxAccounts logs already inform about the error.
+          });
         }
         log.debug(JSON.stringify(this._user));
       },
       reason => { this._serverError(reason); }
     );
   },
 
   /*
--- a/services/fxaccounts/tests/xpcshell/test_manager.js
+++ b/services/fxaccounts/tests/xpcshell/test_manager.js
@@ -97,16 +97,17 @@ const kFxAccounts = fxAccounts;
 FxAccountsManager._fxAccounts = {
   _reject: false,
   _getSignedInUserCalled: false,
   _setSignedInUserCalled: false,
 
   _error: 'error',
   _assertion: 'assertion',
   _keys: 'keys',
+  _profile: 'aprofile',
   _signedInUser: null,
 
   _reset: function() {
     this._getSignedInUserCalled = false;
     this._setSignedInUserCalled = false;
     this._reject = false;
   },
 
@@ -135,16 +136,23 @@ FxAccountsManager._fxAccounts = {
   getSignedInUser: function() {
     this._getSignedInUserCalled = true;
     let deferred = Promise.defer();
     this._reject ? deferred.reject(this._error)
                  : deferred.resolve(this._signedInUser);
     return deferred.promise;
   },
 
+  getSignedInUserProfile: function() {
+    let deferred = Promise.defer();
+    this._reject ? deferred.reject(this._error)
+                 : deferred.resolve(this._profile);
+    return deferred.promise;
+  },
+
   getKeys: function() {
     let deferred = Promise.defer();
     this._reject ? deferred.reject(this._error)
                  : deferred.resolve(this._keys);
     return deferred.promise;
   },
 
   resendVerificationEmail: function() {
@@ -631,21 +639,23 @@ add_test(function(test_getAssertion_refr
     error => {
       do_throw("Unexpected error: " + error);
     }
   );
 });
 
 add_test(function(test_getAccount_existing_verified_session) {
   do_print("= getAccount, existing verified session =");
+  FxAccountsManager._activeSession = null;
   FxAccountsManager.getAccount().then(
     result => {
-      do_check_false(FxAccountsManager._fxAccounts._getSignedInUserCalled);
+      do_check_true(FxAccountsManager._fxAccounts._getSignedInUserCalled);
       do_check_eq(result.email, FxAccountsManager._user.email);
       do_check_eq(result.verified, FxAccountsManager._user.verified);
+      do_check_eq(result.profile, "aprofile");
       run_next_test();
     },
     error => {
       do_throw("Unexpected error: " + error);
     }
   );
 });