how to use express-ntlm to get windows user name without authentication using NODE.js?

5.2k views Asked by At

I am trying to use express-ntlm to get windows user name without authentication.

in my app.js, i put the following:

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.use(favicon());
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', routes);
var ntlm = require('express-ntlm'); 
app.use(ntlm()); 
app.use('/search', search);

This gives me a 401 error in node.js when loading http://localhost:3000/search In chrome console: Failed to load resource: Failed to load resource: net::ERR_UNEXPECTED

what is the correct sequence of routing here? thanks.

========= modified to ==============

var express = require('express');
var path = require('path');
var favicon = require('static-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var routes = require('./routes/index');
var TT = require('./routes/TT');
var KYEC_stat = require('./routes/KYEC_stat');
var ftc = require('./routes/ftc');
var volPerDevice = require('./routes/topVolPerDevice');
var search = require('./routes/search');

var ntlm = require('express-ntlm'); 
var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.use(favicon());
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use(ntlm()); 
app.use('/search', search);
app.use('/tt', TT);
app.use('/kyec', KYEC_stat);
app.use('/ftc', ftc);
app.use('/vol', volPerDevice);
app.use('/', routes);

/// catch 404 and forward to error handler
app.use(function(req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
});

======= topVolPerDevice.js ================

var config = require('../config.json');
var express = require('express');
var query = require('pg-query');
var assert = require('assert');
var async = require('async');
var url = require('url');
var queryString = require('querystring');

var router = express.Router();


/* Display quantity tested per device id since 2011 using d3 */
/* The query will first select all lot records and their earliest test date (which is the 'P' insertion incoming material quantity);
   then use {lotid, lotstartdate} to retrieve all 1st insertion lot records, then add up lots per device;
   then return the 1st 20 device id which tops inocming material quantity;
 */
router.get('/', function(req, res) {
        query.connectionParameters = config.reportConnStr;      //connecting to localhost
        var deviceArray = new Array();
        var sqlstr =  "sdfsfdfsdsfds";
        query(sqlstr, function(err, rows, result) {
            assert.equal(rows, result.rows);
            for (var i = 0; i < rows.length; i++) {
                var device = {};
                device.name = rows[i].device;
                device.value = rows[i].totalqtyout;
                deviceArray.push(device);        
            }
            res.render('d3t1', {deviceArray:deviceArray});                      
        });
});

module.exports = router;

===== update 2/25/2015 =============

I am re-visiting the problem this week, and I reached a little bit further. I tried to put down a few debug statements in express-ntlm.js and also installed firebug in firefox. It turns out that it is probably not about the sequence of middleware.

return function(request, response, next) {
    if (!request.connection.id) {
        request.connection.id = utils.uuidv4();
    }

    var auth_headers = request.headers.authorization;

    var user = request.connection.ntlm;
....
}

over here, my request.connection.ntlm is null.

What could be the reason here? is it about browser settings or my network settings?

I am running this over company network, and I am on a network domain.

enter image description here

1

There are 1 answers

8
AudioBubble On

UPDATE:

Change to this in the app.js:

app.use(ntlm()); 
app.use('/', search);
app.use('/', TT);
app.use('/', KYEC_stat);
app.use('/', ftc);
app.use('/', volPerDevice);
app.use('/', routes);

And add '/vol' in the router in the topVolPerDevice file:

var config = require('../config.json');
var express = require('express');
var query = require('pg-query');
var assert = require('assert');
var async = require('async');
var url = require('url');
var queryString = require('querystring');

var router = express.Router();


/* Display quantity tested per device id since 2011 using d3 */
/* The query will first select all lot records and their earliest test date (which is the 'P' insertion incoming material quantity);
   then use {lotid, lotstartdate} to retrieve all 1st insertion lot records, then add up lots per device;
   then return the 1st 20 device id which tops inocming material quantity;
 */
router.get('/vol', function(req, res) {
        query.connectionParameters = config.reportConnStr;      //connecting to localhost
        var deviceArray = new Array();
        var sqlstr =  "sdfsfdfsdsfds";
        query(sqlstr, function(err, rows, result) {
            assert.equal(rows, result.rows);
            for (var i = 0; i < rows.length; i++) {
                var device = {};
                device.name = rows[i].device;
                device.value = rows[i].totalqtyout;
                deviceArray.push(device);        
            }
            res.render('d3t1', {deviceArray:deviceArray});                      
        });
});

module.exports = router;