2013-10-02 14:48:08 -07: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/. */
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
this.EXPORTED_SYMBOLS = ["BrowserIDManager"];
|
|
|
|
|
|
|
|
const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
|
|
|
|
|
2013-10-17 11:42:18 -07:00
|
|
|
Cu.import("resource://gre/modules/Log.jsm");
|
2013-10-02 14:48:08 -07:00
|
|
|
Cu.import("resource://services-common/async.js");
|
2014-01-29 15:02:09 -08:00
|
|
|
Cu.import("resource://services-common/utils.js");
|
2013-10-02 14:48:08 -07:00
|
|
|
Cu.import("resource://services-common/tokenserverclient.js");
|
|
|
|
Cu.import("resource://services-crypto/utils.js");
|
|
|
|
Cu.import("resource://services-sync/identity.js");
|
|
|
|
Cu.import("resource://services-sync/util.js");
|
2013-12-19 20:57:26 -08:00
|
|
|
Cu.import("resource://services-common/tokenserverclient.js");
|
2013-12-16 20:45:03 -08:00
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
Cu.import("resource://services-sync/constants.js");
|
|
|
|
Cu.import("resource://gre/modules/Promise.jsm");
|
2014-01-28 17:51:09 -08:00
|
|
|
Cu.import("resource://services-sync/stages/cluster.js");
|
2014-01-23 18:04:38 -08:00
|
|
|
Cu.import("resource://gre/modules/FxAccounts.jsm");
|
2013-12-16 20:45:03 -08:00
|
|
|
|
2013-12-19 20:57:26 -08:00
|
|
|
// Lazy imports to prevent unnecessary load on startup.
|
2014-03-06 20:41:32 -08:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "Weave",
|
|
|
|
"resource://services-sync/main.js");
|
|
|
|
|
2013-12-19 20:57:26 -08:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "BulkKeyBundle",
|
|
|
|
"resource://services-sync/keys.js");
|
|
|
|
|
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "fxAccounts",
|
|
|
|
"resource://gre/modules/FxAccounts.jsm");
|
2013-12-16 20:45:03 -08:00
|
|
|
|
2014-02-06 16:53:52 -08:00
|
|
|
XPCOMUtils.defineLazyGetter(this, 'log', function() {
|
|
|
|
let log = Log.repository.getLogger("Sync.BrowserIDManager");
|
|
|
|
log.level = Log.Level[Svc.Prefs.get("log.logger.identity")] || Log.Level.Error;
|
|
|
|
return log;
|
|
|
|
});
|
|
|
|
|
2014-03-06 20:41:33 -08:00
|
|
|
// FxAccountsCommon.js doesn't use a "namespace", so create one here.
|
|
|
|
let fxAccountsCommon = {};
|
|
|
|
Cu.import("resource://gre/modules/FxAccountsCommon.js", fxAccountsCommon);
|
|
|
|
|
|
|
|
const OBSERVER_TOPICS = [
|
|
|
|
fxAccountsCommon.ONLOGIN_NOTIFICATION,
|
|
|
|
fxAccountsCommon.ONLOGOUT_NOTIFICATION,
|
|
|
|
];
|
|
|
|
|
2014-01-29 12:41:07 -08:00
|
|
|
const PREF_SYNC_SHOW_CUSTOMIZATION = "services.sync.ui.showCustomizationDialog";
|
|
|
|
|
2013-12-16 20:45:03 -08:00
|
|
|
function deriveKeyBundle(kB) {
|
|
|
|
let out = CryptoUtils.hkdf(kB, undefined,
|
|
|
|
"identity.mozilla.com/picl/v1/oldsync", 2*32);
|
|
|
|
let bundle = new BulkKeyBundle();
|
|
|
|
// [encryptionKey, hmacKey]
|
|
|
|
bundle.keyPair = [out.slice(0, 32), out.slice(32, 64)];
|
|
|
|
return bundle;
|
|
|
|
}
|
|
|
|
|
2014-01-31 15:26:20 -08:00
|
|
|
/*
|
|
|
|
General authentication error for abstracting authentication
|
2014-03-02 14:55:30 -08:00
|
|
|
errors from multiple sources (e.g., from FxAccounts, TokenServer).
|
|
|
|
details is additional details about the error - it might be a string, or
|
|
|
|
some other error object (which should do the right thing when toString() is
|
|
|
|
called on it)
|
2014-01-31 15:26:20 -08:00
|
|
|
*/
|
2014-03-02 14:55:30 -08:00
|
|
|
function AuthenticationError(details) {
|
|
|
|
this.details = details;
|
|
|
|
}
|
|
|
|
|
|
|
|
AuthenticationError.prototype = {
|
|
|
|
toString: function() {
|
|
|
|
return "AuthenticationError(" + this.details + ")";
|
|
|
|
}
|
2014-01-31 15:26:20 -08:00
|
|
|
}
|
|
|
|
|
2013-12-19 20:57:26 -08:00
|
|
|
this.BrowserIDManager = function BrowserIDManager() {
|
2014-02-04 03:03:18 -08:00
|
|
|
// NOTE: _fxaService and _tokenServerClient are replaced with mocks by
|
|
|
|
// the test suite.
|
2013-12-19 20:57:26 -08:00
|
|
|
this._fxaService = fxAccounts;
|
|
|
|
this._tokenServerClient = new TokenServerClient();
|
2014-01-28 17:51:09 -08:00
|
|
|
// will be a promise that resolves when we are ready to authenticate
|
|
|
|
this.whenReadyToAuthenticate = null;
|
2014-02-06 16:53:52 -08:00
|
|
|
this._log = log;
|
2013-10-02 14:48:08 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
this.BrowserIDManager.prototype = {
|
|
|
|
__proto__: IdentityManager.prototype,
|
|
|
|
|
|
|
|
_fxaService: null,
|
|
|
|
_tokenServerClient: null,
|
|
|
|
// https://docs.services.mozilla.com/token/apis.html
|
|
|
|
_token: null,
|
2014-03-06 20:41:33 -08:00
|
|
|
_signedInUser: null, // the signedinuser we got from FxAccounts.
|
2013-10-02 14:48:08 -07:00
|
|
|
|
2014-03-06 20:41:32 -08:00
|
|
|
// null if no error, otherwise a LOGIN_FAILED_* value that indicates why
|
|
|
|
// we failed to authenticate (but note it might not be an actual
|
|
|
|
// authentication problem, just a transient network error or similar)
|
|
|
|
_authFailureReason: null,
|
|
|
|
|
2014-01-28 17:51:09 -08:00
|
|
|
// it takes some time to fetch a sync key bundle, so until this flag is set,
|
|
|
|
// we don't consider the lack of a keybundle as a failure state.
|
|
|
|
_shouldHaveSyncKeyBundle: false,
|
|
|
|
|
|
|
|
get readyToAuthenticate() {
|
|
|
|
// We are finished initializing when we *should* have a sync key bundle,
|
|
|
|
// although we might not actually have one due to auth failures etc.
|
|
|
|
return this._shouldHaveSyncKeyBundle;
|
|
|
|
},
|
|
|
|
|
2014-01-28 18:57:19 -08:00
|
|
|
get needsCustomization() {
|
|
|
|
try {
|
2014-01-29 12:41:07 -08:00
|
|
|
return Services.prefs.getBoolPref(PREF_SYNC_SHOW_CUSTOMIZATION);
|
2014-01-28 18:57:19 -08:00
|
|
|
} catch (e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-01-28 17:51:09 -08:00
|
|
|
initialize: function() {
|
2014-03-06 20:41:33 -08:00
|
|
|
for (let topic of OBSERVER_TOPICS) {
|
|
|
|
Services.obs.addObserver(this, topic, false);
|
|
|
|
}
|
2014-01-28 17:51:09 -08:00
|
|
|
return this.initializeWithCurrentIdentity();
|
|
|
|
},
|
|
|
|
|
2014-03-06 20:41:32 -08:00
|
|
|
/**
|
|
|
|
* Ensure the user is logged in. Returns a promise that resolves when
|
|
|
|
* the user is logged in, or is rejected if the login attempt has failed.
|
|
|
|
*/
|
|
|
|
ensureLoggedIn: function() {
|
|
|
|
if (!this._shouldHaveSyncKeyBundle) {
|
|
|
|
// We are already in the process of logging in.
|
|
|
|
return this.whenReadyToAuthenticate.promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we are already happy then there is nothing more to do.
|
2014-03-12 14:31:18 -07:00
|
|
|
if (this._syncKeyBundle) {
|
2014-03-06 20:41:32 -08:00
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Similarly, if we have a previous failure that implies an explicit
|
|
|
|
// re-entering of credentials by the user is necessary we don't take any
|
|
|
|
// further action - an observer will fire when the user does that.
|
|
|
|
if (Weave.Status.login == LOGIN_FAILED_LOGIN_REJECTED) {
|
|
|
|
return Promise.reject();
|
|
|
|
}
|
|
|
|
|
|
|
|
// So - we've a previous auth problem and aren't currently attempting to
|
|
|
|
// log in - so fire that off.
|
|
|
|
this.initializeWithCurrentIdentity();
|
|
|
|
return this.whenReadyToAuthenticate.promise;
|
|
|
|
},
|
|
|
|
|
2014-03-06 20:41:33 -08:00
|
|
|
finalize: function() {
|
|
|
|
// After this is called, we can expect Service.identity != this.
|
|
|
|
for (let topic of OBSERVER_TOPICS) {
|
|
|
|
Services.obs.removeObserver(this, topic);
|
|
|
|
}
|
|
|
|
this.resetCredentials();
|
|
|
|
this._signedInUser = null;
|
|
|
|
return Promise.resolve();
|
|
|
|
},
|
|
|
|
|
2014-01-30 19:02:46 -08:00
|
|
|
initializeWithCurrentIdentity: function(isInitialSync=false) {
|
2014-03-06 20:41:32 -08:00
|
|
|
// While this function returns a promise that resolves once we've started
|
|
|
|
// the auth process, that process is complete when
|
|
|
|
// this.whenReadyToAuthenticate.promise resolves.
|
2014-01-28 17:51:09 -08:00
|
|
|
this._log.trace("initializeWithCurrentIdentity");
|
|
|
|
|
|
|
|
// Reset the world before we do anything async.
|
|
|
|
this.whenReadyToAuthenticate = Promise.defer();
|
|
|
|
this._shouldHaveSyncKeyBundle = false;
|
2014-03-06 20:41:32 -08:00
|
|
|
this._authFailureReason = null;
|
2014-01-28 17:51:09 -08:00
|
|
|
|
2014-02-04 03:03:18 -08:00
|
|
|
return this._fxaService.getSignedInUser().then(accountData => {
|
2014-01-28 17:51:09 -08:00
|
|
|
if (!accountData) {
|
|
|
|
this._log.info("initializeWithCurrentIdentity has no user logged in");
|
2014-03-06 20:41:33 -08:00
|
|
|
this.account = null;
|
|
|
|
// and we are as ready as we can ever be for auth.
|
2014-03-06 20:41:32 -08:00
|
|
|
this._shouldHaveSyncKeyBundle = true;
|
|
|
|
this.whenReadyToAuthenticate.reject("no user is logged in");
|
2014-01-28 17:51:09 -08:00
|
|
|
return;
|
|
|
|
}
|
2014-01-28 18:57:19 -08:00
|
|
|
|
2014-03-06 20:41:33 -08:00
|
|
|
this.account = accountData.email;
|
|
|
|
this._updateSignedInUser(accountData);
|
2014-01-30 19:02:46 -08:00
|
|
|
// The user must be verified before we can do anything at all; we kick
|
|
|
|
// this and the rest of initialization off in the background (ie, we
|
|
|
|
// don't return the promise)
|
|
|
|
this._log.info("Waiting for user to be verified.");
|
2014-02-04 03:03:18 -08:00
|
|
|
this._fxaService.whenVerified(accountData).then(accountData => {
|
2014-03-06 20:41:33 -08:00
|
|
|
this._updateSignedInUser(accountData);
|
2014-01-30 19:02:46 -08:00
|
|
|
this._log.info("Starting fetch for key bundle.");
|
|
|
|
if (this.needsCustomization) {
|
|
|
|
// If the user chose to "Customize sync options" when signing
|
|
|
|
// up with Firefox Accounts, ask them to choose what to sync.
|
|
|
|
const url = "chrome://browser/content/sync/customize.xul";
|
|
|
|
const features = "centerscreen,chrome,modal,dialog,resizable=no";
|
|
|
|
let win = Services.wm.getMostRecentWindow("navigator:browser");
|
|
|
|
|
|
|
|
let data = {accepted: false};
|
|
|
|
win.openDialog(url, "_blank", features, data);
|
|
|
|
|
|
|
|
if (data.accepted) {
|
|
|
|
Services.prefs.clearUserPref(PREF_SYNC_SHOW_CUSTOMIZATION);
|
|
|
|
} else {
|
|
|
|
// Log out if the user canceled the dialog.
|
2014-02-04 03:03:18 -08:00
|
|
|
return this._fxaService.signOut();
|
2014-01-30 19:02:46 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}).then(() => {
|
|
|
|
return this._fetchSyncKeyBundle();
|
|
|
|
}).then(() => {
|
2014-01-28 17:51:09 -08:00
|
|
|
this._shouldHaveSyncKeyBundle = true; // and we should actually have one...
|
|
|
|
this.whenReadyToAuthenticate.resolve();
|
|
|
|
this._log.info("Background fetch for key bundle done");
|
2014-03-06 20:41:32 -08:00
|
|
|
Weave.Status.login = LOGIN_SUCCEEDED;
|
2014-01-30 19:02:46 -08:00
|
|
|
if (isInitialSync) {
|
|
|
|
this._log.info("Doing initial sync actions");
|
2014-01-30 20:19:48 -08:00
|
|
|
Svc.Prefs.set("firstSync", "resetClient");
|
2014-01-30 19:02:46 -08:00
|
|
|
Services.obs.notifyObservers(null, "weave:service:setup-complete", null);
|
|
|
|
Weave.Utils.nextTick(Weave.Service.sync, Weave.Service);
|
|
|
|
}
|
2014-01-28 17:51:09 -08:00
|
|
|
}).then(null, err => {
|
|
|
|
this._shouldHaveSyncKeyBundle = true; // but we probably don't have one...
|
|
|
|
this.whenReadyToAuthenticate.reject(err);
|
|
|
|
// report what failed...
|
2014-03-02 14:55:30 -08:00
|
|
|
this._log.error("Background fetch for key bundle failed: " + err);
|
2014-01-28 17:51:09 -08:00
|
|
|
});
|
|
|
|
// and we are done - the fetch continues on in the background...
|
|
|
|
}).then(null, err => {
|
2014-03-02 14:55:30 -08:00
|
|
|
this._log.error("Processing logged in account: " + err);
|
2014-01-28 17:51:09 -08:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2014-03-06 20:41:33 -08:00
|
|
|
_updateSignedInUser: function(userData) {
|
|
|
|
// This object should only ever be used for a single user. It is an
|
|
|
|
// error to update the data if the user changes (but updates are still
|
|
|
|
// necessary, as each call may add more attributes to the user).
|
|
|
|
// We start with no user, so an initial update is always ok.
|
|
|
|
if (this._signedInUser && this._signedInUser.email != userData.email) {
|
|
|
|
throw new Error("Attempting to update to a different user.")
|
|
|
|
}
|
|
|
|
this._signedInUser = userData;
|
|
|
|
},
|
|
|
|
|
|
|
|
logout: function() {
|
|
|
|
// This will be called when sync fails (or when the account is being
|
|
|
|
// unlinked etc). It may have failed because we got a 401 from a sync
|
|
|
|
// server, so we nuke the token. Next time sync runs and wants an
|
|
|
|
// authentication header, we will notice the lack of the token and fetch a
|
|
|
|
// new one.
|
|
|
|
this._token = null;
|
|
|
|
},
|
|
|
|
|
2014-01-28 17:51:09 -08:00
|
|
|
observe: function (subject, topic, data) {
|
2014-02-04 21:09:54 -08:00
|
|
|
this._log.debug("observed " + topic);
|
2014-01-28 17:51:09 -08:00
|
|
|
switch (topic) {
|
|
|
|
case fxAccountsCommon.ONLOGIN_NOTIFICATION:
|
2014-03-06 20:41:33 -08:00
|
|
|
// This should only happen if we've been initialized without a current
|
|
|
|
// user - otherwise we'd have seen the LOGOUT notification and been
|
|
|
|
// thrown away.
|
|
|
|
// The exception is when we've initialized with a user that needs to
|
|
|
|
// reauth with the server - in that case we will also get here, but
|
|
|
|
// should have the same identity.
|
|
|
|
// initializeWithCurrentIdentity will throw and log if these contraints
|
|
|
|
// aren't met, so just go ahead and do the init.
|
2014-01-30 19:02:46 -08:00
|
|
|
this.initializeWithCurrentIdentity(true);
|
2014-01-28 17:51:09 -08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case fxAccountsCommon.ONLOGOUT_NOTIFICATION:
|
2014-03-06 20:41:33 -08:00
|
|
|
Weave.Service.startOver();
|
|
|
|
// startOver will cause this instance to be thrown away, so there's
|
|
|
|
// nothing else to do.
|
2014-01-31 15:26:20 -08:00
|
|
|
break;
|
2014-01-28 17:51:09 -08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-03-06 20:41:33 -08:00
|
|
|
/**
|
2014-01-29 15:02:09 -08:00
|
|
|
* Compute the sha256 of the message bytes. Return bytes.
|
|
|
|
*/
|
|
|
|
_sha256: function(message) {
|
|
|
|
let hasher = Cc["@mozilla.org/security/hash;1"]
|
|
|
|
.createInstance(Ci.nsICryptoHash);
|
|
|
|
hasher.init(hasher.SHA256);
|
|
|
|
return CryptoUtils.digestBytes(message, hasher);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compute the X-Client-State header given the byte string kB.
|
|
|
|
*
|
|
|
|
* Return string: hex(first16Bytes(sha256(kBbytes)))
|
|
|
|
*/
|
|
|
|
_computeXClientState: function(kBbytes) {
|
|
|
|
return CommonUtils.bytesAsHex(this._sha256(kBbytes).slice(0, 16), false);
|
|
|
|
},
|
|
|
|
|
2013-12-16 20:45:03 -08:00
|
|
|
/**
|
|
|
|
* Provide override point for testing token expiration.
|
|
|
|
*/
|
|
|
|
_now: function() {
|
2014-01-31 16:43:36 -08:00
|
|
|
return this._fxaService.now()
|
2014-01-23 18:04:38 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
get _localtimeOffsetMsec() {
|
2014-01-31 16:43:36 -08:00
|
|
|
return this._fxaService.localtimeOffsetMsec;
|
2013-12-16 20:45:03 -08:00
|
|
|
},
|
|
|
|
|
2014-03-06 20:41:33 -08:00
|
|
|
usernameFromAccount: function(val) {
|
|
|
|
// we don't differentiate between "username" and "account"
|
|
|
|
return val;
|
2013-10-02 14:48:08 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-12-16 20:45:03 -08:00
|
|
|
* Obtains the HTTP Basic auth password.
|
|
|
|
*
|
|
|
|
* Returns a string if set or null if it is not set.
|
2013-10-02 14:48:08 -07:00
|
|
|
*/
|
2013-12-16 20:45:03 -08:00
|
|
|
get basicPassword() {
|
|
|
|
this._log.error("basicPassword getter should be not used in BrowserIDManager");
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the HTTP basic password to use.
|
|
|
|
*
|
|
|
|
* Changes will not persist unless persistSyncCredentials() is called.
|
|
|
|
*/
|
|
|
|
set basicPassword(value) {
|
2013-12-19 20:57:25 -08:00
|
|
|
throw "basicPassword setter should be not used in BrowserIDManager";
|
2013-12-16 20:45:03 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Obtain the Sync Key.
|
|
|
|
*
|
|
|
|
* This returns a 26 character "friendly" Base32 encoded string on success or
|
|
|
|
* null if no Sync Key could be found.
|
|
|
|
*
|
|
|
|
* If the Sync Key hasn't been set in this session, this will look in the
|
|
|
|
* password manager for the sync key.
|
|
|
|
*/
|
|
|
|
get syncKey() {
|
|
|
|
if (this.syncKeyBundle) {
|
|
|
|
// TODO: This is probably fine because the code shouldn't be
|
|
|
|
// using the sync key directly (it should use the sync key
|
|
|
|
// bundle), but I don't like it. We should probably refactor
|
|
|
|
// code that is inspecting this to not do validation on this
|
|
|
|
// field directly and instead call a isSyncKeyValid() function
|
|
|
|
// that we can override.
|
|
|
|
return "99999999999999999999999999";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
set syncKey(value) {
|
2013-12-19 20:57:25 -08:00
|
|
|
throw "syncKey setter should be not used in BrowserIDManager";
|
2013-12-16 20:45:03 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
get syncKeyBundle() {
|
|
|
|
return this._syncKeyBundle;
|
|
|
|
},
|
|
|
|
|
2013-12-19 20:57:25 -08:00
|
|
|
/**
|
|
|
|
* Resets/Drops all credentials we hold for the current user.
|
|
|
|
*/
|
|
|
|
resetCredentials: function() {
|
|
|
|
this.resetSyncKey();
|
2014-03-06 20:41:33 -08:00
|
|
|
this._token = null;
|
2013-12-19 20:57:25 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resets/Drops the sync key we hold for the current user.
|
|
|
|
*/
|
|
|
|
resetSyncKey: function() {
|
|
|
|
this._syncKey = null;
|
|
|
|
this._syncKeyBundle = null;
|
|
|
|
this._syncKeyUpdated = true;
|
2014-01-28 17:51:09 -08:00
|
|
|
this._shouldHaveSyncKeyBundle = false;
|
2013-12-19 20:57:25 -08:00
|
|
|
},
|
|
|
|
|
2013-12-16 20:45:03 -08:00
|
|
|
/**
|
|
|
|
* The current state of the auth credentials.
|
|
|
|
*
|
|
|
|
* This essentially validates that enough credentials are available to use
|
|
|
|
* Sync.
|
|
|
|
*/
|
|
|
|
get currentAuthState() {
|
2014-03-06 20:41:32 -08:00
|
|
|
if (this._authFailureReason) {
|
|
|
|
this._log.info("currentAuthState returning " + this._authFailureReason +
|
|
|
|
" due to previous failure");
|
|
|
|
return this._authFailureReason;
|
|
|
|
}
|
2013-12-16 20:45:03 -08:00
|
|
|
// TODO: need to revisit this. Currently this isn't ready to go until
|
|
|
|
// both the username and syncKeyBundle are both configured and having no
|
|
|
|
// username seems to make things fail fast so that's good.
|
|
|
|
if (!this.username) {
|
|
|
|
return LOGIN_FAILED_NO_USERNAME;
|
|
|
|
}
|
|
|
|
|
2013-12-19 20:57:25 -08:00
|
|
|
// No need to check this.syncKey as our getter for that attribute
|
|
|
|
// uses this.syncKeyBundle
|
2014-01-28 17:51:09 -08:00
|
|
|
// If bundle creation started, but failed.
|
|
|
|
if (this._shouldHaveSyncKeyBundle && !this.syncKeyBundle) {
|
2013-12-19 20:57:25 -08:00
|
|
|
return LOGIN_FAILED_NO_PASSPHRASE;
|
2013-12-16 20:45:03 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return STATUS_OK;
|
2013-10-02 14:48:08 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2014-03-06 20:41:33 -08:00
|
|
|
* Do we have a non-null, not yet expired token for the user currently
|
|
|
|
* signed in?
|
2013-10-02 14:48:08 -07:00
|
|
|
*/
|
|
|
|
hasValidToken: function() {
|
|
|
|
if (!this._token) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (this._token.expiration < this._now()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
2014-01-28 17:51:09 -08:00
|
|
|
_fetchSyncKeyBundle: function() {
|
|
|
|
// Fetch a sync token for the logged in user from the token server.
|
2014-01-30 19:02:46 -08:00
|
|
|
return this._fxaService.getKeys().then(userData => {
|
2014-03-06 20:41:33 -08:00
|
|
|
this._updateSignedInUser(userData); // throws if the user changed.
|
|
|
|
return this._fetchTokenForUser().then(token => {
|
2014-01-30 19:02:46 -08:00
|
|
|
this._token = token;
|
|
|
|
// both Jelly and FxAccounts give us kA/kB as hex.
|
|
|
|
let kB = Utils.hexToBytes(userData.kB);
|
|
|
|
this._syncKeyBundle = deriveKeyBundle(kB);
|
|
|
|
return;
|
|
|
|
});
|
2013-12-16 20:45:03 -08:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2014-03-06 20:41:33 -08:00
|
|
|
// Refresh the sync token for our user.
|
|
|
|
_fetchTokenForUser: function() {
|
2013-12-16 20:45:03 -08:00
|
|
|
let tokenServerURI = Svc.Prefs.get("tokenServerURI");
|
|
|
|
let log = this._log;
|
|
|
|
let client = this._tokenServerClient;
|
2014-02-04 03:03:18 -08:00
|
|
|
let fxa = this._fxaService;
|
2014-03-06 20:41:33 -08:00
|
|
|
let userData = this._signedInUser;
|
2014-01-29 15:02:09 -08:00
|
|
|
|
|
|
|
// Both Jelly and FxAccounts give us kB as hex
|
|
|
|
let kBbytes = CommonUtils.hexToBytes(userData.kB);
|
|
|
|
let headers = {"X-Client-State": this._computeXClientState(kBbytes)};
|
2014-02-04 21:09:54 -08:00
|
|
|
log.info("Fetching assertion and token from: " + tokenServerURI);
|
2013-10-02 14:48:08 -07:00
|
|
|
|
2013-12-16 20:45:03 -08:00
|
|
|
function getToken(tokenServerURI, assertion) {
|
2014-02-04 21:09:54 -08:00
|
|
|
log.debug("Getting a token");
|
2013-12-16 20:45:03 -08:00
|
|
|
let deferred = Promise.defer();
|
|
|
|
let cb = function (err, token) {
|
|
|
|
if (err) {
|
2014-03-06 20:41:32 -08:00
|
|
|
log.info("TokenServerClient.getTokenFromBrowserIDAssertion() failed with: " + err);
|
|
|
|
if (err.response && err.response.status === 401) {
|
|
|
|
err = new AuthenticationError(err);
|
|
|
|
}
|
|
|
|
return deferred.reject(err);
|
2013-12-16 20:45:03 -08:00
|
|
|
} else {
|
2014-02-04 21:09:54 -08:00
|
|
|
log.debug("Successfully got a sync token");
|
2013-12-16 20:45:03 -08:00
|
|
|
return deferred.resolve(token);
|
|
|
|
}
|
|
|
|
};
|
2014-01-29 15:02:09 -08:00
|
|
|
|
|
|
|
client.getTokenFromBrowserIDAssertion(tokenServerURI, assertion, cb, headers);
|
2013-12-16 20:45:03 -08:00
|
|
|
return deferred.promise;
|
|
|
|
}
|
|
|
|
|
2014-01-31 15:26:20 -08:00
|
|
|
function getAssertion() {
|
2014-02-04 21:09:54 -08:00
|
|
|
log.debug("Getting an assertion");
|
2014-01-31 15:26:20 -08:00
|
|
|
let audience = Services.io.newURI(tokenServerURI, null, null).prePath;
|
2014-02-04 03:03:18 -08:00
|
|
|
return fxa.getAssertion(audience).then(null, err => {
|
2014-03-06 20:41:32 -08:00
|
|
|
log.error("fxa.getAssertion() failed with: " + err.code + " - " + err.message);
|
2014-01-31 15:26:20 -08:00
|
|
|
if (err.code === 401) {
|
|
|
|
throw new AuthenticationError("Unable to get assertion for user");
|
|
|
|
} else {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-12-16 20:45:03 -08:00
|
|
|
// wait until the account email is verified and we know that
|
|
|
|
// getAssertion() will return a real assertion (not null).
|
2014-03-06 20:41:33 -08:00
|
|
|
return fxa.whenVerified(this._signedInUser)
|
2014-01-31 15:26:20 -08:00
|
|
|
.then(() => getAssertion())
|
2013-12-16 20:45:03 -08:00
|
|
|
.then(assertion => getToken(tokenServerURI, assertion))
|
|
|
|
.then(token => {
|
2014-01-31 15:26:20 -08:00
|
|
|
// TODO: Make it be only 80% of the duration, so refresh the token
|
|
|
|
// before it actually expires. This is to avoid sync storage errors
|
|
|
|
// otherwise, we get a nasty notification bar briefly. Bug 966568.
|
|
|
|
token.expiration = this._now() + (token.duration * 1000) * 0.80;
|
2013-12-16 20:45:03 -08:00
|
|
|
return token;
|
2014-01-30 19:02:46 -08:00
|
|
|
})
|
|
|
|
.then(null, err => {
|
2014-01-31 15:26:20 -08:00
|
|
|
// TODO: write tests to make sure that different auth error cases are handled here
|
|
|
|
// properly: auth error getting assertion, auth error getting token (invalid generation
|
|
|
|
// and client-state error)
|
|
|
|
if (err instanceof AuthenticationError) {
|
2014-03-02 14:55:30 -08:00
|
|
|
this._log.error("Authentication error in _fetchTokenForUser: " + err);
|
2014-03-06 20:41:32 -08:00
|
|
|
// set it to the "fatal" LOGIN_FAILED_LOGIN_REJECTED reason.
|
|
|
|
this._authFailureReason = LOGIN_FAILED_LOGIN_REJECTED;
|
|
|
|
} else {
|
|
|
|
this._log.error("Non-authentication error in _fetchTokenForUser: " + err.message);
|
|
|
|
// for now assume it is just a transient network related problem.
|
|
|
|
this._authFailureReason = LOGIN_FAILED_NETWORK_ERROR;
|
2014-01-31 15:26:20 -08:00
|
|
|
}
|
2014-03-06 20:41:32 -08:00
|
|
|
// Drop the sync key bundle, but still expect to have one.
|
|
|
|
// This will arrange for us to be in the right 'currentAuthState'
|
|
|
|
// such that UI will show the right error.
|
|
|
|
this._shouldHaveSyncKeyBundle = true;
|
|
|
|
Weave.Status.login = this._authFailureReason;
|
2014-03-10 11:54:46 -07:00
|
|
|
Services.obs.notifyObservers(null, "weave:service:login:error", null);
|
2014-01-31 15:26:20 -08:00
|
|
|
throw err;
|
2014-01-30 19:02:46 -08:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2014-03-06 20:41:33 -08:00
|
|
|
// Returns a promise that is resolved when we have a valid token for the
|
|
|
|
// current user stored in this._token. When resolved, this._token is valid.
|
|
|
|
_ensureValidToken: function() {
|
|
|
|
if (this.hasValidToken()) {
|
|
|
|
return Promise.resolve();
|
2014-01-30 19:02:46 -08:00
|
|
|
}
|
2014-03-06 20:41:33 -08:00
|
|
|
return this._fetchTokenForUser().then(
|
|
|
|
token => {
|
|
|
|
this._token = token;
|
|
|
|
}
|
|
|
|
);
|
2013-10-02 14:48:08 -07:00
|
|
|
},
|
|
|
|
|
2013-12-16 20:45:03 -08:00
|
|
|
getResourceAuthenticator: function () {
|
2013-10-02 14:48:08 -07:00
|
|
|
return this._getAuthenticationHeader.bind(this);
|
|
|
|
},
|
|
|
|
|
2013-12-16 20:45:03 -08:00
|
|
|
/**
|
|
|
|
* Obtain a function to be used for adding auth to RESTRequest instances.
|
|
|
|
*/
|
|
|
|
getRESTRequestAuthenticator: function() {
|
|
|
|
return this._addAuthenticationHeader.bind(this);
|
|
|
|
},
|
|
|
|
|
2013-10-02 14:48:08 -07:00
|
|
|
/**
|
|
|
|
* @return a Hawk HTTP Authorization Header, lightly wrapped, for the .uri
|
|
|
|
* of a RESTRequest or AsyncResponse object.
|
|
|
|
*/
|
|
|
|
_getAuthenticationHeader: function(httpObject, method) {
|
2014-03-06 20:41:33 -08:00
|
|
|
let cb = Async.makeSpinningCallback();
|
|
|
|
this._ensureValidToken().then(cb, cb);
|
|
|
|
try {
|
|
|
|
cb.wait();
|
|
|
|
} catch (ex) {
|
|
|
|
this._log.error("Failed to fetch a token for authentication: " + ex);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (!this._token) {
|
|
|
|
return null;
|
2013-10-02 14:48:08 -07:00
|
|
|
}
|
|
|
|
let credentials = {algorithm: "sha256",
|
2013-12-16 20:45:03 -08:00
|
|
|
id: this._token.id,
|
|
|
|
key: this._token.key,
|
2013-10-02 14:48:08 -07:00
|
|
|
};
|
|
|
|
method = method || httpObject.method;
|
2014-01-23 18:04:38 -08:00
|
|
|
|
|
|
|
// Get the local clock offset from the Firefox Accounts server. This should
|
|
|
|
// be close to the offset from the storage server.
|
|
|
|
let options = {
|
|
|
|
now: this._now(),
|
|
|
|
localtimeOffsetMsec: this._localtimeOffsetMsec,
|
|
|
|
credentials: credentials,
|
|
|
|
};
|
|
|
|
|
|
|
|
let headerValue = CryptoUtils.computeHAWK(httpObject.uri, method, options);
|
2013-10-02 14:48:08 -07:00
|
|
|
return {headers: {authorization: headerValue.field}};
|
|
|
|
},
|
|
|
|
|
|
|
|
_addAuthenticationHeader: function(request, method) {
|
|
|
|
let header = this._getAuthenticationHeader(request, method);
|
|
|
|
if (!header) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
request.setHeader("authorization", header.headers.authorization);
|
|
|
|
return request;
|
2014-01-28 17:51:09 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
createClusterManager: function(service) {
|
|
|
|
return new BrowserIDClusterManager(service);
|
2013-10-02 14:48:08 -07:00
|
|
|
}
|
2014-01-28 17:51:09 -08:00
|
|
|
|
2013-10-02 14:48:08 -07:00
|
|
|
};
|
2014-01-28 17:51:09 -08:00
|
|
|
|
|
|
|
/* An implementation of the ClusterManager for this identity
|
|
|
|
*/
|
|
|
|
|
|
|
|
function BrowserIDClusterManager(service) {
|
|
|
|
ClusterManager.call(this, service);
|
|
|
|
}
|
|
|
|
|
|
|
|
BrowserIDClusterManager.prototype = {
|
|
|
|
__proto__: ClusterManager.prototype,
|
|
|
|
|
|
|
|
_findCluster: function() {
|
2014-03-06 20:41:33 -08:00
|
|
|
let endPointFromIdentityToken = function() {
|
|
|
|
let endpoint = this.identity._token.endpoint;
|
|
|
|
// For Sync 1.5 storage endpoints, we use the base endpoint verbatim.
|
|
|
|
// However, it should end in "/" because we will extend it with
|
|
|
|
// well known path components. So we add a "/" if it's missing.
|
|
|
|
if (!endpoint.endsWith("/")) {
|
|
|
|
endpoint += "/";
|
|
|
|
}
|
|
|
|
return endpoint;
|
|
|
|
}.bind(this);
|
|
|
|
|
|
|
|
// Spinningly ensure we are ready to authenticate and have a valid token.
|
2014-01-28 17:51:09 -08:00
|
|
|
let promiseClusterURL = function() {
|
2014-03-06 20:41:33 -08:00
|
|
|
return this.identity.whenReadyToAuthenticate.promise.then(
|
|
|
|
() => this.identity._ensureValidToken()
|
|
|
|
).then(
|
|
|
|
() => endPointFromIdentityToken()
|
|
|
|
);
|
2014-01-28 17:51:09 -08:00
|
|
|
}.bind(this);
|
|
|
|
|
|
|
|
let cb = Async.makeSpinningCallback();
|
|
|
|
promiseClusterURL().then(function (clusterURL) {
|
2014-03-06 20:41:33 -08:00
|
|
|
cb(null, clusterURL);
|
|
|
|
}).then(null, cb);
|
2014-01-28 17:51:09 -08:00
|
|
|
return cb.wait();
|
|
|
|
},
|
2014-01-29 19:26:01 -08:00
|
|
|
|
|
|
|
getUserBaseURL: function() {
|
|
|
|
// Legacy Sync and FxA Sync construct the userBaseURL differently. Legacy
|
|
|
|
// Sync appends path components onto an empty path, and in FxA Sync the
|
|
|
|
// token server constructs this for us in an opaque manner. Since the
|
|
|
|
// cluster manager already sets the clusterURL on Service and also has
|
|
|
|
// access to the current identity, we added this functionality here.
|
|
|
|
return this.service.clusterURL;
|
|
|
|
}
|
2014-01-28 17:51:09 -08:00
|
|
|
}
|