1
votes

I am trying to authenticate against active directory using passport-ldapauth in a Nestjs app. I don't have a service account and want to bind to Active Directory using a username as DN. I am trying to use the Asynchronous configuration retrieval but running into a problem when calling super() in the strategy class's constructor.

I get the following error:

src/ldap.strategy.ts:12:9 - error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. 12 super(this.getLdapConfig,

Any idea how I can make this work in a Nestjs app by extending the strategy interface/passing dynamic config method when calling super()?

my code:

import * as Strategy from 'passport-ldapauth';
import { PassportStrategy } from '@nestjs/passport';
import { Injectable, UnauthorizedException } from '@nestjs/common';
import { Request } from 'express';
import { readFileSync } from 'fs';
import { callbackify } from 'util';

@Injectable()
export class LdapStrategy extends PassportStrategy(Strategy, 'ldap') {
    constructor(
    ) {     
        super(this.getLdapConfig,
         async (req: Request, user: any, done) => {
            console.log(req);
            req.user = user;
            return done(null, user);
        });
    }

    getLdapConfig(req: Request, callback: any) {
        
        process.nextTick(() => {
        let opts = {
            passReqToCallback: true,
            server: {
                url: 'ldaps://eassec.cc.corp:636',
                bindDN: `CN=${req.username}`,
                bindCredentials: '${req.password}',
                tlsOptions: {
                    ca: [
                        readFileSync('./src/public.crt')
                    ],
                    rejectUnauthorized: false
                },
                searchBase: 'ou=BU-IT',
                searchFilter: `(&(&(objectClass=person)(objectClass=user))(sAMAccountName=${req.username}))`
                searchAttributes: ['displayName', 'mail'],
            }

        };

            callback(null, opts);
        });
    }

}
1
Have you tried extracting getLdapConfig to a const or function declared outside this class and then passing it into super? I don't see any reason why it needs to be a class methodJesse Carter
@Jesse Carter thanks that worked, stupid mistake... Please add this as the answer.user2094257
Nice! Glad that it helped out. It's an easy mistake to make I do screw up on stuff like this all the timeJesse Carter

1 Answers

0
votes

You could just extract getLdapConfig to a const (arrow function) or regular function declared outside the class and then pass it into the super call.

It doesn't reference any class members itself so there's no reason that it needs to be a class method.