2013-12-13 15:25:26 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Temporary abstraction layer for common Fx Accounts operations.
|
|
|
|
* For now, we will be using this module only from B2G but in the end we might
|
|
|
|
* want this to be merged with FxAccounts.jsm and let other products also use
|
|
|
|
* it.
|
|
|
|
*/
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
this.EXPORTED_SYMBOLS = ["FxAccountsManager"];
|
|
|
|
|
|
|
|
const { classes: Cc, interfaces: Ci, utils: Cu } = Components;
|
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
Cu.import("resource://gre/modules/FxAccounts.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Promise.jsm");
|
2013-12-13 15:37:55 +04:00
|
|
|
Cu.import("resource://gre/modules/FxAccountsCommon.js");
|
2013-12-13 15:25:26 +04:00
|
|
|
|
2014-07-11 18:13:32 +04:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "permissionManager",
|
|
|
|
"@mozilla.org/permissionmanager;1",
|
|
|
|
"nsIPermissionManager");
|
|
|
|
|
2013-12-13 15:25:26 +04:00
|
|
|
this.FxAccountsManager = {
|
|
|
|
|
2014-01-15 16:07:20 +04:00
|
|
|
init: function() {
|
|
|
|
Services.obs.addObserver(this, ONLOGOUT_NOTIFICATION, false);
|
2014-06-06 21:42:22 +04:00
|
|
|
Services.obs.addObserver(this, ON_FXA_UPDATE_NOTIFICATION, false);
|
2014-01-15 16:07:20 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
observe: function(aSubject, aTopic, aData) {
|
2014-06-06 21:42:22 +04:00
|
|
|
// Both topics indicate our cache is invalid
|
2014-01-15 16:07:20 +04:00
|
|
|
this._activeSession = null;
|
2014-06-06 21:42:22 +04:00
|
|
|
|
|
|
|
if (aData == ONVERIFIED_NOTIFICATION) {
|
|
|
|
log.debug("FxAccountsManager: cache cleared, broadcasting: " + aData);
|
|
|
|
Services.obs.notifyObservers(null, aData, null);
|
|
|
|
}
|
2014-01-15 16:07:20 +04:00
|
|
|
},
|
|
|
|
|
2013-12-13 15:25:26 +04:00
|
|
|
// We don't really need to save fxAccounts instance but this way we allow
|
|
|
|
// to mock FxAccounts from tests.
|
|
|
|
_fxAccounts: fxAccounts,
|
|
|
|
|
|
|
|
// We keep the session details here so consumers don't need to deal with
|
|
|
|
// session tokens and are only required to handle the email.
|
|
|
|
_activeSession: null,
|
|
|
|
|
2014-04-28 22:45:58 +04:00
|
|
|
// Are we refreshing our authentication? If so, allow attempts to sign in
|
|
|
|
// while we are already signed in.
|
|
|
|
_refreshing: false,
|
|
|
|
|
2013-12-13 15:25:26 +04:00
|
|
|
// We only expose the email and the verified status so far.
|
|
|
|
get _user() {
|
|
|
|
if (!this._activeSession || !this._activeSession.email) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2014-04-30 01:47:59 +04:00
|
|
|
email: this._activeSession.email,
|
2013-12-13 15:25:26 +04:00
|
|
|
verified: this._activeSession.verified
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-02-19 14:47:11 +04:00
|
|
|
_error: function(aError, aDetails) {
|
|
|
|
log.error(aError);
|
|
|
|
let reason = {
|
|
|
|
error: aError
|
|
|
|
};
|
|
|
|
if (aDetails) {
|
|
|
|
reason.details = aDetails;
|
|
|
|
}
|
|
|
|
return Promise.reject(reason);
|
|
|
|
},
|
|
|
|
|
2013-12-13 15:25:26 +04:00
|
|
|
_getError: function(aServerResponse) {
|
|
|
|
if (!aServerResponse || !aServerResponse.error || !aServerResponse.error.errno) {
|
|
|
|
return;
|
|
|
|
}
|
2013-12-13 15:37:55 +04:00
|
|
|
let error = SERVER_ERRNO_TO_ERROR[aServerResponse.error.errno];
|
|
|
|
return error;
|
2013-12-13 15:25:26 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
_serverError: function(aServerResponse) {
|
|
|
|
let error = this._getError({ error: aServerResponse });
|
2014-02-19 14:47:11 +04:00
|
|
|
return this._error(error ? error : ERROR_SERVER_ERROR, aServerResponse);
|
2013-12-13 15:25:26 +04:00
|
|
|
},
|
|
|
|
|
2014-02-01 03:21:46 +04:00
|
|
|
// As with _fxAccounts, we don't really need this method, but this way we
|
|
|
|
// allow tests to mock FxAccountsClient. By default, we want to return the
|
|
|
|
// client used by the fxAccounts object because deep down they should have
|
|
|
|
// access to the same hawk request object which will enable them to share
|
|
|
|
// local clock skeq data.
|
|
|
|
_getFxAccountsClient: function() {
|
|
|
|
return this._fxAccounts.getAccountsClient();
|
2013-12-13 15:25:26 +04:00
|
|
|
},
|
|
|
|
|
2014-04-30 01:47:59 +04:00
|
|
|
_signInSignUp: function(aMethod, aEmail, aPassword) {
|
2013-12-13 15:25:26 +04:00
|
|
|
if (Services.io.offline) {
|
2014-02-19 14:47:11 +04:00
|
|
|
return this._error(ERROR_OFFLINE);
|
2013-12-13 15:25:26 +04:00
|
|
|
}
|
|
|
|
|
2014-04-30 01:47:59 +04:00
|
|
|
if (!aEmail) {
|
|
|
|
return this._error(ERROR_INVALID_EMAIL);
|
2013-12-13 15:25:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!aPassword) {
|
2014-02-19 14:47:11 +04:00
|
|
|
return this._error(ERROR_INVALID_PASSWORD);
|
2013-12-13 15:25:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check that there is no signed in account first.
|
2014-04-28 22:45:58 +04:00
|
|
|
if ((!this._refreshing) && this._activeSession) {
|
2014-02-19 14:47:11 +04:00
|
|
|
return this._error(ERROR_ALREADY_SIGNED_IN_USER, {
|
|
|
|
user: this._user
|
2013-12-13 15:25:26 +04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-02-01 03:21:46 +04:00
|
|
|
let client = this._getFxAccountsClient();
|
2013-12-13 15:25:26 +04:00
|
|
|
return this._fxAccounts.getSignedInUser().then(
|
|
|
|
user => {
|
2014-04-28 22:45:58 +04:00
|
|
|
if ((!this._refreshing) && user) {
|
2014-02-19 14:47:11 +04:00
|
|
|
return this._error(ERROR_ALREADY_SIGNED_IN_USER, {
|
|
|
|
user: this._user
|
2013-12-13 15:25:26 +04:00
|
|
|
});
|
|
|
|
}
|
2014-04-30 01:47:59 +04:00
|
|
|
return client[aMethod](aEmail, aPassword);
|
2013-12-13 15:25:26 +04:00
|
|
|
}
|
|
|
|
).then(
|
|
|
|
user => {
|
|
|
|
let error = this._getError(user);
|
|
|
|
if (!user || !user.uid || !user.sessionToken || error) {
|
2014-02-19 14:47:11 +04:00
|
|
|
return this._error(error ? error : ERROR_INTERNAL_INVALID_USER, {
|
|
|
|
user: user
|
2013-12-13 15:25:26 +04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-02-01 03:21:46 +04:00
|
|
|
// If the user object includes an email field, it may differ in
|
|
|
|
// capitalization from what we sent down. This is the server's
|
|
|
|
// canonical capitalization and should be used instead.
|
2014-04-30 01:47:59 +04:00
|
|
|
user.email = user.email || aEmail;
|
2014-07-01 21:31:17 +04:00
|
|
|
|
|
|
|
// If we're using server-side sign to refreshAuthentication
|
|
|
|
// we don't need to update local state; also because of two
|
|
|
|
// interacting glitches we need to bypass an event emission.
|
|
|
|
// See https://bugzilla.mozilla.org/show_bug.cgi?id=1031580
|
|
|
|
if (this._refreshing) {
|
|
|
|
return Promise.resolve({user: this._user});
|
|
|
|
}
|
|
|
|
|
2014-02-01 03:21:46 +04:00
|
|
|
return this._fxAccounts.setSignedInUser(user).then(
|
2013-12-13 15:25:26 +04:00
|
|
|
() => {
|
|
|
|
this._activeSession = user;
|
2013-12-13 15:37:55 +04:00
|
|
|
log.debug("User signed in: " + JSON.stringify(this._user) +
|
|
|
|
" - Account created " + (aMethod == "signUp"));
|
2013-12-13 15:25:26 +04:00
|
|
|
return Promise.resolve({
|
|
|
|
accountCreated: aMethod === "signUp",
|
|
|
|
user: this._user
|
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
reason => { return this._serverError(reason); }
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2014-06-06 19:54:27 +04:00
|
|
|
/**
|
|
|
|
* Determine whether the incoming error means that the current account
|
|
|
|
* has new server-side state via deletion or password change, and if so,
|
|
|
|
* spawn the appropriate UI (sign in or refresh); otherwise re-reject.
|
|
|
|
*
|
|
|
|
* As of May 2014, the only HTTP call triggered by this._getAssertion()
|
|
|
|
* is to /certificate/sign via:
|
|
|
|
* FxAccounts.getAssertion()
|
|
|
|
* FxAccountsInternal.getCertificateSigned()
|
|
|
|
* FxAccountsClient.signCertificate()
|
|
|
|
* See the latter method for possible (error code, errno) pairs.
|
|
|
|
*/
|
2014-07-11 18:13:32 +04:00
|
|
|
_handleGetAssertionError: function(reason, aAudience, aPrincipal) {
|
2014-06-06 19:54:27 +04:00
|
|
|
let errno = (reason ? reason.errno : NaN) || NaN;
|
|
|
|
// If the previously valid email/password pair is no longer valid ...
|
|
|
|
if (errno == ERRNO_INVALID_AUTH_TOKEN) {
|
|
|
|
return this._fxAccounts.accountStatus().then(
|
|
|
|
(exists) => {
|
|
|
|
// ... if the email still maps to an account, the password
|
|
|
|
// must have changed, so ask the user to enter the new one ...
|
|
|
|
if (exists) {
|
|
|
|
return this.getAccount().then(
|
|
|
|
(user) => {
|
2014-07-11 18:13:32 +04:00
|
|
|
return this._refreshAuthentication(aAudience, user.email,
|
|
|
|
aPrincipal,
|
|
|
|
true /* logoutOnFailure */);
|
2014-06-06 19:54:27 +04:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2014-09-09 03:45:03 +04:00
|
|
|
// ... otherwise, the account was deleted, so ask for Sign In/Up
|
|
|
|
return this._localSignOut().then(
|
|
|
|
() => {
|
|
|
|
return this._uiRequest(UI_REQUEST_SIGN_IN_FLOW, aAudience,
|
|
|
|
aPrincipal);
|
|
|
|
},
|
|
|
|
(reason) => {
|
|
|
|
// reject primary problem, not signout failure
|
|
|
|
log.error("Signing out in response to server error threw: " +
|
|
|
|
reason);
|
|
|
|
return this._error(reason);
|
|
|
|
}
|
|
|
|
);
|
2014-07-11 18:13:32 +04:00
|
|
|
}
|
|
|
|
);
|
2014-06-06 19:54:27 +04:00
|
|
|
}
|
2014-07-09 05:00:25 +04:00
|
|
|
return Promise.reject(reason);
|
2014-06-06 19:54:27 +04:00
|
|
|
},
|
|
|
|
|
2014-07-11 18:13:32 +04:00
|
|
|
_getAssertion: function(aAudience, aPrincipal) {
|
2014-06-06 19:54:27 +04:00
|
|
|
return this._fxAccounts.getAssertion(aAudience).then(
|
|
|
|
(result) => {
|
2014-07-11 18:13:32 +04:00
|
|
|
if (aPrincipal) {
|
|
|
|
this._addPermission(aPrincipal);
|
|
|
|
}
|
2014-06-06 19:54:27 +04:00
|
|
|
return result;
|
|
|
|
},
|
|
|
|
(reason) => {
|
2014-07-11 18:13:32 +04:00
|
|
|
return this._handleGetAssertionError(reason, aAudience, aPrincipal);
|
2014-06-06 19:54:27 +04:00
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2014-06-11 03:38:55 +04:00
|
|
|
/**
|
|
|
|
* "Refresh authentication" means:
|
|
|
|
* Interactively demonstrate knowledge of the FxA password
|
|
|
|
* for the currently logged-in account.
|
|
|
|
* There are two very different scenarios:
|
|
|
|
* 1) The password has changed on the server. Failure should log
|
|
|
|
* the current account OUT.
|
|
|
|
* 2) The person typing can't prove knowledge of the password used
|
|
|
|
* to log in. Failure should do nothing.
|
|
|
|
*/
|
2014-07-11 18:13:32 +04:00
|
|
|
_refreshAuthentication: function(aAudience, aEmail, aPrincipal,
|
|
|
|
logoutOnFailure=false) {
|
2014-06-06 19:54:27 +04:00
|
|
|
this._refreshing = true;
|
|
|
|
return this._uiRequest(UI_REQUEST_REFRESH_AUTH,
|
2014-07-11 18:13:32 +04:00
|
|
|
aAudience, aPrincipal, aEmail).then(
|
2014-06-06 19:54:27 +04:00
|
|
|
(assertion) => {
|
|
|
|
this._refreshing = false;
|
|
|
|
return assertion;
|
|
|
|
},
|
|
|
|
(reason) => {
|
|
|
|
this._refreshing = false;
|
2014-06-11 03:38:55 +04:00
|
|
|
if (logoutOnFailure) {
|
|
|
|
return this._signOut().then(
|
|
|
|
() => {
|
|
|
|
return this._error(reason);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return this._error(reason);
|
2014-06-06 19:54:27 +04:00
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
_localSignOut: function() {
|
|
|
|
return this._fxAccounts.signOut(true);
|
2013-12-13 15:25:26 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
_signOut: function() {
|
|
|
|
if (!this._activeSession) {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
2014-01-15 16:07:20 +04:00
|
|
|
// We clear the local session cache as soon as we get the onlogout
|
|
|
|
// notification triggered within FxAccounts.signOut, so we save the
|
|
|
|
// session token value to be able to remove the remote server session
|
|
|
|
// in case that we have network connection.
|
|
|
|
let sessionToken = this._activeSession.sessionToken;
|
|
|
|
|
2014-06-06 19:54:27 +04:00
|
|
|
return this._localSignOut().then(
|
2013-12-13 15:25:26 +04:00
|
|
|
() => {
|
2014-01-15 16:07:20 +04:00
|
|
|
// At this point the local session should already be removed.
|
|
|
|
|
|
|
|
// The client can create new sessions up to the limit (100?).
|
2013-12-13 15:25:26 +04:00
|
|
|
// Orphaned tokens on the server will eventually be garbage collected.
|
|
|
|
if (Services.io.offline) {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
// Otherwise, we try to remove the remote session.
|
2014-02-01 03:21:46 +04:00
|
|
|
let client = this._getFxAccountsClient();
|
2014-01-15 16:07:20 +04:00
|
|
|
return client.signOut(sessionToken).then(
|
2013-12-13 15:25:26 +04:00
|
|
|
result => {
|
|
|
|
let error = this._getError(result);
|
|
|
|
if (error) {
|
2014-02-19 14:47:11 +04:00
|
|
|
return this._error(error, result);
|
2013-12-13 15:25:26 +04:00
|
|
|
}
|
2013-12-13 15:37:55 +04:00
|
|
|
log.debug("Signed out");
|
2013-12-13 15:25:26 +04:00
|
|
|
return Promise.resolve();
|
|
|
|
},
|
|
|
|
reason => {
|
|
|
|
return this._serverError(reason);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2014-07-11 18:13:32 +04:00
|
|
|
_uiRequest: function(aRequest, aAudience, aPrincipal, aParams) {
|
2014-02-19 14:47:11 +04:00
|
|
|
let ui = Cc["@mozilla.org/fxaccounts/fxaccounts-ui-glue;1"]
|
|
|
|
.createInstance(Ci.nsIFxAccountsUIGlue);
|
|
|
|
if (!ui[aRequest]) {
|
|
|
|
return this._error(ERROR_UI_REQUEST);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!aParams || !Array.isArray(aParams)) {
|
|
|
|
aParams = [aParams];
|
|
|
|
}
|
|
|
|
|
|
|
|
return ui[aRequest].apply(this, aParams).then(
|
|
|
|
result => {
|
|
|
|
// Even if we get a successful result from the UI, the account will
|
|
|
|
// most likely be unverified, so we cannot get an assertion.
|
|
|
|
if (result && result.verified) {
|
2014-07-11 18:13:32 +04:00
|
|
|
return this._getAssertion(aAudience, aPrincipal);
|
2014-02-19 14:47:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return this._error(ERROR_UNVERIFIED_ACCOUNT, {
|
|
|
|
user: result
|
|
|
|
});
|
|
|
|
},
|
|
|
|
error => {
|
|
|
|
return this._error(ERROR_UI_ERROR, error);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2014-07-11 18:13:32 +04:00
|
|
|
_addPermission: function(aPrincipal) {
|
|
|
|
// This will fail from tests cause we are running them in the child
|
|
|
|
// process until we have chrome tests in b2g. Bug 797164.
|
|
|
|
try {
|
|
|
|
permissionManager.addFromPrincipal(aPrincipal, FXACCOUNTS_PERMISSION,
|
|
|
|
Ci.nsIPermissionManager.ALLOW_ACTION);
|
|
|
|
} catch (e) {
|
|
|
|
log.warn("Could not add permission " + e);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-12-13 15:25:26 +04:00
|
|
|
// -- API --
|
|
|
|
|
2014-04-30 01:47:59 +04:00
|
|
|
signIn: function(aEmail, aPassword) {
|
|
|
|
return this._signInSignUp("signIn", aEmail, aPassword);
|
2013-12-13 15:25:26 +04:00
|
|
|
},
|
|
|
|
|
2014-04-30 01:47:59 +04:00
|
|
|
signUp: function(aEmail, aPassword) {
|
|
|
|
return this._signInSignUp("signUp", aEmail, aPassword);
|
2013-12-13 15:25:26 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
signOut: function() {
|
|
|
|
if (!this._activeSession) {
|
|
|
|
// If there is no cached active session, we try to get it from the
|
|
|
|
// account storage.
|
|
|
|
return this.getAccount().then(
|
|
|
|
result => {
|
|
|
|
if (!result) {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
return this._signOut();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return this._signOut();
|
|
|
|
},
|
|
|
|
|
2014-05-07 22:14:05 +04:00
|
|
|
resendVerificationEmail: function() {
|
|
|
|
return this._fxAccounts.resendVerificationEmail().then(
|
|
|
|
(result) => {
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
(error) => {
|
|
|
|
return this._error(ERROR_SERVER_ERROR, error);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2013-12-13 15:25:26 +04:00
|
|
|
getAccount: function() {
|
|
|
|
// We check first if we have session details cached.
|
|
|
|
if (this._activeSession) {
|
2014-03-26 20:12:14 +04:00
|
|
|
// If our cache says that the account is not yet verified,
|
|
|
|
// we kick off verification before returning what we have.
|
2013-12-13 15:25:26 +04:00
|
|
|
if (this._activeSession && !this._activeSession.verified &&
|
|
|
|
!Services.io.offline) {
|
2014-03-26 20:12:14 +04:00
|
|
|
this.verificationStatus(this._activeSession);
|
2013-12-13 15:25:26 +04:00
|
|
|
}
|
|
|
|
|
2013-12-13 15:37:55 +04:00
|
|
|
log.debug("Account " + JSON.stringify(this._user));
|
2013-12-13 15:25:26 +04:00
|
|
|
return Promise.resolve(this._user);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If no cached information, we try to get it from the persistent storage.
|
|
|
|
return this._fxAccounts.getSignedInUser().then(
|
|
|
|
user => {
|
|
|
|
if (!user || !user.email) {
|
2013-12-13 15:37:55 +04:00
|
|
|
log.debug("No signed in account");
|
2013-12-13 15:25:26 +04:00
|
|
|
return Promise.resolve(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
this._activeSession = user;
|
|
|
|
// If we get a stored information of a not yet verified account,
|
2014-03-26 20:12:14 +04:00
|
|
|
// we kick off verification before returning what we have.
|
2013-12-13 15:25:26 +04:00
|
|
|
if (!user.verified && !Services.io.offline) {
|
2013-12-13 15:37:55 +04:00
|
|
|
log.debug("Unverified account");
|
2014-03-26 20:12:14 +04:00
|
|
|
this.verificationStatus(user);
|
2013-12-13 15:25:26 +04:00
|
|
|
}
|
|
|
|
|
2013-12-13 15:37:55 +04:00
|
|
|
log.debug("Account " + JSON.stringify(this._user));
|
2013-12-13 15:25:26 +04:00
|
|
|
return Promise.resolve(this._user);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2014-04-30 01:47:59 +04:00
|
|
|
queryAccount: function(aEmail) {
|
|
|
|
log.debug("queryAccount " + aEmail);
|
2013-12-13 15:25:26 +04:00
|
|
|
if (Services.io.offline) {
|
2014-02-19 14:47:11 +04:00
|
|
|
return this._error(ERROR_OFFLINE);
|
2013-12-13 15:25:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
let deferred = Promise.defer();
|
|
|
|
|
2014-04-30 01:47:59 +04:00
|
|
|
if (!aEmail) {
|
|
|
|
return this._error(ERROR_INVALID_EMAIL);
|
2013-12-13 15:25:26 +04:00
|
|
|
}
|
|
|
|
|
2014-02-01 03:21:46 +04:00
|
|
|
let client = this._getFxAccountsClient();
|
2014-04-30 01:47:59 +04:00
|
|
|
return client.accountExists(aEmail).then(
|
2013-12-13 15:25:26 +04:00
|
|
|
result => {
|
2013-12-13 15:37:55 +04:00
|
|
|
log.debug("Account " + result ? "" : "does not" + " exists");
|
2013-12-13 15:25:26 +04:00
|
|
|
let error = this._getError(result);
|
|
|
|
if (error) {
|
2014-02-19 14:47:11 +04:00
|
|
|
return this._error(error, result);
|
2013-12-13 15:25:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.resolve({
|
|
|
|
registered: result
|
|
|
|
});
|
|
|
|
},
|
|
|
|
reason => { this._serverError(reason); }
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
verificationStatus: function() {
|
2013-12-13 15:37:55 +04:00
|
|
|
log.debug("verificationStatus");
|
2013-12-13 15:25:26 +04:00
|
|
|
if (!this._activeSession || !this._activeSession.sessionToken) {
|
2014-03-26 20:12:14 +04:00
|
|
|
this._error(ERROR_NO_TOKEN_SESSION);
|
2013-12-13 15:25:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// There is no way to unverify an already verified account, so we just
|
|
|
|
// return the account details of a verified account
|
|
|
|
if (this._activeSession.verified) {
|
2013-12-13 15:37:55 +04:00
|
|
|
log.debug("Account already verified");
|
2014-03-26 20:12:14 +04:00
|
|
|
return;
|
2013-12-13 15:25:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Services.io.offline) {
|
2014-03-26 20:12:14 +04:00
|
|
|
this._error(ERROR_OFFLINE);
|
2013-12-13 15:25:26 +04:00
|
|
|
}
|
|
|
|
|
2014-02-01 03:21:46 +04:00
|
|
|
let client = this._getFxAccountsClient();
|
2014-03-26 20:12:14 +04:00
|
|
|
client.recoveryEmailStatus(this._activeSession.sessionToken).then(
|
2013-12-13 15:25:26 +04:00
|
|
|
data => {
|
|
|
|
let error = this._getError(data);
|
|
|
|
if (error) {
|
2014-03-26 20:12:14 +04:00
|
|
|
this._error(error, data);
|
2013-12-13 15:25:26 +04:00
|
|
|
}
|
2014-03-26 20:12:14 +04:00
|
|
|
// If the verification status has changed, update state.
|
2013-12-13 15:25:26 +04:00
|
|
|
if (this._activeSession.verified != data.verified) {
|
|
|
|
this._activeSession.verified = data.verified;
|
2014-03-26 20:12:14 +04:00
|
|
|
this._fxAccounts.setSignedInUser(this._activeSession);
|
2013-12-13 15:25:26 +04:00
|
|
|
}
|
2013-12-13 15:37:55 +04:00
|
|
|
log.debug(JSON.stringify(this._user));
|
2013-12-13 15:25:26 +04:00
|
|
|
},
|
2014-03-26 20:12:14 +04:00
|
|
|
reason => { this._serverError(reason); }
|
2013-12-13 15:25:26 +04:00
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2014-03-12 04:49:26 +04:00
|
|
|
/*
|
2014-06-06 19:54:27 +04:00
|
|
|
* Try to get an assertion for the given audience. Here we implement
|
|
|
|
* the heart of the response to navigator.mozId.request() on device.
|
|
|
|
* (We can also be called via the IAC API, but it's request() that
|
|
|
|
* makes this method complex.) The state machine looks like this,
|
|
|
|
* ignoring simple errors:
|
|
|
|
* If no one is signed in, and we aren't suppressing the UI:
|
|
|
|
* trigger the sign in flow.
|
|
|
|
* else if we were asked to refresh and the grace period is up:
|
|
|
|
* trigger the refresh flow.
|
2014-07-11 18:13:32 +04:00
|
|
|
* else:
|
|
|
|
* request user permission to share an assertion if we don't have it
|
|
|
|
* already and ask the core code for an assertion, which might itself
|
|
|
|
* trigger either the sign in or refresh flows (if our account
|
|
|
|
* changed on the server).
|
2014-03-12 04:49:26 +04:00
|
|
|
*
|
|
|
|
* aOptions can include:
|
|
|
|
* refreshAuthentication - (bool) Force re-auth.
|
|
|
|
* silent - (bool) Prevent any UI interaction.
|
|
|
|
* I.e., try to get an automatic assertion.
|
|
|
|
*/
|
2014-07-11 18:13:32 +04:00
|
|
|
getAssertion: function(aAudience, aPrincipal, aOptions) {
|
2013-12-13 15:25:26 +04:00
|
|
|
if (!aAudience) {
|
2014-02-19 14:47:11 +04:00
|
|
|
return this._error(ERROR_INVALID_AUDIENCE);
|
2013-12-13 15:25:26 +04:00
|
|
|
}
|
|
|
|
if (Services.io.offline) {
|
2014-02-19 14:47:11 +04:00
|
|
|
return this._error(ERROR_OFFLINE);
|
2013-12-13 15:25:26 +04:00
|
|
|
}
|
2014-07-11 18:13:32 +04:00
|
|
|
|
|
|
|
let secMan = Cc["@mozilla.org/scriptsecuritymanager;1"]
|
|
|
|
.getService(Ci.nsIScriptSecurityManager);
|
|
|
|
let uri = Services.io.newURI(aPrincipal.origin, null, null);
|
|
|
|
let principal = secMan.getAppCodebasePrincipal(uri,
|
|
|
|
aPrincipal.appId, aPrincipal.isInBrowserElement);
|
|
|
|
|
2013-12-13 15:25:26 +04:00
|
|
|
return this.getAccount().then(
|
|
|
|
user => {
|
|
|
|
if (user) {
|
2014-06-06 19:54:27 +04:00
|
|
|
// Three have-user cases to consider. First: are we unverified?
|
2014-02-19 14:47:11 +04:00
|
|
|
if (!user.verified) {
|
|
|
|
return this._error(ERROR_UNVERIFIED_ACCOUNT, {
|
|
|
|
user: user
|
|
|
|
});
|
2013-12-13 15:25:26 +04:00
|
|
|
}
|
2014-06-06 19:54:27 +04:00
|
|
|
// Second case: do we need to refresh?
|
2014-02-19 14:47:11 +04:00
|
|
|
if (aOptions &&
|
2014-04-28 20:44:36 +04:00
|
|
|
(typeof(aOptions.refreshAuthentication) != "undefined")) {
|
2014-02-19 14:47:11 +04:00
|
|
|
let gracePeriod = aOptions.refreshAuthentication;
|
2014-04-28 22:45:58 +04:00
|
|
|
if (typeof(gracePeriod) !== "number" || isNaN(gracePeriod)) {
|
2014-02-19 14:47:11 +04:00
|
|
|
return this._error(ERROR_INVALID_REFRESH_AUTH_VALUE);
|
2013-12-13 15:25:26 +04:00
|
|
|
}
|
2014-04-28 22:45:58 +04:00
|
|
|
// Forcing refreshAuth to silent is a contradiction in terms,
|
2014-06-06 19:54:27 +04:00
|
|
|
// though it might succeed silently if we didn't reject here.
|
2014-04-28 22:45:58 +04:00
|
|
|
if (aOptions.silent) {
|
|
|
|
return this._error(ERROR_NO_SILENT_REFRESH_AUTH);
|
|
|
|
}
|
2014-07-11 18:13:32 +04:00
|
|
|
let secondsSinceAuth = (Date.now() / 1000) -
|
|
|
|
this._activeSession.authAt;
|
2014-06-06 19:54:27 +04:00
|
|
|
if (secondsSinceAuth > gracePeriod) {
|
2014-07-11 18:13:32 +04:00
|
|
|
return this._refreshAuthentication(aAudience, user.email,
|
|
|
|
principal,
|
|
|
|
false /* logoutOnFailure */);
|
2014-02-19 14:47:11 +04:00
|
|
|
}
|
|
|
|
}
|
2014-06-06 19:54:27 +04:00
|
|
|
// Third case: we are all set *locally*. Probably we just return
|
|
|
|
// the assertion, but the attempt might lead to the server saying
|
|
|
|
// we are deleted or have a new password, which will trigger a flow.
|
2014-07-11 18:13:32 +04:00
|
|
|
// Also we need to check if we have permission to get the assertion,
|
|
|
|
// otherwise we need to show the forceAuth UI to let the user know
|
|
|
|
// that the RP with no fxa permissions is trying to obtain an
|
|
|
|
// assertion. Once the user authenticates herself in the forceAuth UI
|
|
|
|
// the permission will be remembered by default.
|
|
|
|
let permission = permissionManager.testPermissionFromPrincipal(
|
|
|
|
principal,
|
|
|
|
FXACCOUNTS_PERMISSION
|
|
|
|
);
|
|
|
|
if (permission == Ci.nsIPermissionManager.PROMPT_ACTION &&
|
|
|
|
!this._refreshing) {
|
|
|
|
return this._refreshAuthentication(aAudience, user.email,
|
|
|
|
principal,
|
|
|
|
false /* logoutOnFailure */);
|
|
|
|
} else if (permission == Ci.nsIPermissionManager.DENY_ACTION &&
|
|
|
|
!this._refreshing) {
|
|
|
|
return this._error(ERROR_PERMISSION_DENIED);
|
|
|
|
}
|
|
|
|
return this._getAssertion(aAudience, principal);
|
2013-12-13 15:25:26 +04:00
|
|
|
}
|
2013-12-13 15:37:55 +04:00
|
|
|
log.debug("No signed in user");
|
2014-03-26 20:12:14 +04:00
|
|
|
if (aOptions && aOptions.silent) {
|
2014-03-12 04:49:26 +04:00
|
|
|
return Promise.resolve(null);
|
|
|
|
}
|
2014-07-11 18:13:32 +04:00
|
|
|
return this._uiRequest(UI_REQUEST_SIGN_IN_FLOW, aAudience, principal);
|
2013-12-13 15:25:26 +04:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2014-02-19 14:47:11 +04:00
|
|
|
|
2013-12-13 15:25:26 +04:00
|
|
|
};
|
2014-01-15 16:07:20 +04:00
|
|
|
|
|
|
|
FxAccountsManager.init();
|