2

I have an nodejs app which use the http basic authentication in express.js.

In this app I make an http.get request to an external webpage to load and parse the html.

With the basic auth I get in each http.get(url, function(){}) request to the external host an error: "Unauthorized". If I remove the basic authentication, it works fine.

Anybody know's why I'm Unauthorized at an public resource if only my own server has this authentication?

e.g. (pseudocode):

with the express basic authentication, I'm getting "Unauthorized" as body from google.com. without the auth, I get the html

    var auth = express.basicAuth(function(user, pass, callback) {
                var result = (user === 'john' && pass === 'doe') ? true : false;
                callback(null, result);
            });

            app.configure(function(){
                app.use("/", auth, function(next) { next(); });
                app.use("/", express.static(__dirname+'/html'));
            });

http.get('http://google.com', function(res) {

                res.setEncoding('utf8');
                var body = '';

                res.on('data', function (chunk) {
                    body = body + chunk;
                });

                res.on('end', function() {
                    cb(body);
                });

            }).on('error', function(err) {
                cb('error', err);
            }); 
tiefenb
  • 732
  • 3
  • 16
  • 32

2 Answers2

4

You need to restructure your app to have your call to Google done inside a callback, after a GET is issued to your server. Here's working code:

var express = require('express');
var request = require('request');
var app = express();

// Authenticator
app.use(express.basicAuth('john', 'doe'));

app.get('/', function(req, res) {
    request.get('http://www.google.com', function (err, response, body) {
        res.send(body)
    });
});

app.listen(process.env.PORT || 8080);

Here are details if you want to do fancier basic authentication: http://blog.modulus.io/nodejs-and-express-basic-authentication

It appears that you're trying to write code synchronously, which will not work. I recommend reading this article for a review of idiomatic Node: http://blog.ponyfoo.com/2013/07/12/teach-yourself-nodejs-in-10-steps

Dan Kohn
  • 33,811
  • 9
  • 84
  • 100
0

For HTTP Basic/Digest authentication you can also use http-auth module

var express = require('express');
var request = require('request');

// Authentication module.
var auth = require('http-auth');
var basic = auth.basic({
    realm: "Simon Area.",
    file: __dirname + "/../data/users.htpasswd" // gevorg:gpass, Sarah:testpass ...
});

// Application setup.
var app = express();
app.use(auth.connect(basic));

// Setup route.
app.get('/', function(req, res){
    request.get('http://www.google.com', function (err, response, body) {
        res.send(body)
    });
});
gevorg
  • 4,835
  • 4
  • 35
  • 52