9

I would like to authenticate a windows user in NodeJS app. Is there any add-on for this yet ? There is node-krb5 but it doesn't support windows yet.

3 Answers 3

6

If you host on IIS with iisnode https://github.com/auth0/passport-windowsauth works nicely! passport-windowsauth comes with an ad integration but if you only want the username in order to implement your own authorzation logic you can do it like this

web.config:

<system.webServer>
    <iisnode promoteServerVars="LOGON_USER" />
</system.webServer>

server.js:

var passport = require('passport');
var WindowsStrategy = require('passport-windowsauth');

app.use(passport.initialize());
app.use(passport.session());

passport.serializeUser(function(user, done) {
    done(null, user);
});

passport.deserializeUser(function(user, done) {
    done(null, user);
});

passport.use(new WindowsStrategy({
    integrated: true 
}, function(profile,done) {
    var user = {
        id: profile.id,
    };
    done(null, user);
}));

app.all("*", passport.authenticate("WindowsAuthentication"), function (request,response,next){
    next();
});

then you can access the userid on the request object in your other routes:

app.get("/api/testAuthentication", function(request, response){
    console.log(request.user.id + " is authenticated");
});

if you want to implement your own authorization logic using the user id you can define a middleware function like this:

app.get("/api/testAuthorization", hasRole("a role"), function(request, response, next){
    console.log(request.user.id " is authenticated and authorized");
});

where hasRole looks like this:

function hasRole(role) {
    return function(request,response,next){
        //your own authorzation logic

        if(role == "a role")
            next();
        else
            response.status(403).send();
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

But when we put node behind IIS, we loose lots of Node benefits!
I am able to get the response ( request.user.id ) from Url in the browser. but when I am trying to access the same URL, I am getting error Unauthorized error. I know this post is old but if possible, Can you share any example where you have used this kind of thing?
can I use passport-windowsauth without IISNode
I don't understand what passport windows-auth is adding here: already iisnode has promoteServerVars and that's enough to run node under IIS and to get the logon user when the windows auth is negotiated.
1

node-sspi: found it easy and efficient to use.

https://www.npmjs.com/package/node-sspi

Comments

1

Disclaimer: I am the author of node-expose-sspi.

https://github.com/jlguenego/node-expose-sspi

is dedicated to do SSO on Windows using Kerberos or NTLM. It uses Negotiate and SPNEGO token.

5 Comments

Tried it out but it is working only from a localhost url, not from a real domain intranet... In the latter case the browser (chrome, edge) keeps asking user/password without a succesful connection. Tested on the trivial localhost it does work, but how is that useful?
in NTLM yes, but if you use a domain and Kerberos then no popup will appear.
Is it something easily doable only at application level in node.js (how?) or what else does it imply? If you prefer, we can follow up on github. Thank you anyway for your reply!
With Kerberos, your windows account must run on a MS Windows domain server. And the navigator must trust the website url. This is only doable for an intranet application. node-expose-sspi is designed only for this situation. For internet connection authentication, you should use for instance OAuth2.
Yes, I'm on a intranet connection authentication, with domain servers etc.. I don't understand why the authentication is not passed... (btw, if you have time, do you prefer to discuss here or on the github?). Just to say, iisnode is working

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.