Last active
August 29, 2015 14:06
-
-
Save steve-jansen/04e5f251af226589b96c to your computer and use it in GitHub Desktop.
sample files for https://github.com/steve-jansen/json-proxy/issues/19
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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