0

I am trying to make authentication with ActiveDirectory using ldaprecord-laravel. I followed the documentation and made required changes in files. However, I ended up with only php artisan ldap:test working and php artisan ldap:import ldap showing that there are no users to import.

When I use online LDAP test server, I can go further and make Auth::attempt(['uid' => 'einstein', 'password' => 'password']) in Tinker, and import works, but the web login still doesn't work. With AD, I can't auth attempt using neither samaccountname, nor username, nor uid. Though plain auth using ldap_connect and ldap_bind works.

App/User.php

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Support\Facades\Hash; 
use LdapRecord\Laravel\Auth\LdapAuthenticatable;
use LdapRecord\Laravel\Auth\AuthenticatesWithLdap;

class User extends Authenticatable implements LdapAuthenticatable
{
    use Notifiable, AuthenticatesWithLdap;

    protected $table = 'users';
    protected $primaryKey = 'id';
    public $timestamps = false;
    public $incrementing = false;

    /*
    public function getAuthPassword()
    {
        return Hash::make( $this->user_pass );
    }
    */

    /**
     * Настройки пользователя.
     *
     * @return HasMany
     */
    public function settings()
    {
        return $this->hasMany(Models\Settings::class, 'id', 'id');
    }

}

App/Http/Controllers/Auth/LoginController.php

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use LdapRecord\Laravel\Auth\ListensForLdapBindFailure;

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, ListensForLdapBindFailure;

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

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

    /**
     * Переопределяем переменную, в которой хранится логин пользователя
     *
     * @return string
     */
    public function username()
    {
        return 'user_login';
    }

    /**
     * Валидация данных на сервере
     *
     * @param  Request $request
     *
     * @return void
     */
    protected function validateLogin(Request $request)
    {
        $request->validate([
            $this->username() => 'required|string',
            'password' => 'required|string',
        ]);
    }  

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

How can I find out what causes the problem?

Danatela
  • 349
  • 8
  • 28

1 Answers1

0

Troubleshooting in Laravel is usually done with logging. According to the given document, you may log string using

use Illuminate\Support\Facades\Log;

// somewhere in the code
Log::debug('info string');

Laravel puts it's logs in storage/logs folder. There are such entries in the log:

[2021-08-24 10:41:13] local.INFO: LDAP (ldap://ldap.forumsys.com:389) - Operation: Bound - Username: cn=read-only-admin,dc=example,dc=com  
[2021-08-24 10:35:54] local.INFO: LDAP (ldap://ldap.forumsys.com:389) - Operation: Search - Base DN: dc=example,dc=com - Filter: (&(objectclass=\74\6f\70)(objectclass=\70\65\72\73\6f\6e)(objectclass=\6f\72\67\61\6e\69\7a\61\74\69\6f\6e\61\6c\70\65\72\73\6f\6e)(objectclass=\69\6e\65\74\6f\72\67\70\65\72\73\6f\6e)(uid=)) - Selected: (entryuuid,*) - Time Elapsed: 471.78  

We see that uid is not given, and it is because we use user_login instead of username, so the final decision was to change LoginController.php:

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

After doing that, logging in was successful.

Danatela
  • 349
  • 8
  • 28