bhoriuchi / passport-activedirectory

Active Directory strategy for passport.js
29 stars 16 forks source link

Error when updating to 1.3.0 #17

Open wickettc opened 1 year ago

wickettc commented 1 year ago

After upgrading to the latest version 1.3.0 from version 1.0.4 getting this error on server start

TypeError: displayName,objectGUID,givenName,sn,title,mail is not an Option Object

passport config file

  passport.use(new ActiveDirectoryStrategy({
    // Set integrated to false for username and password authentication
    integrated: false,
    ldap: {
      url: env.ldap.serverURL,
      baseDN: env.ldap.searchBase,
      username: env.ldap.appDN,
      password: env.ldap.appPassword,
      // Sets which LDAP attributes are returned in the user object
      attributes: [
        'displayName',
        'objectGUID',
        'givenName',
        'sn',
        'title',
        'mail',
      ],
    },

Removing the attributes array prevents this error from occurring.

bhoriuchi commented 1 year ago

1.1.0 moved to the activedirectory2 package which appears to have a slightly different api. It takes an object with keys user and group whos values are the attributes you wish to return so you can try

  passport.use(new ActiveDirectoryStrategy({
    // Set integrated to false for username and password authentication
    integrated: false,
    ldap: {
      url: env.ldap.serverURL,
      baseDN: env.ldap.searchBase,
      username: env.ldap.appDN,
      password: env.ldap.appPassword,
      // Sets which LDAP attributes are returned in the user object
      attributes: {
        user: [
          'displayName',
          'objectGUID',
          'givenName',
          'sn',
          'title',
          'mail',
        ]
      },
    },
nguThapelo commented 1 year ago

I am receiving the same error on a NextJS application. I have tried the above mentioned solution to change the attributes value to an object. I copied the object straight from the activedirectory2 docs.

However, a new error came up.

See below

TypeError: argument must be a string (was: object).

I have checked and it only occurs when I use an object as the value for the attributes key.

See Login Strategy below

const attributes = {
  user: [
    'dn', 'distinguishedName',
    'userPrincipalName', 'sAMAccountName', 'mail',
    'lockoutTime', 'whenCreated', 'pwdLastSet', 'userAccountControl',
    'employeeID', 'sn', 'givenName', 'initials', 'cn', 'displayName',
    'comment', 'description'
  ],
  group: [
    'dn', 'cn', 'description', 'distinguishedName', 'objectCategory'
  ]
};

passport.use(new ActiveDirectoryStrategy({
  integrated: false,
  ldap: {
    url: "******************",
    baseDN: dn,
    username: '************',
    password: '************',
    attributes: attributes
  }
}, async function (profile, ad, done) {
  console.log(`ad: `, ad);
  console.log(`profile: `, profile);

  let user = profile._json;

  console.log(`AD user: `, user);

 return done(null, user);
}));