2 use BookStack\Auth\Role;
3 use BookStack\Auth\Access\Ldap;
4 use BookStack\Auth\User;
5 use Mockery\MockInterface;
7 class LdapTest extends BrowserKitTest
16 protected $resourceId = 'resource-test';
18 public function setUp()
21 if (!defined('LDAP_OPT_REFERRALS')) define('LDAP_OPT_REFERRALS', 1);
23 'auth.method' => 'ldap',
24 'services.ldap.base_dn' => 'dc=ldap,dc=local',
25 'services.ldap.email_attribute' => 'mail',
26 'services.ldap.display_name_attribute' => 'cn',
27 'services.ldap.user_to_groups' => false,
28 'auth.providers.users.driver' => 'ldap',
30 $this->mockLdap = \Mockery::mock(Ldap::class);
31 $this->app[Ldap::class] = $this->mockLdap;
32 $this->mockUser = factory(User::class)->make();
35 protected function mockEscapes($times = 1)
37 $this->mockLdap->shouldReceive('escape')->times($times)->andReturnUsing(function($val) {
38 return ldap_escape($val);
42 protected function mockExplodes($times = 1)
44 $this->mockLdap->shouldReceive('explodeDn')->times($times)->andReturnUsing(function($dn, $withAttrib) {
45 return ldap_explode_dn($dn, $withAttrib);
49 public function test_login()
51 $this->mockLdap->shouldReceive('connect')->once()->andReturn($this->resourceId);
52 $this->mockLdap->shouldReceive('setVersion')->once();
53 $this->mockLdap->shouldReceive('setOption')->times(4);
54 $this->mockLdap->shouldReceive('searchAndGetEntries')->times(4)
55 ->with($this->resourceId, config('services.ldap.base_dn'), \Mockery::type('string'), \Mockery::type('array'))
56 ->andReturn(['count' => 1, 0 => [
57 'uid' => [$this->mockUser->name],
58 'cn' => [$this->mockUser->name],
59 'dn' => ['dc=test' . config('services.ldap.base_dn')]
61 $this->mockLdap->shouldReceive('bind')->times(6)->andReturn(true);
62 $this->mockEscapes(4);
64 $this->visit('/login')
66 ->type($this->mockUser->name, '#username')
67 ->type($this->mockUser->password, '#password')
69 ->seePageIs('/login')->see('Please enter an email to use for this account.');
71 $this->type($this->mockUser->email, '#email')
74 ->see($this->mockUser->name)
75 ->seeInDatabase('users', ['email' => $this->mockUser->email, 'email_confirmed' => false, 'external_auth_id' => $this->mockUser->name]);
78 public function test_login_works_when_no_uid_provided_by_ldap_server()
80 $this->mockLdap->shouldReceive('connect')->once()->andReturn($this->resourceId);
81 $this->mockLdap->shouldReceive('setVersion')->once();
82 $ldapDn = 'cn=test-user,dc=test' . config('services.ldap.base_dn');
83 $this->mockLdap->shouldReceive('setOption')->times(2);
84 $this->mockLdap->shouldReceive('searchAndGetEntries')->times(2)
85 ->with($this->resourceId, config('services.ldap.base_dn'), \Mockery::type('string'), \Mockery::type('array'))
86 ->andReturn(['count' => 1, 0 => [
87 'cn' => [$this->mockUser->name],
89 'mail' => [$this->mockUser->email]
91 $this->mockLdap->shouldReceive('bind')->times(3)->andReturn(true);
92 $this->mockEscapes(2);
94 $this->visit('/login')
96 ->type($this->mockUser->name, '#username')
97 ->type($this->mockUser->password, '#password')
100 ->see($this->mockUser->name)
101 ->seeInDatabase('users', ['email' => $this->mockUser->email, 'email_confirmed' => false, 'external_auth_id' => $ldapDn]);
104 public function test_initial_incorrect_details()
106 $this->mockLdap->shouldReceive('connect')->once()->andReturn($this->resourceId);
107 $this->mockLdap->shouldReceive('setVersion')->once();
108 $this->mockLdap->shouldReceive('setOption')->times(2);
109 $this->mockLdap->shouldReceive('searchAndGetEntries')->times(2)
110 ->with($this->resourceId, config('services.ldap.base_dn'), \Mockery::type('string'), \Mockery::type('array'))
111 ->andReturn(['count' => 1, 0 => [
112 'uid' => [$this->mockUser->name],
113 'cn' => [$this->mockUser->name],
114 'dn' => ['dc=test' . config('services.ldap.base_dn')]
116 $this->mockLdap->shouldReceive('bind')->times(3)->andReturn(true, true, false);
117 $this->mockEscapes(2);
119 $this->visit('/login')
121 ->type($this->mockUser->name, '#username')
122 ->type($this->mockUser->password, '#password')
124 ->seePageIs('/login')->see('These credentials do not match our records.')
125 ->dontSeeInDatabase('users', ['external_auth_id' => $this->mockUser->name]);
128 public function test_create_user_form()
130 $this->asAdmin()->visit('/settings/users/create')
131 ->dontSee('Password')
132 ->type($this->mockUser->name, '#name')
133 ->type($this->mockUser->email, '#email')
135 ->see('The external auth id field is required.')
136 ->type($this->mockUser->name, '#external_auth_id')
138 ->seePageIs('/settings/users')
139 ->seeInDatabase('users', ['email' => $this->mockUser->email, 'external_auth_id' => $this->mockUser->name, 'email_confirmed' => true]);
142 public function test_user_edit_form()
144 $editUser = $this->getNormalUser();
145 $this->asAdmin()->visit('/settings/users/' . $editUser->id)
147 ->dontSee('Password')
148 ->type('test_auth_id', '#external_auth_id')
150 ->seePageIs('/settings/users')
151 ->seeInDatabase('users', ['email' => $editUser->email, 'external_auth_id' => 'test_auth_id']);
154 public function test_registration_disabled()
156 $this->visit('/register')
157 ->seePageIs('/login');
160 public function test_non_admins_cannot_change_auth_id()
162 $testUser = $this->getNormalUser();
163 $this->actingAs($testUser)->visit('/settings/users/' . $testUser->id)
164 ->dontSee('External Authentication');
167 public function test_login_maps_roles_and_retains_existing_roles()
169 $roleToReceive = factory(Role::class)->create(['name' => 'ldaptester', 'display_name' => 'LdapTester']);
170 $roleToReceive2 = factory(Role::class)->create(['name' => 'ldaptester-second', 'display_name' => 'LdapTester Second']);
171 $existingRole = factory(Role::class)->create(['name' => 'ldaptester-existing']);
172 $this->mockUser->forceFill(['external_auth_id' => $this->mockUser->name])->save();
173 $this->mockUser->attachRole($existingRole);
176 'services.ldap.user_to_groups' => true,
177 'services.ldap.group_attribute' => 'memberOf',
178 'services.ldap.remove_from_groups' => false,
180 $this->mockLdap->shouldReceive('connect')->times(2)->andReturn($this->resourceId);
181 $this->mockLdap->shouldReceive('setVersion')->times(2);
182 $this->mockLdap->shouldReceive('setOption')->times(5);
183 $this->mockLdap->shouldReceive('searchAndGetEntries')->times(5)
184 ->with($this->resourceId, config('services.ldap.base_dn'), \Mockery::type('string'), \Mockery::type('array'))
185 ->andReturn(['count' => 1, 0 => [
186 'uid' => [$this->mockUser->name],
187 'cn' => [$this->mockUser->name],
188 'dn' => ['dc=test' . config('services.ldap.base_dn')],
189 'mail' => [$this->mockUser->email],
192 0 => "cn=ldaptester,ou=groups,dc=example,dc=com",
193 1 => "cn=ldaptester-second,ou=groups,dc=example,dc=com",
196 $this->mockLdap->shouldReceive('bind')->times(6)->andReturn(true);
197 $this->mockEscapes(5);
198 $this->mockExplodes(6);
200 $this->visit('/login')
202 ->type($this->mockUser->name, '#username')
203 ->type($this->mockUser->password, '#password')
207 $user = User::where('email', $this->mockUser->email)->first();
208 $this->seeInDatabase('role_user', [
209 'user_id' => $user->id,
210 'role_id' => $roleToReceive->id
212 $this->seeInDatabase('role_user', [
213 'user_id' => $user->id,
214 'role_id' => $roleToReceive2->id
216 $this->seeInDatabase('role_user', [
217 'user_id' => $user->id,
218 'role_id' => $existingRole->id
222 public function test_login_maps_roles_and_removes_old_roles_if_set()
224 $roleToReceive = factory(Role::class)->create(['name' => 'ldaptester', 'display_name' => 'LdapTester']);
225 $existingRole = factory(Role::class)->create(['name' => 'ldaptester-existing']);
226 $this->mockUser->forceFill(['external_auth_id' => $this->mockUser->name])->save();
227 $this->mockUser->attachRole($existingRole);
230 'services.ldap.user_to_groups' => true,
231 'services.ldap.group_attribute' => 'memberOf',
232 'services.ldap.remove_from_groups' => true,
234 $this->mockLdap->shouldReceive('connect')->times(2)->andReturn($this->resourceId);
235 $this->mockLdap->shouldReceive('setVersion')->times(2);
236 $this->mockLdap->shouldReceive('setOption')->times(4);
237 $this->mockLdap->shouldReceive('searchAndGetEntries')->times(4)
238 ->with($this->resourceId, config('services.ldap.base_dn'), \Mockery::type('string'), \Mockery::type('array'))
239 ->andReturn(['count' => 1, 0 => [
240 'uid' => [$this->mockUser->name],
241 'cn' => [$this->mockUser->name],
242 'dn' => ['dc=test' . config('services.ldap.base_dn')],
243 'mail' => [$this->mockUser->email],
246 0 => "cn=ldaptester,ou=groups,dc=example,dc=com",
249 $this->mockLdap->shouldReceive('bind')->times(5)->andReturn(true);
250 $this->mockEscapes(4);
251 $this->mockExplodes(2);
253 $this->visit('/login')
255 ->type($this->mockUser->name, '#username')
256 ->type($this->mockUser->password, '#password')
260 $user = User::where('email', $this->mockUser->email)->first();
261 $this->seeInDatabase('role_user', [
262 'user_id' => $user->id,
263 'role_id' => $roleToReceive->id
265 $this->dontSeeInDatabase('role_user', [
266 'user_id' => $user->id,
267 'role_id' => $existingRole->id
271 public function test_external_auth_id_visible_in_roles_page_when_ldap_active()
273 $role = factory(Role::class)->create(['name' => 'ldaptester', 'external_auth_id' => 'ex-auth-a, test-second-param']);
274 $this->asAdmin()->visit('/settings/roles/' . $role->id)
278 public function test_login_maps_roles_using_external_auth_ids_if_set()
280 $roleToReceive = factory(Role::class)->create(['name' => 'ldaptester', 'external_auth_id' => 'test-second-param, ex-auth-a']);
281 $roleToNotReceive = factory(Role::class)->create(['name' => 'ldaptester-not-receive', 'display_name' => 'ex-auth-a', 'external_auth_id' => 'test-second-param']);
284 'services.ldap.user_to_groups' => true,
285 'services.ldap.group_attribute' => 'memberOf',
286 'services.ldap.remove_from_groups' => true,
288 $this->mockLdap->shouldReceive('connect')->times(2)->andReturn($this->resourceId);
289 $this->mockLdap->shouldReceive('setVersion')->times(2);
290 $this->mockLdap->shouldReceive('setOption')->times(4);
291 $this->mockLdap->shouldReceive('searchAndGetEntries')->times(4)
292 ->with($this->resourceId, config('services.ldap.base_dn'), \Mockery::type('string'), \Mockery::type('array'))
293 ->andReturn(['count' => 1, 0 => [
294 'uid' => [$this->mockUser->name],
295 'cn' => [$this->mockUser->name],
296 'dn' => ['dc=test' . config('services.ldap.base_dn')],
297 'mail' => [$this->mockUser->email],
300 0 => "cn=ex-auth-a,ou=groups,dc=example,dc=com",
303 $this->mockLdap->shouldReceive('bind')->times(5)->andReturn(true);
304 $this->mockEscapes(4);
305 $this->mockExplodes(2);
307 $this->visit('/login')
309 ->type($this->mockUser->name, '#username')
310 ->type($this->mockUser->password, '#password')
314 $user = User::where('email', $this->mockUser->email)->first();
315 $this->seeInDatabase('role_user', [
316 'user_id' => $user->id,
317 'role_id' => $roleToReceive->id
319 $this->dontSeeInDatabase('role_user', [
320 'user_id' => $user->id,
321 'role_id' => $roleToNotReceive->id
325 public function test_login_group_mapping_does_not_conflict_with_default_role()
327 $roleToReceive = factory(Role::class)->create(['name' => 'ldaptester', 'display_name' => 'LdapTester']);
328 $roleToReceive2 = factory(Role::class)->create(['name' => 'ldaptester-second', 'display_name' => 'LdapTester Second']);
329 $this->mockUser->forceFill(['external_auth_id' => $this->mockUser->name])->save();
331 setting()->put('registration-role', $roleToReceive->id);
334 'services.ldap.user_to_groups' => true,
335 'services.ldap.group_attribute' => 'memberOf',
336 'services.ldap.remove_from_groups' => true,
338 $this->mockLdap->shouldReceive('connect')->times(2)->andReturn($this->resourceId);
339 $this->mockLdap->shouldReceive('setVersion')->times(2);
340 $this->mockLdap->shouldReceive('setOption')->times(5);
341 $this->mockLdap->shouldReceive('searchAndGetEntries')->times(5)
342 ->with($this->resourceId, config('services.ldap.base_dn'), \Mockery::type('string'), \Mockery::type('array'))
343 ->andReturn(['count' => 1, 0 => [
344 'uid' => [$this->mockUser->name],
345 'cn' => [$this->mockUser->name],
346 'dn' => ['dc=test' . config('services.ldap.base_dn')],
347 'mail' => [$this->mockUser->email],
350 0 => "cn=ldaptester,ou=groups,dc=example,dc=com",
351 1 => "cn=ldaptester-second,ou=groups,dc=example,dc=com",
354 $this->mockLdap->shouldReceive('bind')->times(6)->andReturn(true);
355 $this->mockEscapes(5);
356 $this->mockExplodes(6);
358 $this->visit('/login')
360 ->type($this->mockUser->name, '#username')
361 ->type($this->mockUser->password, '#password')
365 $user = User::where('email', $this->mockUser->email)->first();
366 $this->seeInDatabase('role_user', [
367 'user_id' => $user->id,
368 'role_id' => $roleToReceive->id
370 $this->seeInDatabase('role_user', [
371 'user_id' => $user->id,
372 'role_id' => $roleToReceive2->id
376 public function test_login_uses_specified_display_name_attribute()
378 $originalAttribute = config('services.ldap.display_name_attribute');
380 'services.ldap.display_name_attribute' => 'displayName'
383 $this->mockLdap->shouldReceive('connect')->once()->andReturn($this->resourceId);
384 $this->mockLdap->shouldReceive('setVersion')->once();
385 $this->mockLdap->shouldReceive('setOption')->times(4);
386 $this->mockLdap->shouldReceive('searchAndGetEntries')->times(4)
387 ->with($this->resourceId, config('services.ldap.base_dn'), \Mockery::type('string'), \Mockery::type('array'))
388 ->andReturn(['count' => 1, 0 => [
389 'uid' => [$this->mockUser->name],
390 'cn' => [$this->mockUser->name],
391 'dn' => ['dc=test' . config('services.ldap.base_dn')],
392 'displayName' => 'displayNameAttribute'
394 $this->mockLdap->shouldReceive('bind')->times(6)->andReturn(true);
395 $this->mockEscapes(4);
397 $this->visit('/login')
399 ->type($this->mockUser->name, '#username')
400 ->type($this->mockUser->password, '#password')
402 ->seePageIs('/login')->see('Please enter an email to use for this account.');
404 $this->type($this->mockUser->email, '#email')
407 ->see('displayNameAttribute')
408 ->seeInDatabase('users', ['email' => $this->mockUser->email, 'email_confirmed' => false, 'external_auth_id' => $this->mockUser->name, 'name' => 'displayNameAttribute']);
411 'services.ldap.display_name_attribute' => $originalAttribute
415 public function test_login_uses_default_display_name_attribute_if_specified_not_present()
417 $originalAttribute = config('services.ldap.display_name_attribute');
419 'services.ldap.display_name_attribute' => 'displayName'
422 $this->mockLdap->shouldReceive('connect')->once()->andReturn($this->resourceId);
423 $this->mockLdap->shouldReceive('setVersion')->once();
424 $this->mockLdap->shouldReceive('setOption')->times(4);
425 $this->mockLdap->shouldReceive('searchAndGetEntries')->times(4)
426 ->with($this->resourceId, config('services.ldap.base_dn'), \Mockery::type('string'), \Mockery::type('array'))
427 ->andReturn(['count' => 1, 0 => [
428 'uid' => [$this->mockUser->name],
429 'cn' => [$this->mockUser->name],
430 'dn' => ['dc=test' . config('services.ldap.base_dn')]
432 $this->mockLdap->shouldReceive('bind')->times(6)->andReturn(true);
433 $this->mockEscapes(4);
435 $this->visit('/login')
437 ->type($this->mockUser->name, '#username')
438 ->type($this->mockUser->password, '#password')
440 ->seePageIs('/login')->see('Please enter an email to use for this account.');
442 $this->type($this->mockUser->email, '#email')
445 ->see($this->mockUser->name)
446 ->seeInDatabase('users', ['email' => $this->mockUser->email, 'email_confirmed' => false, 'external_auth_id' => $this->mockUser->name, 'name' => $this->mockUser->name]);
449 'services.ldap.display_name_attribute' => $originalAttribute