Bug 975144 - Updating test IdP for new API draft
authorMartin Thomson <martin.thomson@gmail.com>
Mon, 05 Jan 2015 16:58:30 -0800
changeset 235082 f556e8903fda82beec92f97089621ef3d4f88c67
parent 235081 d3672853e251e0e5d01cfd60306f6d183456f1e4
child 235083 592aca7d63c7b257ec0bfc4605bafb4c1a524753
push id349
push usermartin.thomson@gmail.com
push dateTue, 06 Jan 2015 00:59:51 +0000
bugs975144
milestone37.0a1
Bug 975144 - Updating test IdP for new API
dom/media/tests/mochitest/identity/idp-proxy.js
dom/media/tests/mochitest/identity/idp.html
dom/media/tests/mochitest/identity/idp.js
dom/media/tests/mochitest/identity/mochitest.ini
deleted file mode 100644
--- a/dom/media/tests/mochitest/identity/idp-proxy.js
+++ /dev/null
@@ -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));
deleted file mode 100644
--- a/dom/media/tests/mochitest/identity/idp.html
+++ /dev/null
@@ -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>
new file mode 100644
--- /dev/null
+++ b/dom/media/tests/mochitest/identity/idp.js
@@ -0,0 +1,83 @@
+(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 p = global.location.pathname;
+    this.protocol = p.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.registerIdentityProvider(new IDPJS());
+  }
+}(this));
--- a/dom/media/tests/mochitest/identity/mochitest.ini
+++ b/dom/media/tests/mochitest/identity/mochitest.ini
@@ -1,17 +1,16 @@
 [DEFAULT]
 # All tests are disabled on android&b2g due to lack of https support in
 # mochitests (Bug 907770)
 # Tests are also disabled on b2g due to lack of e10s support in WebRTC identity
 # (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]
 [test_getIdentityAssertion.html]
 [test_setIdentityProvider.html]
 [test_setIdentityProviderWithErrors.html]
 [test_peerConnection_peerIdentity.html]
 [test_fingerprints.html]