shariff's avatar
Level 51

laravel adldap2/adldap2-laravel package

Hi

I am implementing LDAP authentication in laravel app. I am not getting LDAP users when I run this code I am getting an empty collection.

 protected function attemptLogin(Request $request)
    {
       $ldap = new Adldap;
       $data = Adldap::getDefaultProvider()->search()->users()->get();
   
       dd($data);

    }

I got LDAP credential from clients but I am not understanding how to add in .env files.

the credential which I got

User Name: username
 Password: password
 IP:172.16.xx.xx
 Port: 389
 Attributes: CN=user.admin,CN=Users,DC=UATADSRV,DC=COM

my .env file

LDAP_HOSTS=172.16.xx.xx
LDAP_BASE_DN=CN=user.admin,CN=Users,DC=UATADSRV,DC=COM
LDAP_USER_ATTRIBUTE=samaccountname
LDAP_CONNECTION=default
LDAP_USERNAME=username
LDAP_PASSWORD=password

ldap.php

return [

    'logging' => env('LDAP_LOGGING', false),

    'connections' => [

        'default' => [
   'auto_connect' => env('LDAP_AUTO_CONNECT', true),

            'connection' => Adldap\Connections\Ldap::class,
       
            'settings' => [
    'schema' => Adldap\Schemas\ActiveDirectory::class,


                'account_prefix' => env('LDAP_ACCOUNT_PREFIX', ''),

                'account_suffix' => env('LDAP_ACCOUNT_SUFFIX', ''),   

                'hosts' => explode(' ', env('LDAP_HOSTS', 'corp-dc1.corp.acme.org corp-dc2.corp.acme.org')),

         'port' => env('LDAP_PORT', 389),

                'timeout' => env('LDAP_TIMEOUT', 5),

                'base_dn' => env('LDAP_BASE_DN', 'dc=corp,dc=acme,dc=org'),

                'username' => env('LDAP_USERNAME'),
                'password' => env('LDAP_PASSWORD'),

                'follow_referrals' => false,


                'use_ssl' => env('LDAP_USE_SSL', false),
                'use_tls' => env('LDAP_USE_TLS', false),

            ],

        ],

    ],

];


ldap_auth.php

return [

    'connection' => env('LDAP_CONNECTION', 'default'),

    'provider' => Adldap\Laravel\Auth\DatabaseUserProvider::class,

    'model' => App\User::class,
    'rules' => [

        // Denys deleted users from authenticating.

        Adldap\Laravel\Validation\Rules\DenyTrashed::class,

        // Allows only manually imported users to authenticate.

        // Adldap\Laravel\Validation\Rules\OnlyImported::class,

    ],

    /*
    |--------------------------------------------------------------------------
    | Scopes
    |--------------------------------------------------------------------------
    |
    | Scopes allow you to restrict the LDAP query that locates
    | users upon import and authentication.
    |
    | All scopes must implement the following interface:
    |
    |   Adldap\Laravel\Scopes\ScopeInterface
    |
    */

    'scopes' => [

        // Only allows users with a user principal name to authenticate.
        // Suitable when using ActiveDirectory.
        // Adldap\Laravel\Scopes\UpnScope::class,

        // Only allows users with a uid to authenticate.
        // Suitable when using OpenLDAP.
        // Adldap\Laravel\Scopes\UidScope::class,

    ],

    'identifiers' => [

    'rules' => [

        // Denys deleted users from authenticating.

        Adldap\Laravel\Validation\Rules\DenyTrashed::class,

        // Allows only manually imported users to authenticate.

        // Adldap\Laravel\Validation\Rules\OnlyImported::class,

    ],

    /*
    |--------------------------------------------------------------------------
    | Scopes
    |--------------------------------------------------------------------------
    |
    | Scopes allow you to restrict the LDAP query that locates
    | users upon import and authentication.
    |
    | All scopes must implement the following interface:
    |
    |   Adldap\Laravel\Scopes\ScopeInterface
    |
    */

    'scopes' => [

        // Only allows users with a user principal name to authenticate.
        // Suitable when using ActiveDirectory.
        // Adldap\Laravel\Scopes\UpnScope::class,

        // Only allows users with a uid to authenticate.
        // Suitable when using OpenLDAP.
        // Adldap\Laravel\Scopes\UidScope::class,

    ],

    'identifiers' => [

 'ldap' => [

         //   'locate_users_by' => 'userprincipalname',
             'locate_users_by' => 'samaccountname',

            'bind_users_by' => 'distinguishedname',

        ],

        'database' => [

            'guid_column' => 'objectguid',
           'username_column' => 'username',

        ],
  'windows' => [

            'locate_users_by' => 'samaccountname',

            'server_key' => 'AUTH_USER',

        ],

    ],

    'passwords' => [

     

        'sync' => env('LDAP_PASSWORD_SYNC', false),

      

        'column' => 'password',
  ],


    'login_fallback' => env('LDAP_LOGIN_FALLBACK', false),

  

    'sync_attributes' => [

        'email' => 'userprincipalname',
        'username' => 'samaccountname',
        'name' => 'cn',

    ],

  'logging' => [

        'enabled' => env('LDAP_LOGGING', true),

        'events' => [

            \Adldap\Laravel\Events\Importing::class                 => \Adldap\Laravel\Listeners\LogImport::class,
            \Adldap\Laravel\Events\Synchronized::class              => \Adldap\Laravel\Listeners\LogSynchronized::class,
            \Adldap\Laravel\Events\Synchronizing::class             => \Adldap\Laravel\Listeners\LogSynchronizing::class,
            \Adldap\Laravel\Events\Authenticated::class             => \Adldap\Laravel\Listeners\LogAuthenticated::class,
            \Adldap\Laravel\Events\Authenticating::class            => \Adldap\Laravel\Listeners\LogAuthentication::class,
            \Adldap\Laravel\Events\AuthenticationFailed::class      => \Adldap\Laravel\Listeners\LogAuthenticationFailure::class,
            \Adldap\Laravel\Events\AuthenticationRejected::class    => \Adldap\Laravel\Listeners\LogAuthenticationRejection::class,
            \Adldap\Laravel\Events\AuthenticationSuccessful::class  => \Adldap\Laravel\Listeners\LogAuthenticationSuccess::class,
            \Adldap\Laravel\Events\DiscoveredWithCredentials::class => \Adldap\Laravel\Listeners\LogDiscovery::class,
            \Adldap\Laravel\Events\AuthenticatedWithWindows::class  => \Adldap\Laravel\Listeners\LogWindowsAuth::class,
            \Adldap\Laravel\Events\AuthenticatedModelTrashed::class => \Adldap\Laravel\Listeners\LogTrashedModel::class,

        ],
    ],

];


I don't whether it is correct or not. Please help me out how to test LDAP connection using this credential.

0 likes
4 replies
jove's avatar

I had a lot of issues when using this myself. I have two things to suggest, check that the users are indeed in that OU and check the logs of the LDAP server to see if you are providing something wrong.

Also I found this while googling for the documentation to try see if you had done something wrong (that I could see) https://github.com/DirectoryTree/LdapRecord-Laravel Have not tested, but that looks promising.

shariff's avatar
Level 51

@jove thanks for helping. I try with that.

What I want is I just want to see whether the user present in the LDAP server. If the user is present then I need to check in my local database. If the user is present then I need to allow to view my application. The main thing is I cannot correct the configuration setting.

If you have, Can you please share your configuration settings it will be very very helpful.

Thank you

jove's avatar

@matheenulla Your config is identical to mine, but I do not have CN in my base dn and I belive I also was given it but it didn't work. Try search without it and see if that works

Please or to participate in this conversation.