2013-12-14 03:31:23 +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/. */
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
this.EXPORTED_SYMBOLS = ["FirefoxAccounts"];
|
|
|
|
|
|
|
|
const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
|
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/Log.jsm");
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
Cu.import("resource://gre/modules/identity/LogUtils.jsm");
|
|
|
|
|
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "objectCopy",
|
|
|
|
"resource://gre/modules/identity/IdentityUtils.jsm");
|
|
|
|
|
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "makeMessageObject",
|
|
|
|
"resource://gre/modules/identity/IdentityUtils.jsm");
|
|
|
|
|
|
|
|
// loglevel preference should be one of: "FATAL", "ERROR", "WARN", "INFO",
|
|
|
|
// "CONFIG", "DEBUG", "TRACE" or "ALL". We will be logging error messages by
|
|
|
|
// default.
|
|
|
|
const PREF_LOG_LEVEL = "identity.fxaccounts.loglevel";
|
|
|
|
try {
|
|
|
|
this.LOG_LEVEL =
|
|
|
|
Services.prefs.getPrefType(PREF_LOG_LEVEL) == Ci.nsIPrefBranch.PREF_STRING
|
|
|
|
&& Services.prefs.getCharPref(PREF_LOG_LEVEL);
|
|
|
|
} catch (e) {
|
|
|
|
this.LOG_LEVEL = Log.Level.Error;
|
|
|
|
}
|
|
|
|
|
|
|
|
let log = Log.repository.getLogger("Identity.FxAccounts");
|
|
|
|
log.level = LOG_LEVEL;
|
|
|
|
log.addAppender(new Log.ConsoleAppender(new Log.BasicFormatter()));
|
|
|
|
|
|
|
|
#ifdef MOZ_B2G
|
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "FxAccountsManager",
|
|
|
|
"resource://gre/modules/FxAccountsManager.jsm",
|
|
|
|
"FxAccountsManager");
|
2014-06-06 21:42:22 +04:00
|
|
|
Cu.import("resource://gre/modules/FxAccountsCommon.js");
|
2013-12-14 03:31:23 +04:00
|
|
|
#else
|
|
|
|
log.warn("The FxAccountsManager is only functional in B2G at this time.");
|
|
|
|
var FxAccountsManager = null;
|
2014-06-06 21:42:22 +04:00
|
|
|
var ONVERIFIED_NOTIFICATION = null;
|
2014-06-26 21:04:03 +04:00
|
|
|
var ONLOGIN_NOTIFICATION = null;
|
2014-06-17 22:28:25 +04:00
|
|
|
var ONLOGOUT_NOTIFICATION = null;
|
2013-12-14 03:31:23 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
function FxAccountsService() {
|
|
|
|
Services.obs.addObserver(this, "quit-application-granted", false);
|
2014-06-06 21:42:22 +04:00
|
|
|
if (ONVERIFIED_NOTIFICATION) {
|
|
|
|
Services.obs.addObserver(this, ONVERIFIED_NOTIFICATION, false);
|
2014-06-26 21:04:03 +04:00
|
|
|
Services.obs.addObserver(this, ONLOGIN_NOTIFICATION, false);
|
2014-06-17 22:28:25 +04:00
|
|
|
Services.obs.addObserver(this, ONLOGOUT_NOTIFICATION, false);
|
2014-06-06 21:42:22 +04:00
|
|
|
}
|
2013-12-14 03:31:23 +04:00
|
|
|
|
|
|
|
// Maintain interface parity with Identity.jsm and MinimalIdentity.jsm
|
|
|
|
this.RP = this;
|
|
|
|
|
|
|
|
this._rpFlows = new Map();
|
|
|
|
|
|
|
|
// Enable us to mock FxAccountsManager service in testing
|
|
|
|
this.fxAccountsManager = FxAccountsManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
FxAccountsService.prototype = {
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports, Ci.nsIObserver]),
|
|
|
|
|
|
|
|
observe: function observe(aSubject, aTopic, aData) {
|
|
|
|
switch (aTopic) {
|
2014-06-06 21:42:22 +04:00
|
|
|
case null:
|
2014-06-26 21:04:03 +04:00
|
|
|
// Guard against matching null ON*_NOTIFICATION
|
2014-06-06 21:42:22 +04:00
|
|
|
break;
|
|
|
|
case ONVERIFIED_NOTIFICATION:
|
|
|
|
log.debug("Received " + ONVERIFIED_NOTIFICATION + "; firing request()s");
|
|
|
|
for (let [rpId,] of this._rpFlows) {
|
|
|
|
this.request(rpId);
|
|
|
|
}
|
|
|
|
break;
|
2014-06-26 21:04:03 +04:00
|
|
|
case ONLOGIN_NOTIFICATION:
|
|
|
|
log.debug("Received " + ONLOGIN_NOTIFICATION + "; doLogin()s fired");
|
|
|
|
for (let [rpId,] of this._rpFlows) {
|
|
|
|
this.request(rpId);
|
|
|
|
}
|
|
|
|
break;
|
2014-06-17 22:28:25 +04:00
|
|
|
case ONLOGOUT_NOTIFICATION:
|
|
|
|
log.debug("Received " + ONLOGOUT_NOTIFICATION + "; doLogout()s fired");
|
|
|
|
for (let [rpId,] of this._rpFlows) {
|
|
|
|
this.doLogout(rpId);
|
|
|
|
}
|
|
|
|
break;
|
2013-12-14 03:31:23 +04:00
|
|
|
case "quit-application-granted":
|
|
|
|
Services.obs.removeObserver(this, "quit-application-granted");
|
2014-06-06 21:42:22 +04:00
|
|
|
if (ONVERIFIED_NOTIFICATION) {
|
|
|
|
Services.obs.removeObserver(this, ONVERIFIED_NOTIFICATION);
|
2014-06-26 21:04:03 +04:00
|
|
|
Services.obs.removeObserver(this, ONLOGIN_NOTIFICATION);
|
|
|
|
Services.obs.removeObserver(this, ONLOGOUT_NOTIFICATION);
|
2014-06-06 21:42:22 +04:00
|
|
|
}
|
2013-12-14 03:31:23 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-07-11 18:13:32 +04:00
|
|
|
cleanupRPRequest: function(aRp) {
|
|
|
|
aRp.pendingRequest = false;
|
|
|
|
this._rpFlows.set(aRp.id, aRp);
|
|
|
|
},
|
|
|
|
|
2013-12-14 03:31:23 +04:00
|
|
|
/**
|
|
|
|
* Register a listener for a given windowID as a result of a call to
|
|
|
|
* navigator.id.watch().
|
|
|
|
*
|
2014-07-11 18:13:32 +04:00
|
|
|
* @param aRPCaller
|
2013-12-14 03:31:23 +04:00
|
|
|
* (Object) an object that represents the caller document, and
|
|
|
|
* is expected to have properties:
|
|
|
|
* - id (unique, e.g. uuid)
|
|
|
|
* - origin (string)
|
|
|
|
*
|
|
|
|
* and a bunch of callbacks
|
|
|
|
* - doReady()
|
|
|
|
* - doLogin()
|
|
|
|
* - doLogout()
|
|
|
|
* - doError()
|
|
|
|
* - doCancel()
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
watch: function watch(aRpCaller) {
|
|
|
|
this._rpFlows.set(aRpCaller.id, aRpCaller);
|
2014-03-12 04:49:26 +04:00
|
|
|
log.debug("watch: " + aRpCaller.id);
|
2013-12-14 03:31:23 +04:00
|
|
|
log.debug("Current rp flows: " + this._rpFlows.size);
|
|
|
|
|
2014-03-12 04:49:26 +04:00
|
|
|
// Log the user in, if possible, and then call ready().
|
2013-12-14 03:31:23 +04:00
|
|
|
let runnable = {
|
|
|
|
run: () => {
|
2014-07-11 18:13:32 +04:00
|
|
|
this.fxAccountsManager.getAssertion(aRpCaller.audience,
|
|
|
|
aRpCaller.principal,
|
|
|
|
{ silent:true }).then(
|
2014-03-12 04:49:26 +04:00
|
|
|
data => {
|
|
|
|
if (data) {
|
|
|
|
this.doLogin(aRpCaller.id, data);
|
|
|
|
} else {
|
|
|
|
this.doLogout(aRpCaller.id);
|
|
|
|
}
|
|
|
|
this.doReady(aRpCaller.id);
|
|
|
|
},
|
|
|
|
error => {
|
|
|
|
log.error("get silent assertion failed: " + JSON.stringify(error));
|
2014-04-24 02:45:06 +04:00
|
|
|
this.doError(aRpCaller.id, error);
|
2014-03-12 04:49:26 +04:00
|
|
|
}
|
|
|
|
);
|
2013-12-14 03:31:23 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
Services.tm.currentThread.dispatch(runnable,
|
|
|
|
Ci.nsIThread.DISPATCH_NORMAL);
|
|
|
|
},
|
|
|
|
|
2014-03-12 04:49:26 +04:00
|
|
|
/**
|
|
|
|
* Delete the flow when the screen is unloaded
|
|
|
|
*/
|
|
|
|
unwatch: function(aRpCallerId, aTargetMM) {
|
|
|
|
log.debug("unwatching: " + aRpCallerId);
|
|
|
|
this._rpFlows.delete(aRpCallerId);
|
2013-12-14 03:31:23 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initiate a login with user interaction as a result of a call to
|
|
|
|
* navigator.id.request().
|
|
|
|
*
|
|
|
|
* @param aRPId
|
2014-07-11 18:13:32 +04:00
|
|
|
* (integer) the id of the doc object obtained in .watch()
|
2013-12-14 03:31:23 +04:00
|
|
|
*
|
|
|
|
* @param aOptions
|
2014-07-11 18:13:32 +04:00
|
|
|
* (Object) options including privacyPolicy, termsOfService
|
2013-12-14 03:31:23 +04:00
|
|
|
*/
|
|
|
|
request: function request(aRPId, aOptions) {
|
|
|
|
aOptions = aOptions || {};
|
|
|
|
let rp = this._rpFlows.get(aRPId);
|
|
|
|
if (!rp) {
|
|
|
|
log.error("request() called before watch()");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-11 18:13:32 +04:00
|
|
|
// We check if we already have a pending request for this RP and in that
|
|
|
|
// case we just bail out. We don't want duplicated onlogin or oncancel
|
|
|
|
// events.
|
|
|
|
if (rp.pendingRequest) {
|
|
|
|
log.debug("request() already called");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, we set the RP flow with the pending request flag.
|
|
|
|
rp.pendingRequest = true;
|
|
|
|
this._rpFlows.set(rp.id, rp);
|
|
|
|
|
2013-12-14 03:31:23 +04:00
|
|
|
let options = makeMessageObject(rp);
|
|
|
|
objectCopy(aOptions, options);
|
|
|
|
|
|
|
|
log.debug("get assertion for " + rp.audience);
|
|
|
|
|
2014-07-11 18:13:32 +04:00
|
|
|
this.fxAccountsManager.getAssertion(rp.audience, rp.principal, options)
|
|
|
|
.then(
|
2013-12-14 03:31:23 +04:00
|
|
|
data => {
|
2014-02-28 03:06:00 +04:00
|
|
|
log.debug("got assertion for " + rp.audience + ": " + data);
|
2013-12-14 03:31:23 +04:00
|
|
|
this.doLogin(aRPId, data);
|
|
|
|
},
|
|
|
|
error => {
|
2014-07-11 05:24:46 +04:00
|
|
|
log.debug("get assertion failed: " + JSON.stringify(error));
|
2014-05-14 00:56:30 +04:00
|
|
|
// Cancellation is passed through an error channel; here we reroute.
|
2014-07-11 05:24:46 +04:00
|
|
|
if ((error.error && (error.error.details == "DIALOG_CLOSED_BY_USER")) ||
|
|
|
|
(error.details == "DIALOG_CLOSED_BY_USER")) {
|
2014-05-14 00:56:30 +04:00
|
|
|
return this.doCancel(aRPId);
|
|
|
|
}
|
2014-01-22 03:57:50 +04:00
|
|
|
this.doError(aRPId, error);
|
2013-12-14 03:31:23 +04:00
|
|
|
}
|
2014-07-11 18:13:32 +04:00
|
|
|
)
|
|
|
|
.then(
|
|
|
|
() => {
|
|
|
|
this.cleanupRPRequest(rp);
|
|
|
|
}
|
|
|
|
)
|
|
|
|
.catch(
|
|
|
|
() => {
|
|
|
|
this.cleanupRPRequest(rp);
|
|
|
|
}
|
2013-12-14 03:31:23 +04:00
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invoked when a user wishes to logout of a site (for instance, when clicking
|
|
|
|
* on an in-content logout button).
|
|
|
|
*
|
|
|
|
* @param aRpCallerId
|
|
|
|
* (integer) the id of the doc object obtained in .watch()
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
logout: function logout(aRpCallerId) {
|
|
|
|
// XXX Bug 945363 - Resolve the SSO story for FXA and implement
|
|
|
|
// logout accordingly.
|
|
|
|
//
|
|
|
|
// For now, it makes no sense to logout from a specific RP in
|
|
|
|
// Firefox Accounts, so just directly call the logout callback.
|
|
|
|
if (!this._rpFlows.has(aRpCallerId)) {
|
|
|
|
log.error("logout() called before watch()");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call logout() on the next tick
|
|
|
|
let runnable = {
|
|
|
|
run: () => {
|
2014-03-12 04:49:26 +04:00
|
|
|
this.fxAccountsManager.signOut().then(() => {
|
|
|
|
this.doLogout(aRpCallerId);
|
|
|
|
});
|
2013-12-14 03:31:23 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
Services.tm.currentThread.dispatch(runnable,
|
|
|
|
Ci.nsIThread.DISPATCH_NORMAL);
|
|
|
|
},
|
|
|
|
|
|
|
|
childProcessShutdown: function childProcessShutdown(messageManager) {
|
|
|
|
for (let [key,] of this._rpFlows) {
|
|
|
|
if (this._rpFlows.get(key)._mm === messageManager) {
|
|
|
|
this._rpFlows.delete(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
doLogin: function doLogin(aRpCallerId, aAssertion) {
|
|
|
|
let rp = this._rpFlows.get(aRpCallerId);
|
|
|
|
if (!rp) {
|
2014-05-14 00:56:30 +04:00
|
|
|
log.warn("doLogin found no rp to go with callerId " + aRpCallerId);
|
2013-12-14 03:31:23 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
rp.doLogin(aAssertion);
|
|
|
|
},
|
|
|
|
|
|
|
|
doLogout: function doLogout(aRpCallerId) {
|
|
|
|
let rp = this._rpFlows.get(aRpCallerId);
|
|
|
|
if (!rp) {
|
2014-05-14 00:56:30 +04:00
|
|
|
log.warn("doLogout found no rp to go with callerId " + aRpCallerId);
|
2013-12-14 03:31:23 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
rp.doLogout();
|
|
|
|
},
|
|
|
|
|
|
|
|
doReady: function doReady(aRpCallerId) {
|
|
|
|
let rp = this._rpFlows.get(aRpCallerId);
|
|
|
|
if (!rp) {
|
2014-05-14 00:56:30 +04:00
|
|
|
log.warn("doReady found no rp to go with callerId " + aRpCallerId);
|
2013-12-14 03:31:23 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
rp.doReady();
|
|
|
|
},
|
|
|
|
|
|
|
|
doCancel: function doCancel(aRpCallerId) {
|
|
|
|
let rp = this._rpFlows.get(aRpCallerId);
|
|
|
|
if (!rp) {
|
2014-05-14 00:56:30 +04:00
|
|
|
log.warn("doCancel found no rp to go with callerId " + aRpCallerId);
|
2013-12-14 03:31:23 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
rp.doCancel();
|
|
|
|
},
|
|
|
|
|
|
|
|
doError: function doError(aRpCallerId, aError) {
|
|
|
|
let rp = this._rpFlows.get(aRpCallerId);
|
|
|
|
if (!rp) {
|
2014-05-14 00:56:30 +04:00
|
|
|
log.warn("doError found no rp to go with callerId " + aRpCallerId);
|
2013-12-14 03:31:23 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
rp.doError(aError);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.FirefoxAccounts = new FxAccountsService();
|
|
|
|
|