title: Authentication/login in Laravel using LDAP with no admin connection?
body: <p>I am trying to get Laravel 5.4 to authenticate users using LDAP instead of the traditional database method.</p>
  
  <p>There is a library for that already, <a href="https://github.com/Adldap2/Adldap2-Laravel" rel="nofollow noreferrer">Adldap2/Adldap2-Laravel</a>. The problem is that this library:</p>
  
  <ol>
  <li>connects first the the LDAP server as an admin user</li>
  <li>then searches for the user that wants to log into my app</li>
  <li>checks that the password is correct</li>
  <li>stores the data associated to that user in the "local" Laravel database</li>
  <li>reconnects to the LDAP as the admin user</li>
  </ol>
  
  <p>This doesn't work in my case, because there are no admins in the LDAP server that I   ̶w̶a̶n̶t̶    have to connect to; only usernames allowed to use the app, with no roles.</p>
  
  <p>As seen in an <a href="https://github.com/Adldap2/Adldap2-Laravel/issues/143" rel="nofollow noreferrer">already existing issue</a>, I modified the <code>LoginController</code> class with the follwoing code (for testing purposes I am trying to connect to the server provided in this web page: <a href="http://www.forumsys.com/tutorials/integration-how-to/ldap/online-ldap-test-server/" rel="nofollow noreferrer">Online LDAP Test Server</a>).</p>
  
  <pre><code>protected function attemptLogin(Request $request) {
      $conn_settings = config('adldap.connections')[config('adldap_auth.connection')]['connection_settings'];
      $credentials = $request-&gt;only(config('adldap_auth.usernames.eloquent'), 'password');
  
      $user_format = env('ADLDAP_USER_FORMAT', 'uid=%s,' . $conn_settings['base_dn']);
      $userdn = sprintf($user_format, $credentials[config('adldap_auth.usernames.eloquent')]);
      $pass = $credentials['password'];
  
      if(Adldap::auth()-&gt;attempt($userdn, $pass, $bindAsUser = true)) {
          return true;
      }
  
      return false;
  }
  </code></pre>
  
  <p>The problem that I have now is that, once logged in with the correct credentials, I am sent back to the login form. When the credentials are wrong, I get the corresponding error message.</p>
  
  <p>My configuration files, just in case:</p>
  
  <p>.env:</p>
  
  <pre><code>ADLDAP_CONTROLLERS=ldap.forumsys.com 
  ADLDAP_BASEDN=dc=example,dc=com
  ADLDAP_USER_FORMAT=uid=%s,dc=example,dc=com
  </code></pre>
  
  <p>adldap_auth.php:</p>
  
  <pre><code>[
      'connection' =&gt; env('ADLDAP_CONNECTION', 'default'),
      'provider' =&gt; Adldap\Laravel\Auth\DatabaseUserProvider::class,
      'resolver' =&gt; Adldap\Laravel\Auth\Resolver::class,
      'importer' =&gt; Adldap\Laravel\Auth\Importer::class,
      'rules' =&gt; [
          Adldap\Laravel\Validation\Rules\DenyTrashed::class,
          // Adldap\Laravel\Validation\Rules\OnlyImported::class,
      ],
      'scopes' =&gt; [
          Adldap\Laravel\Scopes\UpnScope::class,
      ],
      'usernames' =&gt; [
          'ldap' =&gt; 'uid',
          'eloquent' =&gt; 'username',
      ],
      'login_fallback' =&gt; env('ADLDAP_LOGIN_FALLBACK', false),
      'password_sync' =&gt; env('ADLDAP_PASSWORD_SYNC', true),
      'windows_auth_attribute' =&gt; ['samaccountname' =&gt; 'AUTH_USER'],
      'sync_attributes' =&gt; [
          'username' =&gt; 'uid',
          'name' =&gt; 'cn',
      ],
  ];
  </code></pre>
  
  <p>adldap.php:</p>
  
  <pre><code>return [
      'connections' =&gt; [
          'default' =&gt; [
              'auto_connect' =&gt; false,
              'connection' =&gt; Adldap\Connections\Ldap::class,
              'schema' =&gt; Adldap\Schemas\ActiveDirectory::class,
              'connection_settings' =&gt; [
                  'account_prefix' =&gt; env('ADLDAP_ACCOUNT_PREFIX', ''),
                  'account_suffix' =&gt; env('ADLDAP_ACCOUNT_SUFFIX', ''),
                  'domain_controllers' =&gt; explode(' ', env('ADLDAP_CONTROLLERS', 'corp-dc1.corp.acme.org corp-dc2.corp.acme.org')),
                  'port' =&gt; env('ADLDAP_PORT', 389),
                  'timeout' =&gt; env('ADLDAP_TIMEOUT', 5),
                  'base_dn' =&gt; env('ADLDAP_BASEDN', 'dc=corp,dc=acme,dc=org'),
                  'admin_account_suffix' =&gt; env('ADLDAP_ADMIN_ACCOUNT_SUFFIX', ''),
                  'admin_username' =&gt; env('ADLDAP_ADMIN_USERNAME', ''),
                  'admin_password' =&gt; env('ADLDAP_ADMIN_PASSWORD', ''),
                  'follow_referrals' =&gt; true,
                  'use_ssl' =&gt; false,
                  'use_tls' =&gt; false,
              ],
          ],
      ],
  ]
  </code></pre>