2013-12-06 10:46:12 +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/. */
|
|
|
|
|
|
|
|
this.EXPORTED_SYMBOLS = ["fxAccounts", "FxAccounts"];
|
|
|
|
|
|
|
|
const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
|
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/Log.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Promise.jsm");
|
|
|
|
Cu.import("resource://gre/modules/osfile.jsm");
|
|
|
|
Cu.import("resource://services-common/utils.js");
|
|
|
|
Cu.import("resource://services-crypto/utils.js");
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Timer.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Task.jsm");
|
2013-12-13 15:37:55 +04:00
|
|
|
Cu.import("resource://gre/modules/FxAccountsCommon.js");
|
2013-12-06 10:46:12 +04:00
|
|
|
|
2014-03-26 05:13:46 +04:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "FxAccountsClient",
|
|
|
|
"resource://gre/modules/FxAccountsClient.jsm");
|
|
|
|
|
2013-12-06 10:46:12 +04:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "jwcrypto",
|
2014-02-04 14:12:37 +04:00
|
|
|
"resource://gre/modules/identity/jwcrypto.jsm");
|
|
|
|
|
2015-02-06 00:31:23 +03:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "FxAccountsOAuthGrantClient",
|
|
|
|
"resource://gre/modules/FxAccountsOAuthGrantClient.jsm");
|
|
|
|
|
2015-03-26 11:33:38 +03:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "FxAccountsProfile",
|
|
|
|
"resource://gre/modules/FxAccountsProfile.jsm");
|
|
|
|
|
2014-02-04 14:12:37 +04:00
|
|
|
// All properties exposed by the public FxAccounts API.
|
|
|
|
let publicProperties = [
|
2014-05-16 03:52:52 +04:00
|
|
|
"accountStatus",
|
2014-02-19 20:34:42 +04:00
|
|
|
"getAccountsClient",
|
2014-02-18 15:05:13 +04:00
|
|
|
"getAccountsSignInURI",
|
2014-03-12 02:58:27 +04:00
|
|
|
"getAccountsSignUpURI",
|
2014-02-04 14:12:37 +04:00
|
|
|
"getAssertion",
|
|
|
|
"getKeys",
|
|
|
|
"getSignedInUser",
|
2015-02-06 00:31:23 +03:00
|
|
|
"getOAuthToken",
|
2015-03-26 11:33:38 +03:00
|
|
|
"getSignedInUserProfile",
|
2014-02-04 14:12:37 +04:00
|
|
|
"loadAndPoll",
|
|
|
|
"localtimeOffsetMsec",
|
|
|
|
"now",
|
|
|
|
"promiseAccountsForceSigninURI",
|
2015-03-27 12:37:55 +03:00
|
|
|
"promiseAccountsChangeProfileURI",
|
2015-04-24 03:51:20 +03:00
|
|
|
"promiseAccountsManageURI",
|
2015-04-03 04:47:00 +03:00
|
|
|
"removeCachedOAuthToken",
|
2014-02-04 14:12:37 +04:00
|
|
|
"resendVerificationEmail",
|
|
|
|
"setSignedInUser",
|
|
|
|
"signOut",
|
|
|
|
"version",
|
|
|
|
"whenVerified"
|
|
|
|
];
|
2013-12-06 10:46:12 +04:00
|
|
|
|
2014-03-03 03:20:56 +04:00
|
|
|
// An AccountState object holds all state related to one specific account.
|
|
|
|
// Only one AccountState is ever "current" in the FxAccountsInternal object -
|
|
|
|
// whenever a user logs out or logs in, the current AccountState is discarded,
|
|
|
|
// making it impossible for the wrong state or state data to be accidentally
|
|
|
|
// used.
|
|
|
|
// In addition, it has some promise-related helpers to ensure that if an
|
|
|
|
// attempt is made to resolve a promise on a "stale" state (eg, if an
|
|
|
|
// operation starts, but a different user logs in before the operation
|
|
|
|
// completes), the promise will be rejected.
|
|
|
|
// It is intended to be used thusly:
|
|
|
|
// somePromiseBasedFunction: function() {
|
|
|
|
// let currentState = this.currentAccountState;
|
|
|
|
// return someOtherPromiseFunction().then(
|
|
|
|
// data => currentState.resolve(data)
|
|
|
|
// );
|
|
|
|
// }
|
|
|
|
// If the state has changed between the function being called and the promise
|
|
|
|
// being resolved, the .resolve() call will actually be rejected.
|
2015-04-03 04:47:00 +03:00
|
|
|
let AccountState = function(fxaInternal, signedInUserStorage, accountData = null) {
|
2014-03-03 03:20:56 +04:00
|
|
|
this.fxaInternal = fxaInternal;
|
2015-04-03 04:47:00 +03:00
|
|
|
this.signedInUserStorage = signedInUserStorage;
|
|
|
|
this.signedInUser = accountData ? {version: DATA_FORMAT_VERSION, accountData} : null;
|
|
|
|
this.uid = accountData ? accountData.uid : null;
|
|
|
|
this.oauthTokens = {};
|
2014-03-03 03:20:56 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
AccountState.prototype = {
|
|
|
|
cert: null,
|
|
|
|
keyPair: null,
|
|
|
|
signedInUser: null,
|
2015-04-03 04:47:00 +03:00
|
|
|
oauthTokens: null,
|
2014-02-18 21:47:52 +04:00
|
|
|
whenVerifiedDeferred: null,
|
|
|
|
whenKeysReadyDeferred: null,
|
2015-03-26 11:33:38 +03:00
|
|
|
profile: null,
|
2015-04-03 04:47:00 +03:00
|
|
|
promiseInitialAccountData: null,
|
2015-04-03 04:47:00 +03:00
|
|
|
uid: null,
|
2014-03-03 03:20:56 +04:00
|
|
|
|
|
|
|
get isCurrent() this.fxaInternal && this.fxaInternal.currentAccountState === this,
|
|
|
|
|
|
|
|
abort: function() {
|
2014-02-18 21:47:52 +04:00
|
|
|
if (this.whenVerifiedDeferred) {
|
|
|
|
this.whenVerifiedDeferred.reject(
|
2014-03-03 03:20:56 +04:00
|
|
|
new Error("Verification aborted; Another user signing in"));
|
2014-02-18 21:47:52 +04:00
|
|
|
this.whenVerifiedDeferred = null;
|
2014-03-03 03:20:56 +04:00
|
|
|
}
|
|
|
|
|
2014-02-18 21:47:52 +04:00
|
|
|
if (this.whenKeysReadyDeferred) {
|
|
|
|
this.whenKeysReadyDeferred.reject(
|
2014-03-03 03:20:56 +04:00
|
|
|
new Error("Verification aborted; Another user signing in"));
|
2014-02-18 21:47:52 +04:00
|
|
|
this.whenKeysReadyDeferred = null;
|
2014-03-03 03:20:56 +04:00
|
|
|
}
|
2015-03-26 11:33:38 +03:00
|
|
|
|
2014-03-03 03:20:56 +04:00
|
|
|
this.cert = null;
|
|
|
|
this.keyPair = null;
|
|
|
|
this.signedInUser = null;
|
2015-04-03 04:47:00 +03:00
|
|
|
this.uid = null;
|
2014-03-03 03:20:56 +04:00
|
|
|
this.fxaInternal = null;
|
2015-03-26 11:33:38 +03:00
|
|
|
this.initProfilePromise = null;
|
|
|
|
|
|
|
|
if (this.profile) {
|
|
|
|
this.profile.tearDown();
|
|
|
|
this.profile = null;
|
|
|
|
}
|
2014-03-03 03:20:56 +04:00
|
|
|
},
|
|
|
|
|
2015-04-03 04:47:00 +03:00
|
|
|
// Clobber all cached data and write that empty data to storage.
|
|
|
|
signOut() {
|
|
|
|
this.cert = null;
|
|
|
|
this.keyPair = null;
|
|
|
|
this.signedInUser = null;
|
|
|
|
this.oauthTokens = {};
|
|
|
|
this.uid = null;
|
|
|
|
return this.persistUserData();
|
|
|
|
},
|
|
|
|
|
|
|
|
getUserAccountData() {
|
2015-04-03 04:47:00 +03:00
|
|
|
if (!this.isCurrent) {
|
|
|
|
return this.reject(new Error("Another user has signed in"));
|
|
|
|
}
|
|
|
|
if (this.promiseInitialAccountData) {
|
|
|
|
// We are still reading the data for the first and only time.
|
|
|
|
return this.promiseInitialAccountData;
|
|
|
|
}
|
|
|
|
// We've previously read it successfully (and possibly updated it since)
|
2014-03-03 03:20:56 +04:00
|
|
|
if (this.signedInUser) {
|
|
|
|
return this.resolve(this.signedInUser.accountData);
|
|
|
|
}
|
|
|
|
|
2015-04-03 04:47:00 +03:00
|
|
|
// We fetch the signedInUser data first, then fetch the token store and
|
|
|
|
// ensure the uid in the tokens matches our user.
|
|
|
|
let accountData = null;
|
|
|
|
let oauthTokens = {};
|
|
|
|
return this.promiseInitialAccountData = this.signedInUserStorage.get()
|
|
|
|
.then(user => {
|
2014-05-15 06:00:59 +04:00
|
|
|
if (logPII) {
|
2015-04-03 04:47:00 +03:00
|
|
|
log.debug("getUserAccountData", user);
|
2014-03-03 03:20:56 +04:00
|
|
|
}
|
2015-04-03 04:47:00 +03:00
|
|
|
// In an ideal world we could cache the data in this.signedInUser, but
|
|
|
|
// if we do, the interaction with the login manager breaks when the
|
|
|
|
// password is locked as this read may only have obtained partial data.
|
|
|
|
// Therefore every read *must* really read incase the login manager is
|
|
|
|
// now unlocked. We could fix this with a refactor...
|
|
|
|
accountData = user ? user.accountData : null;
|
|
|
|
}, err => {
|
|
|
|
// Error reading signed in user account data.
|
2015-04-03 04:47:00 +03:00
|
|
|
this.promiseInitialAccountData = null;
|
2014-03-03 03:20:56 +04:00
|
|
|
if (err instanceof OS.File.Error && err.becauseNoSuchFile) {
|
|
|
|
// File hasn't been created yet. That will be done
|
2015-04-03 04:47:00 +03:00
|
|
|
// on the first call to setSignedInUser
|
|
|
|
return;
|
2014-03-03 03:20:56 +04:00
|
|
|
}
|
2015-04-03 04:47:00 +03:00
|
|
|
// something else went wrong - report the error but continue without
|
|
|
|
// user data.
|
|
|
|
log.error("Failed to read signed in user data", err);
|
|
|
|
}).then(() => {
|
|
|
|
if (!accountData) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return this.signedInUserStorage.getOAuthTokens();
|
|
|
|
}).then(tokenData => {
|
|
|
|
if (tokenData && tokenData.tokens &&
|
|
|
|
tokenData.version == DATA_FORMAT_VERSION &&
|
|
|
|
tokenData.uid == accountData.uid ) {
|
|
|
|
oauthTokens = tokenData.tokens;
|
|
|
|
}
|
|
|
|
}, err => {
|
|
|
|
// Error reading the OAuth tokens file.
|
|
|
|
if (err instanceof OS.File.Error && err.becauseNoSuchFile) {
|
|
|
|
// File hasn't been created yet, but will be when tokens are saved.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
log.error("Failed to read oauth tokens", err)
|
|
|
|
}).then(() => {
|
|
|
|
// We are done - clear our promise and save the data if we are still
|
|
|
|
// current.
|
|
|
|
this.promiseInitialAccountData = null;
|
|
|
|
if (this.isCurrent) {
|
|
|
|
// As above, we can not cache the data to this.signedInUser as we
|
|
|
|
// may only have partial data due to a locked MP, so the next
|
|
|
|
// request must re-read incase it is now unlocked.
|
|
|
|
// But we do save the tokens and the uid
|
|
|
|
this.oauthTokens = oauthTokens;
|
|
|
|
this.uid = accountData ? accountData.uid : null;
|
|
|
|
}
|
|
|
|
return accountData;
|
|
|
|
});
|
|
|
|
// phew!
|
2014-03-03 03:20:56 +04:00
|
|
|
},
|
|
|
|
|
2015-04-03 04:47:00 +03:00
|
|
|
// XXX - this should really be called "updateCurrentUserData" or similar as
|
|
|
|
// it is only ever used to add new fields to the *current* user, not to
|
|
|
|
// set a new user as current.
|
2014-03-03 03:20:56 +04:00
|
|
|
setUserAccountData: function(accountData) {
|
2015-04-03 04:47:00 +03:00
|
|
|
if (!this.isCurrent) {
|
|
|
|
return this.reject(new Error("Another user has signed in"));
|
|
|
|
}
|
|
|
|
if (this.promiseInitialAccountData) {
|
|
|
|
throw new Error("Can't set account data before it's been read.");
|
|
|
|
}
|
2015-04-03 04:47:00 +03:00
|
|
|
if (!accountData) {
|
|
|
|
// see above - this should really be called "updateCurrentUserData" or similar.
|
|
|
|
throw new Error("Attempt to use setUserAccountData with null user data.");
|
|
|
|
}
|
|
|
|
if (accountData.uid != this.uid) {
|
|
|
|
// see above - this should really be called "updateCurrentUserData" or similar.
|
|
|
|
throw new Error("Attempt to use setUserAccountData with a different user.");
|
|
|
|
}
|
2015-04-03 04:47:00 +03:00
|
|
|
// Set our signedInUser before we start the write, so any updates to the
|
|
|
|
// data while the write completes are still captured.
|
|
|
|
this.signedInUser = {version: DATA_FORMAT_VERSION, accountData: accountData};
|
2015-04-03 04:47:00 +03:00
|
|
|
return this.signedInUserStorage.set(this.signedInUser)
|
2014-03-03 03:20:56 +04:00
|
|
|
.then(() => this.resolve(accountData));
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
getCertificate: function(data, keyPair, mustBeValidUntil) {
|
2014-05-15 06:00:59 +04:00
|
|
|
if (logPII) {
|
|
|
|
// don't stringify unless it will be written. We should replace this
|
|
|
|
// check with param substitutions added in bug 966674
|
|
|
|
log.debug("getCertificate" + JSON.stringify(this.signedInUser));
|
|
|
|
}
|
2014-03-03 03:20:56 +04:00
|
|
|
// TODO: get the lifetime from the cert's .exp field
|
|
|
|
if (this.cert && this.cert.validUntil > mustBeValidUntil) {
|
|
|
|
log.debug(" getCertificate already had one");
|
|
|
|
return this.resolve(this.cert.cert);
|
|
|
|
}
|
2014-10-08 03:09:54 +04:00
|
|
|
|
|
|
|
if (Services.io.offline) {
|
|
|
|
return this.reject(new Error(ERROR_OFFLINE));
|
|
|
|
}
|
|
|
|
|
2014-03-03 03:20:56 +04:00
|
|
|
let willBeValidUntil = this.fxaInternal.now() + CERT_LIFETIME;
|
|
|
|
return this.fxaInternal.getCertificateSigned(data.sessionToken,
|
|
|
|
keyPair.serializedPublicKey,
|
|
|
|
CERT_LIFETIME).then(
|
|
|
|
cert => {
|
2014-05-15 06:00:59 +04:00
|
|
|
log.debug("getCertificate got a new one: " + !!cert);
|
2014-03-03 03:20:56 +04:00
|
|
|
this.cert = {
|
|
|
|
cert: cert,
|
|
|
|
validUntil: willBeValidUntil
|
|
|
|
};
|
|
|
|
return cert;
|
|
|
|
}
|
|
|
|
).then(result => this.resolve(result));
|
|
|
|
},
|
|
|
|
|
|
|
|
getKeyPair: function(mustBeValidUntil) {
|
2014-08-29 04:21:03 +04:00
|
|
|
// If the debugging pref to ignore cached authentication credentials is set for Sync,
|
|
|
|
// then don't use any cached key pair, i.e., generate a new one and get it signed.
|
|
|
|
// The purpose of this pref is to expedite any auth errors as the result of a
|
|
|
|
// expired or revoked FxA session token, e.g., from resetting or changing the FxA
|
|
|
|
// password.
|
|
|
|
let ignoreCachedAuthCredentials = false;
|
|
|
|
try {
|
|
|
|
ignoreCachedAuthCredentials = Services.prefs.getBoolPref("services.sync.debug.ignoreCachedAuthCredentials");
|
|
|
|
} catch(e) {
|
|
|
|
// Pref doesn't exist
|
|
|
|
}
|
|
|
|
if (!ignoreCachedAuthCredentials && this.keyPair && (this.keyPair.validUntil > mustBeValidUntil)) {
|
2014-03-03 03:20:56 +04:00
|
|
|
log.debug("getKeyPair: already have a keyPair");
|
|
|
|
return this.resolve(this.keyPair.keyPair);
|
|
|
|
}
|
|
|
|
// Otherwse, create a keypair and set validity limit.
|
|
|
|
let willBeValidUntil = this.fxaInternal.now() + KEY_LIFETIME;
|
|
|
|
let d = Promise.defer();
|
|
|
|
jwcrypto.generateKeyPair("DS160", (err, kp) => {
|
|
|
|
if (err) {
|
|
|
|
return this.reject(err);
|
|
|
|
}
|
|
|
|
this.keyPair = {
|
|
|
|
keyPair: kp,
|
|
|
|
validUntil: willBeValidUntil
|
|
|
|
};
|
|
|
|
log.debug("got keyPair");
|
|
|
|
delete this.cert;
|
|
|
|
d.resolve(this.keyPair.keyPair);
|
|
|
|
});
|
|
|
|
return d.promise.then(result => this.resolve(result));
|
|
|
|
},
|
|
|
|
|
2015-03-26 11:33:38 +03:00
|
|
|
// Get the account's profile image URL from the profile server
|
|
|
|
getProfile: function () {
|
|
|
|
return this.initProfile()
|
|
|
|
.then(() => this.profile.getProfile());
|
|
|
|
},
|
|
|
|
|
|
|
|
// Instantiate a FxAccountsProfile with a fresh OAuth token if needed
|
|
|
|
initProfile: function () {
|
|
|
|
|
|
|
|
let profileServerUrl = Services.urlFormatter.formatURLPref("identity.fxaccounts.remote.profile.uri");
|
|
|
|
|
|
|
|
let oAuthOptions = {
|
|
|
|
scope: "profile"
|
|
|
|
};
|
|
|
|
|
|
|
|
if (this.initProfilePromise) {
|
|
|
|
return this.initProfilePromise;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.initProfilePromise = this.fxaInternal.getOAuthToken(oAuthOptions)
|
|
|
|
.then(token => {
|
|
|
|
this.profile = new FxAccountsProfile(this, {
|
|
|
|
profileServerUrl: profileServerUrl,
|
|
|
|
token: token
|
|
|
|
});
|
|
|
|
this.initProfilePromise = null;
|
|
|
|
})
|
|
|
|
.then(null, err => {
|
|
|
|
this.initProfilePromise = null;
|
|
|
|
throw err;
|
|
|
|
});
|
|
|
|
|
|
|
|
return this.initProfilePromise;
|
|
|
|
},
|
|
|
|
|
2014-03-03 03:20:56 +04:00
|
|
|
resolve: function(result) {
|
|
|
|
if (!this.isCurrent) {
|
|
|
|
log.info("An accountState promise was resolved, but was actually rejected" +
|
|
|
|
" due to a different user being signed in. Originally resolved" +
|
|
|
|
" with: " + result);
|
|
|
|
return Promise.reject(new Error("A different user signed in"));
|
|
|
|
}
|
|
|
|
return Promise.resolve(result);
|
|
|
|
},
|
|
|
|
|
|
|
|
reject: function(error) {
|
|
|
|
// It could be argued that we should just let it reject with the original
|
|
|
|
// error - but this runs the risk of the error being (eg) a 401, which
|
|
|
|
// might cause the consumer to attempt some remediation and cause other
|
|
|
|
// problems.
|
|
|
|
if (!this.isCurrent) {
|
|
|
|
log.info("An accountState promise was rejected, but we are ignoring that" +
|
|
|
|
"reason and rejecting it due to a different user being signed in." +
|
2014-06-18 03:26:06 +04:00
|
|
|
"Originally rejected with: " + error);
|
2014-03-03 03:20:56 +04:00
|
|
|
return Promise.reject(new Error("A different user signed in"));
|
|
|
|
}
|
|
|
|
return Promise.reject(error);
|
|
|
|
},
|
|
|
|
|
2015-04-03 04:47:00 +03:00
|
|
|
// Abstractions for storage of cached tokens - these are all sync, and don't
|
|
|
|
// handle revocation etc - it's just storage.
|
|
|
|
|
|
|
|
// A preamble for the cache helpers...
|
|
|
|
_cachePreamble() {
|
|
|
|
if (!this.isCurrent) {
|
|
|
|
throw new Error("Another user has signed in");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// Set a cached token. |tokenData| must have a 'token' element, but may also
|
|
|
|
// have additional fields (eg, it probably specifies the server to revoke
|
|
|
|
// from). The 'get' functions below return the entire |tokenData| value.
|
|
|
|
setCachedToken(scopeArray, tokenData) {
|
|
|
|
this._cachePreamble();
|
|
|
|
if (!tokenData.token) {
|
|
|
|
throw new Error("No token");
|
|
|
|
}
|
|
|
|
let key = getScopeKey(scopeArray);
|
|
|
|
this.oauthTokens[key] = tokenData;
|
|
|
|
// And a background save...
|
|
|
|
this._persistCachedTokens();
|
|
|
|
},
|
|
|
|
|
|
|
|
// Return data for a cached token or null (or throws on bad state etc)
|
|
|
|
getCachedToken(scopeArray) {
|
|
|
|
this._cachePreamble();
|
|
|
|
let key = getScopeKey(scopeArray);
|
|
|
|
if (this.oauthTokens[key]) {
|
|
|
|
// later we might want to check an expiry date - but we currently
|
|
|
|
// have no such concept, so just return it.
|
|
|
|
log.trace("getCachedToken returning cached token");
|
|
|
|
return this.oauthTokens[key];
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
|
|
|
// Get an array of tokenData for all cached tokens.
|
|
|
|
getAllCachedTokens() {
|
|
|
|
this._cachePreamble();
|
|
|
|
let result = [];
|
|
|
|
for (let [key, tokenValue] in Iterator(this.oauthTokens)) {
|
|
|
|
result.push(tokenValue);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
|
|
|
|
// Remove a cached token from the cache. Does *not* revoke it from anywhere.
|
|
|
|
// Returns the entire token entry if found, null otherwise.
|
|
|
|
removeCachedToken(token) {
|
|
|
|
this._cachePreamble();
|
|
|
|
let data = this.oauthTokens;
|
|
|
|
for (let [key, tokenValue] in Iterator(data)) {
|
|
|
|
if (tokenValue.token == token) {
|
|
|
|
delete data[key];
|
|
|
|
// And a background save...
|
|
|
|
this._persistCachedTokens();
|
|
|
|
return tokenValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
|
|
|
// A hook-point for tests. Returns a promise that's ignored in most cases
|
|
|
|
// (notable exceptions are tests and when we explicitly are saving the entire
|
|
|
|
// set of user data.)
|
|
|
|
_persistCachedTokens() {
|
|
|
|
this._cachePreamble();
|
|
|
|
let record;
|
|
|
|
if (this.uid) {
|
|
|
|
record = {
|
|
|
|
version: DATA_FORMAT_VERSION,
|
|
|
|
uid: this.uid,
|
|
|
|
tokens: this.oauthTokens,
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
record = null;
|
|
|
|
}
|
|
|
|
return this.signedInUserStorage.setOAuthTokens(record).catch(
|
|
|
|
err => {
|
|
|
|
log.error("Failed to save account data for token cache", err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
persistUserData() {
|
|
|
|
return this._persistCachedTokens().catch(err => {
|
|
|
|
log.error("Failed to persist cached tokens", err);
|
|
|
|
}).then(() => {
|
|
|
|
return this.signedInUserStorage.set(this.signedInUser);
|
|
|
|
}).catch(err => {
|
|
|
|
log.error("Failed to persist account data", err);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Given an array of scopes, make a string key by normalizing. */
|
|
|
|
function getScopeKey(scopeArray) {
|
|
|
|
let normalizedScopes = scopeArray.map(item => item.toLowerCase());
|
|
|
|
return normalizedScopes.sort().join("|");
|
|
|
|
}
|
2014-04-17 07:48:00 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Copies properties from a given object to another object.
|
|
|
|
*
|
|
|
|
* @param from (object)
|
|
|
|
* The object we read property descriptors from.
|
|
|
|
* @param to (object)
|
|
|
|
* The object that we set property descriptors on.
|
|
|
|
* @param options (object) (optional)
|
|
|
|
* {keys: [...]}
|
|
|
|
* Lets the caller pass the names of all properties they want to be
|
|
|
|
* copied. Will copy all properties of the given source object by
|
|
|
|
* default.
|
|
|
|
* {bind: object}
|
|
|
|
* Lets the caller specify the object that will be used to .bind()
|
|
|
|
* all function properties we find to. Will bind to the given target
|
|
|
|
* object by default.
|
|
|
|
*/
|
|
|
|
function copyObjectProperties(from, to, opts = {}) {
|
|
|
|
let keys = (opts && opts.keys) || Object.keys(from);
|
|
|
|
let thisArg = (opts && opts.bind) || to;
|
|
|
|
|
|
|
|
for (let prop of keys) {
|
|
|
|
let desc = Object.getOwnPropertyDescriptor(from, prop);
|
|
|
|
|
|
|
|
if (typeof(desc.value) == "function") {
|
|
|
|
desc.value = desc.value.bind(thisArg);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (desc.get) {
|
|
|
|
desc.get = desc.get.bind(thisArg);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (desc.set) {
|
|
|
|
desc.set = desc.set.bind(thisArg);
|
|
|
|
}
|
|
|
|
|
|
|
|
Object.defineProperty(to, prop, desc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-04 14:12:37 +04:00
|
|
|
/**
|
|
|
|
* The public API's constructor.
|
|
|
|
*/
|
|
|
|
this.FxAccounts = function (mockInternal) {
|
|
|
|
let internal = new FxAccountsInternal();
|
|
|
|
let external = {};
|
|
|
|
|
|
|
|
// Copy all public properties to the 'external' object.
|
|
|
|
let prototype = FxAccountsInternal.prototype;
|
|
|
|
let options = {keys: publicProperties, bind: internal};
|
2014-04-17 07:48:00 +04:00
|
|
|
copyObjectProperties(prototype, external, options);
|
2014-02-04 14:12:37 +04:00
|
|
|
|
|
|
|
// Copy all of the mock's properties to the internal object.
|
|
|
|
if (mockInternal && !mockInternal.onlySetInternal) {
|
2014-04-17 07:48:00 +04:00
|
|
|
copyObjectProperties(mockInternal, internal);
|
2014-02-04 14:12:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mockInternal) {
|
2015-04-03 04:47:00 +03:00
|
|
|
// A little work-around to ensure the initial currentAccountState has
|
|
|
|
// the same mock storage the test passed in.
|
|
|
|
if (mockInternal.signedInUserStorage) {
|
|
|
|
internal.currentAccountState.signedInUserStorage = mockInternal.signedInUserStorage;
|
|
|
|
}
|
2014-02-04 14:12:37 +04:00
|
|
|
// Exposes the internal object for testing only.
|
|
|
|
external.internal = internal;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Object.freeze(external);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The internal API's constructor.
|
|
|
|
*/
|
|
|
|
function FxAccountsInternal() {
|
2013-12-06 10:46:12 +04:00
|
|
|
this.version = DATA_FORMAT_VERSION;
|
|
|
|
|
2015-01-23 03:22:47 +03:00
|
|
|
// Make a local copy of this constant so we can mock it in testing
|
2013-12-06 10:46:12 +04:00
|
|
|
this.POLL_SESSION = POLL_SESSION;
|
|
|
|
|
2015-04-03 04:47:00 +03:00
|
|
|
// The one and only "storage" object. While this is created here, the
|
|
|
|
// FxAccountsInternal object does *not* use it directly, but instead passes
|
|
|
|
// it to AccountState objects which has sole responsibility for storage.
|
|
|
|
// Ideally we would create it in the AccountState objects, but that makes
|
|
|
|
// testing hard as AccountState objects are regularly created and thrown
|
|
|
|
// away. Doing it this way means tests can mock/replace this storage object
|
|
|
|
// and have it used by all AccountState objects, even those created before
|
|
|
|
// and after the mock has been setup.
|
|
|
|
|
|
|
|
// We only want the fancy LoginManagerStorage on desktop.
|
|
|
|
#if defined(MOZ_B2G)
|
|
|
|
this.signedInUserStorage = new JSONStorage({
|
|
|
|
#else
|
|
|
|
this.signedInUserStorage = new LoginManagerStorage({
|
|
|
|
#endif
|
|
|
|
// We don't reference |profileDir| in the top-level module scope
|
|
|
|
// as we may be imported before we know where it is.
|
|
|
|
filename: DEFAULT_STORAGE_FILENAME,
|
|
|
|
oauthTokensFilename: DEFAULT_OAUTH_TOKENS_FILENAME,
|
|
|
|
baseDir: OS.Constants.Path.profileDir,
|
|
|
|
});
|
|
|
|
|
2013-12-06 10:46:12 +04:00
|
|
|
// We interact with the Firefox Accounts auth server in order to confirm that
|
|
|
|
// a user's email has been verified and also to fetch the user's keys from
|
|
|
|
// the server. We manage these processes in possibly long-lived promises
|
|
|
|
// that are internal to this object (never exposed to callers). Because
|
|
|
|
// Firefox Accounts allows for only one logged-in user, and because it's
|
|
|
|
// conceivable that while we are waiting to verify one identity, a caller
|
|
|
|
// could start verification on a second, different identity, we need to be
|
|
|
|
// able to abort all work on the first sign-in process. The currentTimer and
|
2014-03-03 03:20:56 +04:00
|
|
|
// currentAccountState are used for this purpose.
|
|
|
|
// (XXX - should the timer be directly on the currentAccountState?)
|
2013-12-06 10:46:12 +04:00
|
|
|
this.currentTimer = null;
|
2015-04-03 04:47:00 +03:00
|
|
|
this.currentAccountState = new AccountState(this, this.signedInUserStorage);
|
2013-12-06 10:46:12 +04:00
|
|
|
}
|
2014-02-04 14:12:37 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The internal API's prototype.
|
|
|
|
*/
|
|
|
|
FxAccountsInternal.prototype = {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The current data format's version number.
|
|
|
|
*/
|
|
|
|
version: DATA_FORMAT_VERSION,
|
2013-12-06 10:46:12 +04:00
|
|
|
|
2015-04-14 03:35:48 +03:00
|
|
|
// The timeout (in ms) we use to poll for a verified mail for the first 2 mins.
|
|
|
|
VERIFICATION_POLL_TIMEOUT_INITIAL: 5000, // 5 seconds
|
|
|
|
// And how often we poll after the first 2 mins.
|
|
|
|
VERIFICATION_POLL_TIMEOUT_SUBSEQUENT: 15000, // 15 seconds.
|
|
|
|
|
2014-03-26 05:13:46 +04:00
|
|
|
_fxAccountsClient: null,
|
|
|
|
|
|
|
|
get fxAccountsClient() {
|
|
|
|
if (!this._fxAccountsClient) {
|
|
|
|
this._fxAccountsClient = new FxAccountsClient();
|
|
|
|
}
|
|
|
|
return this._fxAccountsClient;
|
|
|
|
},
|
|
|
|
|
2014-01-24 06:04:38 +04:00
|
|
|
/**
|
|
|
|
* Return the current time in milliseconds as an integer. Allows tests to
|
|
|
|
* manipulate the date to simulate certificate expiration.
|
|
|
|
*/
|
|
|
|
now: function() {
|
|
|
|
return this.fxAccountsClient.now();
|
|
|
|
},
|
|
|
|
|
2014-02-19 20:34:42 +04:00
|
|
|
getAccountsClient: function() {
|
|
|
|
return this.fxAccountsClient;
|
|
|
|
},
|
|
|
|
|
2014-01-24 06:04:38 +04:00
|
|
|
/**
|
|
|
|
* Return clock offset in milliseconds, as reported by the fxAccountsClient.
|
|
|
|
* This can be overridden for testing.
|
|
|
|
*
|
|
|
|
* The offset is the number of milliseconds that must be added to the client
|
|
|
|
* clock to make it equal to the server clock. For example, if the client is
|
|
|
|
* five minutes ahead of the server, the localtimeOffsetMsec will be -300000.
|
|
|
|
*/
|
|
|
|
get localtimeOffsetMsec() {
|
|
|
|
return this.fxAccountsClient.localtimeOffsetMsec;
|
|
|
|
},
|
|
|
|
|
2013-12-06 10:46:12 +04:00
|
|
|
/**
|
|
|
|
* Ask the server whether the user's email has been verified
|
|
|
|
*/
|
|
|
|
checkEmailStatus: function checkEmailStatus(sessionToken) {
|
|
|
|
return this.fxAccountsClient.recoveryEmailStatus(sessionToken);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Once the user's email is verified, we can request the keys
|
|
|
|
*/
|
|
|
|
fetchKeys: function fetchKeys(keyFetchToken) {
|
2014-05-15 06:00:59 +04:00
|
|
|
log.debug("fetchKeys: " + !!keyFetchToken);
|
|
|
|
if (logPII) {
|
|
|
|
log.debug("fetchKeys - the token is " + keyFetchToken);
|
|
|
|
}
|
2013-12-06 10:46:12 +04:00
|
|
|
return this.fxAccountsClient.accountKeys(keyFetchToken);
|
|
|
|
},
|
|
|
|
|
2014-02-04 14:12:37 +04:00
|
|
|
// set() makes sure that polling is happening, if necessary.
|
|
|
|
// get() does not wait for verification, and returns an object even if
|
|
|
|
// unverified. The caller of get() must check .verified .
|
|
|
|
// The "fxaccounts:onverified" event will fire only when the verified
|
|
|
|
// state goes from false to true, so callers must register their observer
|
|
|
|
// and then call get(). In particular, it will not fire when the account
|
|
|
|
// was found to be verified in a previous boot: if our stored state says
|
|
|
|
// the account is verified, the event will never fire. So callers must do:
|
|
|
|
// register notification observer (go)
|
|
|
|
// userdata = get()
|
|
|
|
// if (userdata.verified()) {go()}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the user currently signed in to Firefox Accounts.
|
|
|
|
*
|
|
|
|
* @return Promise
|
|
|
|
* The promise resolves to the credentials object of the signed-in user:
|
|
|
|
* {
|
|
|
|
* email: The user's email address
|
|
|
|
* uid: The user's unique id
|
|
|
|
* sessionToken: Session for the FxA server
|
|
|
|
* kA: An encryption key from the FxA server
|
|
|
|
* kB: An encryption key derived from the user's FxA password
|
|
|
|
* verified: email verification status
|
2014-02-19 14:47:11 +04:00
|
|
|
* authAt: The time (seconds since epoch) that this record was
|
|
|
|
* authenticated
|
2014-02-04 14:12:37 +04:00
|
|
|
* }
|
|
|
|
* or null if no user is signed in.
|
|
|
|
*/
|
|
|
|
getSignedInUser: function getSignedInUser() {
|
2014-03-03 03:20:56 +04:00
|
|
|
let currentState = this.currentAccountState;
|
|
|
|
return currentState.getUserAccountData().then(data => {
|
2014-02-04 14:12:37 +04:00
|
|
|
if (!data) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (!this.isUserEmailVerified(data)) {
|
|
|
|
// If the email is not verified, start polling for verification,
|
|
|
|
// but return null right away. We don't want to return a promise
|
|
|
|
// that might not be fulfilled for a long time.
|
|
|
|
this.startVerifiedCheck(data);
|
|
|
|
}
|
|
|
|
return data;
|
2014-03-03 03:20:56 +04:00
|
|
|
}).then(result => currentState.resolve(result));
|
2014-02-04 14:12:37 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the current user signed in to Firefox Accounts.
|
|
|
|
*
|
|
|
|
* @param credentials
|
|
|
|
* The credentials object obtained by logging in or creating
|
|
|
|
* an account on the FxA server:
|
|
|
|
* {
|
2014-03-21 13:23:15 +04:00
|
|
|
* authAt: The time (seconds since epoch) that this record was
|
|
|
|
* authenticated
|
2014-02-04 14:12:37 +04:00
|
|
|
* email: The users email address
|
2014-03-21 13:23:15 +04:00
|
|
|
* keyFetchToken: a keyFetchToken which has not yet been used
|
2014-02-04 14:12:37 +04:00
|
|
|
* sessionToken: Session for the FxA server
|
2014-03-21 13:23:15 +04:00
|
|
|
* uid: The user's unique id
|
|
|
|
* unwrapBKey: used to unwrap kB, derived locally from the
|
|
|
|
* password (not revealed to the FxA server)
|
2014-02-04 14:12:37 +04:00
|
|
|
* verified: true/false
|
|
|
|
* }
|
|
|
|
* @return Promise
|
|
|
|
* The promise resolves to null when the data is saved
|
|
|
|
* successfully and is rejected on error.
|
|
|
|
*/
|
|
|
|
setSignedInUser: function setSignedInUser(credentials) {
|
|
|
|
log.debug("setSignedInUser - aborting any existing flows");
|
|
|
|
this.abortExistingFlow();
|
|
|
|
|
2015-04-03 04:47:00 +03:00
|
|
|
let currentAccountState = this.currentAccountState = new AccountState(
|
|
|
|
this,
|
|
|
|
this.signedInUserStorage,
|
|
|
|
JSON.parse(JSON.stringify(credentials)) // Pass a clone of the credentials object.
|
|
|
|
);
|
2014-02-04 14:12:37 +04:00
|
|
|
|
|
|
|
// This promise waits for storage, but not for verification.
|
|
|
|
// We're telling the caller that this is durable now.
|
2015-04-03 04:47:00 +03:00
|
|
|
return currentAccountState.persistUserData().then(() => {
|
2014-02-04 14:12:37 +04:00
|
|
|
this.notifyObservers(ONLOGIN_NOTIFICATION);
|
|
|
|
if (!this.isUserEmailVerified(credentials)) {
|
|
|
|
this.startVerifiedCheck(credentials);
|
|
|
|
}
|
2015-04-03 04:47:00 +03:00
|
|
|
}).then(() => {
|
|
|
|
return currentAccountState.resolve();
|
|
|
|
});
|
2014-02-04 14:12:37 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* returns a promise that fires with the assertion. If there is no verified
|
|
|
|
* signed-in user, fires with null.
|
|
|
|
*/
|
|
|
|
getAssertion: function getAssertion(audience) {
|
|
|
|
log.debug("enter getAssertion()");
|
2014-03-03 03:20:56 +04:00
|
|
|
let currentState = this.currentAccountState;
|
2014-03-15 01:50:19 +04:00
|
|
|
let mustBeValidUntil = this.now() + ASSERTION_USE_PERIOD;
|
2014-03-03 03:20:56 +04:00
|
|
|
return currentState.getUserAccountData().then(data => {
|
2014-02-04 14:12:37 +04:00
|
|
|
if (!data) {
|
|
|
|
// No signed-in user
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (!this.isUserEmailVerified(data)) {
|
|
|
|
// Signed-in user has not verified email
|
|
|
|
return null;
|
|
|
|
}
|
2014-03-03 03:20:56 +04:00
|
|
|
return currentState.getKeyPair(mustBeValidUntil).then(keyPair => {
|
|
|
|
return currentState.getCertificate(data, keyPair, mustBeValidUntil)
|
2014-02-04 14:12:37 +04:00
|
|
|
.then(cert => {
|
|
|
|
return this.getAssertionFromCert(data, keyPair, cert, audience);
|
|
|
|
});
|
|
|
|
});
|
2014-03-03 03:20:56 +04:00
|
|
|
}).then(result => currentState.resolve(result));
|
2014-02-04 14:12:37 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resend the verification email fot the currently signed-in user.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
resendVerificationEmail: function resendVerificationEmail() {
|
2014-03-03 03:20:56 +04:00
|
|
|
let currentState = this.currentAccountState;
|
2014-02-04 14:12:37 +04:00
|
|
|
return this.getSignedInUser().then(data => {
|
|
|
|
// If the caller is asking for verification to be re-sent, and there is
|
|
|
|
// no signed-in user to begin with, this is probably best regarded as an
|
|
|
|
// error.
|
|
|
|
if (data) {
|
2014-03-03 03:20:56 +04:00
|
|
|
this.pollEmailStatus(currentState, data.sessionToken, "start");
|
2014-02-04 14:12:37 +04:00
|
|
|
return this.fxAccountsClient.resendVerificationEmail(data.sessionToken);
|
|
|
|
}
|
|
|
|
throw new Error("Cannot resend verification email; no signed-in user");
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-12-06 10:46:12 +04:00
|
|
|
/*
|
|
|
|
* Reset state such that any previous flow is canceled.
|
|
|
|
*/
|
|
|
|
abortExistingFlow: function abortExistingFlow() {
|
|
|
|
if (this.currentTimer) {
|
|
|
|
log.debug("Polling aborted; Another user signing in");
|
|
|
|
clearTimeout(this.currentTimer);
|
|
|
|
this.currentTimer = 0;
|
|
|
|
}
|
2014-03-03 03:20:56 +04:00
|
|
|
this.currentAccountState.abort();
|
2015-04-03 04:47:00 +03:00
|
|
|
this.currentAccountState = new AccountState(this, this.signedInUserStorage);
|
2013-12-06 10:46:12 +04:00
|
|
|
},
|
|
|
|
|
2014-05-16 03:52:52 +04:00
|
|
|
accountStatus: function accountStatus() {
|
|
|
|
return this.currentAccountState.getUserAccountData().then(data => {
|
|
|
|
if (!data) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return this.fxAccountsClient.accountStatus(data.uid);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2015-04-03 04:47:00 +03:00
|
|
|
_destroyOAuthToken: function(tokenData) {
|
|
|
|
let client = new FxAccountsOAuthGrantClient({
|
|
|
|
serverURL: tokenData.server,
|
|
|
|
client_id: FX_OAUTH_CLIENT_ID
|
|
|
|
});
|
|
|
|
return client.destroyToken(tokenData.token)
|
|
|
|
},
|
|
|
|
|
|
|
|
_destroyAllOAuthTokens: function(tokenInfos) {
|
|
|
|
// let's just destroy them all in parallel...
|
|
|
|
let promises = [];
|
|
|
|
for (let tokenInfo of tokenInfos) {
|
|
|
|
promises.push(this._destroyOAuthToken(tokenInfo));
|
|
|
|
}
|
|
|
|
return Promise.all(promises);
|
|
|
|
},
|
|
|
|
|
2014-04-23 02:28:18 +04:00
|
|
|
signOut: function signOut(localOnly) {
|
2014-04-10 03:14:19 +04:00
|
|
|
let currentState = this.currentAccountState;
|
|
|
|
let sessionToken;
|
2015-04-03 04:47:00 +03:00
|
|
|
let tokensToRevoke;
|
2014-04-10 03:14:19 +04:00
|
|
|
return currentState.getUserAccountData().then(data => {
|
|
|
|
// Save the session token for use in the call to signOut below.
|
|
|
|
sessionToken = data && data.sessionToken;
|
2015-04-03 04:47:00 +03:00
|
|
|
tokensToRevoke = currentState.getAllCachedTokens();
|
2014-04-23 02:28:18 +04:00
|
|
|
return this._signOutLocal();
|
|
|
|
}).then(() => {
|
|
|
|
// FxAccountsManager calls here, then does its own call
|
|
|
|
// to FxAccountsClient.signOut().
|
|
|
|
if (!localOnly) {
|
|
|
|
// Wrap this in a promise so *any* errors in signOut won't
|
|
|
|
// block the local sign out. This is *not* returned.
|
|
|
|
Promise.resolve().then(() => {
|
|
|
|
// This can happen in the background and shouldn't block
|
|
|
|
// the user from signing out. The server must tolerate
|
|
|
|
// clients just disappearing, so this call should be best effort.
|
|
|
|
return this._signOutServer(sessionToken);
|
2015-04-03 04:47:00 +03:00
|
|
|
}).catch(err => {
|
|
|
|
log.error("Error during remote sign out of Firefox Accounts", err);
|
|
|
|
}).then(() => {
|
|
|
|
return this._destroyAllOAuthTokens(tokensToRevoke);
|
|
|
|
}).catch(err => {
|
|
|
|
log.error("Error during destruction of oauth tokens during signout", err);
|
|
|
|
}).then(() => {
|
|
|
|
// just for testing - notifications are cheap when no observers.
|
|
|
|
this.notifyObservers("testhelper-fxa-signout-complete");
|
2014-04-23 02:28:18 +04:00
|
|
|
});
|
|
|
|
}
|
2014-04-10 03:14:19 +04:00
|
|
|
}).then(() => {
|
2014-01-15 16:07:20 +04:00
|
|
|
this.notifyObservers(ONLOGOUT_NOTIFICATION);
|
2013-12-10 05:15:30 +04:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2014-04-23 02:28:18 +04:00
|
|
|
/**
|
|
|
|
* This function should be called in conjunction with a server-side
|
|
|
|
* signOut via FxAccountsClient.
|
|
|
|
*/
|
|
|
|
_signOutLocal: function signOutLocal() {
|
2015-04-03 04:47:00 +03:00
|
|
|
let currentAccountState = this.currentAccountState;
|
|
|
|
return currentAccountState.signOut().then(() => {
|
|
|
|
this.abortExistingFlow(); // this resets this.currentAccountState.
|
|
|
|
});
|
2014-04-23 02:28:18 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
_signOutServer: function signOutServer(sessionToken) {
|
|
|
|
return this.fxAccountsClient.signOut(sessionToken);
|
|
|
|
},
|
|
|
|
|
2013-12-06 10:46:12 +04:00
|
|
|
/**
|
|
|
|
* Fetch encryption keys for the signed-in-user from the FxA API server.
|
|
|
|
*
|
|
|
|
* Not for user consumption. Exists to cause the keys to be fetch.
|
|
|
|
*
|
|
|
|
* Returns user data so that it can be chained with other methods.
|
|
|
|
*
|
|
|
|
* @return Promise
|
|
|
|
* The promise resolves to the credentials object of the signed-in user:
|
|
|
|
* {
|
|
|
|
* email: The user's email address
|
|
|
|
* uid: The user's unique id
|
|
|
|
* sessionToken: Session for the FxA server
|
|
|
|
* kA: An encryption key from the FxA server
|
|
|
|
* kB: An encryption key derived from the user's FxA password
|
2014-01-14 20:00:36 +04:00
|
|
|
* verified: email verification status
|
2013-12-06 10:46:12 +04:00
|
|
|
* }
|
|
|
|
* or null if no user is signed in
|
|
|
|
*/
|
|
|
|
getKeys: function() {
|
2014-03-03 03:20:56 +04:00
|
|
|
let currentState = this.currentAccountState;
|
2014-04-22 03:45:58 +04:00
|
|
|
return currentState.getUserAccountData().then((userData) => {
|
|
|
|
if (!userData) {
|
2013-12-06 10:46:12 +04:00
|
|
|
throw new Error("Can't get keys; User is not signed in");
|
|
|
|
}
|
2014-04-22 03:45:58 +04:00
|
|
|
if (userData.kA && userData.kB) {
|
|
|
|
return userData;
|
2013-12-06 10:46:12 +04:00
|
|
|
}
|
2014-02-18 21:47:52 +04:00
|
|
|
if (!currentState.whenKeysReadyDeferred) {
|
|
|
|
currentState.whenKeysReadyDeferred = Promise.defer();
|
2014-04-22 03:45:58 +04:00
|
|
|
if (userData.keyFetchToken) {
|
|
|
|
this.fetchAndUnwrapKeys(userData.keyFetchToken).then(
|
|
|
|
(dataWithKeys) => {
|
|
|
|
if (!dataWithKeys.kA || !dataWithKeys.kB) {
|
|
|
|
currentState.whenKeysReadyDeferred.reject(
|
|
|
|
new Error("user data missing kA or kB")
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
currentState.whenKeysReadyDeferred.resolve(dataWithKeys);
|
|
|
|
},
|
|
|
|
(err) => {
|
|
|
|
currentState.whenKeysReadyDeferred.reject(err);
|
2014-04-10 06:08:19 +04:00
|
|
|
}
|
2014-04-22 03:45:58 +04:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
currentState.whenKeysReadyDeferred.reject('No keyFetchToken');
|
|
|
|
}
|
2013-12-06 10:46:12 +04:00
|
|
|
}
|
2014-02-18 21:47:52 +04:00
|
|
|
return currentState.whenKeysReadyDeferred.promise;
|
2014-03-03 03:20:56 +04:00
|
|
|
}).then(result => currentState.resolve(result));
|
2013-12-06 10:46:12 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
fetchAndUnwrapKeys: function(keyFetchToken) {
|
2014-05-15 06:00:59 +04:00
|
|
|
if (logPII) {
|
|
|
|
log.debug("fetchAndUnwrapKeys: token: " + keyFetchToken);
|
|
|
|
}
|
2014-03-03 03:20:56 +04:00
|
|
|
let currentState = this.currentAccountState;
|
2013-12-06 10:46:12 +04:00
|
|
|
return Task.spawn(function* task() {
|
|
|
|
// Sign out if we don't have a key fetch token.
|
|
|
|
if (!keyFetchToken) {
|
2014-04-22 03:45:58 +04:00
|
|
|
log.warn("improper fetchAndUnwrapKeys() call: token missing");
|
2014-02-04 14:12:37 +04:00
|
|
|
yield this.signOut();
|
2013-12-06 10:46:12 +04:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2014-02-04 14:12:37 +04:00
|
|
|
let {kA, wrapKB} = yield this.fetchKeys(keyFetchToken);
|
2013-12-06 10:46:12 +04:00
|
|
|
|
2014-03-03 03:20:56 +04:00
|
|
|
let data = yield currentState.getUserAccountData();
|
2013-12-06 10:46:12 +04:00
|
|
|
|
|
|
|
// Sanity check that the user hasn't changed out from under us
|
|
|
|
if (data.keyFetchToken !== keyFetchToken) {
|
|
|
|
throw new Error("Signed in user changed while fetching keys!");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next statements must be synchronous until we setUserAccountData
|
|
|
|
// so that we don't risk getting into a weird state.
|
|
|
|
let kB_hex = CryptoUtils.xor(CommonUtils.hexToBytes(data.unwrapBKey),
|
|
|
|
wrapKB);
|
|
|
|
|
2014-05-15 06:00:59 +04:00
|
|
|
if (logPII) {
|
|
|
|
log.debug("kB_hex: " + kB_hex);
|
|
|
|
}
|
2013-12-06 10:46:12 +04:00
|
|
|
data.kA = CommonUtils.bytesAsHex(kA);
|
|
|
|
data.kB = CommonUtils.bytesAsHex(kB_hex);
|
|
|
|
|
|
|
|
delete data.keyFetchToken;
|
2014-06-09 19:53:00 +04:00
|
|
|
delete data.unwrapBKey;
|
2013-12-06 10:46:12 +04:00
|
|
|
|
2014-05-15 06:00:59 +04:00
|
|
|
log.debug("Keys Obtained: kA=" + !!data.kA + ", kB=" + !!data.kB);
|
|
|
|
if (logPII) {
|
|
|
|
log.debug("Keys Obtained: kA=" + data.kA + ", kB=" + data.kB);
|
|
|
|
}
|
2013-12-06 10:46:12 +04:00
|
|
|
|
2014-03-03 03:20:56 +04:00
|
|
|
yield currentState.setUserAccountData(data);
|
2013-12-06 10:46:12 +04:00
|
|
|
// We are now ready for business. This should only be invoked once
|
|
|
|
// per setSignedInUser(), regardless of whether we've rebooted since
|
|
|
|
// setSignedInUser() was called.
|
2014-02-04 14:12:37 +04:00
|
|
|
this.notifyObservers(ONVERIFIED_NOTIFICATION);
|
2013-12-06 10:46:12 +04:00
|
|
|
return data;
|
2014-03-03 03:20:56 +04:00
|
|
|
}.bind(this)).then(result => currentState.resolve(result));
|
2013-12-06 10:46:12 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
getAssertionFromCert: function(data, keyPair, cert, audience) {
|
|
|
|
log.debug("getAssertionFromCert");
|
|
|
|
let payload = {};
|
|
|
|
let d = Promise.defer();
|
2014-01-24 06:04:38 +04:00
|
|
|
let options = {
|
2014-03-15 01:50:19 +04:00
|
|
|
duration: ASSERTION_LIFETIME,
|
2014-02-04 14:12:37 +04:00
|
|
|
localtimeOffsetMsec: this.localtimeOffsetMsec,
|
|
|
|
now: this.now()
|
2014-01-24 06:04:38 +04:00
|
|
|
};
|
2014-03-03 03:20:56 +04:00
|
|
|
let currentState = this.currentAccountState;
|
2013-12-06 10:46:12 +04:00
|
|
|
// "audience" should look like "http://123done.org".
|
|
|
|
// The generated assertion will expire in two minutes.
|
2014-01-24 06:04:38 +04:00
|
|
|
jwcrypto.generateAssertion(cert, keyPair, audience, options, (err, signed) => {
|
2013-12-06 10:46:12 +04:00
|
|
|
if (err) {
|
|
|
|
log.error("getAssertionFromCert: " + err);
|
|
|
|
d.reject(err);
|
|
|
|
} else {
|
2014-05-15 06:00:59 +04:00
|
|
|
log.debug("getAssertionFromCert returning signed: " + !!signed);
|
|
|
|
if (logPII) {
|
|
|
|
log.debug("getAssertionFromCert returning signed: " + signed);
|
|
|
|
}
|
2013-12-06 10:46:12 +04:00
|
|
|
d.resolve(signed);
|
|
|
|
}
|
|
|
|
});
|
2014-03-03 03:20:56 +04:00
|
|
|
return d.promise.then(result => currentState.resolve(result));
|
2013-12-06 10:46:12 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
getCertificateSigned: function(sessionToken, serializedPublicKey, lifetime) {
|
2014-05-15 06:00:59 +04:00
|
|
|
log.debug("getCertificateSigned: " + !!sessionToken + " " + !!serializedPublicKey);
|
|
|
|
if (logPII) {
|
|
|
|
log.debug("getCertificateSigned: " + sessionToken + " " + serializedPublicKey);
|
|
|
|
}
|
2014-03-03 03:20:56 +04:00
|
|
|
return this.fxAccountsClient.signCertificate(
|
|
|
|
sessionToken,
|
|
|
|
JSON.parse(serializedPublicKey),
|
|
|
|
lifetime
|
|
|
|
);
|
2013-12-06 10:46:12 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
getUserAccountData: function() {
|
2014-03-03 03:20:56 +04:00
|
|
|
return this.currentAccountState.getUserAccountData();
|
2013-12-06 10:46:12 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
isUserEmailVerified: function isUserEmailVerified(data) {
|
2014-01-14 20:00:36 +04:00
|
|
|
return !!(data && data.verified);
|
2013-12-06 10:46:12 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup for and if necessary do email verification polling.
|
|
|
|
*/
|
|
|
|
loadAndPoll: function() {
|
2014-03-03 03:20:56 +04:00
|
|
|
let currentState = this.currentAccountState;
|
|
|
|
return currentState.getUserAccountData()
|
2013-12-06 10:46:12 +04:00
|
|
|
.then(data => {
|
|
|
|
if (data && !this.isUserEmailVerified(data)) {
|
2014-03-03 03:20:56 +04:00
|
|
|
this.pollEmailStatus(currentState, data.sessionToken, "start");
|
2013-12-06 10:46:12 +04:00
|
|
|
}
|
|
|
|
return data;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
startVerifiedCheck: function(data) {
|
2015-03-02 02:02:13 +03:00
|
|
|
log.debug("startVerifiedCheck", data && data.verified);
|
|
|
|
if (logPII) {
|
|
|
|
log.debug("startVerifiedCheck with user data", data);
|
|
|
|
}
|
|
|
|
|
2013-12-06 10:46:12 +04:00
|
|
|
// Get us to the verified state, then get the keys. This returns a promise
|
|
|
|
// that will fire when we are completely ready.
|
|
|
|
//
|
|
|
|
// Login is truly complete once keys have been fetched, so once getKeys()
|
2014-01-22 17:22:59 +04:00
|
|
|
// obtains and stores kA and kB, it will fire the onverified observer
|
2013-12-06 10:46:12 +04:00
|
|
|
// notification.
|
2014-04-29 02:46:14 +04:00
|
|
|
|
|
|
|
// The callers of startVerifiedCheck never consume a returned promise (ie,
|
|
|
|
// this is simply kicking off a background fetch) so we must add a rejection
|
|
|
|
// handler to avoid runtime warnings about the rejection not being handled.
|
|
|
|
this.whenVerified(data).then(
|
|
|
|
() => this.getKeys(),
|
|
|
|
err => log.info("startVerifiedCheck promise was rejected: " + err)
|
|
|
|
);
|
2013-12-06 10:46:12 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
whenVerified: function(data) {
|
2014-03-03 03:20:56 +04:00
|
|
|
let currentState = this.currentAccountState;
|
2014-01-14 20:00:36 +04:00
|
|
|
if (data.verified) {
|
2013-12-06 10:46:12 +04:00
|
|
|
log.debug("already verified");
|
2014-03-03 03:20:56 +04:00
|
|
|
return currentState.resolve(data);
|
2013-12-06 10:46:12 +04:00
|
|
|
}
|
2014-02-18 21:47:52 +04:00
|
|
|
if (!currentState.whenVerifiedDeferred) {
|
2013-12-06 10:46:12 +04:00
|
|
|
log.debug("whenVerified promise starts polling for verified email");
|
2014-03-03 03:20:56 +04:00
|
|
|
this.pollEmailStatus(currentState, data.sessionToken, "start");
|
2013-12-06 10:46:12 +04:00
|
|
|
}
|
2014-02-18 21:47:52 +04:00
|
|
|
return currentState.whenVerifiedDeferred.promise.then(
|
2014-03-03 03:20:56 +04:00
|
|
|
result => currentState.resolve(result)
|
|
|
|
);
|
2013-12-06 10:46:12 +04:00
|
|
|
},
|
|
|
|
|
2014-06-06 21:42:22 +04:00
|
|
|
notifyObservers: function(topic, data) {
|
2014-01-15 16:07:20 +04:00
|
|
|
log.debug("Notifying observers of " + topic);
|
2014-06-06 21:42:22 +04:00
|
|
|
Services.obs.notifyObservers(null, topic, data);
|
2013-12-06 10:46:12 +04:00
|
|
|
},
|
|
|
|
|
2014-03-03 03:20:56 +04:00
|
|
|
// XXX - pollEmailStatus should maybe be on the AccountState object?
|
|
|
|
pollEmailStatus: function pollEmailStatus(currentState, sessionToken, why) {
|
|
|
|
log.debug("entering pollEmailStatus: " + why);
|
2013-12-06 10:46:12 +04:00
|
|
|
if (why == "start") {
|
2015-04-30 10:02:02 +03:00
|
|
|
if (this.currentTimer) {
|
|
|
|
log.debug("pollEmailStatus starting while existing timer is running");
|
|
|
|
clearTimeout(this.currentTimer);
|
|
|
|
this.currentTimer = null;
|
|
|
|
}
|
|
|
|
|
2014-01-24 04:52:24 +04:00
|
|
|
// If we were already polling, stop and start again. This could happen
|
|
|
|
// if the user requested the verification email to be resent while we
|
|
|
|
// were already polling for receipt of an earlier email.
|
2015-01-23 03:22:47 +03:00
|
|
|
this.pollStartDate = Date.now();
|
2014-02-18 21:47:52 +04:00
|
|
|
if (!currentState.whenVerifiedDeferred) {
|
|
|
|
currentState.whenVerifiedDeferred = Promise.defer();
|
2014-04-10 11:13:53 +04:00
|
|
|
// This deferred might not end up with any handlers (eg, if sync
|
|
|
|
// is yet to start up.) This might cause "A promise chain failed to
|
|
|
|
// handle a rejection" messages, so add an error handler directly
|
|
|
|
// on the promise to log the error.
|
|
|
|
currentState.whenVerifiedDeferred.promise.then(null, err => {
|
|
|
|
log.info("the wait for user verification was stopped: " + err);
|
|
|
|
});
|
2014-01-24 04:52:24 +04:00
|
|
|
}
|
2013-12-06 10:46:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
this.checkEmailStatus(sessionToken)
|
|
|
|
.then((response) => {
|
|
|
|
log.debug("checkEmailStatus -> " + JSON.stringify(response));
|
|
|
|
if (response && response.verified) {
|
2014-03-03 03:20:56 +04:00
|
|
|
currentState.getUserAccountData()
|
2013-12-06 10:46:12 +04:00
|
|
|
.then((data) => {
|
2014-01-14 20:00:36 +04:00
|
|
|
data.verified = true;
|
2014-03-03 03:20:56 +04:00
|
|
|
return currentState.setUserAccountData(data);
|
2013-12-06 10:46:12 +04:00
|
|
|
})
|
|
|
|
.then((data) => {
|
|
|
|
// Now that the user is verified, we can proceed to fetch keys
|
2014-02-18 21:47:52 +04:00
|
|
|
if (currentState.whenVerifiedDeferred) {
|
|
|
|
currentState.whenVerifiedDeferred.resolve(data);
|
|
|
|
delete currentState.whenVerifiedDeferred;
|
2013-12-06 10:46:12 +04:00
|
|
|
}
|
2014-06-06 21:42:22 +04:00
|
|
|
// Tell FxAccountsManager to clear its cache
|
|
|
|
this.notifyObservers(ON_FXA_UPDATE_NOTIFICATION, ONVERIFIED_NOTIFICATION);
|
2013-12-06 10:46:12 +04:00
|
|
|
});
|
|
|
|
} else {
|
2014-06-16 13:47:45 +04:00
|
|
|
// Poll email status again after a short delay.
|
|
|
|
this.pollEmailStatusAgain(currentState, sessionToken);
|
|
|
|
}
|
|
|
|
}, error => {
|
2015-01-23 03:22:47 +03:00
|
|
|
let timeoutMs = undefined;
|
|
|
|
if (error && error.retryAfter) {
|
|
|
|
// If the server told us to back off, back off the requested amount.
|
|
|
|
timeoutMs = (error.retryAfter + 3) * 1000;
|
|
|
|
}
|
2014-06-16 13:47:45 +04:00
|
|
|
// The server will return 401 if a request parameter is erroneous or
|
|
|
|
// if the session token expired. Let's continue polling otherwise.
|
|
|
|
if (!error || !error.code || error.code != 401) {
|
2015-01-23 03:22:47 +03:00
|
|
|
this.pollEmailStatusAgain(currentState, sessionToken, timeoutMs);
|
2013-12-06 10:46:12 +04:00
|
|
|
}
|
|
|
|
});
|
2014-06-16 13:47:45 +04:00
|
|
|
},
|
|
|
|
|
2015-01-23 03:22:47 +03:00
|
|
|
// Poll email status using truncated exponential back-off.
|
|
|
|
pollEmailStatusAgain: function (currentState, sessionToken, timeoutMs) {
|
|
|
|
let ageMs = Date.now() - this.pollStartDate;
|
|
|
|
if (ageMs >= this.POLL_SESSION) {
|
2014-06-16 13:47:45 +04:00
|
|
|
if (currentState.whenVerifiedDeferred) {
|
|
|
|
let error = new Error("User email verification timed out.")
|
|
|
|
currentState.whenVerifiedDeferred.reject(error);
|
|
|
|
delete currentState.whenVerifiedDeferred;
|
|
|
|
}
|
2015-01-23 03:22:47 +03:00
|
|
|
log.debug("polling session exceeded, giving up");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (timeoutMs === undefined) {
|
|
|
|
let currentMinute = Math.ceil(ageMs / 60000);
|
2015-04-14 03:35:48 +03:00
|
|
|
timeoutMs = currentMinute <= 2 ? this.VERIFICATION_POLL_TIMEOUT_INITIAL
|
|
|
|
: this.VERIFICATION_POLL_TIMEOUT_SUBSEQUENT;
|
2014-06-16 13:47:45 +04:00
|
|
|
}
|
2015-01-23 03:22:47 +03:00
|
|
|
log.debug("polling with timeout = " + timeoutMs);
|
|
|
|
this.currentTimer = setTimeout(() => {
|
|
|
|
this.pollEmailStatus(currentState, sessionToken, "timer");
|
|
|
|
}, timeoutMs);
|
2014-06-16 13:47:45 +04:00
|
|
|
},
|
2013-12-06 10:46:12 +04:00
|
|
|
|
2014-12-13 03:32:17 +03:00
|
|
|
_requireHttps: function() {
|
|
|
|
let allowHttp = false;
|
|
|
|
try {
|
|
|
|
allowHttp = Services.prefs.getBoolPref("identity.fxaccounts.allowHttp");
|
|
|
|
} catch(e) {
|
|
|
|
// Pref doesn't exist
|
|
|
|
}
|
|
|
|
return allowHttp !== true;
|
|
|
|
},
|
|
|
|
|
2013-12-06 10:46:12 +04:00
|
|
|
// Return the URI of the remote UI flows.
|
2014-03-12 02:58:27 +04:00
|
|
|
getAccountsSignUpURI: function() {
|
|
|
|
let url = Services.urlFormatter.formatURLPref("identity.fxaccounts.remote.signup.uri");
|
2014-12-13 03:32:17 +03:00
|
|
|
if (this._requireHttps() && !/^https:/.test(url)) { // Comment to un-break emacs js-mode highlighting
|
2013-12-06 10:46:12 +04:00
|
|
|
throw new Error("Firefox Accounts server must use HTTPS");
|
|
|
|
}
|
|
|
|
return url;
|
2014-02-01 05:27:59 +04:00
|
|
|
},
|
|
|
|
|
2014-02-18 15:05:13 +04:00
|
|
|
// Return the URI of the remote UI flows.
|
|
|
|
getAccountsSignInURI: function() {
|
|
|
|
let url = Services.urlFormatter.formatURLPref("identity.fxaccounts.remote.signin.uri");
|
2014-12-13 03:32:17 +03:00
|
|
|
if (this._requireHttps() && !/^https:/.test(url)) { // Comment to un-break emacs js-mode highlighting
|
2014-02-18 15:05:13 +04:00
|
|
|
throw new Error("Firefox Accounts server must use HTTPS");
|
|
|
|
}
|
|
|
|
return url;
|
|
|
|
},
|
|
|
|
|
2014-02-01 05:27:59 +04:00
|
|
|
// Returns a promise that resolves with the URL to use to force a re-signin
|
|
|
|
// of the current account.
|
|
|
|
promiseAccountsForceSigninURI: function() {
|
|
|
|
let url = Services.urlFormatter.formatURLPref("identity.fxaccounts.remote.force_auth.uri");
|
2014-12-13 03:32:17 +03:00
|
|
|
if (this._requireHttps() && !/^https:/.test(url)) { // Comment to un-break emacs js-mode highlighting
|
2014-02-01 05:27:59 +04:00
|
|
|
throw new Error("Firefox Accounts server must use HTTPS");
|
|
|
|
}
|
2014-03-03 03:20:56 +04:00
|
|
|
let currentState = this.currentAccountState;
|
2014-02-01 05:27:59 +04:00
|
|
|
// but we need to append the email address onto a query string.
|
|
|
|
return this.getSignedInUser().then(accountData => {
|
|
|
|
if (!accountData) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
let newQueryPortion = url.indexOf("?") == -1 ? "?" : "&";
|
|
|
|
newQueryPortion += "email=" + encodeURIComponent(accountData.email);
|
|
|
|
return url + newQueryPortion;
|
2014-03-03 03:20:56 +04:00
|
|
|
}).then(result => currentState.resolve(result));
|
2015-02-06 00:31:23 +03:00
|
|
|
},
|
|
|
|
|
2015-03-27 12:37:55 +03:00
|
|
|
// Returns a promise that resolves with the URL to use to change
|
|
|
|
// the current account's profile image.
|
|
|
|
// if settingToEdit is set, the profile page should hightlight that setting
|
|
|
|
// for the user to edit.
|
2015-05-15 22:56:00 +03:00
|
|
|
promiseAccountsChangeProfileURI: function(entrypoint, settingToEdit = null) {
|
2015-03-27 12:37:55 +03:00
|
|
|
let url = Services.urlFormatter.formatURLPref("identity.fxaccounts.settings.uri");
|
|
|
|
|
|
|
|
if (settingToEdit) {
|
|
|
|
url += (url.indexOf("?") == -1 ? "?" : "&") +
|
|
|
|
"setting=" + encodeURIComponent(settingToEdit);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._requireHttps() && !/^https:/.test(url)) { // Comment to un-break emacs js-mode highlighting
|
|
|
|
throw new Error("Firefox Accounts server must use HTTPS");
|
|
|
|
}
|
|
|
|
let currentState = this.currentAccountState;
|
|
|
|
// but we need to append the email address onto a query string.
|
|
|
|
return this.getSignedInUser().then(accountData => {
|
|
|
|
if (!accountData) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
let newQueryPortion = url.indexOf("?") == -1 ? "?" : "&";
|
|
|
|
newQueryPortion += "email=" + encodeURIComponent(accountData.email);
|
|
|
|
newQueryPortion += "&uid=" + encodeURIComponent(accountData.uid);
|
2015-05-15 22:56:00 +03:00
|
|
|
if (entrypoint) {
|
|
|
|
newQueryPortion += "&entrypoint=" + encodeURIComponent(entrypoint);
|
|
|
|
}
|
2015-03-27 12:37:55 +03:00
|
|
|
return url + newQueryPortion;
|
|
|
|
}).then(result => currentState.resolve(result));
|
|
|
|
},
|
|
|
|
|
2015-04-24 03:51:20 +03:00
|
|
|
// Returns a promise that resolves with the URL to use to manage the current
|
|
|
|
// user's FxA acct.
|
2015-05-15 22:56:00 +03:00
|
|
|
promiseAccountsManageURI: function(entrypoint) {
|
2015-04-24 03:51:20 +03:00
|
|
|
let url = Services.urlFormatter.formatURLPref("identity.fxaccounts.settings.uri");
|
|
|
|
if (this._requireHttps() && !/^https:/.test(url)) { // Comment to un-break emacs js-mode highlighting
|
|
|
|
throw new Error("Firefox Accounts server must use HTTPS");
|
|
|
|
}
|
|
|
|
let currentState = this.currentAccountState;
|
|
|
|
// but we need to append the uid and email address onto a query string
|
|
|
|
// (if the server has no matching uid it will offer to sign in with the
|
|
|
|
// email address)
|
|
|
|
return this.getSignedInUser().then(accountData => {
|
|
|
|
if (!accountData) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
let newQueryPortion = url.indexOf("?") == -1 ? "?" : "&";
|
|
|
|
newQueryPortion += "uid=" + encodeURIComponent(accountData.uid) +
|
|
|
|
"&email=" + encodeURIComponent(accountData.email);
|
2015-05-15 22:56:00 +03:00
|
|
|
if (entrypoint) {
|
|
|
|
newQueryPortion += "&entrypoint=" + encodeURIComponent(entrypoint);
|
|
|
|
}
|
2015-04-24 03:51:20 +03:00
|
|
|
return url + newQueryPortion;
|
|
|
|
}).then(result => currentState.resolve(result));
|
|
|
|
},
|
|
|
|
|
2015-02-14 00:54:15 +03:00
|
|
|
/**
|
2015-02-06 00:31:23 +03:00
|
|
|
* Get an OAuth token for the user
|
2015-02-14 00:54:15 +03:00
|
|
|
*
|
|
|
|
* @param options
|
|
|
|
* {
|
2015-04-03 04:47:00 +03:00
|
|
|
* scope: (string/array) the oauth scope(s) being requested. As a
|
|
|
|
* convenience, you may pass a string if only one scope is
|
|
|
|
* required, or an array of strings if multiple are needed.
|
2015-02-14 00:54:15 +03:00
|
|
|
* }
|
|
|
|
*
|
|
|
|
* @return Promise.<string | Error>
|
|
|
|
* The promise resolves the oauth token as a string or rejects with
|
|
|
|
* an error object ({error: ERROR, details: {}}) of the following:
|
|
|
|
* INVALID_PARAMETER
|
|
|
|
* NO_ACCOUNT
|
|
|
|
* UNVERIFIED_ACCOUNT
|
|
|
|
* NETWORK_ERROR
|
|
|
|
* AUTH_ERROR
|
|
|
|
* UNKNOWN_ERROR
|
2015-02-06 00:31:23 +03:00
|
|
|
*/
|
2015-04-03 04:47:00 +03:00
|
|
|
getOAuthToken: Task.async(function* (options = {}) {
|
2015-02-06 00:31:23 +03:00
|
|
|
log.debug("getOAuthToken enter");
|
2015-04-03 04:47:00 +03:00
|
|
|
let scope = options.scope;
|
|
|
|
if (typeof scope === "string") {
|
|
|
|
scope = [scope];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!scope || !scope.length) {
|
|
|
|
throw this._error(ERROR_INVALID_PARAMETER, "Missing or invalid 'scope' option");
|
|
|
|
}
|
|
|
|
|
|
|
|
yield this._getVerifiedAccountOrReject();
|
2015-02-06 00:31:23 +03:00
|
|
|
|
2015-04-03 04:47:00 +03:00
|
|
|
// Early exit for a cached token.
|
|
|
|
let currentState = this.currentAccountState;
|
|
|
|
let cached = currentState.getCachedToken(scope);
|
|
|
|
if (cached) {
|
|
|
|
log.debug("getOAuthToken returning a cached token");
|
|
|
|
return cached.token;
|
2015-02-06 00:31:23 +03:00
|
|
|
}
|
|
|
|
|
2015-04-03 04:47:00 +03:00
|
|
|
// We are going to hit the server - this is the string we pass to it.
|
|
|
|
let scopeString = scope.join(" ");
|
2015-02-14 00:54:15 +03:00
|
|
|
let client = options.client;
|
2015-02-06 00:31:23 +03:00
|
|
|
|
2015-02-14 00:54:15 +03:00
|
|
|
if (!client) {
|
|
|
|
try {
|
2015-04-03 04:47:00 +03:00
|
|
|
let defaultURL = Services.urlFormatter.formatURLPref("identity.fxaccounts.remote.oauth.uri");
|
2015-02-14 00:54:15 +03:00
|
|
|
client = new FxAccountsOAuthGrantClient({
|
2015-04-03 04:47:00 +03:00
|
|
|
serverURL: defaultURL,
|
2015-02-14 00:54:15 +03:00
|
|
|
client_id: FX_OAUTH_CLIENT_ID
|
|
|
|
});
|
|
|
|
} catch (e) {
|
2015-04-03 04:47:00 +03:00
|
|
|
throw this._error(ERROR_INVALID_PARAMETER, e);
|
2015-02-14 00:54:15 +03:00
|
|
|
}
|
|
|
|
}
|
2015-04-03 04:47:00 +03:00
|
|
|
let oAuthURL = client.serverURL.href;
|
2015-02-06 00:31:23 +03:00
|
|
|
|
2015-04-03 04:47:00 +03:00
|
|
|
try {
|
2015-04-03 04:47:00 +03:00
|
|
|
log.debug("getOAuthToken fetching new token from", oAuthURL);
|
2015-04-03 04:47:00 +03:00
|
|
|
let assertion = yield this.getAssertion(oAuthURL);
|
2015-04-03 04:47:00 +03:00
|
|
|
let result = yield client.getTokenFromAssertion(assertion, scopeString);
|
|
|
|
let token = result.access_token;
|
|
|
|
// If we got one, cache it.
|
|
|
|
if (token) {
|
|
|
|
let entry = {token: token, server: oAuthURL};
|
|
|
|
// But before we do, check the cache again - if we find one now, it
|
|
|
|
// means someone else concurrently requested the same scope and beat
|
|
|
|
// us to the cache write. To be nice to the server, we revoke the one
|
|
|
|
// we just got and return the newly cached value.
|
|
|
|
let cached = currentState.getCachedToken(scope);
|
|
|
|
if (cached) {
|
|
|
|
log.debug("Detected a race for this token - revoking the new one.");
|
|
|
|
this._destroyOAuthToken(entry);
|
|
|
|
return cached.token;
|
|
|
|
}
|
|
|
|
currentState.setCachedToken(scope, entry);
|
|
|
|
}
|
|
|
|
return token;
|
2015-04-03 04:47:00 +03:00
|
|
|
} catch (err) {
|
|
|
|
throw this._errorToErrorClass(err);
|
|
|
|
}
|
|
|
|
}),
|
2015-02-14 00:54:15 +03:00
|
|
|
|
2015-04-03 04:47:00 +03:00
|
|
|
/**
|
|
|
|
* Remove an OAuth token from the token cache. Callers should call this
|
|
|
|
* after they determine a token is invalid, so a new token will be fetched
|
|
|
|
* on the next call to getOAuthToken().
|
|
|
|
*
|
|
|
|
* @param options
|
|
|
|
* {
|
|
|
|
* token: (string) A previously fetched token.
|
|
|
|
* }
|
|
|
|
* @return Promise.<undefined> This function will always resolve, even if
|
|
|
|
* an unknown token is passed.
|
|
|
|
*/
|
|
|
|
removeCachedOAuthToken: Task.async(function* (options) {
|
|
|
|
if (!options.token || typeof options.token !== "string") {
|
|
|
|
throw this._error(ERROR_INVALID_PARAMETER, "Missing or invalid 'token' option");
|
|
|
|
}
|
|
|
|
let currentState = this.currentAccountState;
|
|
|
|
let existing = currentState.removeCachedToken(options.token);
|
|
|
|
if (existing) {
|
|
|
|
// background destroy.
|
|
|
|
this._destroyOAuthToken(existing).catch(err => {
|
|
|
|
log.warn("FxA failed to revoke a cached token", err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
|
2015-04-03 04:47:00 +03:00
|
|
|
_getVerifiedAccountOrReject: Task.async(function* () {
|
|
|
|
let data = yield this.currentAccountState.getUserAccountData();
|
|
|
|
if (!data) {
|
|
|
|
// No signed-in user
|
|
|
|
throw this._error(ERROR_NO_ACCOUNT);
|
|
|
|
}
|
|
|
|
if (!this.isUserEmailVerified(data)) {
|
|
|
|
// Signed-in user has not verified email
|
|
|
|
throw this._error(ERROR_UNVERIFIED_ACCOUNT);
|
|
|
|
}
|
|
|
|
}),
|
2015-02-14 00:54:15 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Coerce an error into one of the general error cases:
|
|
|
|
* NETWORK_ERROR
|
|
|
|
* AUTH_ERROR
|
|
|
|
* UNKNOWN_ERROR
|
|
|
|
*
|
|
|
|
* These errors will pass through:
|
|
|
|
* INVALID_PARAMETER
|
|
|
|
* NO_ACCOUNT
|
|
|
|
* UNVERIFIED_ACCOUNT
|
|
|
|
*/
|
|
|
|
_errorToErrorClass: function (aError) {
|
|
|
|
if (aError.errno) {
|
|
|
|
let error = SERVER_ERRNO_TO_ERROR[aError.errno];
|
|
|
|
return this._error(ERROR_TO_GENERAL_ERROR_CLASS[error] || ERROR_UNKNOWN, aError);
|
|
|
|
} else if (aError.message &&
|
2015-03-26 11:33:38 +03:00
|
|
|
(aError.message === "INVALID_PARAMETER" ||
|
2015-02-14 00:54:15 +03:00
|
|
|
aError.message === "NO_ACCOUNT" ||
|
2015-03-26 11:33:38 +03:00
|
|
|
aError.message === "UNVERIFIED_ACCOUNT")) {
|
2015-04-03 04:47:00 +03:00
|
|
|
return aError;
|
2015-02-14 00:54:15 +03:00
|
|
|
}
|
|
|
|
return this._error(ERROR_UNKNOWN, aError);
|
|
|
|
},
|
|
|
|
|
|
|
|
_error: function(aError, aDetails) {
|
|
|
|
log.error("FxA rejecting with error ${aError}, details: ${aDetails}", {aError, aDetails});
|
|
|
|
let reason = new Error(aError);
|
|
|
|
if (aDetails) {
|
|
|
|
reason.details = aDetails;
|
|
|
|
}
|
2015-04-03 04:47:00 +03:00
|
|
|
return reason;
|
2015-03-26 11:33:38 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the user's account and profile data
|
|
|
|
*
|
|
|
|
* @param options
|
|
|
|
* {
|
2015-04-11 15:23:08 +03:00
|
|
|
* contentUrl: (string) Used by the FxAccountsWebChannel.
|
2015-03-26 11:33:38 +03:00
|
|
|
* Defaults to pref identity.fxaccounts.settings.uri
|
2015-04-11 15:23:08 +03:00
|
|
|
* profileServerUrl: (string) Used by the FxAccountsWebChannel.
|
2015-03-26 11:33:38 +03:00
|
|
|
* Defaults to pref identity.fxaccounts.remote.profile.uri
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* @return Promise.<object | Error>
|
|
|
|
* The promise resolves to an accountData object with extra profile
|
|
|
|
* information such as profileImageUrl, or rejects with
|
|
|
|
* an error object ({error: ERROR, details: {}}) of the following:
|
|
|
|
* INVALID_PARAMETER
|
|
|
|
* NO_ACCOUNT
|
|
|
|
* UNVERIFIED_ACCOUNT
|
|
|
|
* NETWORK_ERROR
|
|
|
|
* AUTH_ERROR
|
|
|
|
* UNKNOWN_ERROR
|
|
|
|
*/
|
|
|
|
getSignedInUserProfile: function () {
|
|
|
|
let accountState = this.currentAccountState;
|
|
|
|
return accountState.getProfile()
|
2015-03-27 12:37:55 +03:00
|
|
|
.then((profileData) => {
|
|
|
|
let profile = JSON.parse(JSON.stringify(profileData));
|
|
|
|
return accountState.resolve(profile);
|
|
|
|
},
|
|
|
|
(error) => {
|
|
|
|
log.error("Could not retrieve profile data", error);
|
|
|
|
return accountState.reject(error);
|
2015-03-26 11:33:38 +03:00
|
|
|
})
|
2015-04-03 04:47:00 +03:00
|
|
|
.then(null, err => Promise.reject(this._errorToErrorClass(err)));
|
2015-03-26 11:33:38 +03:00
|
|
|
},
|
2014-02-04 14:12:37 +04:00
|
|
|
};
|
2013-12-06 10:46:12 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* JSONStorage constructor that creates instances that may set/get
|
|
|
|
* to a specified file, in a directory that will be created if it
|
|
|
|
* doesn't exist.
|
|
|
|
*
|
|
|
|
* @param options {
|
|
|
|
* filename: of the file to write to
|
|
|
|
* baseDir: directory where the file resides
|
|
|
|
* }
|
|
|
|
* @return instance
|
|
|
|
*/
|
|
|
|
function JSONStorage(options) {
|
|
|
|
this.baseDir = options.baseDir;
|
|
|
|
this.path = OS.Path.join(options.baseDir, options.filename);
|
2015-04-03 04:47:00 +03:00
|
|
|
this.oauthTokensPath = OS.Path.join(options.baseDir, options.oauthTokensFilename);
|
2013-12-06 10:46:12 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
JSONStorage.prototype = {
|
|
|
|
set: function(contents) {
|
|
|
|
return OS.File.makeDir(this.baseDir, {ignoreExisting: true})
|
|
|
|
.then(CommonUtils.writeJSON.bind(null, contents, this.path));
|
|
|
|
},
|
|
|
|
|
|
|
|
get: function() {
|
|
|
|
return CommonUtils.readJSON(this.path);
|
2015-04-03 04:47:00 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
setOAuthTokens: function(contents) {
|
|
|
|
return OS.File.makeDir(this.baseDir, {ignoreExisting: true})
|
|
|
|
.then(CommonUtils.writeJSON.bind(null, contents, this.oauthTokensPath));
|
|
|
|
},
|
|
|
|
|
|
|
|
getOAuthTokens: function(contents) {
|
|
|
|
return CommonUtils.readJSON(this.oauthTokensPath);
|
|
|
|
},
|
|
|
|
|
2013-12-06 10:46:12 +04:00
|
|
|
};
|
|
|
|
|
2014-06-14 08:33:20 +04:00
|
|
|
/**
|
|
|
|
* LoginManagerStorage constructor that creates instances that may set/get
|
|
|
|
* from a combination of a clear-text JSON file and stored securely in
|
|
|
|
* the nsILoginManager.
|
|
|
|
*
|
|
|
|
* @param options {
|
|
|
|
* filename: of the plain-text file to write to
|
|
|
|
* baseDir: directory where the file resides
|
|
|
|
* }
|
|
|
|
* @return instance
|
|
|
|
*/
|
|
|
|
|
|
|
|
function LoginManagerStorage(options) {
|
|
|
|
// we reuse the JSONStorage for writing the plain-text stuff.
|
|
|
|
this.jsonStorage = new JSONStorage(options);
|
|
|
|
}
|
|
|
|
|
|
|
|
LoginManagerStorage.prototype = {
|
|
|
|
// The fields in the credentials JSON object that are stored in plain-text
|
|
|
|
// in the profile directory. All other fields are stored in the login manager,
|
|
|
|
// and thus are only available when the master-password is unlocked.
|
|
|
|
|
|
|
|
// a hook point for testing.
|
|
|
|
get _isLoggedIn() {
|
|
|
|
return Services.logins.isLoggedIn;
|
|
|
|
},
|
|
|
|
|
|
|
|
// Clear any data from the login manager. Returns true if the login manager
|
|
|
|
// was unlocked (even if no existing logins existed) or false if it was
|
|
|
|
// locked (meaning we don't even know if it existed or not.)
|
|
|
|
_clearLoginMgrData: Task.async(function* () {
|
|
|
|
try { // Services.logins might be third-party and broken...
|
|
|
|
yield Services.logins.initializationPromise;
|
|
|
|
if (!this._isLoggedIn) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
let logins = Services.logins.findLogins({}, FXA_PWDMGR_HOST, null, FXA_PWDMGR_REALM);
|
|
|
|
for (let login of logins) {
|
|
|
|
Services.logins.removeLogin(login);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
} catch (ex) {
|
|
|
|
log.error("Failed to clear login data: ${}", ex);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
|
|
|
|
set: Task.async(function* (contents) {
|
|
|
|
if (!contents) {
|
|
|
|
// User is signing out - write the null to the json file.
|
|
|
|
yield this.jsonStorage.set(contents);
|
|
|
|
|
|
|
|
// And nuke it from the login manager.
|
|
|
|
let cleared = yield this._clearLoginMgrData();
|
|
|
|
if (!cleared) {
|
|
|
|
// just log a message - we verify that the email address matches when
|
|
|
|
// we reload it, so having a stale entry doesn't really hurt.
|
|
|
|
log.info("not removing credentials from login manager - not logged in");
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We are saving actual data.
|
|
|
|
// Split the data into 2 chunks - one to go to the plain-text, and the
|
|
|
|
// other to write to the login manager.
|
|
|
|
let toWriteJSON = {version: contents.version};
|
|
|
|
let accountDataJSON = toWriteJSON.accountData = {};
|
|
|
|
let toWriteLoginMgr = {version: contents.version};
|
|
|
|
let accountDataLoginMgr = toWriteLoginMgr.accountData = {};
|
|
|
|
for (let [name, value] of Iterator(contents.accountData)) {
|
|
|
|
if (FXA_PWDMGR_PLAINTEXT_FIELDS.indexOf(name) >= 0) {
|
|
|
|
accountDataJSON[name] = value;
|
|
|
|
} else {
|
|
|
|
accountDataLoginMgr[name] = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
yield this.jsonStorage.set(toWriteJSON);
|
|
|
|
|
|
|
|
try { // Services.logins might be third-party and broken...
|
|
|
|
// and the stuff into the login manager.
|
|
|
|
yield Services.logins.initializationPromise;
|
|
|
|
// If MP is locked we silently fail - the user may need to re-auth
|
|
|
|
// next startup.
|
|
|
|
if (!this._isLoggedIn) {
|
|
|
|
log.info("not saving credentials to login manager - not logged in");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// write the rest of the data to the login manager.
|
|
|
|
let loginInfo = new Components.Constructor(
|
|
|
|
"@mozilla.org/login-manager/loginInfo;1", Ci.nsILoginInfo, "init");
|
|
|
|
let login = new loginInfo(FXA_PWDMGR_HOST,
|
|
|
|
null, // aFormSubmitURL,
|
|
|
|
FXA_PWDMGR_REALM, // aHttpRealm,
|
|
|
|
contents.accountData.email, // aUsername
|
|
|
|
JSON.stringify(toWriteLoginMgr), // aPassword
|
|
|
|
"", // aUsernameField
|
|
|
|
"");// aPasswordField
|
|
|
|
|
|
|
|
let existingLogins = Services.logins.findLogins({}, FXA_PWDMGR_HOST, null,
|
|
|
|
FXA_PWDMGR_REALM);
|
|
|
|
if (existingLogins.length) {
|
|
|
|
Services.logins.modifyLogin(existingLogins[0], login);
|
|
|
|
} else {
|
|
|
|
Services.logins.addLogin(login);
|
|
|
|
}
|
|
|
|
} catch (ex) {
|
|
|
|
log.error("Failed to save data to the login manager: ${}", ex);
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
|
|
|
|
get: Task.async(function* () {
|
|
|
|
// we need to suck some data from the .json file in the profile dir and
|
|
|
|
// some other from the login manager.
|
|
|
|
let data = yield this.jsonStorage.get();
|
|
|
|
if (!data) {
|
|
|
|
// no user logged in, nuke the storage data incase we couldn't remove
|
|
|
|
// it previously and then we are done.
|
|
|
|
yield this._clearLoginMgrData();
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we have encryption keys it must have been saved before we
|
|
|
|
// used the login manager, so re-save it.
|
|
|
|
if (data.accountData.kA || data.accountData.kB || data.keyFetchToken) {
|
2014-06-18 09:07:41 +04:00
|
|
|
// We need to migrate, but the MP might be locked (eg, on the first run
|
|
|
|
// with this enabled, we will get here very soon after startup, so will
|
|
|
|
// certainly be locked.) This means we can't actually store the data in
|
|
|
|
// the login manager (and thus might lose it if we migrated now)
|
|
|
|
// So if the MP is locked, we *don't* migrate, but still just return
|
|
|
|
// the subset of data we now store in the JSON.
|
|
|
|
// This will cause sync to notice the lack of keys, force an unlock then
|
|
|
|
// re-fetch the account data to see if the keys are there. At *that*
|
|
|
|
// point we will end up back here, but because the MP is now unlocked
|
|
|
|
// we can actually perform the migration.
|
|
|
|
if (!this._isLoggedIn) {
|
|
|
|
// return the "safe" subset but leave the storage alone.
|
|
|
|
log.info("account data needs migration to the login manager but the MP is locked.");
|
|
|
|
let result = {
|
|
|
|
version: data.version,
|
|
|
|
accountData: {},
|
|
|
|
};
|
|
|
|
for (let fieldName of FXA_PWDMGR_PLAINTEXT_FIELDS) {
|
|
|
|
result.accountData[fieldName] = data.accountData[fieldName];
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
// actually migrate - just calling .set() will split everything up.
|
|
|
|
log.info("account data is being migrated to the login manager.");
|
2014-06-14 08:33:20 +04:00
|
|
|
yield this.set(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
try { // Services.logins might be third-party and broken...
|
|
|
|
// read the data from the login manager and merge it for return.
|
|
|
|
yield Services.logins.initializationPromise;
|
|
|
|
|
|
|
|
if (!this._isLoggedIn) {
|
|
|
|
log.info("returning partial account data as the login manager is locked.");
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
let logins = Services.logins.findLogins({}, FXA_PWDMGR_HOST, null, FXA_PWDMGR_REALM);
|
|
|
|
if (logins.length == 0) {
|
|
|
|
// This could happen if the MP was locked when we wrote the data.
|
|
|
|
log.info("Can't find the rest of the credentials in the login manager");
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
let login = logins[0];
|
|
|
|
if (login.username == data.accountData.email) {
|
|
|
|
let lmData = JSON.parse(login.password);
|
|
|
|
if (lmData.version == data.version) {
|
|
|
|
// Merge the login manager data
|
|
|
|
copyObjectProperties(lmData.accountData, data.accountData);
|
|
|
|
} else {
|
|
|
|
log.info("version field in the login manager doesn't match - ignoring it");
|
|
|
|
yield this._clearLoginMgrData();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.info("username in the login manager doesn't match - ignoring it");
|
|
|
|
yield this._clearLoginMgrData();
|
|
|
|
}
|
|
|
|
} catch (ex) {
|
|
|
|
log.error("Failed to get data from the login manager: ${}", ex);
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}),
|
|
|
|
|
2015-04-03 04:47:00 +03:00
|
|
|
// OAuth tokens are always written to disk, so delegate to our JSON storage.
|
|
|
|
// (Bug 1013064 comments 23-25 explain why we save the sessionToken into the
|
|
|
|
// plain JSON file, and the same logic applies for oauthTokens being in JSON)
|
|
|
|
getOAuthTokens() {
|
|
|
|
return this.jsonStorage.getOAuthTokens();
|
|
|
|
},
|
|
|
|
|
|
|
|
setOAuthTokens(contents) {
|
|
|
|
return this.jsonStorage.setOAuthTokens(contents);
|
|
|
|
},
|
2014-06-14 08:33:20 +04:00
|
|
|
}
|
|
|
|
|
2013-12-06 10:46:12 +04:00
|
|
|
// A getter for the instance to export
|
|
|
|
XPCOMUtils.defineLazyGetter(this, "fxAccounts", function() {
|
|
|
|
let a = new FxAccounts();
|
|
|
|
|
|
|
|
// XXX Bug 947061 - We need a strategy for resuming email verification after
|
|
|
|
// browser restart
|
2014-02-04 14:12:37 +04:00
|
|
|
a.loadAndPoll();
|
2013-12-06 10:46:12 +04:00
|
|
|
|
|
|
|
return a;
|
|
|
|
});
|