Phase 6 (Teil 2): OpenID Connect, LDAP/AD und SAML 2
All checks were successful
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 2m41s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 2m48s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 3m15s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 3m17s
CI / PHP 8.3 / mysql:8.0 (pull_request) Successful in 3m48s
CI / PHP 8.4 / mysql:8.0 (pull_request) Successful in 3m41s
CI / PHP 8.3 / mysql:8.4 (pull_request) Successful in 3m33s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m26s
CI / PHP 8.4 / mysql:8.4 (pull_request) Successful in 3m59s

- Gemeinsamer Treiber-Unterbau (App\Identity\IdentityDriver, IdentityDrivers,
  IdentityLogin, AttributeProfile); Entra ID nutzt ihn ebenfalls
- Plugins oidc, ldap und saml mit Profil- und Gruppen-Zuordnung
- Admin: Verfahren wählen, Formular je Verfahren, zuletzt empfangene Angaben
- onelogin/php-saml als neue Abhängigkeit

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Friederich Loheide 2026-09-26 12:38:32 +00:00
parent 0ab0838050
commit 4fcc41ed30
172 changed files with 18940 additions and 293 deletions

View file

@ -0,0 +1,176 @@
<?php
namespace Tests\Feature\Identity;
use App\Filament\Resources\IdentityProviders\Pages\EditIdentityProvider;
use App\Models\IdentityProvider;
use App\Models\IdpGroupMapping;
use App\Models\Role;
use App\Models\User;
use Filament\Actions\Testing\TestAction;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\RateLimiter;
use Inertia\Testing\AssertableInertia as Assert;
use Inhaus\Plugins\Ldap\Connection\LdapConnector;
use Inhaus\Plugins\Ldap\LdapDirectory;
use Livewire\Livewire;
use Tests\Support\FakeLdap;
use Tests\TestCase;
class LdapLoginTest extends TestCase
{
use RefreshDatabase;
private FakeLdap $ldap;
private const ANNA = 'CN=Anna Schmidt,OU=Pflege,DC=firma,DC=local';
private const CHEF = 'CN=Clara Chefin,OU=Leitung,DC=firma,DC=local';
/** objectGUID 3f2504e0-4f89-11d3-9a0c-0305e82c3301 in AD-Byte-Reihenfolge */
private string $guid;
protected function setUp(): void
{
parent::setUp();
Cache::flush();
$this->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));
}
}