From 19ae65c53724e171ca1733d94b6c8ba83a483e72 Mon Sep 17 00:00:00 2001
From: Lloyd Hilaiel <lloyd@hilaiel.com>
Date: Thu, 12 Jul 2012 15:01:02 -0700
Subject: [PATCH] if fetch of .well-known fails, then fall back to proxy_idps
 from the config file

---
 lib/primary.js | 24 ++++++++++++++++++------
 1 file changed, 18 insertions(+), 6 deletions(-)

diff --git a/lib/primary.js b/lib/primary.js
index 37c3fc809..a7c6e06b4 100644
--- a/lib/primary.js
+++ b/lib/primary.js
@@ -104,8 +104,6 @@ function parseWellKnownBody(body, domain, delegates, cb) {
   });
 }
 
-
-
 // Support "shimmed primaries" for local development.  That is an environment variable that is any number of
 // CSV values of the form:
 //  <domain>|<origin>|<path to .well-known/browserid>,
@@ -131,14 +129,29 @@ if (process.env['SHIMMED_PRIMARIES']) {
 }
 
 var getWellKnown = function (domain, delegates, cb) {
+  // called when we fail to fetch well-known.  Looks in configuration for proxyidp
+  // configuration, if that exists, it's as if a delegation of authority existed.
+  function handleProxyIDP() {
+    var proxyIDPs = config.get('proxy_idps');
+
+    if (proxyIDPs.hasOwnProperty(domain)) {
+      var generatedBody = JSON.stringify({
+        authority: proxyIDPs[domain]
+      });
+      cb(null, generatedBody, domain, delegates);
+    } else {
+      cb(null, false, null);
+    }
+  }
+
   function handleResponse(res) {
     if (res.statusCode !== 200) {
       logger.debug(domain + ' is not a browserid primary - non-200 response code to ' + WELL_KNOWN_URL);
-      return cb(null, false, null);
+      return handleProxyIDP();
     }
     if (res.headers['content-type'].indexOf('application/json') !== 0) {
       logger.debug(domain + ' is not a browserid primary - non "application/json" response to ' + WELL_KNOWN_URL);
-      return cb(null, false, null);
+      return handleProxyIDP();
     }
 
     var body = "";
@@ -178,7 +191,7 @@ var getWellKnown = function (domain, delegates, cb) {
 
   req.on('error', function(e) {
     logger.debug(domain + ' is not a browserid primary: ' + e.toString());
-    cb(null, false, null);
+    handleProxyIDP();
   });
 };
 
@@ -198,7 +211,6 @@ exports.checkSupport = function(domain, cb, delegates) {
     return process.nextTick(function() { cb("invalid domain"); });
   }
 
-
   getWellKnown(domain, delegates, function (err, body, domain, cbdelegates) {
     if (err) {
       logger.debug(err);
-- 
GitLab