2012-06-29 16:12:00 -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";
|
|
|
|
|
|
|
|
const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
|
|
|
|
|
|
|
|
// This is the parent process corresponding to nsDOMIdentity.
|
2012-10-31 09:13:28 -07:00
|
|
|
this.EXPORTED_SYMBOLS = ["DOMIdentity"];
|
2012-06-29 16:12:00 -07:00
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
2012-12-05 00:25:21 -08:00
|
|
|
Cu.import("resource://gre/modules/identity/IdentityUtils.jsm");
|
2012-06-29 16:12:00 -07:00
|
|
|
|
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "IdentityService",
|
2012-10-26 04:39:38 -07:00
|
|
|
#ifdef MOZ_B2G_VERSION
|
|
|
|
"resource://gre/modules/identity/MinimalIdentity.jsm");
|
|
|
|
#else
|
2012-06-29 16:12:00 -07:00
|
|
|
"resource://gre/modules/identity/Identity.jsm");
|
2012-10-26 04:39:38 -07:00
|
|
|
#endif
|
2012-06-29 16:12:00 -07:00
|
|
|
|
|
|
|
XPCOMUtils.defineLazyModuleGetter(this,
|
|
|
|
"Logger",
|
|
|
|
"resource://gre/modules/identity/LogUtils.jsm");
|
|
|
|
|
2012-10-26 04:39:38 -07:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "ppmm",
|
|
|
|
"@mozilla.org/parentprocessmessagemanager;1",
|
|
|
|
"nsIMessageListenerManager");
|
|
|
|
|
2012-06-29 16:12:00 -07:00
|
|
|
function log(...aMessageArgs) {
|
|
|
|
Logger.log.apply(Logger, ["DOMIdentity"].concat(aMessageArgs));
|
|
|
|
}
|
|
|
|
|
2012-11-16 15:34:32 -08:00
|
|
|
function IDDOMMessage(aOptions) {
|
|
|
|
objectCopy(aOptions, this);
|
2012-06-29 16:12:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function IDPProvisioningContext(aID, aOrigin, aTargetMM) {
|
|
|
|
this._id = aID;
|
|
|
|
this._origin = aOrigin;
|
|
|
|
this._mm = aTargetMM;
|
|
|
|
}
|
|
|
|
|
|
|
|
IDPProvisioningContext.prototype = {
|
|
|
|
get id() this._id,
|
|
|
|
get origin() this._origin,
|
|
|
|
|
|
|
|
doBeginProvisioningCallback: function IDPPC_doBeginProvCB(aID, aCertDuration) {
|
2012-11-16 15:34:32 -08:00
|
|
|
let message = new IDDOMMessage({id: this.id});
|
2012-06-29 16:12:00 -07:00
|
|
|
message.identity = aID;
|
|
|
|
message.certDuration = aCertDuration;
|
|
|
|
this._mm.sendAsyncMessage("Identity:IDP:CallBeginProvisioningCallback",
|
|
|
|
message);
|
|
|
|
},
|
|
|
|
|
|
|
|
doGenKeyPairCallback: function IDPPC_doGenKeyPairCallback(aPublicKey) {
|
|
|
|
log("doGenKeyPairCallback");
|
2012-11-16 15:34:32 -08:00
|
|
|
let message = new IDDOMMessage({id: this.id});
|
2012-06-29 16:12:00 -07:00
|
|
|
message.publicKey = aPublicKey;
|
|
|
|
this._mm.sendAsyncMessage("Identity:IDP:CallGenKeyPairCallback", message);
|
|
|
|
},
|
|
|
|
|
|
|
|
doError: function(msg) {
|
|
|
|
log("Provisioning ERROR: " + msg);
|
2013-03-14 12:19:34 -07:00
|
|
|
}
|
2012-06-29 16:12:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
function IDPAuthenticationContext(aID, aOrigin, aTargetMM) {
|
|
|
|
this._id = aID;
|
|
|
|
this._origin = aOrigin;
|
|
|
|
this._mm = aTargetMM;
|
|
|
|
}
|
|
|
|
|
|
|
|
IDPAuthenticationContext.prototype = {
|
|
|
|
get id() this._id,
|
|
|
|
get origin() this._origin,
|
|
|
|
|
|
|
|
doBeginAuthenticationCallback: function IDPAC_doBeginAuthCB(aIdentity) {
|
2012-11-16 15:34:32 -08:00
|
|
|
let message = new IDDOMMessage({id: this.id});
|
2012-06-29 16:12:00 -07:00
|
|
|
message.identity = aIdentity;
|
|
|
|
this._mm.sendAsyncMessage("Identity:IDP:CallBeginAuthenticationCallback",
|
|
|
|
message);
|
|
|
|
},
|
|
|
|
|
|
|
|
doError: function IDPAC_doError(msg) {
|
|
|
|
log("Authentication ERROR: " + msg);
|
2013-03-14 12:19:34 -07:00
|
|
|
}
|
2012-06-29 16:12:00 -07:00
|
|
|
};
|
|
|
|
|
2012-11-16 15:34:32 -08:00
|
|
|
function RPWatchContext(aOptions, aTargetMM) {
|
|
|
|
objectCopy(aOptions, this);
|
|
|
|
|
|
|
|
// id and origin are required
|
|
|
|
if (! (this.id && this.origin)) {
|
|
|
|
throw new Error("id and origin are required for RP watch context");
|
|
|
|
}
|
|
|
|
|
|
|
|
// default for no loggedInUser is undefined, not null
|
|
|
|
this.loggedInUser = aOptions.loggedInUser;
|
|
|
|
|
2012-11-20 17:28:34 -08:00
|
|
|
// Maybe internal
|
|
|
|
this._internal = aOptions._internal;
|
|
|
|
|
2012-06-29 16:12:00 -07:00
|
|
|
this._mm = aTargetMM;
|
|
|
|
}
|
|
|
|
|
|
|
|
RPWatchContext.prototype = {
|
2012-11-20 17:28:34 -08:00
|
|
|
doLogin: function RPWatchContext_onlogin(aAssertion, aMaybeInternalParams) {
|
2012-06-29 16:12:00 -07:00
|
|
|
log("doLogin: " + this.id);
|
2012-11-20 17:28:34 -08:00
|
|
|
let message = new IDDOMMessage({id: this.id, assertion: aAssertion});
|
|
|
|
if (aMaybeInternalParams) {
|
|
|
|
message._internalParams = aMaybeInternalParams;
|
|
|
|
}
|
2012-06-29 16:12:00 -07:00
|
|
|
this._mm.sendAsyncMessage("Identity:RP:Watch:OnLogin", message);
|
|
|
|
},
|
|
|
|
|
|
|
|
doLogout: function RPWatchContext_onlogout() {
|
2012-11-16 15:34:32 -08:00
|
|
|
log("doLogout: " + this.id);
|
|
|
|
let message = new IDDOMMessage({id: this.id});
|
2012-06-29 16:12:00 -07:00
|
|
|
this._mm.sendAsyncMessage("Identity:RP:Watch:OnLogout", message);
|
|
|
|
},
|
|
|
|
|
|
|
|
doReady: function RPWatchContext_onready() {
|
|
|
|
log("doReady: " + this.id);
|
2012-11-16 15:34:32 -08:00
|
|
|
let message = new IDDOMMessage({id: this.id});
|
2012-06-29 16:12:00 -07:00
|
|
|
this._mm.sendAsyncMessage("Identity:RP:Watch:OnReady", message);
|
|
|
|
},
|
|
|
|
|
2013-01-10 00:56:10 -08:00
|
|
|
doCancel: function RPWatchContext_oncancel() {
|
|
|
|
log("doCancel: " + this.id);
|
|
|
|
let message = new IDDOMMessage({id: this.id});
|
|
|
|
this._mm.sendAsyncMessage("Identity:RP:Watch:OnCancel", message);
|
|
|
|
},
|
|
|
|
|
2012-06-29 16:12:00 -07:00
|
|
|
doError: function RPWatchContext_onerror(aMessage) {
|
|
|
|
log("doError: " + aMessage);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-10-31 09:13:28 -07:00
|
|
|
this.DOMIdentity = {
|
2012-08-27 07:13:02 -07:00
|
|
|
// nsIMessageListener
|
2012-06-29 16:12:00 -07:00
|
|
|
receiveMessage: function DOMIdentity_receiveMessage(aMessage) {
|
|
|
|
let msg = aMessage.json;
|
|
|
|
|
|
|
|
// Target is the frame message manager that called us and is
|
|
|
|
// used to send replies back to the proper window.
|
2012-10-26 04:39:38 -07:00
|
|
|
let targetMM = aMessage.target;
|
2012-06-29 16:12:00 -07:00
|
|
|
|
|
|
|
switch (aMessage.name) {
|
|
|
|
// RP
|
|
|
|
case "Identity:RP:Watch":
|
|
|
|
this._watch(msg, targetMM);
|
|
|
|
break;
|
2013-03-14 12:19:34 -07:00
|
|
|
case "Identity:RP:Unwatch":
|
|
|
|
this._unwatch(msg, targetMM);
|
|
|
|
break;
|
2012-06-29 16:12:00 -07:00
|
|
|
case "Identity:RP:Request":
|
2013-03-14 12:19:34 -07:00
|
|
|
this._request(msg, targetMM);
|
2012-06-29 16:12:00 -07:00
|
|
|
break;
|
|
|
|
case "Identity:RP:Logout":
|
2013-03-14 12:19:34 -07:00
|
|
|
this._logout(msg, targetMM);
|
2012-06-29 16:12:00 -07:00
|
|
|
break;
|
|
|
|
// IDP
|
|
|
|
case "Identity:IDP:BeginProvisioning":
|
|
|
|
this._beginProvisioning(msg, targetMM);
|
|
|
|
break;
|
|
|
|
case "Identity:IDP:GenKeyPair":
|
|
|
|
this._genKeyPair(msg);
|
|
|
|
break;
|
|
|
|
case "Identity:IDP:RegisterCertificate":
|
|
|
|
this._registerCertificate(msg);
|
|
|
|
break;
|
|
|
|
case "Identity:IDP:ProvisioningFailure":
|
|
|
|
this._provisioningFailure(msg);
|
|
|
|
break;
|
|
|
|
case "Identity:IDP:BeginAuthentication":
|
|
|
|
this._beginAuthentication(msg, targetMM);
|
|
|
|
break;
|
|
|
|
case "Identity:IDP:CompleteAuthentication":
|
|
|
|
this._completeAuthentication(msg);
|
|
|
|
break;
|
|
|
|
case "Identity:IDP:AuthenticationFailure":
|
|
|
|
this._authenticationFailure(msg);
|
|
|
|
break;
|
2013-03-14 12:19:34 -07:00
|
|
|
case "child-process-shutdown":
|
|
|
|
// we receive child-process-shutdown if the appliction crashes,
|
|
|
|
// including if it is crashed by the OS (killed for out-of-memory,
|
|
|
|
// for example)
|
|
|
|
this._childProcessShutdown(targetMM);
|
|
|
|
break;
|
2012-06-29 16:12:00 -07:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// nsIObserver
|
|
|
|
observe: function DOMIdentity_observe(aSubject, aTopic, aData) {
|
|
|
|
switch (aTopic) {
|
|
|
|
case "xpcom-shutdown":
|
2012-10-26 04:39:38 -07:00
|
|
|
this._unsubscribeListeners();
|
2012-06-29 16:12:00 -07:00
|
|
|
Services.obs.removeObserver(this, "xpcom-shutdown");
|
2012-10-26 04:39:38 -07:00
|
|
|
Services.ww.unregisterNotification(this);
|
2012-06-29 16:12:00 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
messages: ["Identity:RP:Watch", "Identity:RP:Request", "Identity:RP:Logout",
|
|
|
|
"Identity:IDP:BeginProvisioning", "Identity:IDP:ProvisioningFailure",
|
|
|
|
"Identity:IDP:RegisterCertificate", "Identity:IDP:GenKeyPair",
|
|
|
|
"Identity:IDP:BeginAuthentication",
|
|
|
|
"Identity:IDP:CompleteAuthentication",
|
2013-03-14 12:19:34 -07:00
|
|
|
"Identity:IDP:AuthenticationFailure",
|
|
|
|
"Identity:RP:Unwatch",
|
|
|
|
"child-process-shutdown"],
|
2012-06-29 16:12:00 -07:00
|
|
|
|
|
|
|
// Private.
|
|
|
|
_init: function DOMIdentity__init() {
|
|
|
|
Services.ww.registerNotification(this);
|
|
|
|
Services.obs.addObserver(this, "xpcom-shutdown", false);
|
2012-10-26 04:39:38 -07:00
|
|
|
this._subscribeListeners();
|
2012-06-29 16:12:00 -07:00
|
|
|
},
|
|
|
|
|
2012-10-26 04:39:38 -07:00
|
|
|
_subscribeListeners: function DOMIdentity__subscribeListeners() {
|
|
|
|
if (!ppmm) return;
|
|
|
|
for (let message of this.messages) {
|
|
|
|
ppmm.addMessageListener(message, this);
|
|
|
|
}
|
|
|
|
},
|
2012-06-29 16:12:00 -07:00
|
|
|
|
2012-10-26 04:39:38 -07:00
|
|
|
_unsubscribeListeners: function DOMIdentity__unsubscribeListeners() {
|
2012-06-29 16:12:00 -07:00
|
|
|
for (let message of this.messages) {
|
2012-10-26 04:39:38 -07:00
|
|
|
ppmm.removeMessageListener(message, this);
|
2012-06-29 16:12:00 -07:00
|
|
|
}
|
2012-10-26 04:39:38 -07:00
|
|
|
ppmm = null;
|
2012-06-29 16:12:00 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
_resetFrameState: function(aContext) {
|
|
|
|
log("_resetFrameState: ", aContext.id);
|
|
|
|
if (!aContext._mm) {
|
|
|
|
throw new Error("ERROR: Trying to reset an invalid context");
|
|
|
|
}
|
2012-11-16 15:34:32 -08:00
|
|
|
let message = new IDDOMMessage({id: aContext.id});
|
2012-06-29 16:12:00 -07:00
|
|
|
aContext._mm.sendAsyncMessage("Identity:ResetState", message);
|
|
|
|
},
|
|
|
|
|
|
|
|
_watch: function DOMIdentity__watch(message, targetMM) {
|
|
|
|
log("DOMIdentity__watch: " + message.id);
|
|
|
|
// Pass an object with the watch members to Identity.jsm so it can call the
|
|
|
|
// callbacks.
|
2012-11-16 15:34:32 -08:00
|
|
|
let context = new RPWatchContext(message, targetMM);
|
2012-06-29 16:12:00 -07:00
|
|
|
IdentityService.RP.watch(context);
|
|
|
|
},
|
|
|
|
|
2013-03-14 12:19:34 -07:00
|
|
|
_unwatch: function DOMIdentity_unwatch(message, targetMM) {
|
|
|
|
IdentityService.RP.unwatch(message.id, targetMM);
|
|
|
|
},
|
|
|
|
|
2012-06-29 16:12:00 -07:00
|
|
|
_request: function DOMIdentity__request(message) {
|
|
|
|
IdentityService.RP.request(message.id, message);
|
|
|
|
},
|
|
|
|
|
|
|
|
_logout: function DOMIdentity__logout(message) {
|
2012-11-16 15:34:32 -08:00
|
|
|
IdentityService.RP.logout(message.id, message.origin, message);
|
2012-06-29 16:12:00 -07:00
|
|
|
},
|
|
|
|
|
2013-03-14 12:19:34 -07:00
|
|
|
_childProcessShutdown: function DOMIdentity__childProcessShutdown(targetMM) {
|
|
|
|
IdentityService.RP.childProcessShutdown(targetMM);
|
|
|
|
},
|
|
|
|
|
2012-06-29 16:12:00 -07:00
|
|
|
_beginProvisioning: function DOMIdentity__beginProvisioning(message, targetMM) {
|
|
|
|
let context = new IDPProvisioningContext(message.id, message.origin,
|
|
|
|
targetMM);
|
|
|
|
IdentityService.IDP.beginProvisioning(context);
|
|
|
|
},
|
|
|
|
|
|
|
|
_genKeyPair: function DOMIdentity__genKeyPair(message) {
|
|
|
|
IdentityService.IDP.genKeyPair(message.id);
|
|
|
|
},
|
|
|
|
|
|
|
|
_registerCertificate: function DOMIdentity__registerCertificate(message) {
|
|
|
|
IdentityService.IDP.registerCertificate(message.id, message.cert);
|
|
|
|
},
|
|
|
|
|
|
|
|
_provisioningFailure: function DOMIdentity__provisioningFailure(message) {
|
|
|
|
IdentityService.IDP.raiseProvisioningFailure(message.id, message.reason);
|
|
|
|
},
|
|
|
|
|
|
|
|
_beginAuthentication: function DOMIdentity__beginAuthentication(message, targetMM) {
|
|
|
|
let context = new IDPAuthenticationContext(message.id, message.origin,
|
|
|
|
targetMM);
|
|
|
|
IdentityService.IDP.beginAuthentication(context);
|
|
|
|
},
|
|
|
|
|
|
|
|
_completeAuthentication: function DOMIdentity__completeAuthentication(message) {
|
|
|
|
IdentityService.IDP.completeAuthentication(message.id);
|
|
|
|
},
|
|
|
|
|
|
|
|
_authenticationFailure: function DOMIdentity__authenticationFailure(message) {
|
|
|
|
IdentityService.IDP.cancelAuthentication(message.id);
|
2013-03-14 12:19:34 -07:00
|
|
|
}
|
2012-06-29 16:12:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
// Object is initialized by nsIDService.js
|