Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to authenticate users. #21

Open
DarrenRainey opened this issue Mar 5, 2023 · 1 comment
Open

Unable to authenticate users. #21

DarrenRainey opened this issue Mar 5, 2023 · 1 comment

Comments

@DarrenRainey
Copy link

Hello I'm trying to use this module in my expressjs application however after some testing I can seen to get the module to authenticate users below is some working code from the standalone active directory module which does authenticate followed by the passports code which does not. I'm sending the username and passport via a html form / post request to the /login page.

Working for authentication

var passport = require('passport')
var ActiveDirectoryStrategy = require('passport-activedirectory')
var ActiveDirectory = require('activedirectory')

var ad = new ActiveDirectory({
  url: 'ldap://labnet.local',
  baseDN: 'DC=labnet,DC=local',
  username: '[email protected]',
  password: 'password_here'
})

passport.use(new ActiveDirectoryStrategy({
  integrated: false,
  ldap: ad
}, function (profile, ad, done) {
  ad.isUserMemberOf(profile._json.dn, 'AccessGroup', function (err, isMember) {
    if (err) return done(err)
    return done(null, profile)
  })
}))

Not working in passportjs

// Passport Requires
const passport = require('passport');
const session = require('express-session');
const express = require('express')
const bodyParser = require("body-parser");
const app = express()
const port = 3000
var ActiveDirectoryStrategy = require('passport-activedirectory');

// Setup
app.use(session({
    secret: 'secret',
    resave: false,
    saveUninitialized: false,
    cookie: { secure: false, maxAge: 600000 }
}));
app.use(bodyParser.json());
app.use(passport.initialize());
app.use(passport.session());

passport.serializeUser(function(user, done) {
    console.log('userStrategy -- serialized:', user)
    done(null, user);
});

passport.deserializeUser(function(user, done) {
    console.log('userStrategy -- deserializeUser', user)
    done(null, user);
});

passport.use(new ActiveDirectoryStrategy({
    integrated: false,
    passReqToCallback: true,
    ldap: {
  url: 'ldap://labnet.local',
  baseDN: 'CN=Domain Users,CN=Users,DC=labnet,DC=local',
  username: '[email protected]',
  password: 'password_here'
    }
}, function (req, profile, ad, done) {
    ad.isUserMemberOf(profile._json.dn, 'Domain Users', function (err, isMember) {
        console.log('isMember:', isMember)
        if (err) {
            return done(err)
        } else {
            return done(null, profile)
        }
    })
}))

// Login Route

app.post('/login',passport.authenticate('ActiveDirectory', { failWithError: true }),
    function (req, res) {
        console.log(res);
        console.log(req);
       console.log('Authenticated');
        return res.status(200).send(req.user);
    }, function (err) {
        console.log('Not Authenticated');
        return res.sendStatus(401).send(err);
    }
)


// Test endpoint to check whether user is authenticated
app.get('/test', function(req, res) {
    if (req.isAuthenticated()) {
        res.send('Youre authenticated!')
    } else {
        res.send('Youre not authenticated!')
    }
})

app.get('/', function(req, res){
        res.sendFile(__dirname + "/page.html", {} )
});

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`)//
})

any help would be greatly appreciated thanks.

@rodriguesabner
Copy link

rodriguesabner commented Apr 13, 2023

Hello!

Put a breakpoint in the 'index.js' file of the 'passport-active-directory' lib, on line 146 and see what options appear inside 'result'.

Here with me they appeared inside 'others' instead of 'users'.
I created this fork to help:

#22

image


and also added a custom filter when instantiating ldap

passport.use(new ActiveDirectoryStrategy({
            integrated: false,
            group: 'user',
            ldap: {
                url: url.href,
                baseDN: AD_BASE_DN,
                username: AD_USERNAME,
                password: AD_PASSWORD,
                filter: (username) => {
                    // return `(&(dn=${username})(objectClass=*))`;
                    return `(cn=${username})`;
                }
            }
        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants