php – Laravel LDAP Authentication not working – LdapRecord Package

exception or error:

I’m trying to implements an LDAP authentication in an application for my company. I’m using Laravel 5.8 and the LdapRecord package (https://github.com/DirectoryTree/LdapRecord-Laravel).

I have succeed to connect the application with the LDAP server but the authentication still not working and idk why… 🙁

Here is my code :

The LoginController

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use LdapRecord\Container;
use Illuminate\Http\Request;
use App\User;

class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */
    use AuthenticatesUsers;

    /**
     * LDAP Connection 
     */
    private $connection;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/home';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->connection = Container::getConnection('default');
        $this->middleware('guest')->except('logout');
    }

    public function username() {
        return 'username';
    }

    protected function credentials(Request $request)
    {
        return [
            'comptent' => $request->username,
            'password' => $request->password,
        ];
    }
}

Here is the configuration – auth.php :

    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'ldap',
        ],
// some code
    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\Ldap\User::class,
        ],
        'ldap' => [
            'driver' => 'ldap',
            'model' => LdapRecord\Models\ActiveDirectory\User::class,
            'rules' => [],
            'database' => [
                'model' => App\Ldap\User::class,
                'sync_passwords' => false,
                'sync_attributes' => [
                    'LASTNAME' => 'sn',
                    'FIRSTNAME' => 'givenname',
                    'ACTIVE_DIRECTORY_USER' => 'comptent'

The user model – User.php:

<?php

namespace App\Ldap;

//use Illuminate\Database\Eloquent\Model;
use LdapRecord\Laravel\Auth\HasLdapUser;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use LdapRecord\Laravel\Auth\AuthenticatesWithLdap;
use LdapRecord\Laravel\Auth\LdapAuthenticatable;
use LdapRecord\Models\Model;

/**
 * Class User
 * 
 * @property int $ID_USER
 * @property string $LASTNAME
 * @property string $FIRSTNAME
 * @property string $ACTIVE_DIRECTORY_USER
 * @property int $ID_ROLE
 * 
 * @property Role $role
 *
 * @package App\Models
 */
class User extends Model
{

    use Notifiable, AuthenticatesWithLdap;

    /**
     * The object classes of the LDAP model.
     *
     * @var array
     */
    public static $objectClasses = [];
    protected $table = 'user';
    protected $primaryKey = 'ID_USER';
    public $incrementing = false;
    public $timestamps = false;

    protected $casts = [
        'ID_USER' => 'int',
        'ID_ROLE' => 'int'
    ];

    protected $fillable = [
        'LASTNAME',
        'FIRSTNAME',
        'ACTIVE_DIRECTORY_USER',
        'ID_ROLE'
    ];

    public function role()
    {
        return $this->belongsTo(Role::class, 'ID_ROLE');
    }
}

Here is some logs that i have when i try to login :

[2020-02-19 15:49:12] local.INFO: LDAP (ldap://srv-gldap1:389) - Operation: Listing - Base DN: ou=utilisateurs,dc=rms,dc=fr - Filter: (objectclass=*) - Selected: (*) - Time Elapsed: 922.65  
[2020-02-19 15:49:12] local.INFO: LDAP (ldap://srvil-gdldap1:389) - Operation: Search - Base DN: ou=utilisateurs,dc=rms,dc=fr - Filter: (comptentdomaine=hippolyte.massicot@rms.local) - Selected: (*) - Time Elapsed: 101.91

Is there anyone who has already used this package and can help me on my code ?
thank you in advance 🙂

How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *