tyler36/ldap

Authenticate in Laravel via LDAP

1.0 2021-04-22 07:06 UTC

This package is auto-updated.

Last update: 2024-05-13 09:02:16 UTC


README

Latest Version on Packagist Total Downloads Build Status StyleCI

This package is designed to offer a quick authentication process via LDAP. The example below describes a setup that authenticates using a username and ```password`` combination.

Installation

Via Composer

$ composer require tyler36/ldap

Usage

  1. Publish the config file
$ php artisan vendor:publish --provider="Tyler36\Ldap\LdapServiceProvider"
  1. Update .ENV with server settings
LDAP_HOST=
LDAP_USERNAME=
LDAP_USERNAME_PREFIX=
LDAP_FILTER=
LDAP_DOMAIN_COMP=
LDAP_COMMON_NAME=
LDAP_BASE_DN=
  1. Add a username column to your user migration.
$table->string('username')->unique();
  1. Update login view by replacing email with username. Remember to remove the type="email"
<input id="username"
    class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline{{ $errors->has('username') ? ' border-red-500' : '' }}"
    name="username" value="{{ old('username') }}" required autofocus>

Laravel 7

  1. Update your LoginController file. Remember to import Tyler36\Ldap\LdapAuthenticator;
/**
  * Get the login username to be used by the controller. ['email']
  *
  * @return string
  */
public function username()
{
    return config('ldap.username');
}

/**
 * Attempt to log the user into the application.
 *
 * @param \Illuminate\Http\Request $request
 *
 * @return mixed
 */
protected function attemptLogin(Request $request)
{
    // This is where authentication happens. It SHOULD return an array containing the user
    $ldap     = new LdapAuthenticator($request);
    $ldapUser = $ldap->authenticate();
    if (!$ldapUser || 'array' !== gettype($ldapUser)) {
        return $ldapUser;
    }

    // Un-comment the following to see details of the user array
    // dd($ldapUser)

    // Update or create a new user based on the username. The second array determines how to populate new users.
    $user   = User::updateOrCreate(
        [$this->username() => $request->get('username')],
        [
            $this->username() => $request->get('username'),
            'name'            => optional($ldapUser)['displayname'][0],
            'email'           => optional($ldapUser)['mail'][0],
        ]
    );

    auth()->login($user);
}

laravel/breeze

  • Replace the rules section in app/Http/Requests/Auth/LoginRequest.php.
    public function rules()
    {
        return config('ldap.rules');
    }

Change log

Please see the changelog for more information on what has changed recently.

Testing

$ composer test

Contributing

Please see contributing.md for details and a todolist.

Security

If you discover any security related issues, please email author email instead of using the issue tracker.

Credits

License

license. Please see the license file for more information.