ldap = new FakeLdap; $this->app->instance(LdapConnector::class, $this->ldap); $this->guid = hex2bin('e004253f894fd3119a0c0305e82c3301'); $this->ldap->add('CN=svc-portal,DC=firma,DC=local', 'dienst', []); $this->ldap->add(self::ANNA, 'richtig', [ 'objectGUID' => $this->guid, 'sAMAccountName' => 'aschmidt', 'userPrincipalName' => 'aschmidt@firma.local', 'givenName' => 'Anna', 'sn' => 'Schmidt', 'displayName' => 'Anna Schmidt', 'mail' => 'anna@firma.de', 'title' => 'Pflegedienstleitung', 'department' => 'Pflege', 'memberOf' => ['CN=GG-Pflege,OU=Gruppen,DC=firma,DC=local'], 'manager' => self::CHEF, 'thumbnailPhoto' => 'binär', 'userAccountControl' => '512', ]); $this->ldap->add(self::CHEF, 'chef', ['objectGUID' => hex2bin('00112233445566778899aabbccddeeff'), 'sAMAccountName' => 'cchefin', 'mail' => 'clara@firma.de', 'givenName' => 'Clara', 'sn' => 'Chefin']); $this->ldap->nestedGroups[self::ANNA] = ['CN=GG-Pflege,OU=Gruppen,DC=firma,DC=local', 'CN=Alle-Mitarbeitenden,OU=Gruppen,DC=firma,DC=local']; } private function provider(array $config = []): IdentityProvider { return IdentityProvider::query()->create([ 'type' => 'ldap', 'name' => 'Active Directory', 'slug' => 'ad', 'enabled' => true, 'config' => array_merge([ 'schema' => 'ad', 'hosts' => 'dc1.firma.local dc2.firma.local', 'encryption' => 'ldaps', 'base_dn' => 'DC=firma,DC=local', 'bind_dn' => 'CN=svc-portal,DC=firma,DC=local', 'bind_password' => 'dienst', 'group_search' => true, 'auto_provision' => true, 'sync_manager' => true, 'sync_photo' => false, ], $config), ]); } public function test_login_form_and_successful_login_with_profile_groups_and_manager(): void { $provider = $this->provider(); $role = Role::query()->create(['name' => 'Alle']); $provider->mappings()->create(['kind' => IdpGroupMapping::GROUP, 'external_id' => 'cn=alle-mitarbeitenden,ou=gruppen,dc=firma,dc=local', 'role_id' => $role->id]); // Vorgesetzte hat sich schon einmal angemeldet. $this->post('/auth/ad/login', ['username' => 'cchefin', 'password' => 'chef']); auth()->logout(); $this->get('/auth/ad/redirect')->assertOk()->assertInertia(fn (Assert $page) => $page ->component('Auth/DirectoryLogin') ->where('provider.label', 'Mit Firmenkonto anmelden')); $this->post('/auth/ad/login', ['username' => 'aschmidt', 'password' => 'richtig'])->assertRedirect('/'); $user = User::query()->where('email', 'anna@firma.de')->sole(); $this->assertAuthenticatedAs($user); $this->assertSame('Pflegedienstleitung', $user->job_title); $this->assertSame('3f2504e0-4f89-11d3-9a0c-0305e82c3301', $user->identities()->value('subject')); $this->assertSame(User::query()->where('email', 'clara@firma.de')->value('id'), $user->manager_id); $this->assertTrue($user->roles()->whereKey($role->id)->exists(), 'Verschachtelte Gruppe, DN ohne Rücksicht auf Groß-/Kleinschreibung'); $this->assertSame(['dc1.firma.local', 'dc2.firma.local'], $this->ldap->lastOptions['hosts']); } public function test_wrong_password_and_empty_password_are_rejected(): void { $this->provider(); $this->from('/auth/ad/redirect')->post('/auth/ad/login', ['username' => 'aschmidt', 'password' => 'falsch']) ->assertRedirect('/auth/ad/redirect')->assertSessionHasErrors(['username' => 'Benutzername oder Passwort ist falsch.']); $this->post('/auth/ad/login', ['username' => 'aschmidt', 'password' => ''])->assertSessionHasErrors('password'); $this->post('/auth/ad/login', ['username' => 'niemand', 'password' => 'x'])->assertSessionHasErrors(['username' => 'Benutzername oder Passwort ist falsch.']); $this->assertGuest(); } public function test_filter_injection_is_escaped(): void { $this->provider(); $this->post('/auth/ad/login', ['username' => '*)(sAMAccountName=*', 'password' => 'richtig'])->assertSessionHasErrors('username'); $this->assertGuest(); $this->assertStringContainsString('\2a\29\28sAMAccountName=\2a', $this->ldap->filters[0]); } public function test_disabled_directory_accounts_cannot_sign_in(): void { $this->provider(); $this->ldap->entries[self::ANNA]['attributes']['useraccountcontrol'] = ['514']; $this->post('/auth/ad/login', ['username' => 'aschmidt', 'password' => 'richtig'])->assertSessionHasErrors('username'); $this->assertGuest(); } public function test_attempts_are_rate_limited(): void { $this->provider(); RateLimiter::clear('x'); for ($i = 0; $i < 5; $i++) { $this->post('/auth/ad/login', ['username' => 'aschmidt', 'password' => 'falsch']); } $this->post('/auth/ad/login', ['username' => 'aschmidt', 'password' => 'richtig']) ->assertSessionHasErrors(['username']); $this->assertGuest(); } public function test_openldap_preset_uses_entry_uuid_and_group_search(): void { $this->provider(['schema' => 'openldap']); $this->ldap->add('uid=bernd,ou=people,dc=firma,dc=local', 'pw', ['uid' => 'bernd', 'entryUUID' => 'uuid-bernd', 'mail' => 'bernd@firma.de', 'givenName' => 'Bernd', 'sn' => 'Brot']); $this->post('/auth/ad/login', ['username' => 'bernd', 'password' => 'pw'])->assertRedirect('/'); $this->assertSame('uuid-bernd', User::query()->where('email', 'bernd@firma.de')->sole()->identities()->value('subject')); $this->assertStringContainsString('(memberUid=bernd)', end($this->ldap->filters)); } public function test_admin_can_test_the_connection(): void { $provider = $this->provider(); $this->actingAs(User::factory()->admin()->create()); Livewire::test(EditIdentityProvider::class, ['record' => $provider->getRouteKey()]) ->assertFormSet(['config.bind_password' => null]) ->callAction(TestAction::make('testLdap')->schemaComponent('connection', 'form'), ['username' => 'aschmidt']) ->assertNotified(); } public function test_guid_conversion(): void { $this->assertSame('3f2504e0-4f89-11d3-9a0c-0305e82c3301', LdapDirectory::guid($this->guid)); } }