From d32460070f8e608ec20cc58ddcfa137057086dc5 Mon Sep 17 00:00:00 2001 From: Dan Brown Date: Mon, 8 Feb 2016 19:45:01 +0000 Subject: [PATCH] Made ldap auth use the 'dn' if a 'uid' is not present. Fixes #56 --- app/Services/LdapService.php | 2 +- ...1_11_210908_add_external_auth_to_users.php | 2 +- tests/Auth/LdapTest.php | 28 +++++++++++++++++-- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/app/Services/LdapService.php b/app/Services/LdapService.php index 84883b09a..3d89e1e44 100644 --- a/app/Services/LdapService.php +++ b/app/Services/LdapService.php @@ -46,7 +46,7 @@ class LdapService $user = $users[0]; return [ - 'uid' => $user['uid'][0], + 'uid' => (isset($user['uid'])) ? $user['uid'][0] : $user['dn'], 'name' => $user['cn'][0], 'dn' => $user['dn'], 'email' => (isset($user['mail'])) ? $user['mail'][0] : null diff --git a/database/migrations/2016_01_11_210908_add_external_auth_to_users.php b/database/migrations/2016_01_11_210908_add_external_auth_to_users.php index dda8f3d74..b7663054c 100644 --- a/database/migrations/2016_01_11_210908_add_external_auth_to_users.php +++ b/database/migrations/2016_01_11_210908_add_external_auth_to_users.php @@ -28,4 +28,4 @@ class AddExternalAuthToUsers extends Migration $table->dropColumn('external_auth_id'); }); } -} +} \ No newline at end of file diff --git a/tests/Auth/LdapTest.php b/tests/Auth/LdapTest.php index 14f2f8196..d80b8d50d 100644 --- a/tests/Auth/LdapTest.php +++ b/tests/Auth/LdapTest.php @@ -28,7 +28,7 @@ class LdapTest extends \TestCase ->andReturn(['count' => 1, 0 => [ 'uid' => [$this->mockUser->name], 'cn' => [$this->mockUser->name], - 'dn' => ['dc=test'.config('services.ldap.base_dn')] + 'dn' => ['dc=test' . config('services.ldap.base_dn')] ]]); $this->mockLdap->shouldReceive('bind')->times(6)->andReturn(true); @@ -46,6 +46,30 @@ class LdapTest extends \TestCase ->seeInDatabase('users', ['email' => $this->mockUser->email, 'email_confirmed' => 1, 'external_auth_id' => $this->mockUser->name]); } + public function test_login_works_when_no_uid_provided_by_ldap_server() + { + $this->mockLdap->shouldReceive('connect')->once()->andReturn($this->resourceId); + $this->mockLdap->shouldReceive('setOption')->once(); + $ldapDn = 'cn=test-user,dc=test' . config('services.ldap.base_dn'); + $this->mockLdap->shouldReceive('searchAndGetEntries')->times(2) + ->with($this->resourceId, config('services.ldap.base_dn'), Mockery::type('string'), Mockery::type('array')) + ->andReturn(['count' => 1, 0 => [ + 'cn' => [$this->mockUser->name], + 'dn' => $ldapDn, + 'mail' => [$this->mockUser->email] + ]]); + $this->mockLdap->shouldReceive('bind')->times(3)->andReturn(true); + + $this->visit('/login') + ->see('Username') + ->type($this->mockUser->name, '#username') + ->type($this->mockUser->password, '#password') + ->press('Sign In') + ->seePageIs('/') + ->see($this->mockUser->name) + ->seeInDatabase('users', ['email' => $this->mockUser->email, 'email_confirmed' => 1, 'external_auth_id' => $ldapDn]); + } + public function test_initial_incorrect_details() { $this->mockLdap->shouldReceive('connect')->once()->andReturn($this->resourceId); @@ -55,7 +79,7 @@ class LdapTest extends \TestCase ->andReturn(['count' => 1, 0 => [ 'uid' => [$this->mockUser->name], 'cn' => [$this->mockUser->name], - 'dn' => ['dc=test'.config('services.ldap.base_dn')] + 'dn' => ['dc=test' . config('services.ldap.base_dn')] ]]); $this->mockLdap->shouldReceive('bind')->times(3)->andReturn(true, true, false);