Is there a good way to use Active Directory authentication with nestjs?

Viewed 2171

I'm in the process of creating a login system using nestjs framework as my back-end. As part of the process, my system has to communicate with Active Directory in order to authenticate a group of users. I was googling for a good amount of time and I couldn't find any way to integrate Active Directory with nestjs. Any kind of suggestion is encouraged.

1 Answers

The easiest way is to use the Passport utility module for nestjs:

https://github.com/nestjs/passport

Using passport you can implement the active directory strategy.

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

passport.use(new ActiveDirectoryStrategy({
  integrated: false,
  ldap: {
    url: 'ldap://my.domain.com',
    baseDN: 'DC=my,DC=domain,DC=com',
    username: 'readuser@my.domain.com',
    password: 'readuserspassword'
  }
}, function (profile, ad, done) {
  ad.isUserMemberOf(profile._json.dn, 'AccessGroup', function (err, isMember) {
   if (err) return done(err)
   return done(null, profile)
  })
}))
Related