1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/media/tests/identity/idp-proxy.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,112 @@ 1.4 +(function(global) { 1.5 + "use strict"; 1.6 + 1.7 + function IDPJS() { 1.8 + this.domain = window.location.host; 1.9 + var p = window.location.pathname; 1.10 + this.protocol = p.substring(p.lastIndexOf('/') + 1) + window.location.hash; 1.11 + this.username = "someone@" + this.domain; 1.12 + // so rather than create a million different IdP configurations and litter 1.13 + // the world with files all containing near-identical code, let's use the 1.14 + // hash/URL fragment as a way of generating instructions for the IdP 1.15 + this.instructions = window.location.hash.replace("#", "").split(":"); 1.16 + this.port = window.rtcwebIdentityPort; 1.17 + this.port.onmessage = this.receiveMessage.bind(this); 1.18 + this.sendResponse({ 1.19 + type : "READY" 1.20 + }); 1.21 + } 1.22 + 1.23 + IDPJS.prototype.getDelay = function() { 1.24 + // instructions in the form "delay123" have that many milliseconds 1.25 + // added before sending the response 1.26 + var delay = 0; 1.27 + function addDelay(instruction) { 1.28 + var m = instruction.match(/^delay(\d+)$/); 1.29 + if (m) { 1.30 + delay += parseInt(m[1], 10); 1.31 + } 1.32 + } 1.33 + this.instructions.forEach(addDelay); 1.34 + return delay; 1.35 + }; 1.36 + 1.37 + function is(target) { 1.38 + return function(instruction) { 1.39 + return instruction === target; 1.40 + }; 1.41 + } 1.42 + 1.43 + IDPJS.prototype.sendResponse = function(response) { 1.44 + // we don't touch the READY message unless told to 1.45 + if (response.type === "READY" && !this.instructions.some(is("ready"))) { 1.46 + this.port.postMessage(response); 1.47 + return; 1.48 + } 1.49 + 1.50 + // if any instruction is "error", return an error. 1.51 + if (this.instructions.some(is("error"))) { 1.52 + response.type = "ERROR"; 1.53 + } 1.54 + 1.55 + window.setTimeout(function() { 1.56 + this.port.postMessage(response); 1.57 + }.bind(this), this.getDelay()); 1.58 + }; 1.59 + 1.60 + IDPJS.prototype.receiveMessage = function(ev) { 1.61 + var message = ev.data; 1.62 + switch (message.type) { 1.63 + case "SIGN": 1.64 + if (message.username) { 1.65 + var at = message.username.indexOf("@"); 1.66 + if (at < 0) { 1.67 + this.username = message.username + "@" + this.domain; 1.68 + } else if (message.username.substring(at + 1) === this.domain) { 1.69 + this.username = message.username; 1.70 + } 1.71 + } 1.72 + this.sendResponse({ 1.73 + type : "SUCCESS", 1.74 + id : message.id, 1.75 + message : { 1.76 + idp : { 1.77 + domain : this.domain, 1.78 + protocol : this.protocol 1.79 + }, 1.80 + assertion : JSON.stringify({ 1.81 + username : this.username, 1.82 + contents : message.message 1.83 + }) 1.84 + } 1.85 + }); 1.86 + break; 1.87 + 1.88 + case "VERIFY": 1.89 + var payload = JSON.parse(message.message); 1.90 + var contents = payload.contents; 1.91 + if (this.instructions.some(is("bad"))) { 1.92 + contents = {}; 1.93 + } 1.94 + this.sendResponse({ 1.95 + type : "SUCCESS", 1.96 + id : message.id, 1.97 + message : { 1.98 + identity : payload.username, 1.99 + contents : contents 1.100 + } 1.101 + }); 1.102 + break; 1.103 + 1.104 + default: 1.105 + this.sendResponse({ 1.106 + type : "ERROR", 1.107 + id : message.id, 1.108 + error : JSON.stringify(message) 1.109 + }); 1.110 + break; 1.111 + } 1.112 + }; 1.113 + 1.114 + global.idp = new IDPJS(); 1.115 +}(this));