ldap/connection-helper #3
@ -19,6 +19,8 @@ class LdapAuthService
|
|||||||
/** @var array<string, mixed> LDAP-spezifische Konfiguration (Server, Port, Domain-Suffix, Timeout, etc.) */
|
/** @var array<string, mixed> LDAP-spezifische Konfiguration (Server, Port, Domain-Suffix, Timeout, etc.) */
|
||||||
private array $config;
|
private array $config;
|
||||||
|
|
||||||
|
private LdapConnectionHelper $connectionHelper;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Erwartet den Teilbereich "ldap" aus der allgemeinen Konfiguration (config.php).
|
* Erwartet den Teilbereich "ldap" aus der allgemeinen Konfiguration (config.php).
|
||||||
*
|
*
|
||||||
@ -28,6 +30,7 @@ class LdapAuthService
|
|||||||
{
|
{
|
||||||
// LDAP-Konfiguration in der Instanz speichern.
|
// LDAP-Konfiguration in der Instanz speichern.
|
||||||
$this->config = $ldapConfig;
|
$this->config = $ldapConfig;
|
||||||
|
$this->connectionHelper = new LdapConnectionHelper($ldapConfig);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -50,30 +53,17 @@ class LdapAuthService
|
|||||||
|
|
||||||
// Benötigte Werte aus der LDAP-Konfiguration auslesen.
|
// Benötigte Werte aus der LDAP-Konfiguration auslesen.
|
||||||
// Falls einzelne Werte fehlen, werden sinnvolle Standardwerte gesetzt.
|
// Falls einzelne Werte fehlen, werden sinnvolle Standardwerte gesetzt.
|
||||||
$server = (string)($this->config['server'] ?? '');
|
|
||||||
$port = (int)($this->config['port'] ?? 389);
|
|
||||||
$domainSuffix = (string)($this->config['domain_suffix'] ?? '');
|
$domainSuffix = (string)($this->config['domain_suffix'] ?? '');
|
||||||
$timeout = (int)($this->config['timeout'] ?? 5);
|
|
||||||
|
|
||||||
// Ohne Server-Adresse oder Domain-Suffix ist eine sinnvolle Anmeldung nicht möglich.
|
// Ohne Server-Adresse oder Domain-Suffix ist eine sinnvolle Anmeldung nicht möglich.
|
||||||
// Das ist ein Konfigurationsfehler und wird als technischer Fehler behandelt.
|
// Das ist ein Konfigurationsfehler und wird als technischer Fehler behandelt.
|
||||||
if ($server === '' || $domainSuffix === '') {
|
if ($domainSuffix === '') {
|
||||||
throw new RuntimeException('LDAP-Konfiguration ist unvollständig.');
|
throw new RuntimeException('LDAP-Konfiguration ist unvollständig (domain_suffix fehlt).');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Verbindung zum LDAP/AD-Server herstellen.
|
// Verbindung zum LDAP/AD-Server herstellen.
|
||||||
// Rückgabewert ist eine Ressource (Verbindungshandle) oder false bei Fehlern.
|
// Rückgabewert ist eine Ressource (Verbindungshandle) oder false bei Fehlern.
|
||||||
$connection = ldap_connect($server, $port);
|
$connection = $this->connectionHelper->createConnection();
|
||||||
|
|
||||||
if ($connection === false) {
|
|
||||||
throw new RuntimeException('LDAP-Verbindung konnte nicht aufgebaut werden.');
|
|
||||||
}
|
|
||||||
|
|
||||||
// LDAP-Optionen setzen:
|
|
||||||
// - Protokollversion 3 (Standard in aktuellen Umgebungen)
|
|
||||||
// - Netzwerk-Timeout, damit die Anfrage nicht unendlich hängt
|
|
||||||
ldap_set_option($connection, LDAP_OPT_PROTOCOL_VERSION, 3);
|
|
||||||
ldap_set_option($connection, LDAP_OPT_NETWORK_TIMEOUT, $timeout);
|
|
||||||
|
|
||||||
// UPN (User Principal Name) aus Benutzername und Domain-Suffix bilden.
|
// UPN (User Principal Name) aus Benutzername und Domain-Suffix bilden.
|
||||||
// Beispiel: "administrator" + "@ITFA-PROJ-DOM.local" -> "administrator@ITFA-PROJ-DOM.local"
|
// Beispiel: "administrator" + "@ITFA-PROJ-DOM.local" -> "administrator@ITFA-PROJ-DOM.local"
|
||||||
|
|||||||
49
app/Services/Ldap/LdapConnectionHelper.php
Normal file
49
app/Services/Ldap/LdapConnectionHelper.php
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Services\Ldap;
|
||||||
|
|
||||||
|
use RuntimeException;
|
||||||
|
|
||||||
|
class LdapConnectionHelper
|
||||||
|
{
|
||||||
|
/** @var array<string, mixed> */
|
||||||
|
private array $config;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array<string, mixed> $ldapConfig
|
||||||
|
*/
|
||||||
|
public function __construct(array $ldapConfig)
|
||||||
|
{
|
||||||
|
$this->config = $ldapConfig;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Erstellt eine LDAP-Verbindung mit gesetzten Optionen (Protokollversion, Timeout),
|
||||||
|
* aber ohne Bind. Den Bind führen die aufrufenden Services durch.
|
||||||
|
*
|
||||||
|
* @return resource LDAP-Verbindungs-Handle
|
||||||
|
*/
|
||||||
|
public function createConnection()
|
||||||
|
{
|
||||||
|
$server = (string)($this->config['server'] ?? '');
|
||||||
|
$port = (int)($this->config['port'] ?? 636);
|
||||||
|
$timeout = (int)($this->config['timeout'] ?? 5);
|
||||||
|
|
||||||
|
if ($server === '') {
|
||||||
|
throw new RuntimeException('LDAP-Konfiguration ist unvollständig (server fehlt).');
|
||||||
|
}
|
||||||
|
|
||||||
|
$connection = ldap_connect($server, $port);
|
||||||
|
|
||||||
|
if ($connection === false) {
|
||||||
|
throw new RuntimeException('LDAP-Verbindung konnte nicht aufgebaut werden.');
|
||||||
|
}
|
||||||
|
|
||||||
|
ldap_set_option($connection, LDAP_OPT_PROTOCOL_VERSION, 3);
|
||||||
|
ldap_set_option($connection, LDAP_OPT_NETWORK_TIMEOUT, $timeout);
|
||||||
|
|
||||||
|
return $connection;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user