Permalink
Browse files

implement retargeting - closes #21

  • Loading branch information...
1 parent e456990 commit 2863e30e6f75b649d55203db3734b1fb809b56f0 @lloyd lloyd committed May 16, 2012
Showing with 47 additions and 3 deletions.
  1. +2 −1 package.json
  2. +41 −0 retarget.js
  3. +4 −2 server.js
View
@@ -6,7 +6,8 @@
"dependencies": {
"express": "2.5.0",
"client-sessions": "0.0.6",
- "redis": "0.7.1"
+ "redis": "0.7.1",
+ "postprocess": "0.2.4"
},
"optionalDependencies": {
"hiredis": "0.1.14"
View
@@ -0,0 +1,41 @@
+/* This is code to allow one instance of 123done.org to run against
+ * all browserid environments - production, staging, development, and
+ * ephemeral testing deployments.
+ *
+ * The details of this code aren't very important, but what it basically
+ * does is populate res.persona_url with the appropriate url based on the
+ * Host headers sent with the request.
+ */
+var url = require('url'),
+ crypto = require('crypto');
+
+var pp = require('postprocess')(function(req, buf) {
+ var re = new RegExp('https://browserid.org', 'g');
+ return buf.replace(re, req.persona_url);
+});
+
+module.exports = function(req, res, next) {
+ // determine the URL of the browserid deployment we'll use
+ req.headers.host = req.headers.host || '123done.org';
+ var host = req.headers.host.split(':')[0].toString();
+ if (host === 'beta.123done.org') req.persona_url = 'https://diresworb.org';
+ else if (host === 'dev.123done.org') req.persona_url = 'https://dev.diresworb.org';
+ else if (/\.hacksign\.in$/.test(host)) {
+ req.persona_url = 'https://' + host.substr(0, host.length - 12) + '.hacksign.in'
+ } else {
+ req.persona_url = 'https://browserid.org';
+ }
+
+ // concoct a unique identifier for the (virtual) instance
+ // of 123done being hit
+ var hash = crypto.createHash('md5');
+ hash.update(req.headers.host);
+ req.deployment_id = hash.digest('hex').slice(0, 6);
+
+ // and determine the hostname of the verifier
+ req.verifier_host = url.parse(req.persona_url).hostname;
+
+ console.log(req.deployment_id, req.persona_url, req.verifier_host);
+
+ pp(req, res, next);
+};
View
@@ -16,6 +16,8 @@ var app = express.createServer(
express.bodyParser()
);
+app.use(require('./retarget.js'));
+
app.use(function (req, res, next) {
if (/^\/api/.test(req.url)) {
res.setHeader('Cache-Control', 'no-cache, max-age=0');
@@ -45,11 +47,11 @@ function checkAuth(req, res, next) {
app.post('/api/verify', function(req, res) {
var body = JSON.stringify({
assertion: req.body.assertion,
- audience: process.env['PUBLIC_URL'] || 'http://127.0.0.1:8080'
+ audience: 'http://' + req.headers.host
});
var vreq = https.request({
- host: 'browserid.org',
+ host: req.verifier_host,
path: '/verify',
method: 'POST',
headers: {

0 comments on commit 2863e30

Please sign in to comment.