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);
            }); 
2
  • Provide some code please. Commented Jul 22, 2013 at 11:37
  • Servers can allow unauthenticated requests, for some pages/resources. But giving incorrect/invalid credentials will cause request to fail. Can you check if your credentials are correct. Commented Jul 22, 2013 at 11:39

2 Answers 2

5

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

Sign up to request clarification or add additional context in comments.

Comments

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)
    });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.