Bug 975144 - Updating test IdP for new API, r=jib

This commit is contained in:
Martin Thomson 2015-02-22 10:57:20 +13:00
parent acbef1189d
commit 2d332dd414
4 changed files with 85 additions and 125 deletions

View File

@ -1,112 +0,0 @@
(function(global) {
"use strict";
function IDPJS() {
this.domain = window.location.host;
var p = window.location.pathname;
this.protocol = p.substring(p.lastIndexOf('/') + 1) + window.location.hash;
this.username = "someone@" + this.domain;
// so rather than create a million different IdP configurations and litter
// the world with files all containing near-identical code, let's use the
// hash/URL fragment as a way of generating instructions for the IdP
this.instructions = window.location.hash.replace("#", "").split(":");
this.port = window.rtcwebIdentityPort;
this.port.onmessage = this.receiveMessage.bind(this);
this.sendResponse({
type : "READY"
});
}
IDPJS.prototype.getDelay = function() {
// instructions in the form "delay123" have that many milliseconds
// added before sending the response
var delay = 0;
function addDelay(instruction) {
var m = instruction.match(/^delay(\d+)$/);
if (m) {
delay += parseInt(m[1], 10);
}
}
this.instructions.forEach(addDelay);
return delay;
};
function is(target) {
return function(instruction) {
return instruction === target;
};
}
IDPJS.prototype.sendResponse = function(response) {
// we don't touch the READY message unless told to
if (response.type === "READY" && !this.instructions.some(is("ready"))) {
this.port.postMessage(response);
return;
}
// if any instruction is "error", return an error.
if (this.instructions.some(is("error"))) {
response.type = "ERROR";
}
window.setTimeout(function() {
this.port.postMessage(response);
}.bind(this), this.getDelay());
};
IDPJS.prototype.receiveMessage = function(ev) {
var message = ev.data;
switch (message.type) {
case "SIGN":
if (message.username) {
var at = message.username.indexOf("@");
if (at < 0) {
this.username = message.username + "@" + this.domain;
} else if (message.username.substring(at + 1) === this.domain) {
this.username = message.username;
}
}
this.sendResponse({
type : "SUCCESS",
id : message.id,
message : {
idp : {
domain : this.domain,
protocol : this.protocol
},
assertion : JSON.stringify({
username : this.username,
contents : message.message
})
}
});
break;
case "VERIFY":
var payload = JSON.parse(message.message);
var contents = payload.contents;
if (this.instructions.some(is("bad"))) {
contents = {};
}
this.sendResponse({
type : "SUCCESS",
id : message.id,
message : {
identity : payload.username,
contents : contents
}
});
break;
default:
this.sendResponse({
type : "ERROR",
id : message.id,
error : JSON.stringify(message)
});
break;
}
};
global.idp = new IDPJS();
}(this));

View File

@ -1,11 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>IDP Proxy</title>
<script src="idp-proxy.js"></script>
</head>
<body>
Test IDP Proxy
</body>
</html>

View File

@ -0,0 +1,84 @@
(function(global) {
"use strict";
// rather than create a million different IdP configurations and litter the
// world with files all containing near-identical code, let's use the hash/URL
// fragment as a way of generating instructions for the IdP
var instructions = global.location.hash.replace("#", "").split(":");
function is(target) {
return function(instruction) {
return instruction === target;
};
}
function IDPJS() {
this.domain = global.location.host;
var path = global.location.pathname;
this.protocol =
path.substring(p.lastIndexOf('/') + 1) + global.location.hash;
}
function borkResult(result) {
if (instructions.some(is("throw"))) {
throw new Error('Throwing!');
}
if (instructions.some(is("fail"))) {
return Promise.reject(new Error('Failing!'));
}
if (instructions.some(is("hang"))) {
return new Promise(r => {});
}
dump('idp: result=' + JSON.stringify(result) + '\n');
return Promise.resolve(result);
};
IDPJS.prototype = {
_selectUsername: function(usernameHint) {
var username = "someone@" + this.domain;
if (usernameHint) {
var at = usernameHint.indexOf("@");
if (at < 0) {
username = usernameHint + "@" + this.domain;
} else if (usernameHint.substring(at + 1) === this.domain) {
username = usernameHint;
}
}
return username;
},
generateAssertion: function(payload, origin, usernameHint) {
dump('idp: generateAssertion(' + payload + ')\n');
var idpDetails = {
domain: this.domain,
protocol: this.protocol
};
if (instructions.some(is("bad-assert"))) {
idpDetails = {};
}
return borkResult({
idp: idpDetails,
assertion: JSON.stringify({
username: this._selectUsername(usernameHint),
contents: payload
})
});
},
validateAssertion: function(assertion, origin) {
dump('idp: validateAssertion(' + assertion + ')\n');
var assertion = JSON.parse(assertion);
if (instructions.some(is("bad-validate"))) {
assertion.contents = {};
}
return borkResult({
identity: assertion.username,
contents: assertion.contents
});
}
};
if (!instructions.some(is("not_ready"))) {
dump('registering idp.js' + global.location.hash + '\n');
global.rtcIdentityProvider.register(new IDPJS());
}
}(this));

View File

@ -5,8 +5,7 @@
# (Bug 975144)
skip-if = e10s || os == "android" || appname == "b2g"
support-files =
/.well-known/idp-proxy/idp.html
/.well-known/idp-proxy/idp-proxy.js
/.well-known/idp-proxy/idp.js
identityevent.js
[test_idpproxy.html]