2014-02-10 14:41:46 -08:00
|
|
|
/* jshint moz:true, browser:true */
|
|
|
|
/* 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 = ["PeerConnectionIdp"];
|
|
|
|
|
|
|
|
const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
|
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "IdpProxy",
|
|
|
|
"resource://gre/modules/media/IdpProxy.jsm");
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an IdP helper.
|
|
|
|
*
|
|
|
|
* @param window (object) the window object to use for miscellaneous goodies
|
|
|
|
* @param timeout (int) the timeout in milliseconds
|
|
|
|
* @param warningFunc (function) somewhere to dump warning messages
|
2014-04-08 05:26:00 -07:00
|
|
|
* @param dispatchEventFunc (function) somewhere to dump error events
|
2014-02-10 14:41:46 -08:00
|
|
|
*/
|
2014-04-08 05:26:00 -07:00
|
|
|
function PeerConnectionIdp(window, timeout, warningFunc, dispatchEventFunc) {
|
2014-02-10 14:41:46 -08:00
|
|
|
this._win = window;
|
|
|
|
this._timeout = timeout || 5000;
|
|
|
|
this._warning = warningFunc;
|
2014-04-08 05:26:00 -07:00
|
|
|
this._dispatchEvent = dispatchEventFunc;
|
2014-02-10 14:41:46 -08:00
|
|
|
|
|
|
|
this.assertion = null;
|
|
|
|
this.provider = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
PeerConnectionIdp._mLinePattern = new RegExp("^m=", "m");
|
|
|
|
// attributes are funny, the 'a' is case sensitive, the name isn't
|
|
|
|
let pattern = "^a=[iI][dD][eE][nN][tT][iI][tT][yY]:(\\S+)";
|
|
|
|
PeerConnectionIdp._identityPattern = new RegExp(pattern, "m");
|
|
|
|
pattern = "^a=[fF][iI][nN][gG][eE][rR][pP][rR][iI][nN][tT]:(\\S+) (\\S+)";
|
|
|
|
PeerConnectionIdp._fingerprintPattern = new RegExp(pattern, "m");
|
|
|
|
})();
|
|
|
|
|
|
|
|
PeerConnectionIdp.prototype = {
|
2014-03-14 11:25:37 -07:00
|
|
|
setIdentityProvider: function(provider, protocol, username) {
|
2014-02-10 14:41:46 -08:00
|
|
|
this.provider = provider;
|
2014-04-08 05:26:00 -07:00
|
|
|
this.protocol = protocol;
|
2014-03-14 11:25:33 -07:00
|
|
|
this.username = username;
|
2014-03-14 11:25:37 -07:00
|
|
|
if (this._idpchannel) {
|
|
|
|
if (this._idpchannel.isSame(provider, protocol)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this._idpchannel.close();
|
|
|
|
}
|
2014-03-14 11:25:33 -07:00
|
|
|
this._idpchannel = new IdpProxy(provider, protocol);
|
2014-02-10 14:41:46 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
close: function() {
|
|
|
|
this.assertion = null;
|
|
|
|
this.provider = null;
|
|
|
|
if (this._idpchannel) {
|
|
|
|
this._idpchannel.close();
|
|
|
|
this._idpchannel = null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-04-08 05:26:00 -07:00
|
|
|
/**
|
|
|
|
* Generate an error event of the identified type;
|
|
|
|
* and put a little more precise information in the console.
|
|
|
|
*/
|
|
|
|
reportError: function(type, message, extra) {
|
|
|
|
let args = {
|
|
|
|
idp: this.provider,
|
|
|
|
protocol: this.protocol
|
|
|
|
};
|
|
|
|
if (extra) {
|
|
|
|
Object.keys(extra).forEach(function(k) {
|
|
|
|
args[k] = extra[k];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
this._warning("RTC identity: " + message, null, 0);
|
|
|
|
let ev = new this._win.RTCPeerConnectionIdentityErrorEvent('idp' + type + 'error', args);
|
|
|
|
this._dispatchEvent(ev);
|
|
|
|
},
|
|
|
|
|
2014-06-20 02:09:00 -07:00
|
|
|
_getFingerprintsFromSdp: function(sdp) {
|
|
|
|
let fingerprints = {};
|
|
|
|
let m = sdp.match(PeerConnectionIdp._fingerprintPattern);
|
|
|
|
while (m) {
|
|
|
|
fingerprints[m[0]] = { algorithm: m[1], digest: m[2] };
|
|
|
|
sdp = sdp.substring(m.index + m[0].length);
|
|
|
|
m = sdp.match(PeerConnectionIdp._fingerprintPattern);
|
2014-02-10 14:41:46 -08:00
|
|
|
}
|
|
|
|
|
2014-06-20 02:09:00 -07:00
|
|
|
return Object.keys(fingerprints).map(k => fingerprints[k]);
|
2014-02-10 14:41:46 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
_getIdentityFromSdp: function(sdp) {
|
2014-04-08 05:26:00 -07:00
|
|
|
// a=identity is session level
|
2014-02-10 14:41:46 -08:00
|
|
|
let mLineMatch = sdp.match(PeerConnectionIdp._mLinePattern);
|
|
|
|
let sessionLevel = sdp.substring(0, mLineMatch.index);
|
|
|
|
let idMatch = sessionLevel.match(PeerConnectionIdp._identityPattern);
|
|
|
|
if (idMatch) {
|
|
|
|
let assertion = {};
|
|
|
|
try {
|
|
|
|
assertion = JSON.parse(atob(idMatch[1]));
|
|
|
|
} catch (e) {
|
2014-04-08 05:26:00 -07:00
|
|
|
this.reportError("validation",
|
|
|
|
"invalid identity assertion: " + e);
|
2014-02-10 14:41:46 -08:00
|
|
|
} // for JSON.parse
|
|
|
|
if (typeof assertion.idp === "object" &&
|
|
|
|
typeof assertion.idp.domain === "string" &&
|
|
|
|
typeof assertion.assertion === "string") {
|
|
|
|
return assertion;
|
|
|
|
}
|
2014-04-08 05:26:00 -07:00
|
|
|
|
|
|
|
this.reportError("validation", "assertion missing" +
|
|
|
|
" idp/idp.domain/assertion");
|
2014-02-10 14:41:46 -08:00
|
|
|
}
|
|
|
|
// undefined!
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Queues a task to verify the a=identity line the given SDP contains, if any.
|
|
|
|
* If the verification succeeds callback is called with the message from the
|
|
|
|
* IdP proxy as parameter, else (verification failed OR no a=identity line in
|
|
|
|
* SDP at all) null is passed to callback.
|
|
|
|
*/
|
|
|
|
verifyIdentityFromSDP: function(sdp, callback) {
|
|
|
|
let identity = this._getIdentityFromSdp(sdp);
|
2014-06-20 02:09:00 -07:00
|
|
|
let fingerprints = this._getFingerprintsFromSdp(sdp);
|
2014-04-08 05:26:00 -07:00
|
|
|
// it's safe to use the fingerprint we got from the SDP here,
|
2014-02-10 14:41:46 -08:00
|
|
|
// only because we ensure that there is only one
|
2014-06-20 02:09:00 -07:00
|
|
|
if (!identity || fingerprints.length <= 0) {
|
2014-02-10 14:41:46 -08:00
|
|
|
callback(null);
|
|
|
|
return;
|
|
|
|
}
|
2014-04-08 12:30:31 -07:00
|
|
|
|
2014-04-08 05:26:00 -07:00
|
|
|
this.setIdentityProvider(identity.idp.domain, identity.idp.protocol);
|
2014-06-20 02:09:00 -07:00
|
|
|
this._verifyIdentity(identity.assertion, fingerprints, callback);
|
2014-02-10 14:41:46 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks that the name in the identity provided by the IdP is OK.
|
|
|
|
*
|
|
|
|
* @param name (string) the name to validate
|
|
|
|
* @returns (string) an error message, iff the name isn't good
|
|
|
|
*/
|
|
|
|
_validateName: function(name) {
|
|
|
|
if (typeof name !== "string") {
|
|
|
|
return "name not a string";
|
|
|
|
}
|
|
|
|
let atIdx = name.indexOf("@");
|
|
|
|
if (atIdx > 0) {
|
|
|
|
// no third party assertions... for now
|
|
|
|
let tail = name.substring(atIdx + 1);
|
|
|
|
|
|
|
|
// strip the port number, if present
|
|
|
|
let provider = this.provider;
|
|
|
|
let providerPortIdx = provider.indexOf(":");
|
|
|
|
if (providerPortIdx > 0) {
|
|
|
|
provider = provider.substring(0, providerPortIdx);
|
|
|
|
}
|
2014-04-08 05:26:00 -07:00
|
|
|
let idnService = Components.classes["@mozilla.org/network/idn-service;1"].
|
2014-01-22 21:22:12 -08:00
|
|
|
getService(Components.interfaces.nsIIDNService);
|
|
|
|
if (idnService.convertUTF8toACE(tail) !==
|
|
|
|
idnService.convertUTF8toACE(provider)) {
|
2014-02-10 14:41:46 -08:00
|
|
|
return "name '" + identity.name +
|
|
|
|
"' doesn't match IdP: '" + this.provider + "'";
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return "missing authority in name from IdP";
|
|
|
|
},
|
|
|
|
|
|
|
|
// we are very defensive here when handling the message from the IdP
|
|
|
|
// proxy so that broken IdPs can only do as little harm as possible.
|
2014-06-20 02:09:00 -07:00
|
|
|
_checkVerifyResponse: function(message, fingerprints) {
|
|
|
|
let warn = msg => {
|
2014-04-08 05:26:00 -07:00
|
|
|
this.reportError("validation",
|
|
|
|
"assertion validation failure: " + msg);
|
2014-06-20 02:09:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
let isSubsetOf = (outer, inner, cmp) => {
|
|
|
|
return inner.some(i => {
|
|
|
|
return !outer.some(o => cmp(i, o));
|
|
|
|
});
|
|
|
|
};
|
|
|
|
let compareFingerprints = (a, b) => {
|
|
|
|
return (a.digest === b.digest) && (a.algorithm === b.algorithm);
|
|
|
|
};
|
2014-02-10 14:41:46 -08:00
|
|
|
|
|
|
|
try {
|
|
|
|
let contents = JSON.parse(message.contents);
|
2014-06-20 02:09:00 -07:00
|
|
|
if (!Array.isArray(contents.fingerprint)) {
|
|
|
|
warn("fingerprint is not an array");
|
|
|
|
} else if (isSubsetOf(contents.fingerprint, fingerprints,
|
|
|
|
compareFingerprints)) {
|
|
|
|
warn("fingerprints in SDP aren't a subset of those in the assertion");
|
2014-02-10 14:41:46 -08:00
|
|
|
} else {
|
2014-03-20 15:49:33 -07:00
|
|
|
let error = this._validateName(message.identity);
|
2014-02-10 14:41:46 -08:00
|
|
|
if (error) {
|
|
|
|
warn(error);
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch(e) {
|
|
|
|
warn("invalid JSON in content");
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Asks the IdP proxy to verify an identity.
|
|
|
|
*/
|
2014-06-20 02:09:00 -07:00
|
|
|
_verifyIdentity: function(assertion, fingerprints, callback) {
|
2014-02-10 14:41:46 -08:00
|
|
|
function onVerification(message) {
|
2014-06-20 02:09:00 -07:00
|
|
|
if (message && this._checkVerifyResponse(message, fingerprints)) {
|
2014-02-10 14:41:46 -08:00
|
|
|
callback(message);
|
|
|
|
} else {
|
2014-04-08 05:26:00 -07:00
|
|
|
this._warning("RTC identity: assertion validation failure", null, 0);
|
2014-02-10 14:41:46 -08:00
|
|
|
callback(null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-14 11:25:33 -07:00
|
|
|
let request = {
|
|
|
|
type: "VERIFY",
|
|
|
|
message: assertion
|
|
|
|
};
|
2014-04-08 05:26:00 -07:00
|
|
|
this._sendToIdp(request, "validation", onVerification.bind(this));
|
2014-02-10 14:41:46 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Asks the IdP proxy for an identity assertion and, on success, enriches the
|
|
|
|
* given SDP with an a=identity line and calls callback with the new SDP as
|
|
|
|
* parameter. If no IdP is configured the original SDP (without a=identity
|
|
|
|
* line) is passed to the callback.
|
|
|
|
*/
|
2014-03-14 11:25:37 -07:00
|
|
|
appendIdentityToSDP: function(sdp, fingerprint, callback) {
|
2014-04-08 05:26:00 -07:00
|
|
|
let onAssertion = function() {
|
|
|
|
callback(this.wrapSdp(sdp), this.assertion);
|
|
|
|
}.bind(this);
|
2014-02-10 14:41:46 -08:00
|
|
|
|
2014-04-08 05:26:00 -07:00
|
|
|
if (!this._idpchannel || this.assertion) {
|
|
|
|
onAssertion();
|
2014-02-10 14:41:46 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-04-08 05:26:00 -07:00
|
|
|
this._getIdentityAssertion(fingerprint, onAssertion);
|
2014-02-10 14:41:46 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Inserts an identity assertion into the given SDP.
|
|
|
|
*/
|
|
|
|
wrapSdp: function(sdp) {
|
|
|
|
if (!this.assertion) {
|
|
|
|
return sdp;
|
|
|
|
}
|
|
|
|
|
|
|
|
// yes, we assume that this matches; if it doesn't something is *wrong*
|
|
|
|
let match = sdp.match(PeerConnectionIdp._mLinePattern);
|
|
|
|
return sdp.substring(0, match.index) +
|
|
|
|
"a=identity:" + this.assertion + "\r\n" +
|
|
|
|
sdp.substring(match.index);
|
|
|
|
},
|
|
|
|
|
2014-03-14 11:25:33 -07:00
|
|
|
getIdentityAssertion: function(fingerprint, callback) {
|
2014-02-10 14:41:46 -08:00
|
|
|
if (!this._idpchannel) {
|
2014-04-08 05:26:00 -07:00
|
|
|
this.reportError("assertion", "IdP not set");
|
|
|
|
callback(null);
|
|
|
|
return;
|
2014-02-10 14:41:46 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
this._getIdentityAssertion(fingerprint, callback);
|
|
|
|
},
|
|
|
|
|
2014-03-14 11:25:33 -07:00
|
|
|
_getIdentityAssertion: function(fingerprint, callback) {
|
2014-06-20 02:09:00 -07:00
|
|
|
let [algorithm, digest] = fingerprint.split(" ", 2);
|
2014-02-10 14:41:46 -08:00
|
|
|
let message = {
|
2014-06-20 02:09:00 -07:00
|
|
|
fingerprint: [{
|
2014-02-10 14:41:46 -08:00
|
|
|
algorithm: algorithm,
|
|
|
|
digest: digest
|
2014-06-20 02:09:00 -07:00
|
|
|
}]
|
2014-02-10 14:41:46 -08:00
|
|
|
};
|
2014-03-14 11:25:33 -07:00
|
|
|
let request = {
|
|
|
|
type: "SIGN",
|
|
|
|
message: JSON.stringify(message),
|
|
|
|
username: this.username
|
|
|
|
};
|
|
|
|
|
|
|
|
// catch the assertion, clean it up, warn if absent
|
|
|
|
function trapAssertion(assertion) {
|
|
|
|
if (!assertion) {
|
|
|
|
this._warning("RTC identity: assertion generation failure", null, 0);
|
|
|
|
this.assertion = null;
|
|
|
|
} else {
|
|
|
|
this.assertion = btoa(JSON.stringify(assertion));
|
|
|
|
}
|
|
|
|
callback(this.assertion);
|
|
|
|
}
|
|
|
|
|
2014-04-08 05:26:00 -07:00
|
|
|
this._sendToIdp(request, "assertion", trapAssertion.bind(this));
|
2014-02-10 14:41:46 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Packages a message and sends it to the IdP.
|
2014-04-08 05:26:00 -07:00
|
|
|
* @param request (dictionary) the message to send
|
|
|
|
* @param type (DOMString) the type of message (assertion/validation)
|
|
|
|
* @param callback (function) the function to call with the results
|
2014-02-10 14:41:46 -08:00
|
|
|
*/
|
2014-04-08 05:26:00 -07:00
|
|
|
_sendToIdp: function(request, type, callback) {
|
|
|
|
request.origin = Cu.getWebIDLCallerPrincipal().origin;
|
|
|
|
this._idpchannel.send(request, this._wrapCallback(type, callback));
|
|
|
|
},
|
2014-02-10 14:41:46 -08:00
|
|
|
|
2014-04-08 05:26:00 -07:00
|
|
|
_reportIdpError: function(type, message) {
|
|
|
|
let args = {};
|
|
|
|
let msg = "";
|
|
|
|
if (message.type === "ERROR") {
|
|
|
|
msg = message.error;
|
|
|
|
} else {
|
|
|
|
msg = JSON.stringify(message.message);
|
|
|
|
if (message.type === "LOGINNEEDED") {
|
|
|
|
args.loginUrl = message.loginUrl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.reportError(type, "received response of type '" +
|
|
|
|
message.type + "' from IdP: " + msg, args);
|
2014-02-10 14:41:46 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wraps a callback, adding a timeout and ensuring that the callback doesn't
|
|
|
|
* receive any message other than one where the IdP generated a "SUCCESS"
|
|
|
|
* response.
|
|
|
|
*/
|
2014-04-08 05:26:00 -07:00
|
|
|
_wrapCallback: function(type, callback) {
|
2014-02-10 14:41:46 -08:00
|
|
|
let timeout = this._win.setTimeout(function() {
|
2014-04-08 05:26:00 -07:00
|
|
|
this.reportError(type, "IdP timeout for " + this._idpchannel + " " +
|
|
|
|
(this._idpchannel.ready ? "[ready]" : "[not ready]"));
|
2014-02-10 14:41:46 -08:00
|
|
|
timeout = null;
|
|
|
|
callback(null);
|
|
|
|
}.bind(this), this._timeout);
|
|
|
|
|
|
|
|
return function(message) {
|
|
|
|
if (!timeout) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this._win.clearTimeout(timeout);
|
|
|
|
timeout = null;
|
2014-04-08 05:26:00 -07:00
|
|
|
|
|
|
|
let content = null;
|
2014-02-10 14:41:46 -08:00
|
|
|
if (message.type === "SUCCESS") {
|
|
|
|
content = message.message;
|
|
|
|
} else {
|
2014-04-08 05:26:00 -07:00
|
|
|
this._reportIdpError(type, message);
|
2014-02-10 14:41:46 -08:00
|
|
|
}
|
|
|
|
callback(content);
|
|
|
|
}.bind(this);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.PeerConnectionIdp = PeerConnectionIdp;
|