From 82d3c832a56fbe5b947063572fabc90f574fc582 Mon Sep 17 00:00:00 2001 From: Fernando Jimenez Date: Tue, 21 Jul 2015 21:34:37 +0200 Subject: [PATCH] Bug 1182071 - Allow System app to fetch Sync keys. r=spenrose --- b2g/components/FxAccountsMgmtService.jsm | 17 ++-- services/fxaccounts/FxAccountsCommon.js | 1 + services/fxaccounts/FxAccountsManager.jsm | 41 +++++++++- .../fxaccounts/tests/xpcshell/test_manager.js | 78 +++++++++++++++++++ 4 files changed, 130 insertions(+), 7 deletions(-) diff --git a/b2g/components/FxAccountsMgmtService.jsm b/b2g/components/FxAccountsMgmtService.jsm index ee11c53894f..427dbcdf09b 100644 --- a/b2g/components/FxAccountsMgmtService.jsm +++ b/b2g/components/FxAccountsMgmtService.jsm @@ -93,12 +93,19 @@ this.FxAccountsMgmtService = { delete data.accountId; } + // XXX dirty hack because Gaia is sending getAccounts. + if (data.method == "getAccounts") { + data.method = "getAccount"; + } + switch(data.method) { - case "getAccounts": - FxAccountsManager.getAccount().then( - account => { - // We only expose the email and verification status so far. - self._onFulfill(msg.id, account); + case "getAccount": + case "getKeys": + FxAccountsManager[data.method]().then( + result => { + // For the getAccounts case, we only expose the email and + // verification status so far. + self._onFulfill(msg.id, result); }, reason => { self._onReject(msg.id, reason); diff --git a/services/fxaccounts/FxAccountsCommon.js b/services/fxaccounts/FxAccountsCommon.js index 9d90ad38062..065403fb9eb 100644 --- a/services/fxaccounts/FxAccountsCommon.js +++ b/services/fxaccounts/FxAccountsCommon.js @@ -176,6 +176,7 @@ exports.ERROR_OFFLINE = "OFFLINE"; exports.ERROR_PERMISSION_DENIED = "PERMISSION_DENIED"; exports.ERROR_REQUEST_BODY_TOO_LARGE = "REQUEST_BODY_TOO_LARGE"; exports.ERROR_SERVER_ERROR = "SERVER_ERROR"; +exports.ERROR_SYNC_DISABLED = "SYNC_DISABLED"; exports.ERROR_TOO_MANY_CLIENT_REQUESTS = "TOO_MANY_CLIENT_REQUESTS"; exports.ERROR_SERVICE_TEMP_UNAVAILABLE = "SERVICE_TEMPORARY_UNAVAILABLE"; exports.ERROR_UI_ERROR = "UI_ERROR"; diff --git a/services/fxaccounts/FxAccountsManager.jsm b/services/fxaccounts/FxAccountsManager.jsm index 3531a7bd0ed..02d33b806cf 100644 --- a/services/fxaccounts/FxAccountsManager.jsm +++ b/services/fxaccounts/FxAccountsManager.jsm @@ -127,7 +127,15 @@ this.FxAccountsManager = { user: this._user }); } - return client[aMethod](aEmail, aPassword); + let syncEnabled = false; + try { + syncEnabled = Services.prefs.getBoolPref("services.sync.enabled"); + } catch(e) { + dump(e + "\n"); + } + // XXX Refetch FxA credentials if services.sync.enabled preference + // changes. Bug 1183103 + return client[aMethod](aEmail, aPassword, syncEnabled); } ).then( user => { @@ -577,8 +585,37 @@ this.FxAccountsManager = { return this._uiRequest(UI_REQUEST_SIGN_IN_FLOW, aAudience, principal); } ); - } + }, + getKeys: function() { + let syncEnabled = false; + try { + syncEnabled = Services.prefs.getBoolPref("services.sync.enabled"); + } catch(e) { + dump("Sync is disabled, so you won't get the keys. " + e + "\n"); + } + + if (!syncEnabled) { + return Promise.reject(ERROR_SYNC_DISABLED); + } + + return this.getAccount().then( + user => { + if (!user) { + log.debug("No signed in user"); + return Promise.resolve(null); + } + + if (!user.verified) { + return this._error(ERROR_UNVERIFIED_ACCOUNT, { + user: user + }); + } + + return this._fxAccounts.getKeys(); + } + ); + } }; FxAccountsManager.init(); diff --git a/services/fxaccounts/tests/xpcshell/test_manager.js b/services/fxaccounts/tests/xpcshell/test_manager.js index 36e2d0a9850..37774bd6fcc 100644 --- a/services/fxaccounts/tests/xpcshell/test_manager.js +++ b/services/fxaccounts/tests/xpcshell/test_manager.js @@ -101,6 +101,7 @@ FxAccountsManager._fxAccounts = { _error: 'error', _assertion: 'assertion', + _keys: 'keys', _signedInUser: null, _reset: function() { @@ -139,6 +140,13 @@ FxAccountsManager._fxAccounts = { return deferred.promise; }, + getKeys: function() { + let deferred = Promise.defer(); + this._reject ? deferred.reject(this._error) + : deferred.resolve(this._keys); + return deferred.promise; + }, + resendVerificationEmail: function() { return this.getSignedInUser().then(data => { if (data) { @@ -899,3 +907,73 @@ add_test(function() { run_next_test(); }); }); + +add_test(function(test_getKeys_sync_disabled) { + do_print("= getKeys sync disabled ="); + Services.prefs.setBoolPref("services.sync.enabled", false); + FxAccountsManager.getKeys().then( + result => { + do_throw("Unexpected success"); + }, + error => { + do_check_eq(error, ERROR_SYNC_DISABLED); + Services.prefs.clearUserPref("services.sync.enabled"); + run_next_test(); + } + ); +}); + +add_test(function(test_getKeys_no_session) { + do_print("= getKeys no session ="); + Services.prefs.setBoolPref("services.sync.enabled", true); + FxAccountsManager._fxAccounts._signedInUser = null; + FxAccountsManager._activeSession = null; + FxAccountsManager.getKeys().then( + result => { + do_check_null(result); + FxAccountsManager._fxAccounts._reset(); + Services.prefs.clearUserPref("services.sync.enabled"); + run_next_test(); + }, + error => { + do_throw("Unexpected error: " + error); + } + ); +}); + +add_test(function(test_getKeys_unverified_account) { + do_print("= getKeys unverified ="); + Services.prefs.setBoolPref("services.sync.enabled", true); + FakeFxAccountsClient._verified = false; + FxAccountsManager.signIn("user@domain.org", "password").then(result => { + do_check_false(result.verified); + return FxAccountsManager.getKeys(); + }).then(result => { + do_throw("Unexpected success"); + }, + error => { + do_check_eq(error.error, ERROR_UNVERIFIED_ACCOUNT); + FxAccountsManager._fxAccounts._reset(); + Services.prefs.clearUserPref("services.sync.enabled"); + FxAccountsManager.signOut().then(run_next_test) + } + ); +}); + +add_test(function(test_getKeys_success) { + do_print("= getKeys success ="); + Services.prefs.setBoolPref("services.sync.enabled", true); + FakeFxAccountsClient._verified = true; + FxAccountsManager.signIn("user@domain.org", "password").then(result => { + return FxAccountsManager.getKeys(); + }).then(result => { + do_check_eq(result, FxAccountsManager._fxAccounts._keys); + FxAccountsManager._fxAccounts._reset(); + Services.prefs.clearUserPref("services.sync.enabled"); + run_next_test(); + }, + error => { + do_throw("Unexpected error " + error); + } + ); +});