angular-fullstack / generator-angular-fullstack

Yeoman generator for an Angular app with an Express server
https://awk34.gitbook.io/generator-angular-fullstack
6.12k stars 1.24k forks source link

LDAP Auth Sample #1786

Closed whittssg closed 8 years ago

whittssg commented 8 years ago

Hello,

Does anyone have a LDAP login example for this generator?

I have used https://github.com/vesse/passport-ldapauth before but i cant seem to figure out how to integrate it into this generator.

Thanks,

whittssg commented 8 years ago

Probably not the best at all but i changed a few things in the local passport login files to get it to work:

import passport from 'passport';
import {
    Strategy as LocalStrategy
}
from 'passport-local';

var LdapAuth = require('ldapauth-fork');

function localAuthenticate(User, email, password, done) {

    var ldap = new LdapAuth({
        url: "LDAP://",
        bindDn: "ldapuser",
        bindCredentials: "ldappass",
        searchBase: "DC=domain,DC=com",
        searchFilter: "(sAMAccountName={{username}})",
        cache: false
    });

    ldap.authenticate(email, password, function (err, ldapuser) {
        if (err) {
            console.log("LDAP Error: %s", err);
            return done(null, false, {
                message: 'The password or username is not correct.'
            });
        } else {

            User.findOneAsync({
                    email: ldapuser.mail.toLowerCase()
                })
                .then(user => {
                    if (!user) {
                        user = new User({
                            name: ldapuser.givenName + " " + ldapuser.sn,
                            email: ldapuser.mail.toLowerCase(),
                            role: 'user',
                            username: ldapuser.sAMAccountName.toLowerCase(),
                            provider: 'local'
                        });
                        user.saveAsync()

                            .then(user => done(null, user))
                            .catch(err => done(err));
                        return done(null, user);

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

                })
                .catch(err => {
                    console.log("Error: ", err);
                    done(err)
                });

        }
    });
}

export function setup(User, config) {

    passport.use(new LocalStrategy({

        usernameField: 'email',
        passwordField: 'password' // this is the virtual field on the model
    }, function (email, password, done) {
        return localAuthenticate(User, email, password, done);
    }));
}

Does this look OK? i am new to passport and javascript in general :)

Awk34 commented 8 years ago

Questions like this are better served on a question/answer site like Stack Overflow;

whittssg commented 8 years ago

OK no problem, just thought you guys might have a sample hanging around. I will post it there.

Awk34 commented 8 years ago

If you get it working, feel free to submit it to the Wiki. I try to keep the issues section to bugs, feature requests, and questions specific to this project.

EmilianoGaytan commented 6 years ago

consider using the npm install https://www.npmjs.com/package/passport-ldapauth