Search code examples
laraveladldap

ADLDAP openLDAP authentication - Session not stored - returning to login page


My environment is a laravel 5.8 with adldap2 in version 6.0.8 web app and an openLDAP directory.

After hours, I finally could authenticate my user against the openLDAP directory and also the database import into the users table works:

Id name username password remember_token created_at updated_at
King king $2y$10$YF9q7cYqjYnkl.We4Evwv.u/a2sddrfBA3pohgpS2vR... j4AOUHSlkHE3IQW7bsgF7pOIY8EAss6iukfnKhwi2lqXR0eTjE... NULL NULL

When I check the variable user in the function: attemptLogin -> $this->guard()->login($user, true); it is from the DB and seems to be fine. But still after I log in, I also get the message "Redirecting to http://localhost/home.", it returns to the login page and is still not logged in.

For LDAP authentication I followed mostly this example: https://jotaelesalinas.github.io/laravel-simple-ldap-auth/ even if it is a bit obsolete.

My attemptLogin function looks like this:

protected function attemptLogin(Request $request)
{
    $username = Adldap::search()->users()->select('mail','uid','displayName')->findBy('cn', request()->get('username'));
    $result = 1;
    if($username){
        if(Adldap::auth()->attempt($username->getdistinguishedName(), request()->get('password'))){
            echo("success");
            // Check group
            $group = Adldap::search()->groups()->findOrFail('cio');
            foreach ($group->getMemberNames() as $name) {
                if($name === $username->getAccountName()){
                    echo("The user is a member of the group.");
                    $result = 0;
                }
            } 
            if ($result != 0){
                $result = 2;
            }
        } else {
            echo("Password wrong");
            $result = 1;
        }
    } else {
        echo(request()->get('username') . " not found");
        $result = 1;
    }

    if($result == 0) {
        // the user exists in the LDAP server, with the provided password
        echo("Everything ok");
        $user = \App\User::where($this->username(), $username->getAccountName())->first();
        if (!$user) {
            // the user doesn't exist in the local database, so we have to create one

            $user = new \App\User();
            $user->username = $username;
            $user->password = '';

            // you can skip this if there are no extra attributes to read from the LDAP server
            // or you can move it below this if(!$user) block if you want to keep the user always
            // in sync with the LDAP server 
            //dd($username->getDisplayName());
            $sync_attrs = $this->retrieveSyncAttributes($username->getAccountName());
            //dd($sync_attrs);
            foreach ($sync_attrs as $field => $value) {
                $user->$field = $value !== null ? $value : '';
            }
        }

        $this->guard()->login($user, true);
        return 0;
    }

    // the user doesn't exist in the LDAP server or the password is wrong
    // log error
    return $result;
}

Web.php

Route::get('login', 'Auth\LoginController@showLoginForm')->name('login');
Route::post('login', 'Auth\LoginController@login');
Route::post('logout', 'Auth\LoginController@logout')->name('logout');

Route::get('/home', 'HomeController@index')->name('home');

Has anyone an idea what I am missing? Or if you need more information, please tell me. It seems like the session is not stored.

Thanks in advance

Stephan


A small update after playing around some more hours. It seems like that Auth is after the successful login null. So tried different approaches I could find on the internet like changing the web.php routes or adding the protected $user variable to the LoginController.php but of course without any success.


I figured out that when I change the middleware from auth to web, I will get a session but the Auth::User() is still empty

 Route::group(['middleware' => 'auth'], function () {
    Route::get('/home', 'HomeController@index')->name('home');
}); 

Solution

  • After spending more and more hours, I finally found the solution in this thread: Laravel Auth:attempt() will not persist login

    My issue was that I was using "echo's".

    It cost me probably some days of my life