vesse / passport-ldapauth

LDAP authentication strategy for Passport
MIT License
312 stars 100 forks source link

How to use dynamic ldap config options in a Nestjs app? #103

Closed DeonNel1 closed 3 years ago

DeonNel1 commented 4 years ago

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);
    });
}

}If you know how to fix the issue, make a pull request instead.

Note: if the issue template is not used, the issue will be closed.

Problem Description

Steps to Reproduce

vesse commented 3 years ago

Pretty easy to see that the error is not caused by this module as the file src/ldap.strategy.ts is not even in this repository.