From e371b2ad65a3bd5a7f06815e932a1ca5ccc7897c Mon Sep 17 00:00:00 2001 From: John Kleinschmidt Date: Wed, 16 Mar 2016 15:36:58 -0700 Subject: [PATCH] Added support for oauth authentication for couchdb This is used when google auth is used for authentication. --- forward.js | 18 ++++++++++++++++-- routes/10-couchproxy.js | 2 +- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/forward.js b/forward.js index 7c1ee0e..915d704 100644 --- a/forward.js +++ b/forward.js @@ -1,13 +1,27 @@ var onHeaders = require('on-headers'); var request = require('request'); -module.exports = function(host, config) { +module.exports = function(host, config, useOauth) { return function(req, res) { var forwardURL = host + req.url; + var requestOptions = { + url: forwardURL + }; var reqMethod = req.method.toLowerCase(); if (reqMethod == 'delete') { reqMethod = 'del'; } + if (config.useGoogleAuth && useOauth) { + if (req.get('x-oauth-consumer-key')) { + requestOptions.oauth = { + consumer_key: req.get('x-oauth-consumer-key'), + consumer_secret: req.get('x-oauth-consumer-secret'), + token: req.get('x-oauth-token'), + token_secret: req.get('x-oauth-token-secret') + }; + } + } + if (config.emberCLI) { // Ember CLI uses compression (https://www.npmjs.com/package/compression) which // causes issues when proxying requests, so turn off compression for proxied requests. @@ -15,7 +29,7 @@ module.exports = function(host, config) { res.header('Cache-Control','no-transform'); }); } - req.pipe(request[reqMethod](forwardURL).on('error', function(err) { + req.pipe(request[reqMethod](requestOptions).on('error', function(err) { console.log('Got error forwarding: ', err); })).pipe(res); }; diff --git a/routes/10-couchproxy.js b/routes/10-couchproxy.js index 51fbdc6..fa48364 100644 --- a/routes/10-couchproxy.js +++ b/routes/10-couchproxy.js @@ -1,5 +1,5 @@ var forward = require('../forward.js'); module.exports = function(app, config) { - app.use('/db/', forward(config.couchDbURL, config)); + app.use('/db/', forward(config.couchDbURL, config, true)); };