diff --git a/src/FreeDSx/Ldap/ClientOptions.php b/src/FreeDSx/Ldap/ClientOptions.php index e4fafd32..9fc269c1 100644 --- a/src/FreeDSx/Ldap/ClientOptions.php +++ b/src/FreeDSx/Ldap/ClientOptions.php @@ -31,7 +31,7 @@ final class ClientOptions private bool $sslValidateCert = true; - private ?bool $sslAllowSelfSigned = null; + private bool $sslAllowSelfSigned = false; private ?string $sslCaCert = null; @@ -161,12 +161,12 @@ public function setSslValidateCert(bool $sslValidateCert): self return $this; } - public function getSslAllowSelfSigned(): ?bool + public function isSslAllowSelfSigned(): bool { return $this->sslAllowSelfSigned; } - public function setSslAllowSelfSigned(?bool $sslAllowSelfSigned): self + public function setSslAllowSelfSigned(bool $sslAllowSelfSigned): self { $this->sslAllowSelfSigned = $sslAllowSelfSigned; @@ -257,6 +257,29 @@ public function setReferralLimit(int $referralLimit): self return $this; } - - + + /** + * @return array{version: int, servers: string[], port: int, base_dn: ?string, page_size: int, use_ssl: bool, ssl_validate_cert: bool, ssl_allow_self_signed: bool, ssl_ca_cert: ?string, ssl_peer_name: ?string, timeout_connect: int, timeout_read: int, referral: string, referral_chaser: ?ReferralChaserInterface, referral_limit: int} + */ + public function toArray(): array + { + return [ + 'version' => $this->getVersion(), + 'servers' => $this->getServers(), + 'port' => $this->getPort(), + 'transport' => $this->getTransport(), + 'base_dn' => $this->getBaseDn(), + 'page_size' => $this->getPageSize(), + 'use_ssl' => $this->isUseSsl(), + 'ssl_validate_cert' => $this->isSslValidateCert(), + 'ssl_allow_self_signed' => $this->isSslAllowSelfSigned(), + 'ssl_ca_cert' => $this->getSslCaCert(), + 'ssl_peer_name' => $this->getSslPeerName(), + 'timeout_connect' => $this->getTimeoutConnect(), + 'timeout_read' => $this->getTimeoutRead(), + 'referral' => $this->getReferral(), + 'referral_chaser' => $this->getReferralChaser(), + 'referral_limit' => $this->getReferralLimit(), + ]; + } } diff --git a/src/FreeDSx/Ldap/Control/ControlBag.php b/src/FreeDSx/Ldap/Control/ControlBag.php index 98cd85ae..54c026b1 100644 --- a/src/FreeDSx/Ldap/Control/ControlBag.php +++ b/src/FreeDSx/Ldap/Control/ControlBag.php @@ -17,8 +17,8 @@ use Countable; use IteratorAggregate; use Traversable; -use function count; use function array_search; +use function count; use function in_array; use function is_string; diff --git a/src/FreeDSx/Ldap/LdapClient.php b/src/FreeDSx/Ldap/LdapClient.php index d7f59af6..2c26394e 100644 --- a/src/FreeDSx/Ldap/LdapClient.php +++ b/src/FreeDSx/Ldap/LdapClient.php @@ -52,39 +52,13 @@ class LdapClient public const REFERRAL_THROW = 'throw'; - /** - * @var array - */ - private array $options = [ - 'version' => 3, - 'servers' => [], - 'port' => 389, - 'transport' => 'tcp', - 'base_dn' => null, - 'page_size' => 1000, - 'use_ssl' => false, - 'ssl_validate_cert' => true, - 'ssl_allow_self_signed' => null, - 'ssl_ca_cert' => null, - 'ssl_peer_name' => null, - 'timeout_connect' => 3, - 'timeout_read' => 10, - 'referral' => 'throw', - 'referral_chaser' => null, - 'referral_limit' => 10, - ]; + private ClientOptions $options; private ?ClientProtocolHandler $handler = null; - /** - * @param array $options - */ - public function __construct(array $options = []) + public function __construct(ClientOptions $options = new ClientOptions()) { - $this->options = array_merge( - $this->options, - $options, - ); + $this->options = $options; } /** @@ -98,7 +72,7 @@ public function bind( ): LdapMessageResponse { return $this->sendAndReceive( Operations::bind($username, $password) - ->setVersion($this->options['version']) + ->setVersion($this->options->getVersion()) ); } @@ -117,7 +91,7 @@ public function bindSasl( ): LdapMessageResponse { return $this->sendAndReceive( Operations::bindSasl($options, $mechanism) - ->setVersion($this->options['version']) + ->setVersion($this->options->getVersion()) ); } @@ -312,7 +286,7 @@ public function paging( return new Paging( client: $this, search: $search, - size: $size ?? (int) $this->options['page_size'] + size: $size ?? $this->options->getPageSize() ); } @@ -446,10 +420,8 @@ public function controls(): ControlBag /** * Get the options currently set. - * - * @return array */ - public function getOptions(): array + public function getOptions(): ClientOptions { return $this->options; } @@ -458,18 +430,14 @@ public function getOptions(): array * Merge a set of options. Depending on what you are changing, you many want to set the $forceDisconnect param to * true, which forces the client to disconnect. After which you would have to manually bind again. * - * @param array $options The set of options to merge in. * @param bool $forceDisconnect Whether the client should disconnect; forcing a manual re-connect / bind. This is * false by default. */ public function setOptions( - array $options, + ClientOptions $options, bool $forceDisconnect = false ): self { - $this->options = array_merge( - $this->options, - $options - ); + $this->options = $options; if ($forceDisconnect) { $this->unbindIfConnected(); } diff --git a/src/FreeDSx/Ldap/LdapServer.php b/src/FreeDSx/Ldap/LdapServer.php index 4584d576..56799753 100644 --- a/src/FreeDSx/Ldap/LdapServer.php +++ b/src/FreeDSx/Ldap/LdapServer.php @@ -13,7 +13,7 @@ namespace FreeDSx\Ldap; -use FreeDSx\Ldap\Exception\RuntimeException; +use FreeDSx\Ldap\Server\LoggerTrait; use FreeDSx\Ldap\Server\RequestHandler\PagingHandlerInterface; use FreeDSx\Ldap\Server\RequestHandler\ProxyHandler; use FreeDSx\Ldap\Server\RequestHandler\ProxyPagingHandler; @@ -34,45 +34,13 @@ class LdapServer { use LoggerTrait; - /** - * @var array - */ - private array $options = [ - 'ip' => '0.0.0.0', - 'port' => 389, - 'unix_socket' => '/var/run/ldap.socket', - 'transport' => 'tcp', - 'idle_timeout' => 600, - 'require_authentication' => true, - 'allow_anonymous' => false, - 'request_handler' => null, - 'rootdse_handler' => null, - 'paging_handler' => null, - 'logger' => null, - 'use_ssl' => false, - 'ssl_cert' => null, - 'ssl_cert_passphrase' => null, - 'dse_alt_server' => null, - 'dse_naming_contexts' => 'dc=FreeDSx,dc=local', - 'dse_vendor_name' => 'FreeDSx', - 'dse_vendor_version' => null, - ]; - private ?ServerRunnerInterface $runner; - /** - * @param array $options - * - * @throws RuntimeException - */ public function __construct( - array $options = [], + ServerOptions $options = new ServerOptions(), ?ServerRunnerInterface $serverRunner = null ) { - $this->options = array_merge( - $this->options, - $options - ); + $this->options = $options; $this->runner = $serverRunner; } @@ -83,10 +51,10 @@ public function __construct( */ public function run(): void { - $isUnixSocket = $this->options['transport'] === 'unix'; + $isUnixSocket = $this->options->getTransport() === 'unix'; $resource = $isUnixSocket - ? $this->options['unix_socket'] - : $this->options['ip']; + ? $this->options->getUnixSocket() + : $this->options->getIp(); if ($isUnixSocket) { $this->removeExistingSocketIfNeeded($resource); @@ -94,8 +62,8 @@ public function run(): void $socketServer = SocketServer::bind( $resource, - $this->options['port'], - $this->options + $this->options->getPort(), + $this->options->toArray(), ); $this->runner()->run($socketServer); @@ -103,10 +71,8 @@ public function run(): void /** * Get the options currently set for the LDAP server. - * - * @return array */ - public function getOptions(): array + public function getOptions(): ServerOptions { return $this->options; } @@ -116,7 +82,7 @@ public function getOptions(): array */ public function useRequestHandler(RequestHandlerInterface $requestHandler): self { - $this->options['request_handler'] = $requestHandler; + $this->options->setRequestHandler($requestHandler); return $this; } @@ -126,7 +92,7 @@ public function useRequestHandler(RequestHandlerInterface $requestHandler): self */ public function useRootDseHandler(RootDseHandlerInterface $rootDseHandler): self { - $this->options['rootdse_handler'] = $rootDseHandler; + $this->options->setRootDseHandler($rootDseHandler); return $this; } @@ -136,7 +102,7 @@ public function useRootDseHandler(RootDseHandlerInterface $rootDseHandler): self */ public function usePagingHandler(PagingHandlerInterface $pagingHandler): self { - $this->options['paging_handler'] = $pagingHandler; + $this->options->setPagingHandler($pagingHandler); return $this; } @@ -146,7 +112,7 @@ public function usePagingHandler(PagingHandlerInterface $pagingHandler): self */ public function useLogger(LoggerInterface $logger): self { - $this->options['logger'] = $logger; + $this->options->setLogger($logger); return $this; } @@ -157,17 +123,17 @@ public function useLogger(LoggerInterface $logger): self * Note: This is only intended to work with the PCNTL server runner. * * @param string|string[] $servers The LDAP server(s) to proxy the request to. - * @param array $clientOptions Any additional client options for the proxy connection. - * @param array $serverOptions Any additional server options for the LDAP server. + * @param ClientOptions $clientOptions Any additional client options for the proxy connection. + * @param ServerOptions $serverOptions Any additional server options for the LDAP server. */ public static function makeProxy( array|string $servers, - array $clientOptions = [], - array $serverOptions = [] + ClientOptions $clientOptions = new ClientOptions(), + ServerOptions $serverOptions = new ServerOptions(), ): LdapServer { - $client = new LdapClient(array_merge([ - 'servers' => $servers, - ], $clientOptions)); + $client = new LdapClient( + $clientOptions->setServers((array) $servers) + ); $proxyRequestHandler = new ProxyHandler($client); $server = new LdapServer($serverOptions); diff --git a/src/FreeDSx/Ldap/Protocol/ClientProtocolHandler.php b/src/FreeDSx/Ldap/Protocol/ClientProtocolHandler.php index 6e354a74..1bdb4ff0 100644 --- a/src/FreeDSx/Ldap/Protocol/ClientProtocolHandler.php +++ b/src/FreeDSx/Ldap/Protocol/ClientProtocolHandler.php @@ -14,6 +14,7 @@ namespace FreeDSx\Ldap\Protocol; use FreeDSx\Asn1\Exception\EncoderException; +use FreeDSx\Ldap\ClientOptions; use FreeDSx\Ldap\Control\Control; use FreeDSx\Ldap\Control\ControlBag; use FreeDSx\Ldap\Entry\Entry; @@ -51,7 +52,7 @@ class ClientProtocolHandler private ?ClientQueue $queue; - private array $options; + private ClientOptions $options; private ControlBag $controls; @@ -60,13 +61,13 @@ class ClientProtocolHandler private ?Entry $rootDse = null; public function __construct( - array $options, + ClientOptions $options, ClientQueue $queue = null, SocketPool $pool = null, ClientProtocolHandlerFactory $clientProtocolHandlerFactory = null ) { $this->options = $options; - $this->pool = $pool ?? new SocketPool($options); + $this->pool = $pool ?? new SocketPool($this->options->toArray()); $this->protocolHandlerFactory = $clientProtocolHandlerFactory ?? new ClientProtocolHandlerFactory(); $this->controls = new ControlBag(); $this->queue = $queue; diff --git a/src/FreeDSx/Ldap/Protocol/ClientProtocolHandler/ClientBasicHandler.php b/src/FreeDSx/Ldap/Protocol/ClientProtocolHandler/ClientBasicHandler.php index 8354537f..f94fe42f 100644 --- a/src/FreeDSx/Ldap/Protocol/ClientProtocolHandler/ClientBasicHandler.php +++ b/src/FreeDSx/Ldap/Protocol/ClientProtocolHandler/ClientBasicHandler.php @@ -14,6 +14,7 @@ namespace FreeDSx\Ldap\Protocol\ClientProtocolHandler; use FreeDSx\Asn1\Exception\EncoderException; +use FreeDSx\Ldap\ClientOptions; use FreeDSx\Ldap\Exception\BindException; use FreeDSx\Ldap\Exception\OperationException; use FreeDSx\Ldap\Exception\ProtocolException; @@ -68,7 +69,7 @@ public function handleResponse( LdapMessageRequest $messageTo, LdapMessageResponse $messageFrom, ClientQueue $queue, - array $options + ClientOptions $options ): ?LdapMessageResponse { $result = $messageFrom->getResponse(); diff --git a/src/FreeDSx/Ldap/Protocol/ClientProtocolHandler/ClientProtocolContext.php b/src/FreeDSx/Ldap/Protocol/ClientProtocolHandler/ClientProtocolContext.php index f73893a0..6bc2648b 100644 --- a/src/FreeDSx/Ldap/Protocol/ClientProtocolHandler/ClientProtocolContext.php +++ b/src/FreeDSx/Ldap/Protocol/ClientProtocolHandler/ClientProtocolContext.php @@ -14,6 +14,7 @@ namespace FreeDSx\Ldap\Protocol\ClientProtocolHandler; use FreeDSx\Asn1\Exception\EncoderException; +use FreeDSx\Ldap\ClientOptions; use FreeDSx\Ldap\Control\Control; use FreeDSx\Ldap\Entry\Entry; use FreeDSx\Ldap\Exception\BindException; @@ -35,16 +36,13 @@ */ class ClientProtocolContext { - /** - * @var array - */ - private array $options; - /** * @var Control[] */ private array $controls; + private ClientOptions $options; + private ClientProtocolHandler $protocolHandler; private ClientQueue $clientQueue; @@ -58,7 +56,7 @@ public function __construct( array $controls, ClientProtocolHandler $protocolHandler, ClientQueue $queue, - array $options + ClientOptions $options ) { $this->request = $request; $this->controls = $controls; @@ -90,7 +88,7 @@ public function getQueue(): ClientQueue return $this->clientQueue; } - public function getOptions(): array + public function getOptions(): ClientOptions { return $this->options; } diff --git a/src/FreeDSx/Ldap/Protocol/ClientProtocolHandler/ClientReferralHandler.php b/src/FreeDSx/Ldap/Protocol/ClientProtocolHandler/ClientReferralHandler.php index fcd16883..71bbff91 100644 --- a/src/FreeDSx/Ldap/Protocol/ClientProtocolHandler/ClientReferralHandler.php +++ b/src/FreeDSx/Ldap/Protocol/ClientProtocolHandler/ClientReferralHandler.php @@ -13,6 +13,7 @@ namespace FreeDSx\Ldap\Protocol\ClientProtocolHandler; +use FreeDSx\Ldap\ClientOptions; use FreeDSx\Ldap\Exception\ConnectionException; use FreeDSx\Ldap\Exception\FilterParseException; use FreeDSx\Ldap\Exception\OperationException; @@ -42,10 +43,9 @@ */ class ClientReferralHandler implements ResponseHandlerInterface { - /** - * @var array - */ - private array $options = []; + private ClientOptions $options; + + private ?ReferralContext $referralContext = null; /** * {@inheritDoc} @@ -57,11 +57,11 @@ public function handleResponse( LdapMessageRequest $messageTo, LdapMessageResponse $messageFrom, ClientQueue $queue, - array $options + ClientOptions $options ): ?LdapMessageResponse { $this->options = $options; $result = $messageFrom->getResponse(); - switch ($this->options['referral']) { + switch ($this->options->getReferral()) { case 'throw': $message = $result instanceof LdapResult ? $result->getDiagnosticMessage() @@ -79,7 +79,7 @@ public function handleResponse( default: throw new RuntimeException(sprintf( 'The referral option "%s" is invalid.', - $this->options['referral'] + $this->options->getReferral() )); } } @@ -93,43 +93,37 @@ private function followReferral( LdapMessageRequest $messageTo, LdapMessageResponse $messageFrom ): ?LdapMessageResponse { - $referralChaser = $this->options['referral_chaser']; - if (!($referralChaser === null || $referralChaser instanceof ReferralChaserInterface)) { - throw new RuntimeException(sprintf( - 'The referral_chaser must implement "%s" or be null.', - ReferralChaserInterface::class - )); - } - if (!$messageFrom->getResponse() instanceof LdapResult || count($messageFrom->getResponse()->getReferrals()) === 0) { + $referralChaser = $this->getReferralChaser(); + + $response = $messageFrom->getResponse(); + $referrals = $this->getReferralsFromResponse($messageFrom); + if (!$response instanceof LdapResult || count($referrals) === 0) { throw new OperationException( 'Encountered a referral request, but no referrals were supplied.', ResultCode::REFERRAL ); } - # Initialize a referral context to track the referrals we have already visited as well as count. - if (!isset($this->options['_referral_context'])) { - $this->options['_referral_context'] = new ReferralContext(); - } + $referralContext = $this->getReferralContext(); - foreach ($messageFrom->getResponse()->getReferrals() as $referral) { + foreach ($referrals as $referral) { # We must skip referrals we have already visited to avoid a referral loop - if ($this->options['_referral_context']->hasReferral($referral)) { + if ($referralContext->hasReferral($referral)) { continue; } - $this->options['_referral_context']->addReferral($referral); - if ($this->options['_referral_context']->count() > $this->options['referral_limit']) { + $referralContext->addReferral($referral); + if ($referralContext->count() > $this->options->getReferralLimit()) { throw new OperationException(sprintf( 'The referral limit of %s has been reached.', - $this->options['referral_limit'] + $this->options->getReferralLimit() )); } $bind = null; try { # @todo Remove the bind parameter from the interface in a future release. - $bind = $referralChaser?->chase( + $bind = $referralChaser->chase( request: $messageTo, referral: $referral, bind: null, @@ -137,12 +131,14 @@ private function followReferral( } catch (SkipReferralException) { continue; } - $options = $this->options; - $options['servers'] = $referral->getHost() !== null - ? [$referral->getHost()] - : []; - $options['port'] = $referral->getPort() ?? 389; - $options['use_ssl'] = $referral->getUseSsl(); + $options = clone $this->options; + $options->setServers( + $referral->getHost() !== null + ? [$referral->getHost()] + : [] + ); + $options->setPort($referral->getPort() ?? 389); + $options->setUseSsl($referral->getUseSsl()); # Each referral could potentially modify different aspects of the request, depending on the URL. Clone it # here, merge the options, then use that request to send to LDAP. This makes sure we don't accidentally mix @@ -151,7 +147,7 @@ private function followReferral( $this->mergeReferralOptions($request, $referral); try { - $client = $referralChaser !== null ? $referralChaser->client($options) : new LdapClient($options); + $client = $referralChaser->client($options); # If we have a referral on a bind request, then do not bind initially. # @@ -182,7 +178,7 @@ private function followReferral( # If we have exhausted all referrals consider it an operation exception. throw new OperationException(sprintf( 'All referral attempts have been exhausted. %s', - $messageFrom->getResponse()->getDiagnosticMessage() + $response->getDiagnosticMessage() ), ResultCode::REFERRAL); } @@ -213,4 +209,33 @@ private function mergeReferralOptions( $request->setFilter(Filters::raw($referral->getFilter())); } } + + /** + * @return LdapUrl[] + */ + private function getReferralsFromResponse(LdapMessageResponse $messageFrom): array + { + $response = $messageFrom->getResponse(); + + return !$response instanceof LdapResult + ? [] + : $response->getReferrals(); + } + + private function getReferralChaser(): ReferralChaserInterface + { + return $this->options->getReferralChaser() ?? throw new RuntimeException( + 'No referral chaser was provided.' + ); + } + + private function getReferralContext(): ReferralContext + { + # Initialize a referral context to track the referrals we have already visited as well as count. + if ($this->referralContext === null) { + $this->referralContext = new ReferralContext(); + } + + return $this->referralContext; + } } diff --git a/src/FreeDSx/Ldap/Protocol/ClientProtocolHandler/ClientSearchHandler.php b/src/FreeDSx/Ldap/Protocol/ClientProtocolHandler/ClientSearchHandler.php index e17c17a6..54b0430e 100644 --- a/src/FreeDSx/Ldap/Protocol/ClientProtocolHandler/ClientSearchHandler.php +++ b/src/FreeDSx/Ldap/Protocol/ClientProtocolHandler/ClientSearchHandler.php @@ -14,6 +14,7 @@ namespace FreeDSx\Ldap\Protocol\ClientProtocolHandler; use FreeDSx\Asn1\Exception\EncoderException; +use FreeDSx\Ldap\ClientOptions; use FreeDSx\Ldap\Entry\Entries; use FreeDSx\Ldap\Exception\BindException; use FreeDSx\Ldap\Exception\OperationException; @@ -47,7 +48,7 @@ public function handleRequest(ClientProtocolContext $context): ?LdapMessageRespo /** @var SearchRequest $request */ $request = $context->getRequest(); if ($request->getBaseDn() === null) { - $request->setBaseDn($context->getOptions()['base_dn'] ?? null); + $request->setBaseDn($context->getOptions()->getBaseDn() ?? null); } return parent::handleRequest($context); @@ -62,10 +63,10 @@ public function handleRequest(ClientProtocolContext $context): ?LdapMessageRespo * @throws ConnectionException */ public function handleResponse( - LdapMessageRequest $messageTo, + LdapMessageRequest $messageTo, LdapMessageResponse $messageFrom, ClientQueue $queue, - array $options, + ClientOptions $options, ): ?LdapMessageResponse { $entries = []; diff --git a/src/FreeDSx/Ldap/Protocol/ClientProtocolHandler/ClientStartTlsHandler.php b/src/FreeDSx/Ldap/Protocol/ClientProtocolHandler/ClientStartTlsHandler.php index f24a50c4..3b7ebc48 100644 --- a/src/FreeDSx/Ldap/Protocol/ClientProtocolHandler/ClientStartTlsHandler.php +++ b/src/FreeDSx/Ldap/Protocol/ClientProtocolHandler/ClientStartTlsHandler.php @@ -13,6 +13,7 @@ namespace FreeDSx\Ldap\Protocol\ClientProtocolHandler; +use FreeDSx\Ldap\ClientOptions; use FreeDSx\Ldap\Exception\ConnectionException; use FreeDSx\Ldap\Operation\Response\ExtendedResponse; use FreeDSx\Ldap\Operation\ResultCode; @@ -36,7 +37,7 @@ public function handleResponse( LdapMessageRequest $messageTo, LdapMessageResponse $messageFrom, ClientQueue $queue, - array $options + ClientOptions $options ): ?LdapMessageResponse { /** @var ExtendedResponse $response */ $response = $messageFrom->getResponse(); diff --git a/src/FreeDSx/Ldap/Protocol/ClientProtocolHandler/ResponseHandlerInterface.php b/src/FreeDSx/Ldap/Protocol/ClientProtocolHandler/ResponseHandlerInterface.php index dd75a1d9..d1bc4f50 100644 --- a/src/FreeDSx/Ldap/Protocol/ClientProtocolHandler/ResponseHandlerInterface.php +++ b/src/FreeDSx/Ldap/Protocol/ClientProtocolHandler/ResponseHandlerInterface.php @@ -13,6 +13,7 @@ namespace FreeDSx\Ldap\Protocol\ClientProtocolHandler; +use FreeDSx\Ldap\ClientOptions; use FreeDSx\Ldap\Protocol\LdapMessageRequest; use FreeDSx\Ldap\Protocol\LdapMessageResponse; use FreeDSx\Ldap\Protocol\Queue\ClientQueue; @@ -28,12 +29,12 @@ interface ResponseHandlerInterface /** * Pass the message response to the handler specific to it. * - * @param array $options + * @param ClientOptions $options */ public function handleResponse( LdapMessageRequest $messageTo, LdapMessageResponse $messageFrom, ClientQueue $queue, - array $options + ClientOptions $options ): ?LdapMessageResponse; } diff --git a/src/FreeDSx/Ldap/Protocol/ServerAuthorization.php b/src/FreeDSx/Ldap/Protocol/ServerAuthorization.php index 8342464a..14f08952 100644 --- a/src/FreeDSx/Ldap/Protocol/ServerAuthorization.php +++ b/src/FreeDSx/Ldap/Protocol/ServerAuthorization.php @@ -37,11 +37,14 @@ class ServerAuthorization private TokenInterface $token; - public function __construct(TokenInterface $token = null, array $options = []) - { - $this->token = $token ?? new AnonToken(); - $this->isAuthRequired = !isset($options['require_authentication']) || $options['require_authentication']; - $this->isAnonymousAllowed = isset($options['allow_anonymous']) && $options['allow_anonymous']; + public function __construct( + TokenInterface $token = new AnonToken(), + bool $isAnonymousAllowed = false, + bool $isAuthRequired = true, + ) { + $this->token = $token; + $this->isAuthRequired = $isAuthRequired; + $this->isAnonymousAllowed = $isAnonymousAllowed; } /** diff --git a/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler.php b/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler.php index 9c3102cd..093437c6 100644 --- a/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler.php +++ b/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler.php @@ -18,7 +18,7 @@ use FreeDSx\Ldap\Exception\OperationException; use FreeDSx\Ldap\Exception\ProtocolException; use FreeDSx\Ldap\Exception\RuntimeException; -use FreeDSx\Ldap\LoggerTrait; +use FreeDSx\Ldap\Server\LoggerTrait; use FreeDSx\Ldap\Operation\Response\ExtendedResponse; use FreeDSx\Ldap\Operation\ResultCode; use FreeDSx\Ldap\Protocol\Factory\ResponseFactory; @@ -28,6 +28,7 @@ use FreeDSx\Ldap\Server\HandlerFactoryInterface; use FreeDSx\Ldap\Server\RequestHistory; use FreeDSx\Ldap\Server\Token\TokenInterface; +use FreeDSx\Ldap\ServerOptions; use FreeDSx\Socket\Exception\ConnectionException; use Throwable; use function array_merge; @@ -42,18 +43,7 @@ class ServerProtocolHandler { use LoggerTrait; - /** - * @var array - */ - private array $options = [ - 'allow_anonymous' => false, - 'require_authentication' => true, - 'request_handler' => null, - 'dse_alt_server' => null, - 'dse_naming_contexts' => 'dc=FreeDSx,dc=local', - 'dse_vendor_name' => 'FreeDSx', - 'dse_vendor_version' => null, - ]; + private ServerOptions $options; private ServerQueue $queue; @@ -72,28 +62,28 @@ class ServerProtocolHandler private ServerBindHandlerFactory $bindHandlerFactory; - /** - * @param array $options - */ public function __construct( ServerQueue $queue, HandlerFactoryInterface $handlerFactory, - array $options = [], + ServerOptions $options, ServerProtocolHandlerFactory $protocolHandlerFactory = null, - ServerBindHandlerFactory $bindHandlerFactory = null, + ServerBindHandlerFactory $bindHandlerFactory = new ServerBindHandlerFactory(), ServerAuthorization $authorizer = null, - ResponseFactory $responseFactory = null + ResponseFactory $responseFactory = new ResponseFactory() ) { $this->queue = $queue; $this->handlerFactory = $handlerFactory; - $this->options = array_merge($this->options, $options); - $this->authorizer = $authorizer ?? new ServerAuthorization(null, $this->options); + $this->options = $options; + $this->authorizer = $authorizer ?? new ServerAuthorization( + isAnonymousAllowed: $options->isAllowAnonymous(), + isAuthRequired: $options->isRequireAuthentication(), + ); $this->protocolHandlerFactory = $protocolHandlerFactory ?? new ServerProtocolHandlerFactory( - $handlerFactory, - new RequestHistory() + handlerFactory: $handlerFactory, + requestHistory: new RequestHistory(), ); - $this->bindHandlerFactory = $bindHandlerFactory ?? new ServerBindHandlerFactory(); - $this->responseFactory = $responseFactory ?? new ResponseFactory(); + $this->bindHandlerFactory = $bindHandlerFactory; + $this->responseFactory = $responseFactory; } /** @@ -267,8 +257,7 @@ private function handleAuthRequest(LdapMessageRequest $message): TokenInterface return $this->bindHandlerFactory->get($message->getRequest())->handleBind( $message, $this->handlerFactory->makeRequestHandler(), - $this->queue, - $this->options + $this->queue ); } diff --git a/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/BindHandlerInterface.php b/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/BindHandlerInterface.php index 8ef45674..ecef8ed0 100644 --- a/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/BindHandlerInterface.php +++ b/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/BindHandlerInterface.php @@ -29,13 +29,11 @@ interface BindHandlerInterface /** * Returns a token indicating the outcome of a bind request. * - * @param array $options * @throws OperationException */ public function handleBind( LdapMessageRequest $message, RequestHandlerInterface $dispatcher, - ServerQueue $queue, - array $options + ServerQueue $queue ): TokenInterface; } diff --git a/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerAnonBindHandler.php b/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerAnonBindHandler.php index 3d1c8fdc..a7edcb55 100644 --- a/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerAnonBindHandler.php +++ b/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerAnonBindHandler.php @@ -22,6 +22,7 @@ use FreeDSx\Ldap\Server\RequestHandler\RequestHandlerInterface; use FreeDSx\Ldap\Server\Token\AnonToken; use FreeDSx\Ldap\Server\Token\TokenInterface; +use FreeDSx\Ldap\ServerOptions; /** * Handles anonymous bind requests. @@ -39,8 +40,7 @@ class ServerAnonBindHandler extends ServerBindHandler public function handleBind( LdapMessageRequest $message, RequestHandlerInterface $dispatcher, - ServerQueue $queue, - array $options + ServerQueue $queue ): TokenInterface { $request = $message->getRequest(); if (!$request instanceof AnonBindRequest) { diff --git a/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerBindHandler.php b/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerBindHandler.php index 7bab0d5f..815ce932 100644 --- a/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerBindHandler.php +++ b/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerBindHandler.php @@ -23,6 +23,7 @@ use FreeDSx\Ldap\Server\RequestHandler\RequestHandlerInterface; use FreeDSx\Ldap\Server\Token\BindToken; use FreeDSx\Ldap\Server\Token\TokenInterface; +use FreeDSx\Ldap\ServerOptions; /** * Handles a simple bind request. @@ -39,8 +40,7 @@ class ServerBindHandler extends BaseServerHandler implements BindHandlerInterfac public function handleBind( LdapMessageRequest $message, RequestHandlerInterface $dispatcher, - ServerQueue $queue, - array $options + ServerQueue $queue ): TokenInterface { /** @var BindRequest $request */ $request = $message->getRequest(); diff --git a/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerDispatchHandler.php b/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerDispatchHandler.php index a4f46fc2..c5a41870 100644 --- a/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerDispatchHandler.php +++ b/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerDispatchHandler.php @@ -22,6 +22,7 @@ use FreeDSx\Ldap\Server\RequestContext; use FreeDSx\Ldap\Server\RequestHandler\RequestHandlerInterface; use FreeDSx\Ldap\Server\Token\TokenInterface; +use FreeDSx\Ldap\ServerOptions; /** * Handles generic requests that can be sent to the user supplied dispatcher / handler. @@ -40,7 +41,7 @@ public function handleRequest( TokenInterface $token, RequestHandlerInterface $dispatcher, ServerQueue $queue, - array $options + ServerOptions $options ): void { $context = new RequestContext($message->controls(), $token); $request = $message->getRequest(); diff --git a/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerPagingHandler.php b/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerPagingHandler.php index 2d55e308..f8ded65e 100644 --- a/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerPagingHandler.php +++ b/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerPagingHandler.php @@ -30,6 +30,7 @@ use FreeDSx\Ldap\Server\RequestHandler\RequestHandlerInterface; use FreeDSx\Ldap\Server\RequestHistory; use FreeDSx\Ldap\Server\Token\TokenInterface; +use FreeDSx\Ldap\ServerOptions; use Throwable; /** @@ -66,7 +67,7 @@ public function handleRequest( TokenInterface $token, RequestHandlerInterface $dispatcher, ServerQueue $queue, - array $options + ServerOptions $options ): void { $context = new RequestContext( $message->controls(), diff --git a/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerPagingUnsupportedHandler.php b/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerPagingUnsupportedHandler.php index 12dcb76f..b5bb1c79 100644 --- a/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerPagingUnsupportedHandler.php +++ b/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerPagingUnsupportedHandler.php @@ -20,6 +20,7 @@ use FreeDSx\Ldap\Server\RequestContext; use FreeDSx\Ldap\Server\RequestHandler\RequestHandlerInterface; use FreeDSx\Ldap\Server\Token\TokenInterface; +use FreeDSx\Ldap\ServerOptions; /** * Determines whether we can page results if no paging handler is defined. @@ -38,7 +39,7 @@ public function handleRequest( TokenInterface $token, RequestHandlerInterface $dispatcher, ServerQueue $queue, - array $options + ServerOptions $options ): void { $context = new RequestContext( $message->controls(), diff --git a/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerProtocolHandlerInterface.php b/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerProtocolHandlerInterface.php index 591839ac..cb95a1b0 100644 --- a/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerProtocolHandlerInterface.php +++ b/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerProtocolHandlerInterface.php @@ -18,6 +18,7 @@ use FreeDSx\Ldap\Protocol\Queue\ServerQueue; use FreeDSx\Ldap\Server\RequestHandler\RequestHandlerInterface; use FreeDSx\Ldap\Server\Token\TokenInterface; +use FreeDSx\Ldap\ServerOptions; use FreeDSx\Socket\Exception\ConnectionException; /** @@ -31,7 +32,7 @@ interface ServerProtocolHandlerInterface /** * Handle protocol actions specific to the request received. * - * @param array $options + * @param ServerOptions $options * @throws OperationException * @throws ConnectionException */ @@ -40,6 +41,6 @@ public function handleRequest( TokenInterface $token, RequestHandlerInterface $dispatcher, ServerQueue $queue, - array $options + ServerOptions $options ): void; } diff --git a/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerRootDseHandler.php b/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerRootDseHandler.php index 1e2674b3..db9c51e1 100644 --- a/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerRootDseHandler.php +++ b/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerRootDseHandler.php @@ -28,6 +28,7 @@ use FreeDSx\Ldap\Server\RequestHandler\RequestHandlerInterface; use FreeDSx\Ldap\Server\RequestHandler\RootDseHandlerInterface; use FreeDSx\Ldap\Server\Token\TokenInterface; +use FreeDSx\Ldap\ServerOptions; use function count; /** @@ -53,33 +54,33 @@ public function handleRequest( TokenInterface $token, RequestHandlerInterface $dispatcher, ServerQueue $queue, - array $options + ServerOptions $options ): void { $entry = Entry::fromArray('', [ - 'namingContexts' => $options['dse_naming_contexts'] ?? '', + 'namingContexts' => $options->getDseNamingContexts(), 'supportedExtension' => [ ExtendedRequest::OID_WHOAMI, ], 'supportedLDAPVersion' => ['3'], - 'vendorName' => $options['dse_vendor_name'] ?? '', + 'vendorName' => $options->getDseVendorName(), ]); - if (isset($options['ssl_cert'])) { + if ($options->getSslCert()) { $entry->add( 'supportedExtension', ExtendedRequest::OID_START_TLS ); } - if (isset($options['paging_handler'])) { + if ($options->getPagingHandler()) { $entry->add( 'supportedControl', Control::OID_PAGING ); } - if (isset($options['vendor_version'])) { - $entry->set('vendorVersion', $options['vendor_version']); + if ($options->getDseVendorVersion()) { + $entry->set('vendorVersion', (string) $options->getDseVendorVersion()); } - if (isset($options['alt_server'])) { - $entry->set('altServer', $options['alt_server']); + if ($options->getDseAlServer()) { + $entry->set('altServer', (string) $options->getDseAlServer()); } /** @var SearchRequest $request */ diff --git a/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerSearchHandler.php b/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerSearchHandler.php index 802cfee1..08b1fe17 100644 --- a/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerSearchHandler.php +++ b/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerSearchHandler.php @@ -19,6 +19,7 @@ use FreeDSx\Ldap\Server\RequestContext; use FreeDSx\Ldap\Server\RequestHandler\RequestHandlerInterface; use FreeDSx\Ldap\Server\Token\TokenInterface; +use FreeDSx\Ldap\ServerOptions; /** * Handles search request logic. @@ -37,7 +38,7 @@ public function handleRequest( TokenInterface $token, RequestHandlerInterface $dispatcher, ServerQueue $queue, - array $options + ServerOptions $options ): void { $context = new RequestContext( $message->controls(), diff --git a/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerStartTlsHandler.php b/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerStartTlsHandler.php index 444dea54..4decf31f 100644 --- a/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerStartTlsHandler.php +++ b/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerStartTlsHandler.php @@ -23,6 +23,7 @@ use FreeDSx\Ldap\Protocol\Queue\ServerQueue; use FreeDSx\Ldap\Server\RequestHandler\RequestHandlerInterface; use FreeDSx\Ldap\Server\Token\TokenInterface; +use FreeDSx\Ldap\ServerOptions; use FreeDSx\Socket\Exception\ConnectionException; use function extension_loaded; @@ -52,10 +53,10 @@ public function handleRequest( TokenInterface $token, RequestHandlerInterface $dispatcher, ServerQueue $queue, - array $options + ServerOptions $options ): void { # If we don't have a SSL cert or the OpenSSL extension is not available, then we can do nothing... - if (!isset($options['ssl_cert']) || !self::$hasOpenssl) { + if ($options->getSslCert() === null || !self::$hasOpenssl) { $queue->sendMessage(new LdapMessageResponse($message->getMessageId(), new ExtendedResponse( new LdapResult(ResultCode::PROTOCOL_ERROR), ExtendedRequest::OID_START_TLS diff --git a/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerUnbindHandler.php b/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerUnbindHandler.php index 05eaea02..bd11e8de 100644 --- a/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerUnbindHandler.php +++ b/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerUnbindHandler.php @@ -17,6 +17,7 @@ use FreeDSx\Ldap\Protocol\Queue\ServerQueue; use FreeDSx\Ldap\Server\RequestHandler\RequestHandlerInterface; use FreeDSx\Ldap\Server\Token\TokenInterface; +use FreeDSx\Ldap\ServerOptions; /** * Handles an un-bind. Which just closes the connection. @@ -33,7 +34,7 @@ public function handleRequest( TokenInterface $token, RequestHandlerInterface $dispatcher, ServerQueue $queue, - array $options + ServerOptions $options ): void { $queue->close(); } diff --git a/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerWhoAmIHandler.php b/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerWhoAmIHandler.php index a0a0a2e4..080cce42 100644 --- a/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerWhoAmIHandler.php +++ b/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerWhoAmIHandler.php @@ -24,6 +24,7 @@ use FreeDSx\Ldap\Protocol\Queue\ServerQueue; use FreeDSx\Ldap\Server\RequestHandler\RequestHandlerInterface; use FreeDSx\Ldap\Server\Token\TokenInterface; +use FreeDSx\Ldap\ServerOptions; /** * Handles a whoami request. @@ -41,7 +42,7 @@ public function handleRequest( TokenInterface $token, RequestHandlerInterface $dispatcher, ServerQueue $queue, - array $options + ServerOptions $options ): void { $userId = $token->getUsername(); diff --git a/src/FreeDSx/Ldap/ReferralChaserInterface.php b/src/FreeDSx/Ldap/ReferralChaserInterface.php index c85fa9bb..7f987678 100644 --- a/src/FreeDSx/Ldap/ReferralChaserInterface.php +++ b/src/FreeDSx/Ldap/ReferralChaserInterface.php @@ -39,9 +39,7 @@ public function chase( ): ?BindRequest; /** - * Construct the LdapClient with the options you want, and perform other tasks (such as StartTLS) - * - * @param array $options + * Construct the LdapClient with the options you want, and perform other tasks (such as StartTLS). */ - public function client(array $options): LdapClient; + public function client(ClientOptions $options): LdapClient; } diff --git a/src/FreeDSx/Ldap/LoggerTrait.php b/src/FreeDSx/Ldap/Server/LoggerTrait.php similarity index 84% rename from src/FreeDSx/Ldap/LoggerTrait.php rename to src/FreeDSx/Ldap/Server/LoggerTrait.php index 49a5e58e..8a62bf40 100644 --- a/src/FreeDSx/Ldap/LoggerTrait.php +++ b/src/FreeDSx/Ldap/Server/LoggerTrait.php @@ -11,10 +11,10 @@ * file that was distributed with this source code. */ -namespace FreeDSx\Ldap; +namespace FreeDSx\Ldap\Server; use FreeDSx\Ldap\Exception\RuntimeException; -use Psr\Log\LoggerInterface; +use FreeDSx\Ldap\ServerOptions; use Psr\Log\LogLevel; /** @@ -24,6 +24,8 @@ */ trait LoggerTrait { + private ServerOptions $options; + /** * Logs a message and then throws a runtime exception. * @@ -80,12 +82,10 @@ private function log( string $message, array $context = [] ): void { - if (isset($this->options['logger']) && $this->options['logger'] instanceof LoggerInterface) { - $this->options['logger']->log( - level: $level, - message: $message, - context: $context, - ); - } + $this->options->getLogger()?->log( + level: $level, + message: $message, + context: $context, + ); } } diff --git a/src/FreeDSx/Ldap/Server/RequestHandler/HandlerFactory.php b/src/FreeDSx/Ldap/Server/RequestHandler/HandlerFactory.php index b15e5d72..76758562 100644 --- a/src/FreeDSx/Ldap/Server/RequestHandler/HandlerFactory.php +++ b/src/FreeDSx/Ldap/Server/RequestHandler/HandlerFactory.php @@ -15,6 +15,7 @@ use FreeDSx\Ldap\Exception\RuntimeException; use FreeDSx\Ldap\Server\HandlerFactoryInterface; +use FreeDSx\Ldap\ServerOptions; use Throwable; /** @@ -31,15 +32,9 @@ class HandlerFactory implements HandlerFactoryInterface private ?PagingHandlerInterface $pagingHandler = null; - /** - * @var array - */ - private array $options; + private ServerOptions $options; - /** - * @param array $options - */ - public function __construct(array $options) + public function __construct(ServerOptions $options) { $this->options = $options; } @@ -50,20 +45,7 @@ public function __construct(array $options) public function makeRequestHandler(): RequestHandlerInterface { if (!$this->requestHandler) { - $requestHandler = !isset($this->options['request_handler']) - ? new GenericRequestHandler() - : $this->makeOrReturnInstanceOf( - 'request_handler', - RequestHandlerInterface::class - ); - if (!$requestHandler instanceof RequestHandlerInterface) { - throw new RuntimeException(sprintf( - 'Expected an instance of %s, got: %s', - RequestHandlerInterface::class, - get_class($requestHandler) - )); - } - $this->requestHandler = $requestHandler; + $this->requestHandler = $this->options->getRequestHandler() ?? new GenericRequestHandler(); } return $this->requestHandler; @@ -85,17 +67,7 @@ public function makeRootDseHandler(): ?RootDseHandlerInterface if ($this->rootdseHandler) { return $this->rootdseHandler; } - - if (isset($this->options['rootdse_handler'])) { - $handler = $this->makeOrReturnInstanceOf( - 'rootdse_handler', - RootDseHandlerInterface::class - ); - } - - if ($handler instanceof RootDseHandlerInterface) { - $this->rootdseHandler = $handler; - } + $this->rootdseHandler = $this->options->getRootDseHandler(); return $this->rootdseHandler; } @@ -108,72 +80,8 @@ public function makePagingHandler(): ?PagingHandlerInterface if ($this->pagingHandler) { return $this->pagingHandler; } - - $handler = null; - if (isset($this->options['paging_handler'])) { - $handler = $this->makeOrReturnInstanceOf( - 'paging_handler', - PagingHandlerInterface::class - ); - } - - if ($handler !== null && !$handler instanceof PagingHandlerInterface) { - throw new RuntimeException(sprintf( - 'Expected an instance of %s, got: %s', - PagingHandlerInterface::class, - get_class($handler) - )); - } - $this->pagingHandler = $handler; + $this->pagingHandler = $this->options->getPagingHandler(); return $this->pagingHandler; } - - /** - * @param class-string $class - */ - private function makeOrReturnInstanceOf( - string $optionName, - string $class - ): object { - if (!isset($this->options[$optionName])) { - throw new RuntimeException(sprintf( - 'Option "%s" must be an instance of or fully qualified class-name implementing "%s".', - $optionName, - $class - )); - } - - $objOrString = $this->options[$optionName]; - if (!(is_object($objOrString) || is_string($objOrString))) { - throw new RuntimeException(sprintf( - 'Option "%s" must be an instance of or fully qualified class-name implementing "%s".', - $optionName, - $class - )); - } - - if (is_object($objOrString) && is_subclass_of($objOrString, $class)) { - return $objOrString; - } - - if (is_string($objOrString) && is_subclass_of($objOrString, $class)) { - try { - return new $objOrString(); - } catch (Throwable $e) { - throw new RuntimeException(sprintf( - 'Unable to instantiate class "%s" for option "%s": %s', - $objOrString, - $optionName, - $e->getMessage() - ), $e->getCode(), $e); - } - } - - throw new RuntimeException(sprintf( - 'Option "%s" must be an instance of or fully qualified class-name implementing "%s".', - $optionName, - $class - )); - } } diff --git a/src/FreeDSx/Ldap/Server/RequestHandler/ProxyRequestHandler.php b/src/FreeDSx/Ldap/Server/RequestHandler/ProxyRequestHandler.php index 306ae093..1dc83c9d 100644 --- a/src/FreeDSx/Ldap/Server/RequestHandler/ProxyRequestHandler.php +++ b/src/FreeDSx/Ldap/Server/RequestHandler/ProxyRequestHandler.php @@ -13,6 +13,7 @@ namespace FreeDSx\Ldap\Server\RequestHandler; +use FreeDSx\Ldap\ClientOptions; use FreeDSx\Ldap\Entry\Entries; use FreeDSx\Ldap\Exception\BindException; use FreeDSx\Ldap\Exception\OperationException; @@ -38,10 +39,12 @@ class ProxyRequestHandler implements RequestHandlerInterface { protected ?LdapClient $ldap = null; - /** - * @var array - */ - protected array $options = []; + protected ClientOptions $options; + + public function __construct(ClientOptions $options = new ClientOptions()) + { + $this->options = $options; + } /** * @inheritDoc diff --git a/src/FreeDSx/Ldap/Server/ServerRunner/PcntlServerRunner.php b/src/FreeDSx/Ldap/Server/ServerRunner/PcntlServerRunner.php index b3093670..7933c219 100644 --- a/src/FreeDSx/Ldap/Server/ServerRunner/PcntlServerRunner.php +++ b/src/FreeDSx/Ldap/Server/ServerRunner/PcntlServerRunner.php @@ -15,11 +15,12 @@ use FreeDSx\Asn1\Exception\EncoderException; use FreeDSx\Ldap\Exception\RuntimeException; -use FreeDSx\Ldap\LoggerTrait; +use FreeDSx\Ldap\Server\LoggerTrait; use FreeDSx\Ldap\Protocol\Queue\ServerQueue; use FreeDSx\Ldap\Protocol\ServerProtocolHandler; use FreeDSx\Ldap\Server\ChildProcess; use FreeDSx\Ldap\Server\RequestHandler\HandlerFactory; +use FreeDSx\Ldap\ServerOptions; use FreeDSx\Socket\Socket; use FreeDSx\Socket\SocketServer; @@ -44,10 +45,7 @@ class PcntlServerRunner implements ServerRunnerInterface private SocketServer $server; - /** - * @var array - */ - private array $options; + private ServerOptions $options; /** * @var ChildProcess[] @@ -73,10 +71,9 @@ class PcntlServerRunner implements ServerRunnerInterface private array $defaultContext = []; /** - * @param array $options * @throws RuntimeException */ - public function __construct(array $options = []) + public function __construct(ServerOptions $options) { if (!extension_loaded('pcntl')) { throw new RuntimeException('The PCNTL extension is needed to fork incoming requests, which is only available on Linux.'); diff --git a/tests/bin/ldapserver.php b/tests/bin/ldapserver.php index 07f9b340..9e98426a 100644 --- a/tests/bin/ldapserver.php +++ b/tests/bin/ldapserver.php @@ -16,6 +16,7 @@ use FreeDSx\Ldap\Server\RequestContext; use FreeDSx\Ldap\Server\RequestHandler\GenericRequestHandler; use FreeDSx\Ldap\Server\RequestHandler\PagingHandlerInterface; +use FreeDSx\Ldap\ServerOptions; require __DIR__ . '/../../vendor/autoload.php'; @@ -200,18 +201,20 @@ private function logRequest( $useSsl = true; } -$server = new LdapServer([ - 'request_handler' => LdapServerRequestHandler::class, - 'port' => 3389, - 'transport' => $transport, - 'ssl_cert' => $sslCert, - 'ssl_cert_key' => $sslKey, - 'use_ssl' => $useSsl, -]); - -if ($handler === 'paging') { - $server->usePagingHandler(new LdapServerPagingHandler()); -} +$server = new LdapServer( + (new ServerOptions()) + ->setRequestHandler(new LdapServerRequestHandler()) + ->setPort(3389) + ->setTransport($transport) + ->setSslCert($sslCert) + ->setSslCertKey($sslKey) + ->setUseSsl($useSsl) + ->setPagingHandler( + $handler === 'paging' + ? new LdapServerPagingHandler() + : null + ) +); echo "server starting..." . PHP_EOL; diff --git a/tests/integration/FreeDSx/Ldap/LdapClientTest.php b/tests/integration/FreeDSx/Ldap/LdapClientTest.php index 7ffccaa4..ce11882e 100644 --- a/tests/integration/FreeDSx/Ldap/LdapClientTest.php +++ b/tests/integration/FreeDSx/Ldap/LdapClientTest.php @@ -440,7 +440,10 @@ public function testStartTls(): void public function testStartTlsFailure(): void { - $this->client = $this->getClient(['servers' => 'foo.com']); + $this->client = $this->getClient( + $this->makeOptions() + ->setServers(['foo.com']) + ); $this->expectException(ConnectionException::class); @$this->client->startTls(); @@ -448,10 +451,11 @@ public function testStartTlsFailure(): void public function testStartTlsIgnoreCertValidation(): void { - $this->client = $this->getClient([ - 'servers' => 'foo.com', - 'ssl_validate_cert' => false, - ]); + $this->client = $this->getClient( + $this->makeOptions() + ->setServers(['foo.com']) + ->setSslValidateCert(false) + ); $this->client->startTls(); $this->assertTrue($this->client->isConnected()); @@ -459,10 +463,11 @@ public function testStartTlsIgnoreCertValidation(): void public function testUseSsl(): void { - $this->client = $this->getClient([ - 'use_ssl' => true, - 'port' => 636 - ]); + $this->client = $this->getClient( + $this->makeOptions() + ->setUseSsl(true) + ->setPort(636) + ); $this->client->read(''); $this->assertTrue($this->client->isConnected()); @@ -473,10 +478,11 @@ public function testItCanWorkOverUnixSocket(): void if ($this->isActiveDirectory()) { $this->markTestSkipped('Connecting via a unix socket only tested on OpenLDAP.'); } - $this->client = $this->getClient([ - 'transport' => 'unix', - 'servers' => '/var/run/slapd/ldapi', - ]); + $this->client = $this->getClient( + $this->makeOptions() + ->setTransport('unix') + ->setServers(['/var/run/slapd/ldapi']) + ); $entry = $this->client->read(''); $this->assertNotNull($entry); @@ -484,11 +490,12 @@ public function testItCanWorkOverUnixSocket(): void public function testUseSslFailure(): void { - $this->client = $this->getClient([ - 'servers' => 'foo.com', - 'use_ssl' => true, - 'port' => 636 - ]); + $this->client = $this->getClient( + $this->makeOptions() + ->setServers(['foo.com']) + ->setUseSsl(true) + ->setPort(636) + ); $this->expectException(ConnectionException::class); @@ -497,12 +504,13 @@ public function testUseSslFailure(): void public function testUseSslIgnoreCertValidation(): void { - $this->client = $this->getClient([ - 'servers' => 'foo.com', - 'ssl_validate_cert' => false, - 'use_ssl' => true, - 'port' => 636, - ]); + $this->client = $this->getClient( + $this->makeOptions() + ->setServers(['foo.com']) + ->setSslValidateCert(false) + ->setUseSsl(true) + ->setPort(636) + ); $this->client->read(''); diff --git a/tests/integration/FreeDSx/Ldap/LdapTestCase.php b/tests/integration/FreeDSx/Ldap/LdapTestCase.php index b862fce0..8f50d619 100644 --- a/tests/integration/FreeDSx/Ldap/LdapTestCase.php +++ b/tests/integration/FreeDSx/Ldap/LdapTestCase.php @@ -13,6 +13,7 @@ namespace integration\FreeDSx\Ldap; +use FreeDSx\Ldap\ClientOptions; use FreeDSx\Ldap\LdapClient; use PHPUnit\Framework\TestCase; use Throwable; @@ -21,22 +22,22 @@ class LdapTestCase extends TestCase { protected static ?bool $isActiveDirectory = null; - /** - * @param array $options - */ - protected function getClient(array $options = []): LdapClient + protected function makeOptions(): ClientOptions { - return new LdapClient(array_merge( - [ - 'servers' => $_ENV['LDAP_SERVER'], - 'port' => $_ENV['LDAP_PORT'], - 'ssl_ca_cert' => $_ENV['LDAP_CA_CERT'] === '' + return (new ClientOptions()) + ->setBaseDn((string) $_ENV['LDAP_BASE_DN']) + ->setServers([(string) $_ENV['LDAP_SERVER']]) + ->setSslCaCert( + $_ENV['LDAP_CA_CERT'] === '' ? __DIR__ . '/../../../resources/cert/ca.crt' - : $_ENV['LDAP_CA_CERT'], - 'base_dn' => $_ENV['LDAP_BASE_DN'], - ], - $options, - )); + : (string) $_ENV['LDAP_CA_CERT'] + ) + ->setBaseDn((string) $_ENV['LDAP_BASE_DN']); + } + + protected function getClient(?ClientOptions $options = null): LdapClient + { + return new LdapClient($options ?? $this->makeOptions()); } protected function bindClient(LdapClient $client): void diff --git a/tests/integration/FreeDSx/Ldap/ServerTestCase.php b/tests/integration/FreeDSx/Ldap/ServerTestCase.php index becdaed4..ae9280ac 100644 --- a/tests/integration/FreeDSx/Ldap/ServerTestCase.php +++ b/tests/integration/FreeDSx/Ldap/ServerTestCase.php @@ -96,13 +96,14 @@ protected function createServerProcess( $servers = '/var/run/ldap.socket'; } - $this->client = $this->getClient([ - 'port' => 3389, - 'transport' => $transport, - 'servers' => $servers, - 'ssl_validate_cert' => false, - 'use_ssl' => $useSsl, - ]); + $this->client = $this->getClient( + $this->makeOptions() + ->setPort(3389) + ->setTransport($transport) + ->setServers((array) $servers) + ->setSslValidateCert(false) + ->setUseSsl($useSsl) + ); } protected function stopServer(): void diff --git a/tests/spec/FreeDSx/Ldap/LdapClientSpec.php b/tests/spec/FreeDSx/Ldap/LdapClientSpec.php index d66f8883..855e2bcc 100644 --- a/tests/spec/FreeDSx/Ldap/LdapClientSpec.php +++ b/tests/spec/FreeDSx/Ldap/LdapClientSpec.php @@ -13,6 +13,7 @@ namespace spec\FreeDSx\Ldap; +use FreeDSx\Ldap\ClientOptions; use FreeDSx\Ldap\Entry\Entries; use FreeDSx\Ldap\Entry\Entry; use FreeDSx\Ldap\Exception\OperationException; @@ -405,9 +406,11 @@ public function it_should_send_a_base_search_on_a_read_and_throw_an_unrelated_op ); } - public function it_should_get_the_options(): void + public function it_should_get_the_default_options(): void { - $this->getOptions()->shouldBeEqualTo([ + $this->getOptions() + ->toArray() + ->shouldBeEqualTo([ 'version' => 3, 'servers' => [], 'port' => 389, @@ -416,7 +419,7 @@ public function it_should_get_the_options(): void 'page_size' => 1000, 'use_ssl' => false, 'ssl_validate_cert' => true, - 'ssl_allow_self_signed' => null, + 'ssl_allow_self_signed' => false, 'ssl_ca_cert' => null, 'ssl_peer_name' => null, 'timeout_connect' => 3, @@ -429,20 +432,15 @@ public function it_should_get_the_options(): void public function it_should_set_the_options(): void { - $this->setOptions([ - 'servers' => [ - 'bar', + $options = (new ClientOptions()) + ->setServers([ 'foo', - ] - ]); + 'bar', + ]); + + $this->setOptions($options); $this->getOptions() - ->shouldHaveKeyWithValue( - 'servers', - [ - 'bar', - 'foo', - ] - ); + ->shouldBeEqualTo($options); } } diff --git a/tests/spec/FreeDSx/Ldap/LdapServerSpec.php b/tests/spec/FreeDSx/Ldap/LdapServerSpec.php index a1fc398e..08192a1f 100644 --- a/tests/spec/FreeDSx/Ldap/LdapServerSpec.php +++ b/tests/spec/FreeDSx/Ldap/LdapServerSpec.php @@ -13,11 +13,16 @@ namespace spec\FreeDSx\Ldap; +use FreeDSx\Ldap\ClientOptions; +use FreeDSx\Ldap\LdapClient; use FreeDSx\Ldap\LdapServer; use FreeDSx\Ldap\Server\RequestHandler\PagingHandlerInterface; +use FreeDSx\Ldap\Server\RequestHandler\ProxyHandler; +use FreeDSx\Ldap\Server\RequestHandler\ProxyPagingHandler; use FreeDSx\Ldap\Server\RequestHandler\RequestHandlerInterface; use FreeDSx\Ldap\Server\RequestHandler\RootDseHandlerInterface; use FreeDSx\Ldap\Server\ServerRunner\ServerRunnerInterface; +use FreeDSx\Ldap\ServerOptions; use FreeDSx\Socket\SocketServer; use PhpSpec\ObjectBehavior; use Prophecy\Argument; @@ -28,7 +33,7 @@ class LdapServerSpec extends ObjectBehavior public function let(ServerRunnerInterface $serverRunner): void { $this->beConstructedWith( - ['port' => 33389], + (new ServerOptions())->setPort(33389), $serverRunner ); } @@ -51,10 +56,8 @@ public function it_should_use_the_request_handler_specified(RequestHandlerInterf $this->useRequestHandler($requestHandler); $this->getOptions() - ->shouldHaveKeyWithValue( - 'request_handler', - $requestHandler - ); + ->getRequestHandler() + ->shouldBeEqualTo($requestHandler); } public function it_should_use_the_rootdse_handler_specified(RootDseHandlerInterface $rootDseHandler): void @@ -62,10 +65,8 @@ public function it_should_use_the_rootdse_handler_specified(RootDseHandlerInterf $this->useRootDseHandler($rootDseHandler); $this->getOptions() - ->shouldHaveKeyWithValue( - 'rootdse_handler', - $rootDseHandler - ); + ->getRootDseHandler() + ->shouldBeEqualTo($rootDseHandler); } public function it_should_use_the_paging_handler_specified(PagingHandlerInterface $pagingHandler): void @@ -73,10 +74,8 @@ public function it_should_use_the_paging_handler_specified(PagingHandlerInterfac $this->usePagingHandler($pagingHandler); $this->getOptions() - ->shouldHaveKeyWithValue( - 'paging_handler', - $pagingHandler - ); + ->getPagingHandler() + ->shouldBeEqualTo($pagingHandler); } public function it_should_use_the_logger_specified(LoggerInterface $logger): void @@ -84,15 +83,15 @@ public function it_should_use_the_logger_specified(LoggerInterface $logger): voi $this->useLogger($logger); $this->getOptions() - ->shouldHaveKeyWithValue( - 'logger', - $logger - ); + ->getLogger() + ->shouldBeEqualTo($logger); } public function it_should_get_the_default_options_with_any_merged_values(): void { - $this->getOptions()->shouldBeEqualTo([ + $this->getOptions() + ->toArray() + ->shouldBeEqualTo([ 'ip' => "0.0.0.0", 'port' => 33389, 'unix_socket' => "/var/run/ldap.socket", @@ -108,7 +107,9 @@ public function it_should_get_the_default_options_with_any_merged_values(): void 'ssl_cert' => null, 'ssl_cert_passphrase' => null, 'dse_alt_server' => null, - 'dse_naming_contexts' => "dc=FreeDSx,dc=local", + 'dse_naming_contexts' => [ + "dc=FreeDSx,dc=local" + ], 'dse_vendor_name' => "FreeDSx", 'dse_vendor_version' => null, ]); @@ -116,16 +117,18 @@ public function it_should_get_the_default_options_with_any_merged_values(): void public function it_should_make_a_proxy_server(): void { - $this->beConstructedThrough( - 'makeProxy', - ['localhost'] + $client = new LdapClient( + (new ClientOptions()) + ->setServers(['localhost']) ); - - $this->getOptions() - ->shouldHaveKey('request_handler'); - $this->getOptions() - ->shouldHaveKey('paging_handler'); - $this->getOptions() - ->shouldHaveKey('rootdse_handler'); + $serverOptions = new ServerOptions(); + $proxyRequestHandler = new ProxyHandler($client); + $server = new LdapServer($serverOptions); + $server->useRequestHandler($proxyRequestHandler); + $server->useRootDseHandler($proxyRequestHandler); + $server->usePagingHandler(new ProxyPagingHandler($client)); + + $this::makeProxy('localhost') + ->shouldBeLike($server); } } diff --git a/tests/spec/FreeDSx/Ldap/Protocol/ClientProtocolHandler/ClientBasicHandlerSpec.php b/tests/spec/FreeDSx/Ldap/Protocol/ClientProtocolHandler/ClientBasicHandlerSpec.php index 92d3bcc7..983f637c 100644 --- a/tests/spec/FreeDSx/Ldap/Protocol/ClientProtocolHandler/ClientBasicHandlerSpec.php +++ b/tests/spec/FreeDSx/Ldap/Protocol/ClientProtocolHandler/ClientBasicHandlerSpec.php @@ -13,6 +13,7 @@ namespace spec\FreeDSx\Ldap\Protocol\ClientProtocolHandler; +use FreeDSx\Ldap\ClientOptions; use FreeDSx\Ldap\Exception\BindException; use FreeDSx\Ldap\Operation\LdapResult; use FreeDSx\Ldap\Operation\Request\CompareRequest; @@ -71,25 +72,44 @@ public function it_should_handle_a_response(ClientQueue $queue): void $messageRequest = new LdapMessageRequest(1, new SimpleBindRequest('foo', 'bar')); $messageFrom = new LdapMessageResponse(1, new BindResponse(new LdapResult(0))); - $options = []; - $this->handleResponse($messageRequest, $messageFrom, $queue, $options)->shouldBeEqualTo($messageFrom); + $this->handleResponse( + $messageRequest, + $messageFrom, + $queue, + new ClientOptions() + )->shouldBeEqualTo($messageFrom); } public function it_should_handle_a_response_with_non_error_codes(ClientQueue $queue): void { - $options = []; + $options = new ClientOptions(); $messageRequest = new LdapMessageRequest(1, new CompareRequest('foo', new EqualityFilter('foo', 'bar'))); $messageFrom = new LdapMessageResponse(1, new CompareResponse(ResultCode::COMPARE_FALSE)); - $this->handleResponse($messageRequest, $messageFrom, $queue, $options)->shouldBeEqualTo($messageFrom); + $this->handleResponse( + $messageRequest, + $messageFrom, + $queue, + $options + )->shouldBeEqualTo($messageFrom); $messageFrom = new LdapMessageResponse(1, new CompareResponse(ResultCode::COMPARE_TRUE)); - $this->handleResponse($messageRequest, $messageFrom, $queue, $options)->shouldBeEqualTo($messageFrom); + $this->handleResponse( + $messageRequest, + $messageFrom, + $queue, + $options + )->shouldBeEqualTo($messageFrom); $messageFrom = new LdapMessageResponse(1, new CompareResponse(ResultCode::REFERRAL)); - $this->handleResponse($messageRequest, $messageFrom, $queue, $options)->shouldBeEqualTo($messageFrom); + $this->handleResponse( + $messageRequest, + $messageFrom, + $queue, + $options + )->shouldBeEqualTo($messageFrom); } public function it_should_throw_an_operation_exception_on_errors(ClientQueue $queue): void @@ -97,8 +117,12 @@ public function it_should_throw_an_operation_exception_on_errors(ClientQueue $qu $messageRequest = new LdapMessageRequest(1, new CompareRequest('foo', new EqualityFilter('foo', 'bar'))); $messageFrom = new LdapMessageResponse(1, new CompareResponse(ResultCode::COMPARE_FALSE)); - $options = []; - $this->handleResponse($messageRequest, $messageFrom, $queue, $options)->shouldBeEqualTo($messageFrom); + $this->handleResponse( + $messageRequest, + $messageFrom, + $queue, + new ClientOptions() + )->shouldBeEqualTo($messageFrom); } public function it_should_throw_a_specific_bind_exception_for_a_bind_response(ClientQueue $queue): void @@ -106,7 +130,17 @@ public function it_should_throw_a_specific_bind_exception_for_a_bind_response(Cl $messageRequest = new LdapMessageRequest(1, new SimpleBindRequest('foo', 'bar')); $messageFrom = new LdapMessageResponse(1, new BindResponse(new LdapResult(ResultCode::INVALID_CREDENTIALS, 'foo', 'message'))); - $options = []; - $this->shouldThrow(new BindException('Unable to bind to LDAP. message', ResultCode::INVALID_CREDENTIALS))->during('handleResponse', [$messageRequest, $messageFrom, $queue, $options]); + $this->shouldThrow(new BindException( + 'Unable to bind to LDAP. message', + ResultCode::INVALID_CREDENTIALS + ))->during( + 'handleResponse', + [ + $messageRequest, + $messageFrom, + $queue, + new ClientOptions(), + ] + ); } } diff --git a/tests/spec/FreeDSx/Ldap/Protocol/ClientProtocolHandler/ClientExtendedOperationHandlerSpec.php b/tests/spec/FreeDSx/Ldap/Protocol/ClientProtocolHandler/ClientExtendedOperationHandlerSpec.php index ee134664..1a309f96 100644 --- a/tests/spec/FreeDSx/Ldap/Protocol/ClientProtocolHandler/ClientExtendedOperationHandlerSpec.php +++ b/tests/spec/FreeDSx/Ldap/Protocol/ClientProtocolHandler/ClientExtendedOperationHandlerSpec.php @@ -13,6 +13,7 @@ namespace spec\FreeDSx\Ldap\Protocol\ClientProtocolHandler; +use FreeDSx\Ldap\ClientOptions; use FreeDSx\Ldap\Operation\LdapResult; use FreeDSx\Ldap\Operation\Request\ExtendedRequest; use FreeDSx\Ldap\Operation\Response\ExtendedResponse; @@ -54,7 +55,7 @@ public function it_should_handle_a_response(ExtendedResponseFactory $responseFac new LdapMessageRequest(1, new ExtendedRequest('foo', 'bar')), $response, $queue, - [] + new ClientOptions(), )->shouldBeEqualTo($response); } diff --git a/tests/spec/FreeDSx/Ldap/Protocol/ClientProtocolHandler/ClientProtocolContextSpec.php b/tests/spec/FreeDSx/Ldap/Protocol/ClientProtocolHandler/ClientProtocolContextSpec.php index a7750598..3c80afff 100644 --- a/tests/spec/FreeDSx/Ldap/Protocol/ClientProtocolHandler/ClientProtocolContextSpec.php +++ b/tests/spec/FreeDSx/Ldap/Protocol/ClientProtocolHandler/ClientProtocolContextSpec.php @@ -13,6 +13,7 @@ namespace spec\FreeDSx\Ldap\Protocol\ClientProtocolHandler; +use FreeDSx\Ldap\ClientOptions; use FreeDSx\Ldap\Entry\Dn; use FreeDSx\Ldap\Entry\Entry; use FreeDSx\Ldap\Operation\Request\DeleteRequest; @@ -26,7 +27,13 @@ class ClientProtocolContextSpec extends ObjectBehavior { public function let(ClientQueue $queue, ClientProtocolHandler $protocolHandler): void { - $this->beConstructedWith(new DeleteRequest('foo'), [], $protocolHandler, $queue, ['foo']); + $this->beConstructedWith( + new DeleteRequest('foo'), + [], + $protocolHandler, + $queue, + new ClientOptions() + ); } public function it_is_initializable(): void @@ -64,7 +71,7 @@ public function it_should_get_the_controls(): void public function it_should_get_the_options(): void { - $this->getOptions()->shouldBeEqualTo(['foo']); + $this->getOptions()->shouldBeLike(new ClientOptions()); } public function it_should_get_the_queue(ClientQueue $queue): void diff --git a/tests/spec/FreeDSx/Ldap/Protocol/ClientProtocolHandler/ClientReferralHandlerSpec.php b/tests/spec/FreeDSx/Ldap/Protocol/ClientProtocolHandler/ClientReferralHandlerSpec.php index ecb8b8f6..b5908308 100644 --- a/tests/spec/FreeDSx/Ldap/Protocol/ClientProtocolHandler/ClientReferralHandlerSpec.php +++ b/tests/spec/FreeDSx/Ldap/Protocol/ClientProtocolHandler/ClientReferralHandlerSpec.php @@ -13,6 +13,7 @@ namespace spec\FreeDSx\Ldap\Protocol\ClientProtocolHandler; +use FreeDSx\Ldap\ClientOptions; use FreeDSx\Ldap\Exception\ConnectionException; use FreeDSx\Ldap\Exception\OperationException; use FreeDSx\Ldap\Exception\ReferralException; @@ -45,11 +46,23 @@ public function it_should_throw_an_exception_on_referrals(ClientQueue $queue): v $response = new LdapMessageResponse(1, new DeleteResponse(ResultCode::REFERRAL, '', 'foo', new LdapUrl('foo'))); $request = new LdapMessageRequest(1, new DeleteRequest('cn=foo')); - $this->shouldThrow(ReferralException::class)->during('handleResponse', [$request, $response, $queue, ['referral' => 'throw']]); + $this->shouldThrow(ReferralException::class) + ->during( + 'handleResponse', + [ + $request, + $response, + $queue, + (new ClientOptions())->setReferral('throw') + ] + ); } - public function it_should_follow_referrals_with_a_referral_chaser_if_specified(ReferralChaserInterface $chaser, ClientQueue $queue, LdapClient $ldapClient): void - { + public function it_should_follow_referrals_with_a_referral_chaser_if_specified( + ReferralChaserInterface $chaser, + ClientQueue $queue, + LdapClient $ldapClient + ): void { $chaser->client(Argument::any())->willReturn($ldapClient); $bind = new SimpleBindRequest('foo', 'bar'); $chaser->chase(Argument::any(), Argument::any(), Argument::any())->willReturn($bind); @@ -62,34 +75,91 @@ public function it_should_follow_referrals_with_a_referral_chaser_if_specified(R new LdapMessageRequest(2, new DeleteRequest('foo')), new LdapMessageResponse(1, new DeleteResponse(ResultCode::REFERRAL, '', '', new LdapUrl('foo'))), $queue, - ['referral' => 'follow', 'referral_limit' => 10, 'referral_chaser' => $chaser] + (new ClientOptions()) + ->setReferral('follow') + ->setReferralLimit(10) + ->setReferralChaser($chaser->getWrappedObject()) )->shouldBeLike($message); } - public function it_should_throw_an_exception_if_the_referral_limit_is_reached(ReferralChaserInterface $chaser, ClientQueue $queue): void - { - $this->shouldThrow(new OperationException('The referral limit of -1 has been reached.'))->during('handleResponse', [ - new LdapMessageRequest(2, new DeleteRequest('foo')), - new LdapMessageResponse(1, new DeleteResponse(ResultCode::REFERRAL, '', '', new LdapUrl('foo'))), - $queue, - ['referral' => 'follow', 'referral_limit' => -1, 'referral_chaser' => $chaser] - ]); + public function it_should_throw_an_exception_if_the_referral_limit_is_reached( + ReferralChaserInterface $chaser, + ClientQueue $queue + ): void { + $clientOptions = (new ClientOptions()) + ->setReferral('follow') + ->setReferralLimit(-1) + ->setReferralChaser($chaser->getWrappedObject()); + + $chaser->client(Argument::any()) + ->willReturn(new LdapClient($clientOptions)); + + $this->shouldThrow(new OperationException( + 'The referral limit of -1 has been reached.' + ))->during( + 'handleResponse', + [ + new LdapMessageRequest( + 2, + new DeleteRequest('foo') + ), + new LdapMessageResponse( + 1, + new DeleteResponse( + ResultCode::REFERRAL, + '', + '', + new LdapUrl('foo') + ) + ), + $queue, + $clientOptions, + ] + ); } - public function it_should_throw_an_exception_if_all_referrals_have_been_tried_and_follow_is_specified(ReferralChaserInterface $chaser, ClientQueue $queue): void - { - $chaser->chase(Argument::any(), Argument::any(), Argument::any())->willThrow(new SkipReferralException()); - - $this->shouldThrow(new OperationException('All referral attempts have been exhausted. ', ResultCode::REFERRAL))->during('handleResponse', [ - new LdapMessageRequest(2, new DeleteRequest('foo')), - new LdapMessageResponse(1, new DeleteResponse(ResultCode::REFERRAL, '', '', new LdapUrl('foo'))), - $queue, - ['referral' => 'follow', 'referral_limit' => 10, 'referral_chaser' => $chaser] - ]); + public function it_should_throw_an_exception_if_all_referrals_have_been_tried_and_follow_is_specified( + ReferralChaserInterface $chaser, + ClientQueue $queue + ): void { + $chaser->chase( + Argument::any(), + Argument::any(), + Argument::any(), + )->willThrow(new SkipReferralException()); + + $this->shouldThrow(new OperationException( + 'All referral attempts have been exhausted. ', + ResultCode::REFERRAL + ))->during('handleResponse', + [ + new LdapMessageRequest( + 2, + new DeleteRequest('foo') + ), + new LdapMessageResponse( + 1, + new DeleteResponse( + ResultCode::REFERRAL, + '', + '', + new LdapUrl('foo') + ) + ), + $queue, + (new ClientOptions()) + ->setReferral('follow') + ->setReferralLimit(10) + ->setReferralChaser($chaser->getWrappedObject()) + ] + ); } - public function it_should_continue_to_the_next_referral_if_a_connection_exception_is_thrown(ReferralChaserInterface $chaser, ClientQueue $queue, LdapClient $ldapClient): void - { + public function it_should_continue_to_the_next_referral_if_a_connection_exception_is_thrown( + ReferralChaserInterface $chaser, + ClientQueue $queue, + LdapClient $ldapClient + ): void { $chaser->client(Argument::any())->willReturn($ldapClient); $bind = new SimpleBindRequest('foo', 'bar'); @@ -106,12 +176,18 @@ public function it_should_continue_to_the_next_referral_if_a_connection_exceptio new LdapMessageRequest(1, new DeleteRequest('foo')), new LdapMessageResponse(1, new DeleteResponse(ResultCode::REFERRAL, '', '', new LdapUrl('foo'))), $queue, - ['referral' => 'follow', 'referral_limit' => 10, 'referral_chaser' => $chaser] + (new ClientOptions()) + ->setReferral('follow') + ->setReferralLimit(10) + ->setReferralChaser($chaser->getWrappedObject()) )->shouldBeLike($message); } - public function it_should_continue_to_the_next_referral_if_an_operation_exception_with_a_referral_result_code_is_thrown(ReferralChaserInterface $chaser, ClientQueue $queue, LdapClient $ldapClient): void - { + public function it_should_continue_to_the_next_referral_if_an_operation_exception_with_a_referral_result_code_is_thrown( + ReferralChaserInterface $chaser, + ClientQueue $queue, + LdapClient $ldapClient + ): void { $chaser->client(Argument::any())->willReturn($ldapClient); $bind = new SimpleBindRequest('foo', 'bar'); $chaser->chase(Argument::any(), Argument::any(), Argument::any())->willReturn($bind); @@ -126,12 +202,18 @@ public function it_should_continue_to_the_next_referral_if_an_operation_exceptio new LdapMessageRequest(1, new DeleteRequest('foo')), new LdapMessageResponse(1, new DeleteResponse(ResultCode::REFERRAL, '', '', new LdapUrl('foo'))), $queue, - ['referral' => 'follow', 'referral_limit' => 10, 'referral_chaser' => $chaser] + (new ClientOptions()) + ->setReferral('follow') + ->setReferralLimit(10) + ->setReferralChaser($chaser->getWrappedObject()) )->shouldBeLike($message); } - public function it_should_not_bind_on_the_referral_client_initially_if_the_referral_is_for_a_bind_request(ReferralChaserInterface $chaser, ClientQueue $queue, LdapClient $ldapClient): void - { + public function it_should_not_bind_on_the_referral_client_initially_if_the_referral_is_for_a_bind_request( + ReferralChaserInterface $chaser, + ClientQueue $queue, + LdapClient $ldapClient + ): void { $chaser->client(Argument::any())->willReturn($ldapClient); $chaser->chase(Argument::any(), Argument::any(), Argument::any())->willReturn(new SimpleBindRequest('foo', 'bar')); $ldapClient->send(Argument::any())->shouldBeCalledTimes(1); @@ -143,11 +225,10 @@ public function it_should_not_bind_on_the_referral_client_initially_if_the_refer new LdapMessageRequest(1, new SimpleBindRequest('foo', 'bar')), new LdapMessageResponse(1, new BindResponse(new LdapResult(ResultCode::REFERRAL, '', '', new LdapUrl('foo')))), $queue, - [ - 'referral' => 'follow', - 'referral_limit' => 10, - 'referral_chaser' => $chaser, - ] + (new ClientOptions()) + ->setReferral('follow') + ->setReferralLimit(10) + ->setReferralChaser($chaser->getWrappedObject()) )->shouldBeLike($message); } } diff --git a/tests/spec/FreeDSx/Ldap/Protocol/ClientProtocolHandler/ClientSearchHandlerSpec.php b/tests/spec/FreeDSx/Ldap/Protocol/ClientProtocolHandler/ClientSearchHandlerSpec.php index d4e2fdbd..076b1e6d 100644 --- a/tests/spec/FreeDSx/Ldap/Protocol/ClientProtocolHandler/ClientSearchHandlerSpec.php +++ b/tests/spec/FreeDSx/Ldap/Protocol/ClientProtocolHandler/ClientSearchHandlerSpec.php @@ -13,6 +13,7 @@ namespace spec\FreeDSx\Ldap\Protocol\ClientProtocolHandler; +use FreeDSx\Ldap\ClientOptions; use FreeDSx\Ldap\Entry\Entries; use FreeDSx\Ldap\Entry\Entry; use FreeDSx\Ldap\Exception\OperationException; @@ -52,8 +53,11 @@ public function it_should_implement_RequestHandlerInterface(): void $this->shouldBeAnInstanceOf(RequestHandlerInterface::class); } - public function it_should_send_a_request_and_get_a_response(ClientProtocolContext $context, ClientQueue $queue, LdapMessageResponse $response): void - { + public function it_should_send_a_request_and_get_a_response( + ClientProtocolContext $context, + ClientQueue $queue, + LdapMessageResponse $response + ): void { $request = Operations::search(new EqualityFilter('foo', 'bar')); $message = new LdapMessageRequest(1, $request); @@ -63,13 +67,18 @@ public function it_should_send_a_request_and_get_a_response(ClientProtocolContex $context->getRequest()->willReturn($request); $context->messageToSend()->willReturn($message); $context->getQueue()->willReturn($queue); - $context->getOptions()->willReturn([]); + $context->getOptions()->willReturn(new ClientOptions()); $this->handleRequest($context)->shouldBeEqualTo($response); } - public function it_should_set_a_default_DN_for_a_request_that_has_none(ClientProtocolContext $context, LdapMessageResponse $response, ClientQueue $queue, LdapMessageRequest $message, SearchRequest $request): void - { + public function it_should_set_a_default_DN_for_a_request_that_has_none( + ClientProtocolContext $context, + LdapMessageResponse $response, + ClientQueue $queue, + LdapMessageRequest $message, + SearchRequest $request + ): void { $queue->getMessage(1)->shouldBeCalled()->willReturn($response); $queue->sendMessage($message)->shouldBeCalledOnce(); @@ -80,9 +89,14 @@ public function it_should_set_a_default_DN_for_a_request_that_has_none(ClientPro $context->messageToSend()->willReturn($message); $context->getRequest()->willReturn($request); $context->getQueue()->willReturn($queue); - $context->getOptions()->willReturn(['base_dn' => 'cn=foo']); + $context->getOptions()->willReturn( + (new ClientOptions()) + ->setBaseDn('cn=foo') + ); + + $request->setBaseDn('cn=foo') + ->shouldBeCalledOnce(); - $request->setBaseDn('cn=foo')->shouldBeCalledOnce(); $this->handleRequest($context); } @@ -91,8 +105,16 @@ public function it_should_not_keep_getting_messages_when_the_first_result_is_sea $messageTo = new LdapMessageRequest(1, new SearchRequest(new EqualityFilter('foo', 'bar'))); $response = new LdapMessageResponse(1, new SearchResultDone(0)); - $queue->getMessage(Argument::any())->shouldNotBeCalled(); - $this->handleResponse($messageTo, $response, $queue, [])->getResponse()->shouldBeAnInstanceOf(SearchResponse::class); + $queue->getMessage(Argument::any()) + ->shouldNotBeCalled(); + + $this->handleResponse( + $messageTo, + $response, + $queue, + new ClientOptions() + )->getResponse() + ->shouldBeAnInstanceOf(SearchResponse::class); } public function it_should_retrieve_results_until_it_receives_a_search_done_and_return_all_results(ClientQueue $queue): void @@ -109,7 +131,12 @@ public function it_should_retrieve_results_until_it_receives_a_search_done_and_r ); $queue->getMessage(1)->shouldBeCalledTimes(5); - $this->handleResponse($messageTo, $response, $queue, [])->shouldBeLike( + $this->handleResponse( + $messageTo, + $response, + $queue, + new ClientOptions() + )->shouldBeLike( new LdapMessageResponse(1, new SearchResponse(new LdapResult(0, 'cn=foo', 'bar'), new Entries( new Entry('bar'), new Entry('foo'), @@ -124,6 +151,14 @@ public function it_should_throw_an_exception_if_the_result_code_is_not_success(C $messageTo = new LdapMessageRequest(1, new SearchRequest(new EqualityFilter('foo', 'bar'))); $response = new LdapMessageResponse(1, new SearchResultDone(ResultCode::SIZE_LIMIT_EXCEEDED)); - $this->shouldThrow(OperationException::class)->during('handleResponse', [$messageTo, $response, $queue, []]); + $this->shouldThrow(OperationException::class)->during( + 'handleResponse', + [ + $messageTo, + $response, + $queue, + new ClientOptions() + ] + ); } } diff --git a/tests/spec/FreeDSx/Ldap/Protocol/ClientProtocolHandler/ClientStartTlsHandlerSpec.php b/tests/spec/FreeDSx/Ldap/Protocol/ClientProtocolHandler/ClientStartTlsHandlerSpec.php index 312fa182..26d39c7a 100644 --- a/tests/spec/FreeDSx/Ldap/Protocol/ClientProtocolHandler/ClientStartTlsHandlerSpec.php +++ b/tests/spec/FreeDSx/Ldap/Protocol/ClientProtocolHandler/ClientStartTlsHandlerSpec.php @@ -13,6 +13,7 @@ namespace spec\FreeDSx\Ldap\Protocol\ClientProtocolHandler; +use FreeDSx\Ldap\ClientOptions; use FreeDSx\Ldap\Exception\ConnectionException; use FreeDSx\Ldap\Operation\LdapResult; use FreeDSx\Ldap\Operation\Request\ExtendedRequest; @@ -43,7 +44,7 @@ public function it_should_encrypt_the_queue_if_the_message_response_is_successfu $response = new LdapMessageResponse(1, new ExtendedResponse(new LdapResult(0), ExtendedRequest::OID_START_TLS)); $queue->encrypt()->shouldBeCalledOnce()->willReturn($queue); - $this->handleResponse($startTls, $response, $queue, [])->shouldBeAnInstanceOf(LdapMessageResponse::class); + $this->handleResponse($startTls, $response, $queue, new ClientOptions())->shouldBeAnInstanceOf(LdapMessageResponse::class); } public function it_should_throw_an_exception_if_the_message_response_is_unsuccessful(ClientQueue $queue): void @@ -52,6 +53,6 @@ public function it_should_throw_an_exception_if_the_message_response_is_unsucces $response = new LdapMessageResponse(1, new ExtendedResponse(new LdapResult(ResultCode::UNAVAILABLE_CRITICAL_EXTENSION), ExtendedRequest::OID_START_TLS)); $queue->encrypt(true)->shouldNotBeCalled(); - $this->shouldThrow(ConnectionException::class)->during('handleResponse', [$startTls, $response, $queue, []]); + $this->shouldThrow(ConnectionException::class)->during('handleResponse', [$startTls, $response, $queue, new ClientOptions()]); } } diff --git a/tests/spec/FreeDSx/Ldap/Protocol/ClientProtocolHandlerSpec.php b/tests/spec/FreeDSx/Ldap/Protocol/ClientProtocolHandlerSpec.php index 8c385095..8cca2ca0 100644 --- a/tests/spec/FreeDSx/Ldap/Protocol/ClientProtocolHandlerSpec.php +++ b/tests/spec/FreeDSx/Ldap/Protocol/ClientProtocolHandlerSpec.php @@ -13,6 +13,7 @@ namespace spec\FreeDSx\Ldap\Protocol; +use FreeDSx\Ldap\ClientOptions; use FreeDSx\Ldap\Entry\Dn; use FreeDSx\Ldap\Entry\Entries; use FreeDSx\Ldap\Entry\Entry; @@ -39,13 +40,23 @@ class ClientProtocolHandlerSpec extends ObjectBehavior { - public function let(SocketPool $pool, ClientQueue $queue, ClientProtocolHandlerFactory $protocolHandlerFactory, ResponseHandlerInterface $responseHandler, RequestHandlerInterface $requestHandler): void - { + public function let( + SocketPool $pool, + ClientQueue $queue, + ClientProtocolHandlerFactory $protocolHandlerFactory, + ResponseHandlerInterface $responseHandler, + RequestHandlerInterface $requestHandler + ): void { $protocolHandlerFactory->forResponse(Argument::any(), Argument::any())->willReturn($responseHandler); $protocolHandlerFactory->forRequest(Argument::any())->willReturn($requestHandler); $queue->generateId()->willReturn(1); - $this->beConstructedWith([], $queue, $pool, $protocolHandlerFactory); + $this->beConstructedWith( + new ClientOptions(), + $queue, + $pool, + $protocolHandlerFactory + ); } public function it_is_initializable(): void @@ -75,10 +86,15 @@ public function it_should_send_a_request_and_handle_a_response(RequestHandlerInt $messageRequest = new LdapMessageRequest(1, $request); $requestHandler->handleRequest(Argument::that(function (ClientProtocolHandler\ClientProtocolContext $context) use ($request) { - return $context->getRequest() === $request && $context->getOptions() === []; + return $context->getRequest() === $request; }))->shouldBeCalledOnce() ->willReturn($messageResponse); - $responseHandler->handleResponse($messageRequest, $messageResponse, $queue, [])->shouldBeCalledOnce() + $responseHandler->handleResponse( + $messageRequest, + $messageResponse, + $queue, + new ClientOptions() + )->shouldBeCalledOnce() ->willReturn($messageResponse); $this->send($request)->shouldBeLike($messageResponse); @@ -93,7 +109,12 @@ public function it_should_return_null_if_no_response_was_returned(ResponseHandle return $context->getRequest() === $request; }))->shouldBeCalledOnce() ->willReturn(null); - $responseHandler->handleResponse(Argument::any(), Argument::any(), Argument::any(), [])->shouldNotBeCalled(); + $responseHandler->handleResponse( + Argument::any(), + Argument::any(), + Argument::any(), + Argument::any() + )->shouldNotBeCalled(); $this->send($request)->shouldBeEqualTo(null); } @@ -110,7 +131,12 @@ public function it_should_throw_a_LDAP_specific_connection_exception_if_the_resp }) )->shouldBeCalledOnce() ->willReturn($messageResponse); - $responseHandler->handleResponse($messageRequest, $messageResponse, $queue, [])->shouldBeCalledOnce() + $responseHandler->handleResponse( + $messageRequest, + $messageResponse, + $queue, + new ClientOptions() + )->shouldBeCalledOnce() ->willThrow(new \FreeDSx\Socket\Exception\ConnectionException('foo')); $this->shouldThrow(ConnectionException::class)->during('send', [$request]); @@ -126,7 +152,12 @@ public function it_should_fetch_the_root_dse(RequestHandlerInterface $requestHan return $context->getRequest() instanceof SearchRequest && $request->getFilter()->toString() === '(objectClass=*)'; }))->shouldBeCalledOnce() ->willReturn($messageResponse); - $responseHandler->handleResponse($messageRequest, $messageResponse, $queue, [])->shouldBeCalledOnce() + $responseHandler->handleResponse( + $messageRequest, + $messageResponse, + $queue, + new ClientOptions() + )->shouldBeCalledOnce() ->willReturn($messageResponse); $this->fetchRootDse()->shouldBeLike(new Entry(new Dn(''))); diff --git a/tests/spec/FreeDSx/Ldap/Protocol/ServerAuthorizationSpec.php b/tests/spec/FreeDSx/Ldap/Protocol/ServerAuthorizationSpec.php index b676502c..4ab81fd2 100644 --- a/tests/spec/FreeDSx/Ldap/Protocol/ServerAuthorizationSpec.php +++ b/tests/spec/FreeDSx/Ldap/Protocol/ServerAuthorizationSpec.php @@ -83,7 +83,7 @@ public function it_should_require_authentication_for_all_other_operations(): voi public function it_should_not_require_authentication_if_it_has_been_explicitly_disabled(): void { - $this->beConstructedWith(null, ['require_authentication' => false]); + $this->beConstructedWith(new AnonToken(), false, false); $this->isAuthenticationRequired(Operations::read('cn=bar'))->shouldBeEqualTo(false); $this->isAuthenticationRequired(Operations::list(new EqualityFilter('foo', 'bar'), 'cn=foo'))->shouldBeEqualTo(false); @@ -104,7 +104,7 @@ public function it_should_not_allow_anonymous_authentication_by_default(): void public function it_should_respect_the_option_for_whether_anon_binds_are_allowed(): void { - $this->beConstructedWith(null, ['allow_anonymous' => true]); + $this->beConstructedWith(new AnonToken(), true); $this->isAuthenticationTypeSupported(Operations::bindAnonymously())->shouldBeEqualTo(true); } diff --git a/tests/spec/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerAnonBindHandlerSpec.php b/tests/spec/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerAnonBindHandlerSpec.php index f71c68b4..94c4084c 100644 --- a/tests/spec/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerAnonBindHandlerSpec.php +++ b/tests/spec/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerAnonBindHandlerSpec.php @@ -23,6 +23,7 @@ use FreeDSx\Ldap\Protocol\ServerProtocolHandler\ServerAnonBindHandler; use FreeDSx\Ldap\Server\RequestHandler\RequestHandlerInterface; use FreeDSx\Ldap\Server\Token\AnonToken; +use FreeDSx\Ldap\ServerOptions; use PhpSpec\ObjectBehavior; use Prophecy\Argument; @@ -44,7 +45,7 @@ public function it_should_validate_the_version(ServerQueue $queue, RequestHandle $this->shouldThrow(OperationException::class)->during( 'handleBind', - [$bind, $dispatcher, $queue, []] + [$bind, $dispatcher, $queue] ); } @@ -59,7 +60,7 @@ public function it_should_return_an_anon_token_with_the_supplied_username(Server new LdapResult(0) )))->shouldBeCalled()->willReturn($queue); - $this->handleBind($bind, $dispatcher, $queue, [])->shouldBeLike( + $this->handleBind($bind, $dispatcher, $queue)->shouldBeLike( new AnonToken('foo') ); } diff --git a/tests/spec/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerBindHandlerSpec.php b/tests/spec/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerBindHandlerSpec.php index 0d2edeb5..6547db49 100644 --- a/tests/spec/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerBindHandlerSpec.php +++ b/tests/spec/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerBindHandlerSpec.php @@ -45,7 +45,7 @@ public function it_should_return_a_token_on_success(ServerQueue $queue, RequestH new LdapResult(0) )))->shouldBeCalled()->willReturn($queue); - $this->handleBind($bind, $dispatcher, $queue, [])->shouldBeLike( + $this->handleBind($bind, $dispatcher, $queue)->shouldBeLike( new BindToken('foo@bar', 'bar') ); } @@ -62,7 +62,7 @@ public function it_should_throw_an_operations_exception_with_invalid_credentials $this->shouldThrow(new OperationException('Invalid credentials.', ResultCode::INVALID_CREDENTIALS)) ->during( 'handleBind', - [$bind, $dispatcher, $queue, []] + [$bind, $dispatcher, $queue] ); } @@ -75,7 +75,7 @@ public function it_should_validate_the_version(ServerQueue $queue, RequestHandle $this->shouldThrow(OperationException::class) ->during( 'handleBind', - [$bind, $dispatcher, $queue, []] + [$bind, $dispatcher, $queue] ); } } diff --git a/tests/spec/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerDispatchHandlerSpec.php b/tests/spec/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerDispatchHandlerSpec.php index 64484613..d828847b 100644 --- a/tests/spec/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerDispatchHandlerSpec.php +++ b/tests/spec/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerDispatchHandlerSpec.php @@ -29,6 +29,7 @@ use FreeDSx\Ldap\Search\Filters; use FreeDSx\Ldap\Server\RequestHandler\RequestHandlerInterface; use FreeDSx\Ldap\Server\Token\TokenInterface; +use FreeDSx\Ldap\ServerOptions; use PhpSpec\ObjectBehavior; use Prophecy\Argument; @@ -49,7 +50,7 @@ public function it_should_send_an_add_request_to_the_request_handler(ServerQueue $add = new LdapMessageRequest(1, new AddRequest(Entry::create('cn=foo,dc=bar'))); $handler->add(Argument::any(), $add->getRequest())->shouldBeCalled(); - $this->handleRequest($add, $token, $handler, $queue, []); + $this->handleRequest($add, $token, $handler, $queue, new ServerOptions()); } public function it_should_send_a_delete_request_to_the_request_handler(ServerQueue $queue, RequestHandlerInterface $handler, TokenInterface $token): void @@ -57,7 +58,7 @@ public function it_should_send_a_delete_request_to_the_request_handler(ServerQue $delete = new LdapMessageRequest(1, new DeleteRequest('cn=foo,dc=bar')); $handler->delete(Argument::any(), $delete->getRequest())->shouldBeCalled(); - $this->handleRequest($delete, $token, $handler, $queue, []); + $this->handleRequest($delete, $token, $handler, $queue, new ServerOptions()); } public function it_should_send_a_modify_request_to_the_request_handler(ServerQueue $queue, RequestHandlerInterface $handler, TokenInterface $token): void @@ -65,7 +66,7 @@ public function it_should_send_a_modify_request_to_the_request_handler(ServerQue $modify = new LdapMessageRequest(1, new ModifyRequest('cn=foo,dc=bar', Change::add('foo', 'bar'))); $handler->modify(Argument::any(), $modify->getRequest())->shouldBeCalled(); - $this->handleRequest($modify, $token, $handler, $queue, []); + $this->handleRequest($modify, $token, $handler, $queue, new ServerOptions()); } public function it_should_send_a_modify_dn_request_to_the_request_handler(ServerQueue $queue, RequestHandlerInterface $handler, TokenInterface $token): void @@ -73,7 +74,7 @@ public function it_should_send_a_modify_dn_request_to_the_request_handler(Server $modifyDn = new LdapMessageRequest(1, new ModifyDnRequest('cn=foo,dc=bar', 'cn=bar', true)); $handler->modifyDn(Argument::any(), $modifyDn->getRequest())->shouldBeCalled(); - $this->handleRequest($modifyDn, $token, $handler, $queue, []); + $this->handleRequest($modifyDn, $token, $handler, $queue, new ServerOptions()); } public function it_should_send_an_extended_request_to_the_request_handler(ServerQueue $queue, RequestHandlerInterface $handler, TokenInterface $token): void @@ -81,7 +82,7 @@ public function it_should_send_an_extended_request_to_the_request_handler(Server $ext = new LdapMessageRequest(1, new ExtendedRequest('foo', 'bar')); $handler->extended(Argument::any(), $ext->getRequest())->shouldBeCalled(); - $this->handleRequest($ext, $token, $handler, $queue, []); + $this->handleRequest($ext, $token, $handler, $queue, new ServerOptions()); } public function it_should_send_a_compare_request_to_the_request_handler(ServerQueue $queue, RequestHandlerInterface $handler, TokenInterface $token): void @@ -89,7 +90,7 @@ public function it_should_send_a_compare_request_to_the_request_handler(ServerQu $compare = new LdapMessageRequest(1, new CompareRequest('cn=foo,dc=bar', Filters::equal('foo', 'bar'))); $handler->compare(Argument::any(), $compare->getRequest())->shouldBeCalled()->willReturn(true); - $this->handleRequest($compare, $token, $handler, $queue, []); + $this->handleRequest($compare, $token, $handler, $queue, new ServerOptions()); } public function it_should_throw_an_operation_exception_if_the_request_is_unsupported(ServerQueue $queue, RequestHandlerInterface $handler, TokenInterface $token): void @@ -98,7 +99,7 @@ public function it_should_throw_an_operation_exception_if_the_request_is_unsuppo $this->shouldThrow(OperationException::class)->during( 'handleRequest', - [$request, $token, $handler, $queue, []] + [$request, $token, $handler, $queue, new ServerOptions()] ); } } diff --git a/tests/spec/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerPagingHandlerSpec.php b/tests/spec/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerPagingHandlerSpec.php index 5ab608af..c5f907a7 100644 --- a/tests/spec/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerPagingHandlerSpec.php +++ b/tests/spec/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerPagingHandlerSpec.php @@ -34,6 +34,7 @@ use FreeDSx\Ldap\Server\RequestHandler\RequestHandlerInterface; use FreeDSx\Ldap\Server\RequestHistory; use FreeDSx\Ldap\Server\Token\TokenInterface; +use FreeDSx\Ldap\ServerOptions; use PhpSpec\ObjectBehavior; use Prophecy\Argument; @@ -100,7 +101,7 @@ public function it_should_send_a_request_to_the_paging_handler_on_paging_start( $token, $handler, $queue, - [] + new ServerOptions() ); } @@ -151,7 +152,7 @@ public function it_should_send_the_correct_response_if_paging_is_complete( $token, $handler, $queue, - [] + new ServerOptions() ); } @@ -188,7 +189,7 @@ public function it_should_send_the_correct_response_if_paging_is_abandoned( $token, $handler, $queue, - [] + new ServerOptions() ); } @@ -229,7 +230,7 @@ public function it_sends_a_result_code_error_in_SearchResultDone_if_the_old_and_ $token, $handler, $queue, - [] + new ServerOptions() ); } @@ -249,7 +250,7 @@ public function it_throws_an_exception_if_the_paging_cookie_does_not_exist( $token, $handler, $queue, - [] + new ServerOptions() ]); } diff --git a/tests/spec/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerPagingUnsupportedHandlerSpec.php b/tests/spec/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerPagingUnsupportedHandlerSpec.php index b3fdb740..6f7e015e 100644 --- a/tests/spec/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerPagingUnsupportedHandlerSpec.php +++ b/tests/spec/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerPagingUnsupportedHandlerSpec.php @@ -28,6 +28,7 @@ use FreeDSx\Ldap\Search\Filters; use FreeDSx\Ldap\Server\RequestHandler\RequestHandlerInterface; use FreeDSx\Ldap\Server\Token\TokenInterface; +use FreeDSx\Ldap\ServerOptions; use PhpSpec\ObjectBehavior; use Prophecy\Argument; @@ -80,7 +81,7 @@ public function it_should_send_a_search_request_to_the_request_handler_if_paging $token, $handler, $queue, - [] + new ServerOptions() ); } @@ -103,7 +104,7 @@ public function it_should_throw_an_unavailable_critical_extension_if_paging_is_m $token, $handler, $queue, - [] + new ServerOptions() ]); } @@ -145,7 +146,7 @@ public function it_should_send_a_SearchResultDone_with_an_operation_exception_th $token, $handler, $queue, - [] + new ServerOptions() ); } } diff --git a/tests/spec/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerRootDseHandlerSpec.php b/tests/spec/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerRootDseHandlerSpec.php index cb3df13d..d407c6cd 100644 --- a/tests/spec/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerRootDseHandlerSpec.php +++ b/tests/spec/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerRootDseHandlerSpec.php @@ -30,6 +30,7 @@ use FreeDSx\Ldap\Server\RequestHandler\RequestHandlerInterface; use FreeDSx\Ldap\Server\RequestHandler\RootDseHandlerInterface; use FreeDSx\Ldap\Server\Token\TokenInterface; +use FreeDSx\Ldap\ServerOptions; use PhpSpec\ObjectBehavior; use Prophecy\Argument; @@ -72,14 +73,17 @@ public function it_should_send_back_a_RootDSE( $token, $handler, $queue, - ['dse_vendor_name' => 'Foo', 'dse_naming_contexts' => ['dc=Foo,dc=Bar']] + (new ServerOptions()) + ->setDseVendorName('Foo') + ->setDseNamingContexts('dc=Foo,dc=Bar') ); } public function it_should_send_back_a_RootDSE_with_paging_support_if_the_paging_handler_is_set( ServerQueue $queue, RequestHandlerInterface $handler, - TokenInterface $token + TokenInterface $token, + PagingHandlerInterface $pagingHandler, ): void { $search = new LdapMessageRequest( 1, @@ -103,11 +107,10 @@ public function it_should_send_back_a_RootDSE_with_paging_support_if_the_paging_ $token, $handler, $queue, - [ - 'paging_handler' => PagingHandlerInterface::class, - 'dse_vendor_name' => 'Foo', - 'dse_naming_contexts' => ['dc=Foo,dc=Bar'], - ] + (new ServerOptions()) + ->setDseVendorName('Foo') + ->setDseNamingContexts('dc=Foo,dc=Bar') + ->setPagingHandler($pagingHandler->getWrappedObject()) ); } @@ -148,7 +151,9 @@ public function it_should_send_a_request_to_the_dispatcher_if_it_implements_a_ro $token, $handler, $queue, - ['dse_vendor_name' => 'Foo', 'dse_naming_contexts' => ['dc=Foo,dc=Bar']] + (new ServerOptions()) + ->setDseVendorName('Foo') + ->setDseNamingContexts('dc=Foo,dc=Bar') ); } @@ -180,7 +185,9 @@ public function it_should_only_return_attribute_names_from_the_RootDSE_if_reques $token, $handler, $queue, - ['dse_vendor_name' => 'Foo', 'dse_naming_contexts' => ['dc=Foo,dc=Bar']] + (new ServerOptions()) + ->setDseVendorName('Foo') + ->setDseNamingContexts('dc=Foo,dc=Bar') ); } @@ -215,7 +222,9 @@ public function it_should_only_return_specific_attributes_from_the_RootDSE_if_re $token, $handler, $queue, - ['dse_vendor_name' => 'Foo', 'dse_naming_contexts' => ['dc=Foo,dc=Bar']] + (new ServerOptions()) + ->setDseVendorName('Foo') + ->setDseNamingContexts('dc=Foo,dc=Bar') ); } } diff --git a/tests/spec/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerSearchHandlerSpec.php b/tests/spec/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerSearchHandlerSpec.php index 23cf29e0..01a2ac4c 100644 --- a/tests/spec/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerSearchHandlerSpec.php +++ b/tests/spec/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerSearchHandlerSpec.php @@ -27,6 +27,7 @@ use FreeDSx\Ldap\Search\Filters; use FreeDSx\Ldap\Server\RequestHandler\RequestHandlerInterface; use FreeDSx\Ldap\Server\Token\TokenInterface; +use FreeDSx\Ldap\ServerOptions; use PhpSpec\ObjectBehavior; use Prophecy\Argument; @@ -58,7 +59,7 @@ public function it_should_send_a_search_request_to_the_request_handler(ServerQue new LdapMessageResponse(2, new SearchResultDone(0, 'dc=foo,dc=bar')) )->shouldBeCalled(); - $this->handleRequest($search, $token, $handler, $queue, []); + $this->handleRequest($search, $token, $handler, $queue, new ServerOptions()); } public function it_should_send_a_SearchResultDone_with_an_operation_exception_thrown_from_the_handler( @@ -95,7 +96,7 @@ public function it_should_send_a_SearchResultDone_with_an_operation_exception_th $token, $handler, $queue, - [] + new ServerOptions() ); } } diff --git a/tests/spec/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerStartTlsHandlerSpec.php b/tests/spec/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerStartTlsHandlerSpec.php index b660d441..66972575 100644 --- a/tests/spec/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerStartTlsHandlerSpec.php +++ b/tests/spec/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerStartTlsHandlerSpec.php @@ -23,6 +23,7 @@ use FreeDSx\Ldap\Protocol\ServerProtocolHandler\ServerStartTlsHandler; use FreeDSx\Ldap\Server\RequestHandler\RequestHandlerInterface; use FreeDSx\Ldap\Server\Token\TokenInterface; +use FreeDSx\Ldap\ServerOptions; use PhpSpec\ObjectBehavior; class ServerStartTlsHandlerSpec extends ObjectBehavior @@ -46,7 +47,14 @@ public function it_should_handle_a_start_tls_request(ServerQueue $queue, TokenIn ))->shouldBeCalled(); $startTls = new LdapMessageRequest(1, new ExtendedRequest(ExtendedRequest::OID_START_TLS)); - $this->handleRequest($startTls, $token, $dispatcher, $queue, ['ssl_cert' => 'foo']); + $this->handleRequest( + $startTls, + $token, + $dispatcher, + $queue, + (new ServerOptions()) + ->setSslCert('foo') + ); } public function it_should_send_back_an_error_if_the_queue_is_already_encrypted(ServerQueue $queue, TokenInterface $token, RequestHandlerInterface $dispatcher): void @@ -63,7 +71,14 @@ public function it_should_send_back_an_error_if_the_queue_is_already_encrypted(S ))->shouldBeCalled(); $startTls = new LdapMessageRequest(1, new ExtendedRequest(ExtendedRequest::OID_START_TLS)); - $this->handleRequest($startTls, $token, $dispatcher, $queue, ['ssl_cert' => 'foo']); + $this->handleRequest( + $startTls, + $token, + $dispatcher, + $queue, + (new ServerOptions()) + ->setSslCert('foo') + ); } public function it_should_send_back_an_error_if_encryption_is_not_supported(ServerQueue $queue, TokenInterface $token, RequestHandlerInterface $dispatcher): void @@ -80,6 +95,12 @@ public function it_should_send_back_an_error_if_encryption_is_not_supported(Serv ))->shouldBeCalled(); $startTls = new LdapMessageRequest(1, new ExtendedRequest(ExtendedRequest::OID_START_TLS)); - $this->handleRequest($startTls, $token, $dispatcher, $queue, []); + $this->handleRequest( + $startTls, + $token, + $dispatcher, + $queue, + new ServerOptions() + ); } } diff --git a/tests/spec/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerUnbindHandlerSpec.php b/tests/spec/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerUnbindHandlerSpec.php index 1cd3e823..d7e2cc0a 100644 --- a/tests/spec/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerUnbindHandlerSpec.php +++ b/tests/spec/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerUnbindHandlerSpec.php @@ -19,6 +19,7 @@ use FreeDSx\Ldap\Protocol\ServerProtocolHandler\ServerUnbindHandler; use FreeDSx\Ldap\Server\RequestHandler\RequestHandlerInterface; use FreeDSx\Ldap\Server\Token\TokenInterface; +use FreeDSx\Ldap\ServerOptions; use PhpSpec\ObjectBehavior; use Prophecy\Argument; @@ -35,6 +36,6 @@ public function it_should_handle_an_unbind_request(ServerQueue $queue, TokenInte $queue->sendMessage(Argument::any())->shouldNotBeCalled(); $unbind = new LdapMessageRequest(1, new UnbindRequest()); - $this->handleRequest($unbind, $token, $dispatcher, $queue, []); + $this->handleRequest($unbind, $token, $dispatcher, $queue, new ServerOptions()); } } diff --git a/tests/spec/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerWhoAmIHandlerSpec.php b/tests/spec/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerWhoAmIHandlerSpec.php index 3336337b..f520994d 100644 --- a/tests/spec/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerWhoAmIHandlerSpec.php +++ b/tests/spec/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerWhoAmIHandlerSpec.php @@ -23,6 +23,7 @@ use FreeDSx\Ldap\Server\RequestHandler\RequestHandlerInterface; use FreeDSx\Ldap\Server\Token\AnonToken; use FreeDSx\Ldap\Server\Token\BindToken; +use FreeDSx\Ldap\ServerOptions; use PhpSpec\ObjectBehavior; class ServerWhoAmIHandlerSpec extends ObjectBehavior @@ -46,7 +47,7 @@ public function it_should_handle_a_who_am_i_when_there_is_a_token_with_a_DN_name new BindToken('cn=foo,dc=foo,dc=bar', '12345'), $handler, $queue, - [] + new ServerOptions() ); } @@ -64,7 +65,7 @@ public function it_should_handle_a_who_am_i_when_there_is_a_token_with_a_non_DN_ new BindToken('foo@bar.local', '12345'), $handler, $queue, - [] + new ServerOptions() ); } @@ -83,7 +84,7 @@ public function it_should_handle_a_who_am_i_when_there_is_no_token_yet(ServerQue new AnonToken(), $handler, $queue, - [] + new ServerOptions() ); } } diff --git a/tests/spec/FreeDSx/Ldap/Protocol/ServerProtocolHandlerSpec.php b/tests/spec/FreeDSx/Ldap/Protocol/ServerProtocolHandlerSpec.php index c6756109..9be18625 100644 --- a/tests/spec/FreeDSx/Ldap/Protocol/ServerProtocolHandlerSpec.php +++ b/tests/spec/FreeDSx/Ldap/Protocol/ServerProtocolHandlerSpec.php @@ -36,6 +36,7 @@ use FreeDSx\Ldap\Server\HandlerFactoryInterface; use FreeDSx\Ldap\Server\RequestHandler\RequestHandlerInterface; use FreeDSx\Ldap\Server\Token\BindToken; +use FreeDSx\Ldap\ServerOptions; use FreeDSx\Socket\Exception\ConnectionException; use PhpSpec\ObjectBehavior; use Prophecy\Argument; @@ -62,7 +63,7 @@ public function let( $this->beConstructedWith( $queue, $handlerFactory, - [], + new ServerOptions(), $protocolHandlerFactory, $bindHandlerFactory ); @@ -101,7 +102,7 @@ public function it_should_not_allow_a_previous_message_ID_from_a_new_request(Ser null ); - $bindHandler->handleBind(Argument::any(), Argument::any(), Argument::any(), Argument::any())->willReturn( + $bindHandler->handleBind(Argument::any(), Argument::any(), Argument::any())->willReturn( new BindToken('foo', 'bar') ); $protocolHandler->handleRequest(Argument::any(), Argument::any(), Argument::any(), Argument::any(), Argument::any()) @@ -196,7 +197,7 @@ public function it_should_send_a_bind_request_to_the_bind_request_handler(Server null ); - $bindHandler->handleBind(Argument::any(), Argument::any(), Argument::any(), Argument::any()) + $bindHandler->handleBind(Argument::any(), Argument::any(), Argument::any()) ->shouldBeCalledOnce() ->willReturn(new BindToken('foo@bar', 'bar')); $protocolHandler->handleRequest(Argument::any(), Argument::any(), Argument::any(), Argument::any(), Argument::any()) @@ -214,7 +215,7 @@ public function it_should_handle_operation_errors_thrown_from_the_request_handle null ); - $bindHandler->handleBind(Argument::any(), Argument::any(), Argument::any(), Argument::any()) + $bindHandler->handleBind(Argument::any(), Argument::any(), Argument::any()) ->willReturn(new BindToken('foo@bar', 'bar')); $protocolHandler->handleRequest(Argument::any(), Argument::any(), Argument::any(), Argument::any(), Argument::any()) diff --git a/tests/spec/FreeDSx/Ldap/Server/RequestHandler/HandlerFactorySpec.php b/tests/spec/FreeDSx/Ldap/Server/RequestHandler/HandlerFactorySpec.php index fbc074c2..02f9981a 100644 --- a/tests/spec/FreeDSx/Ldap/Server/RequestHandler/HandlerFactorySpec.php +++ b/tests/spec/FreeDSx/Ldap/Server/RequestHandler/HandlerFactorySpec.php @@ -13,17 +13,12 @@ namespace spec\FreeDSx\Ldap\Server\RequestHandler; -use FreeDSx\Ldap\Entry\Entries; -use FreeDSx\Ldap\Entry\Entry; -use FreeDSx\Ldap\Exception\RuntimeException; -use FreeDSx\Ldap\Operation\Request\SearchRequest; -use FreeDSx\Ldap\Server\Paging\PagingRequest; -use FreeDSx\Ldap\Server\Paging\PagingResponse; -use FreeDSx\Ldap\Server\RequestContext; +use FreeDSx\Ldap\LdapClient; use FreeDSx\Ldap\Server\RequestHandler\GenericRequestHandler; use FreeDSx\Ldap\Server\RequestHandler\PagingHandlerInterface; -use FreeDSx\Ldap\Server\RequestHandler\ProxyRequestHandler; -use FreeDSx\Ldap\Server\RequestHandler\RootDseHandlerInterface; +use FreeDSx\Ldap\Server\RequestHandler\ProxyHandler; +use FreeDSx\Ldap\Server\RequestHandler\ProxyPagingHandler; +use FreeDSx\Ldap\ServerOptions; use PhpSpec\ObjectBehavior; class HandlerFactorySpec extends ObjectBehavior @@ -31,116 +26,47 @@ class HandlerFactorySpec extends ObjectBehavior public function it_should_allow_a_request_handler_as_an_object(): void { $handler = new GenericRequestHandler(); - $this->beConstructedWith([ - 'request_handler' => $handler, - ]); + $this->beConstructedWith( + (new ServerOptions())->setRequestHandler($handler) + ); $this->makeRequestHandler()->shouldBeEqualTo($handler); } - public function it_should_only_allow_a_request_handler_implementing_request_handler_interface(): void + public function it_should_allow_a_rootdse_handler_as_an_object(): void { - $this->beConstructedWith([ - 'request_handler' => new Entry('foo'), - ]); - - $this->shouldThrow(RuntimeException::class)->during('makeRequestHandler'); - } - - public function it_should_allow_a_request_handler_as_a_string_implementing_request_handler_interface(): void - { - $this->beConstructedWith([ - 'request_handler' => ProxyRequestHandler::class, - ]); - - $this->shouldNotThrow(RuntimeException::class)->during('makeRequestHandler'); - } - - public function it_should_allow_a_rootdse_handler_as_an_object(RootDseHandlerInterface $rootDseHandler): void - { - $this->beConstructedWith([ - 'rootdse_handler' => $rootDseHandler, - ]); + $rootDseHandler = new ProxyHandler(new LdapClient()); + $this->beConstructedWith( + (new ServerOptions())->setRootDseHandler($rootDseHandler) + ); $this->makeRootDseHandler()->shouldBeEqualTo($rootDseHandler); } - public function it_should_only_allow_a_rootdse_handler_implementing_rootdse_handler_interface(): void - { - $this->beConstructedWith([ - 'rootdse_handler' => new Entry('foo'), - ]); - - $this->shouldThrow(RuntimeException::class)->during('makeRootDseHandler'); - } - - public function it_should_allow_a_rootdse_handler_as_a_string_implementing_rootdse_handler_interface(): void - { - $handler = new class() implements RootDseHandlerInterface { - public function rootDse(RequestContext $context, SearchRequest $request, Entry $rootDse): Entry - { - return new Entry(''); - } - }; - - $this->beConstructedWith([ - 'rootdse_handler' => get_class($handler), - ]); - - $this->shouldNotThrow(RuntimeException::class)->during('makeRootDseHandler'); - } - public function it_should_allow_a_null_rootdse_handler(): void { - $this->beConstructedWith([ - 'rootdse_handler' => null, - ]); + $this->beConstructedWith( + (new ServerOptions())->setRootDseHandler(null) + ); $this->makeRootDseHandler()->shouldBeNull(); } - public function it_should_allow_a_paging_handler_implementing_paging_handler_interface(): void - { - $this->beConstructedWith([ - 'paging_handler' => new Entry('foo'), - ]); - - $this->shouldThrow(RuntimeException::class)->during('makePagingHandler'); - } - - public function it_should_allow_a_paging_handler_as_a_string_implementing_paging_handler_interface(): void - { - $handler = new class() implements PagingHandlerInterface { - public function page(PagingRequest $pagingRequest, RequestContext $context): PagingResponse - { - return PagingResponse::make(new Entries()); - } - - public function remove(PagingRequest $pagingRequest, RequestContext $context): void - { - } - }; - - $this->beConstructedWith([ - 'paging_handler' => get_class($handler), - ]); - - $this->shouldNotThrow(RuntimeException::class)->during('makePagingHandler'); - } public function it_should_allow_a_paging_handler_as_an_object(PagingHandlerInterface $pagingHandler): void { - $this->beConstructedWith([ - 'paging_handler' => $pagingHandler, - ]); + $pagingHandler = new ProxyPagingHandler(new LdapClient()); + $this->beConstructedWith( + (new ServerOptions())->setPagingHandler($pagingHandler) + ); $this->makePagingHandler()->shouldBeEqualTo($pagingHandler); } public function it_should_allow_a_null_paging_handler(): void { - $this->beConstructedWith([ - 'paging_handler' => null, - ]); + $this->beConstructedWith( + (new ServerOptions())->setPagingHandler(null) + ); $this->makePagingHandler()->shouldBeNull(); }