Skip to content

Instantly share code, notes, and snippets.

@steve-jansen
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save steve-jansen/04e5f251af226589b96c to your computer and use it in GitHub Desktop.
Save steve-jansen/04e5f251af226589b96c to your computer and use it in GitHub Desktop.
var express = require('express'),
proxy = require('./lib/proxy');
var app = express();
app.configure(function() {
app.use(express.favicon(false));
app.use(express.logger('dev'));
app.use(proxy.initialize({
proxy: {
forward: {
'/(.*)': 'http://nylen.tv/digest.php?$1'
},
headers: {
/**
* callback to perform digest authentication tranparently
* @param {[type]} req the http-proxy incoming request
*/
'Authorization': function(req) {
console.log('Authorization:', process.env.JSON_PROXY_DIGEST_TOKEN)
return process.env.JSON_PROXY_DIGEST_TOKEN;
}
}
}
}));
app.use(express.static(__dirname));
});
app.listen(5000);
console.log('listening on http://localhost:5000');
#!/bin/bash
# use curl to perform the digest auth
# steal the auth header from the 2nd request
export JSON_PROXY_DIGEST_TOKEN=`curl -s -v --digest -u "admin:mypa" http://nylen.tv/digest.php 2>&1 | grep Authorization | cut -b 18-`
node test.js
var express = require('express'),
proxy = require('./lib/proxy');
var app = express();
app.configure(function() {
app.use(express.favicon(false));
app.use(express.logger('dev'));
app.use(proxy.initialize({
proxy: {
forward: {
'/digest/(.*)': 'http://nylen.tv/digest.php?$1'
},
headers: {
/**
* callback to perform digest authentication tranparently
* @param {[type]} req the http-proxy incoming request
*/
'Authorization': function(req) {
var token = process.env.JSON_PROXY_DIGEST_TOKEN,
request;
if (typeof(token) === 'string' && token.length > 0) {
return token;
}
/* the request module is not installed by json-proxy - verify it's available */
try {
request = require('request');
} catch (e) {
throw new Error('request module not installed - please `npm install request`');
}
var r = request({
/* Credit to @nylen for this demo! https://github.com/mikeal/request/issues/580 */
uri : 'http://nylen.tv/digest.php',
method: 'HEAD',
auth : {
user : 'admin',
pass : 'mypass',
sendImmediately : false
}
}, function(err, res, body) {
if (err) {
console.error('Error performing digest authentication:', err.message);
return;
} else if (res.statusCode !== 200) {
console.error('Error response performing digest authentication:', res.statusCode);
return;
}
// TOTAL HACK: cache the digest response using an env variable
// this is awful, and json-proxy should be refactored to
// support a callback in this function if enough people
// ask for proper digest auth support
process.env.JSON_PROXY_DIGEST_TOKEN = r.req._headers.authorization;
console.log('Created digest authentication header');
console.log(process.env.JSON_PROXY_DIGEST_TOKEN)
});
}
}
}
}));
app.use(express.static(__dirname));
});
app.listen(5000);
console.log('listening on http://localhost:5000');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment