Bug 1182071 - Allow System app to fetch Sync keys. r=spenrose

This commit is contained in:
Fernando Jimenez 2015-07-21 21:34:37 +02:00
parent 5a56d15529
commit 82d3c832a5
4 changed files with 130 additions and 7 deletions

View File

@ -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);

View File

@ -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";

View File

@ -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();

View File

@ -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);
}
);
});