authenticate API requests to the proxy

and add configproxy cli via mininimist
This commit is contained in:
MinRK
2014-07-07 17:35:01 -05:00
parent d2c08b1611
commit 308209f71a
3 changed files with 74 additions and 12 deletions

View File

@@ -42,11 +42,28 @@ var json_handler = function (handler) {
};
};
var authorized = function (method) {
return function (req, res) {
console.log(req.headers);
auth = req.headers.authorization;
console.log(auth, this.auth_token);
if (!this.auth_token || auth == this.auth_token) {
return method.apply(this, arguments);
} else {
res.writeHead(403);
res.end();
}
};
};
var ConfigurableProxy = function (options) {
var that = this;
this.options = options || {};
this.auth_token = this.options.auth_token;
this.upstream_ip = this.options.upstream_ip || 'localhost';
this.upstream_port = this.options.upstream_port || 8001;
this.default_target = 'http://localhost:' + this.upstream_port;
this.default_target = "http://" + this.upstream_ip + ":" + this.upstream_port;
this.routes = {};
var proxy = this.proxy = httpProxy.createProxyServer({
@@ -57,15 +74,15 @@ var ConfigurableProxy = function (options) {
this.handlers = [
[ /^\/api\/routes$/, {
get : bound(this, this.get_routes)
get : bound(this, authorized(this.get_routes))
} ],
[ /^\/api\/routes(\/.*)$/, {
post : json_handler(bound(this, this.post_routes)),
'delete' : bound(this, this.delete_routes)
post : json_handler(bound(this, authorized(this.post_routes))),
'delete' : bound(this, authorized(this.delete_routes))
} ]
];
this.server = http.createServer(
this.server = this.proxy_server = http.createServer(
function (req, res) {
try {
return that.handle_request(req, res);
@@ -80,10 +97,6 @@ var ConfigurableProxy = function (options) {
this.server.on('upgrade', bound(this, this.handle_ws));
};
ConfigurableProxy.prototype.listen = function (port) {
this.server.listen(port);
};
ConfigurableProxy.prototype.fail = function (res, code, msg) {
res.writeHead(code);
res.write(msg);