dom/media/tests/identity/idp-proxy.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 (function(global) {
michael@0 2 "use strict";
michael@0 3
michael@0 4 function IDPJS() {
michael@0 5 this.domain = window.location.host;
michael@0 6 var p = window.location.pathname;
michael@0 7 this.protocol = p.substring(p.lastIndexOf('/') + 1) + window.location.hash;
michael@0 8 this.username = "someone@" + this.domain;
michael@0 9 // so rather than create a million different IdP configurations and litter
michael@0 10 // the world with files all containing near-identical code, let's use the
michael@0 11 // hash/URL fragment as a way of generating instructions for the IdP
michael@0 12 this.instructions = window.location.hash.replace("#", "").split(":");
michael@0 13 this.port = window.rtcwebIdentityPort;
michael@0 14 this.port.onmessage = this.receiveMessage.bind(this);
michael@0 15 this.sendResponse({
michael@0 16 type : "READY"
michael@0 17 });
michael@0 18 }
michael@0 19
michael@0 20 IDPJS.prototype.getDelay = function() {
michael@0 21 // instructions in the form "delay123" have that many milliseconds
michael@0 22 // added before sending the response
michael@0 23 var delay = 0;
michael@0 24 function addDelay(instruction) {
michael@0 25 var m = instruction.match(/^delay(\d+)$/);
michael@0 26 if (m) {
michael@0 27 delay += parseInt(m[1], 10);
michael@0 28 }
michael@0 29 }
michael@0 30 this.instructions.forEach(addDelay);
michael@0 31 return delay;
michael@0 32 };
michael@0 33
michael@0 34 function is(target) {
michael@0 35 return function(instruction) {
michael@0 36 return instruction === target;
michael@0 37 };
michael@0 38 }
michael@0 39
michael@0 40 IDPJS.prototype.sendResponse = function(response) {
michael@0 41 // we don't touch the READY message unless told to
michael@0 42 if (response.type === "READY" && !this.instructions.some(is("ready"))) {
michael@0 43 this.port.postMessage(response);
michael@0 44 return;
michael@0 45 }
michael@0 46
michael@0 47 // if any instruction is "error", return an error.
michael@0 48 if (this.instructions.some(is("error"))) {
michael@0 49 response.type = "ERROR";
michael@0 50 }
michael@0 51
michael@0 52 window.setTimeout(function() {
michael@0 53 this.port.postMessage(response);
michael@0 54 }.bind(this), this.getDelay());
michael@0 55 };
michael@0 56
michael@0 57 IDPJS.prototype.receiveMessage = function(ev) {
michael@0 58 var message = ev.data;
michael@0 59 switch (message.type) {
michael@0 60 case "SIGN":
michael@0 61 if (message.username) {
michael@0 62 var at = message.username.indexOf("@");
michael@0 63 if (at < 0) {
michael@0 64 this.username = message.username + "@" + this.domain;
michael@0 65 } else if (message.username.substring(at + 1) === this.domain) {
michael@0 66 this.username = message.username;
michael@0 67 }
michael@0 68 }
michael@0 69 this.sendResponse({
michael@0 70 type : "SUCCESS",
michael@0 71 id : message.id,
michael@0 72 message : {
michael@0 73 idp : {
michael@0 74 domain : this.domain,
michael@0 75 protocol : this.protocol
michael@0 76 },
michael@0 77 assertion : JSON.stringify({
michael@0 78 username : this.username,
michael@0 79 contents : message.message
michael@0 80 })
michael@0 81 }
michael@0 82 });
michael@0 83 break;
michael@0 84
michael@0 85 case "VERIFY":
michael@0 86 var payload = JSON.parse(message.message);
michael@0 87 var contents = payload.contents;
michael@0 88 if (this.instructions.some(is("bad"))) {
michael@0 89 contents = {};
michael@0 90 }
michael@0 91 this.sendResponse({
michael@0 92 type : "SUCCESS",
michael@0 93 id : message.id,
michael@0 94 message : {
michael@0 95 identity : payload.username,
michael@0 96 contents : contents
michael@0 97 }
michael@0 98 });
michael@0 99 break;
michael@0 100
michael@0 101 default:
michael@0 102 this.sendResponse({
michael@0 103 type : "ERROR",
michael@0 104 id : message.id,
michael@0 105 error : JSON.stringify(message)
michael@0 106 });
michael@0 107 break;
michael@0 108 }
michael@0 109 };
michael@0 110
michael@0 111 global.idp = new IDPJS();
michael@0 112 }(this));

mercurial