From f3c2afac9250eeb277ba35753d56a1c8c89e5195 Mon Sep 17 00:00:00 2001 From: Chad Sikorra Date: Wed, 9 Aug 2023 09:13:07 -0500 Subject: [PATCH 1/2] Remove the ServerQueue from the BindHandlerInterface. Remove an unneeded class BaseServerHandler. --- .../Ldap/Protocol/ServerProtocolHandler.php | 7 ++-- .../BaseServerHandler.php | 28 --------------- .../BindHandlerInterface.php | 4 +-- .../BindVersionValidatorTrait.php | 35 ++++++++++++++++++ .../ServerAnonBindHandler.php | 19 +++++----- .../ServerBindHandler.php | 36 ++++++++----------- .../ServerDispatchHandler.php | 5 +-- .../ServerAnonBindHandlerSpec.php | 2 +- .../ServerBindHandlerSpec.php | 4 +-- .../Protocol/ServerProtocolHandlerSpec.php | 6 ++-- 10 files changed, 73 insertions(+), 73 deletions(-) delete mode 100644 src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/BaseServerHandler.php create mode 100644 src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/BindVersionValidatorTrait.php diff --git a/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler.php b/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler.php index 1a42c5b1..9193b21a 100644 --- a/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler.php +++ b/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler.php @@ -92,14 +92,14 @@ public function handle(array $defaultContext = []): void ) ); } catch (EncoderException | ProtocolException) { - # Per RFC 4511, 4.1.1 if the PDU cannot be parsed or is otherwise malformed a disconnect should be sent with a - # result code of protocol error. + # Per RFC 4511, 4.1.1 if the PDU cannot be parsed or is otherwise malformed a disconnect should be sent with + # a result code of protocol error. $this->sendNoticeOfDisconnect('The message encoding is malformed.'); $this->logError( 'The client sent a malformed request. Terminating their connection.', $defaultContext ); - } catch (Exception | Throwable $e) { + } catch (Throwable $e) { $this->logError( 'An unexpected exception was caught while handling the client. Terminating their connection.', array_merge( @@ -226,7 +226,6 @@ private function handleAuthRequest(LdapMessageRequest $message): TokenInterface return $this->bindHandlerFactory->get($message->getRequest())->handleBind( $message, $this->handlerFactory->makeRequestHandler(), - $this->queue ); } diff --git a/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/BaseServerHandler.php b/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/BaseServerHandler.php deleted file mode 100644 index 5c1d9535..00000000 --- a/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/BaseServerHandler.php +++ /dev/null @@ -1,28 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace FreeDSx\Ldap\Protocol\ServerProtocolHandler; - -use FreeDSx\Ldap\Protocol\Factory\ResponseFactory; - -/** - * Base handler (easy access to the response factory). - * - * @author Chad Sikorra - */ -abstract class BaseServerHandler -{ - public function __construct(protected readonly ResponseFactory $responseFactory = new ResponseFactory()) - { - } -} diff --git a/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/BindHandlerInterface.php b/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/BindHandlerInterface.php index ecef8ed0..162a2442 100644 --- a/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/BindHandlerInterface.php +++ b/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/BindHandlerInterface.php @@ -15,7 +15,6 @@ use FreeDSx\Ldap\Exception\OperationException; use FreeDSx\Ldap\Protocol\LdapMessageRequest; -use FreeDSx\Ldap\Protocol\Queue\ServerQueue; use FreeDSx\Ldap\Server\RequestHandler\RequestHandlerInterface; use FreeDSx\Ldap\Server\Token\TokenInterface; @@ -33,7 +32,6 @@ interface BindHandlerInterface */ public function handleBind( LdapMessageRequest $message, - RequestHandlerInterface $dispatcher, - ServerQueue $queue + RequestHandlerInterface $dispatcher ): TokenInterface; } diff --git a/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/BindVersionValidatorTrait.php b/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/BindVersionValidatorTrait.php new file mode 100644 index 00000000..71afc030 --- /dev/null +++ b/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/BindVersionValidatorTrait.php @@ -0,0 +1,35 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace FreeDSx\Ldap\Protocol\ServerProtocolHandler; + +use FreeDSx\Ldap\Exception\OperationException; +use FreeDSx\Ldap\Operation\Request\BindRequest; +use FreeDSx\Ldap\Operation\ResultCode; + +trait BindVersionValidatorTrait +{ + /** + * @throws OperationException + */ + private static function validateVersion(BindRequest $request): void + { + # Per RFC 4.2, a result code of protocol error must be sent back for unsupported versions. + if ($request->getVersion() !== 3) { + throw new OperationException( + 'Only LDAP version 3 is supported.', + ResultCode::PROTOCOL_ERROR + ); + } + } +} diff --git a/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerAnonBindHandler.php b/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerAnonBindHandler.php index 57f15dd4..e6969eed 100644 --- a/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerAnonBindHandler.php +++ b/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerAnonBindHandler.php @@ -17,6 +17,7 @@ use FreeDSx\Ldap\Exception\OperationException; use FreeDSx\Ldap\Exception\RuntimeException; use FreeDSx\Ldap\Operation\Request\AnonBindRequest; +use FreeDSx\Ldap\Protocol\Factory\ResponseFactory; use FreeDSx\Ldap\Protocol\LdapMessageRequest; use FreeDSx\Ldap\Protocol\Queue\ServerQueue; use FreeDSx\Ldap\Server\RequestHandler\RequestHandlerInterface; @@ -28,11 +29,14 @@ * * @author Chad Sikorra */ -class ServerAnonBindHandler extends ServerBindHandler +class ServerAnonBindHandler implements BindHandlerInterface { - public function __construct(private readonly ServerQueue $queue) - { - parent::__construct($this->queue); + use BindVersionValidatorTrait; + + public function __construct( + private readonly ServerQueue $queue, + private readonly ResponseFactory $responseFactory = new ResponseFactory() + ) { } /** @@ -43,8 +47,7 @@ public function __construct(private readonly ServerQueue $queue) */ public function handleBind( LdapMessageRequest $message, - RequestHandlerInterface $dispatcher, - ServerQueue $queue + RequestHandlerInterface $dispatcher ): TokenInterface { $request = $message->getRequest(); if (!$request instanceof AnonBindRequest) { @@ -54,12 +57,12 @@ public function handleBind( )); } - $this->validateVersion($request); + self::validateVersion($request); $this->queue->sendMessage($this->responseFactory->getStandardResponse($message)); return new AnonToken( $request->getUsername(), - $request->getVersion() + $request->getVersion(), ); } } diff --git a/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerBindHandler.php b/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerBindHandler.php index 8fd2ad88..fcbdca66 100644 --- a/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerBindHandler.php +++ b/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerBindHandler.php @@ -18,6 +18,7 @@ use FreeDSx\Ldap\Operation\Request\BindRequest; use FreeDSx\Ldap\Operation\Request\SimpleBindRequest; use FreeDSx\Ldap\Operation\ResultCode; +use FreeDSx\Ldap\Protocol\Factory\ResponseFactory; use FreeDSx\Ldap\Protocol\LdapMessageRequest; use FreeDSx\Ldap\Protocol\Queue\ServerQueue; use FreeDSx\Ldap\Server\RequestHandler\RequestHandlerInterface; @@ -29,11 +30,14 @@ * * @author Chad Sikorra */ -class ServerBindHandler extends BaseServerHandler implements BindHandlerInterface +class ServerBindHandler implements BindHandlerInterface { - public function __construct(private readonly ServerQueue $queue) - { - parent::__construct(); + use BindVersionValidatorTrait; + + public function __construct( + private readonly ServerQueue $queue, + private readonly ResponseFactory $responseFactory = new ResponseFactory() + ) { } /** @@ -43,8 +47,7 @@ public function __construct(private readonly ServerQueue $queue) */ public function handleBind( LdapMessageRequest $message, - RequestHandlerInterface $dispatcher, - ServerQueue $queue + RequestHandlerInterface $dispatcher ): TokenInterface { /** @var BindRequest $request */ $request = $message->getRequest(); @@ -55,27 +58,13 @@ public function handleBind( )); } - $this->validateVersion($request); + self::validateVersion($request); $token = $this->simpleBind($dispatcher, $request); $this->queue->sendMessage($this->responseFactory->getStandardResponse($message)); return $token; } - /** - * @throws OperationException - */ - protected function validateVersion(BindRequest $request): void - { - # Per RFC 4.2, a result code of protocol error must be sent back for unsupported versions. - if ($request->getVersion() !== 3) { - throw new OperationException( - 'Only LDAP version 3 is supported.', - ResultCode::PROTOCOL_ERROR - ); - } - } - /** * @throws OperationException */ @@ -90,6 +79,9 @@ private function simpleBind( ); } - return new BindToken($request->getUsername(), $request->getPassword()); + return new BindToken( + $request->getUsername(), + $request->getPassword() + ); } } diff --git a/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerDispatchHandler.php b/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerDispatchHandler.php index 74774aa5..f394eaa8 100644 --- a/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerDispatchHandler.php +++ b/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerDispatchHandler.php @@ -17,6 +17,7 @@ use FreeDSx\Ldap\Exception\OperationException; use FreeDSx\Ldap\Operation\Request; use FreeDSx\Ldap\Operation\ResultCode; +use FreeDSx\Ldap\Protocol\Factory\ResponseFactory; use FreeDSx\Ldap\Protocol\LdapMessageRequest; use FreeDSx\Ldap\Protocol\Queue\ServerQueue; use FreeDSx\Ldap\Server\RequestContext; @@ -28,13 +29,13 @@ * * @author Chad Sikorra */ -class ServerDispatchHandler extends BaseServerHandler implements ServerProtocolHandlerInterface +class ServerDispatchHandler implements ServerProtocolHandlerInterface { public function __construct( private readonly ServerQueue $queue, private readonly RequestHandlerInterface $dispatcher, + private readonly ResponseFactory $responseFactory = new ResponseFactory(), ) { - parent::__construct(); } /** diff --git a/tests/spec/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerAnonBindHandlerSpec.php b/tests/spec/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerAnonBindHandlerSpec.php index c66469ab..718f5c72 100644 --- a/tests/spec/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerAnonBindHandlerSpec.php +++ b/tests/spec/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerAnonBindHandlerSpec.php @@ -64,7 +64,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)->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 0e346a3a..862f4a2d 100644 --- a/tests/spec/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerBindHandlerSpec.php +++ b/tests/spec/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerBindHandlerSpec.php @@ -50,7 +50,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)->shouldBeLike( new BindToken('foo@bar', 'bar') ); } @@ -80,7 +80,7 @@ public function it_should_validate_the_version(ServerQueue $queue, RequestHandle $this->shouldThrow(OperationException::class) ->during( 'handleBind', - [$bind, $dispatcher, $queue] + [$bind, $dispatcher] ); } } diff --git a/tests/spec/FreeDSx/Ldap/Protocol/ServerProtocolHandlerSpec.php b/tests/spec/FreeDSx/Ldap/Protocol/ServerProtocolHandlerSpec.php index 541d2db3..0ed541b0 100644 --- a/tests/spec/FreeDSx/Ldap/Protocol/ServerProtocolHandlerSpec.php +++ b/tests/spec/FreeDSx/Ldap/Protocol/ServerProtocolHandlerSpec.php @@ -106,7 +106,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())->willReturn( + $bindHandler->handleBind(Argument::any(), Argument::any())->willReturn( new BindToken('foo', 'bar') ); $protocolHandler->handleRequest(Argument::any(), Argument::any()) @@ -201,7 +201,7 @@ public function it_should_send_a_bind_request_to_the_bind_request_handler(Server null ); - $bindHandler->handleBind(Argument::any(), Argument::any(), Argument::any()) + $bindHandler->handleBind(Argument::any(), Argument::any()) ->shouldBeCalledOnce() ->willReturn(new BindToken('foo@bar', 'bar')); $protocolHandler->handleRequest(Argument::any(), Argument::any()) @@ -222,7 +222,7 @@ public function it_should_handle_operation_errors_thrown_from_the_request_handle null ); - $bindHandler->handleBind(Argument::any(), Argument::any(), Argument::any()) + $bindHandler->handleBind(Argument::any(), Argument::any()) ->willReturn(new BindToken('foo@bar', 'bar')); $protocolHandler->handleRequest(Argument::any(), Argument::any()) From 228377c5458d83ec06f3049a4b000cb804f44df2 Mon Sep 17 00:00:00 2001 From: Chad Sikorra Date: Wed, 9 Aug 2023 09:39:09 -0500 Subject: [PATCH 2/2] Remove the RequestHandlerInterface from the BindHandlerInterface. --- .../Factory/ServerBindHandlerFactory.php | 13 ++++++-- .../Ldap/Protocol/ServerProtocolHandler.php | 10 +++--- .../BindHandlerInterface.php | 6 +--- .../ServerAnonBindHandler.php | 7 ++--- .../ServerBindHandler.php | 19 +++++------- .../Ldap/Server/ServerProtocolFactory.php | 8 +++-- .../Factory/ServerBindHandlerFactorySpec.php | 23 +++++++++++--- .../ServerAnonBindHandlerSpec.php | 2 +- .../ServerBindHandlerSpec.php | 31 ++++++++++++------- .../Protocol/ServerProtocolHandlerSpec.php | 12 +++---- .../Ldap/Server/SocketServerFactorySpec.php | 4 +-- 11 files changed, 75 insertions(+), 60 deletions(-) diff --git a/src/FreeDSx/Ldap/Protocol/Factory/ServerBindHandlerFactory.php b/src/FreeDSx/Ldap/Protocol/Factory/ServerBindHandlerFactory.php index 8a251f3d..8379721d 100644 --- a/src/FreeDSx/Ldap/Protocol/Factory/ServerBindHandlerFactory.php +++ b/src/FreeDSx/Ldap/Protocol/Factory/ServerBindHandlerFactory.php @@ -22,6 +22,8 @@ use FreeDSx\Ldap\Protocol\ServerProtocolHandler\BindHandlerInterface; use FreeDSx\Ldap\Protocol\ServerProtocolHandler\ServerAnonBindHandler; use FreeDSx\Ldap\Protocol\ServerProtocolHandler\ServerBindHandler; +use FreeDSx\Ldap\Server\HandlerFactoryInterface; +use FreeDSx\Ldap\Server\RequestHandler\RequestHandlerInterface; /** * Determines the correct bind handler for the request. @@ -30,8 +32,10 @@ */ class ServerBindHandlerFactory { - public function __construct(private readonly ServerQueue $queue) - { + public function __construct( + private readonly ServerQueue $queue, + private readonly HandlerFactoryInterface $handlerFactory, + ) { } /** @@ -42,7 +46,10 @@ public function __construct(private readonly ServerQueue $queue) public function get(RequestInterface $request): BindHandlerInterface { if ($request instanceof SimpleBindRequest) { - return new ServerBindHandler($this->queue); + return new ServerBindHandler( + queue: $this->queue, + dispatcher: $this->handlerFactory->makeRequestHandler(), + ); } elseif ($request instanceof AnonBindRequest) { return new ServerAnonBindHandler($this->queue); } else { diff --git a/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler.php b/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler.php index 9193b21a..64e413d5 100644 --- a/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler.php +++ b/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler.php @@ -49,11 +49,10 @@ class ServerProtocolHandler public function __construct( private readonly ServerQueue $queue, - private readonly HandlerFactoryInterface $handlerFactory, - private readonly ?LoggerInterface $logger, private readonly ServerProtocolHandlerFactory $protocolHandlerFactory, private readonly ServerAuthorization $authorizer, private readonly ServerBindHandlerFactory $bindHandlerFactory, + private readonly ?LoggerInterface $logger, private readonly ResponseFactory $responseFactory = new ResponseFactory() ) { } @@ -223,10 +222,9 @@ private function handleAuthRequest(LdapMessageRequest $message): TokenInterface ); } - return $this->bindHandlerFactory->get($message->getRequest())->handleBind( - $message, - $this->handlerFactory->makeRequestHandler(), - ); + return $this->bindHandlerFactory + ->get($message->getRequest()) + ->handleBind($message); } /** diff --git a/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/BindHandlerInterface.php b/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/BindHandlerInterface.php index 162a2442..23e98e85 100644 --- a/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/BindHandlerInterface.php +++ b/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/BindHandlerInterface.php @@ -15,7 +15,6 @@ use FreeDSx\Ldap\Exception\OperationException; use FreeDSx\Ldap\Protocol\LdapMessageRequest; -use FreeDSx\Ldap\Server\RequestHandler\RequestHandlerInterface; use FreeDSx\Ldap\Server\Token\TokenInterface; /** @@ -30,8 +29,5 @@ interface BindHandlerInterface * * @throws OperationException */ - public function handleBind( - LdapMessageRequest $message, - RequestHandlerInterface $dispatcher - ): TokenInterface; + public function handleBind(LdapMessageRequest $message): TokenInterface; } diff --git a/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerAnonBindHandler.php b/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerAnonBindHandler.php index e6969eed..77770887 100644 --- a/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerAnonBindHandler.php +++ b/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerAnonBindHandler.php @@ -20,7 +20,6 @@ use FreeDSx\Ldap\Protocol\Factory\ResponseFactory; use FreeDSx\Ldap\Protocol\LdapMessageRequest; use FreeDSx\Ldap\Protocol\Queue\ServerQueue; -use FreeDSx\Ldap\Server\RequestHandler\RequestHandlerInterface; use FreeDSx\Ldap\Server\Token\AnonToken; use FreeDSx\Ldap\Server\Token\TokenInterface; @@ -45,10 +44,8 @@ public function __construct( * @throws OperationException * @throws RuntimeException */ - public function handleBind( - LdapMessageRequest $message, - RequestHandlerInterface $dispatcher - ): TokenInterface { + public function handleBind(LdapMessageRequest $message): TokenInterface + { $request = $message->getRequest(); if (!$request instanceof AnonBindRequest) { throw new RuntimeException(sprintf( diff --git a/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerBindHandler.php b/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerBindHandler.php index fcbdca66..05900b23 100644 --- a/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerBindHandler.php +++ b/src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerBindHandler.php @@ -36,7 +36,8 @@ class ServerBindHandler implements BindHandlerInterface public function __construct( private readonly ServerQueue $queue, - private readonly ResponseFactory $responseFactory = new ResponseFactory() + private readonly RequestHandlerInterface $dispatcher, + private readonly ResponseFactory $responseFactory = new ResponseFactory(), ) { } @@ -45,10 +46,8 @@ public function __construct( * @throws RuntimeException * @throws OperationException */ - public function handleBind( - LdapMessageRequest $message, - RequestHandlerInterface $dispatcher - ): TokenInterface { + public function handleBind(LdapMessageRequest $message): TokenInterface + { /** @var BindRequest $request */ $request = $message->getRequest(); if (!$request instanceof SimpleBindRequest) { @@ -59,7 +58,7 @@ public function handleBind( } self::validateVersion($request); - $token = $this->simpleBind($dispatcher, $request); + $token = $this->simpleBind($request); $this->queue->sendMessage($this->responseFactory->getStandardResponse($message)); return $token; @@ -68,11 +67,9 @@ public function handleBind( /** * @throws OperationException */ - private function simpleBind( - RequestHandlerInterface $dispatcher, - SimpleBindRequest $request - ): TokenInterface { - if (!$dispatcher->bind($request->getUsername(), $request->getPassword())) { + private function simpleBind(SimpleBindRequest $request): TokenInterface + { + if (!$this->dispatcher->bind($request->getUsername(), $request->getPassword())) { throw new OperationException( 'Invalid credentials.', ResultCode::INVALID_CREDENTIALS diff --git a/src/FreeDSx/Ldap/Server/ServerProtocolFactory.php b/src/FreeDSx/Ldap/Server/ServerProtocolFactory.php index 8245104c..16260dbe 100644 --- a/src/FreeDSx/Ldap/Server/ServerProtocolFactory.php +++ b/src/FreeDSx/Ldap/Server/ServerProtocolFactory.php @@ -36,8 +36,6 @@ public function make(Socket $socket): ServerProtocolHandler return new ServerProtocolHandler( queue: $serverQueue, - handlerFactory: $this->handlerFactory, - logger: $this->options->getLogger(), protocolHandlerFactory: new ServerProtocolHandlerFactory( handlerFactory: $this->handlerFactory, options: $this->options, @@ -45,7 +43,11 @@ public function make(Socket $socket): ServerProtocolHandler queue: $serverQueue, ), authorizer: $this->serverAuthorization, - bindHandlerFactory: new ServerBindHandlerFactory($serverQueue), + bindHandlerFactory: new ServerBindHandlerFactory( + queue: $serverQueue, + handlerFactory: $this->handlerFactory, + ), + logger: $this->options->getLogger(), ); } } diff --git a/tests/spec/FreeDSx/Ldap/Protocol/Factory/ServerBindHandlerFactorySpec.php b/tests/spec/FreeDSx/Ldap/Protocol/Factory/ServerBindHandlerFactorySpec.php index 6701ace4..5d6e2b64 100644 --- a/tests/spec/FreeDSx/Ldap/Protocol/Factory/ServerBindHandlerFactorySpec.php +++ b/tests/spec/FreeDSx/Ldap/Protocol/Factory/ServerBindHandlerFactorySpec.php @@ -21,13 +21,21 @@ use FreeDSx\Ldap\Protocol\Queue\ServerQueue; use FreeDSx\Ldap\Protocol\ServerProtocolHandler\ServerAnonBindHandler; use FreeDSx\Ldap\Protocol\ServerProtocolHandler\ServerBindHandler; +use FreeDSx\Ldap\Server\HandlerFactoryInterface; +use FreeDSx\Ldap\Server\RequestHandler\GenericRequestHandler; +use FreeDSx\Ldap\Server\RequestHandler\RequestHandlerInterface; use PhpSpec\ObjectBehavior; class ServerBindHandlerFactorySpec extends ObjectBehavior { - public function let(ServerQueue $queue): void - { - $this->beConstructedWith($queue); + public function let( + ServerQueue $queue, + HandlerFactoryInterface $handlerFactory, + ): void { + $this->beConstructedWith( + $queue, + $handlerFactory, + ); } public function it_is_initializable(): void @@ -41,14 +49,19 @@ public function it_should_get_an_anon_bind_handler(): void ->shouldBeAnInstanceOf(ServerAnonBindHandler::class); } - public function it_should_get_a_simple_bind_handler(): void + public function it_should_get_a_simple_bind_handler(HandlerFactoryInterface $handlerFactory): void { + $handlerFactory + ->makeRequestHandler() + ->willReturn(new GenericRequestHandler()); + $this->get(new SimpleBindRequest('foo', 'bar')) ->shouldBeAnInstanceOf(ServerBindHandler::class); } public function it_should_throw_an_exception_on_an_unknown_bind_type(BindRequest $request): void { - $this->shouldThrow(OperationException::class)->during('get', [$request]); + $this->shouldThrow(OperationException::class) + ->during('get', [$request]); } } diff --git a/tests/spec/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerAnonBindHandlerSpec.php b/tests/spec/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerAnonBindHandlerSpec.php index 718f5c72..4da93fb7 100644 --- a/tests/spec/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerAnonBindHandlerSpec.php +++ b/tests/spec/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerAnonBindHandlerSpec.php @@ -64,7 +64,7 @@ public function it_should_return_an_anon_token_with_the_supplied_username(Server new LdapResult(0) )))->shouldBeCalled()->willReturn($queue); - $this->handleBind($bind, $dispatcher)->shouldBeLike( + $this->handleBind($bind)->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 862f4a2d..d063bf5f 100644 --- a/tests/spec/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerBindHandlerSpec.php +++ b/tests/spec/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerBindHandlerSpec.php @@ -29,9 +29,14 @@ class ServerBindHandlerSpec extends ObjectBehavior { - public function let(ServerQueue $queue): void - { - $this->beConstructedWith($queue); + public function let( + ServerQueue $queue, + RequestHandlerInterface $dispatcher, + ): void { + $this->beConstructedWith( + $queue, + $dispatcher, + ); } public function it_is_initializable(): void @@ -39,8 +44,10 @@ public function it_is_initializable(): void $this->shouldHaveType(ServerBindHandler::class); } - public function it_should_return_a_token_on_success(ServerQueue $queue, RequestHandlerInterface $dispatcher): void - { + public function it_should_return_a_token_on_success( + ServerQueue $queue, + RequestHandlerInterface $dispatcher, + ): void { $bind = new LdapMessageRequest(1, new SimpleBindRequest('foo@bar', 'bar')); $dispatcher->bind('foo@bar', 'bar') @@ -50,13 +57,15 @@ public function it_should_return_a_token_on_success(ServerQueue $queue, RequestH new LdapResult(0) )))->shouldBeCalled()->willReturn($queue); - $this->handleBind($bind, $dispatcher)->shouldBeLike( + $this->handleBind($bind)->shouldBeLike( new BindToken('foo@bar', 'bar') ); } - public function it_should_throw_an_operations_exception_with_invalid_credentials_if_they_are_wrong(ServerQueue $queue, RequestHandlerInterface $dispatcher): void - { + public function it_should_throw_an_operations_exception_with_invalid_credentials_if_they_are_wrong( + ServerQueue $queue, + RequestHandlerInterface $dispatcher, + ): void { $bind = new LdapMessageRequest(1, new SimpleBindRequest('foo@bar', 'bar')); $dispatcher->bind('foo@bar', 'bar') @@ -67,11 +76,11 @@ 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] ); } - public function it_should_validate_the_version(ServerQueue $queue, RequestHandlerInterface $dispatcher): void + public function it_should_validate_the_version(ServerQueue $queue, ): void { $bind = new LdapMessageRequest(1, new SimpleBindRequest('foo@bar', 'bar', 5)); @@ -80,7 +89,7 @@ public function it_should_validate_the_version(ServerQueue $queue, RequestHandle $this->shouldThrow(OperationException::class) ->during( 'handleBind', - [$bind, $dispatcher] + [$bind] ); } } diff --git a/tests/spec/FreeDSx/Ldap/Protocol/ServerProtocolHandlerSpec.php b/tests/spec/FreeDSx/Ldap/Protocol/ServerProtocolHandlerSpec.php index 0ed541b0..06c593c2 100644 --- a/tests/spec/FreeDSx/Ldap/Protocol/ServerProtocolHandlerSpec.php +++ b/tests/spec/FreeDSx/Ldap/Protocol/ServerProtocolHandlerSpec.php @@ -34,7 +34,6 @@ use FreeDSx\Ldap\Protocol\Queue\ServerQueue; use FreeDSx\Ldap\Protocol\ServerAuthorization; use FreeDSx\Ldap\Protocol\ServerProtocolHandler; -use FreeDSx\Ldap\Server\HandlerFactoryInterface; use FreeDSx\Ldap\Server\RequestHandler\RequestHandlerInterface; use FreeDSx\Ldap\Server\Token\BindToken; use FreeDSx\Ldap\ServerOptions; @@ -49,7 +48,6 @@ public function let( ServerQueue $queue, ServerProtocolHandlerFactory $protocolHandlerFactory, LoggerInterface $logger, - HandlerFactoryInterface $handlerFactory, ServerBindHandlerFactory $bindHandlerFactory, RequestHandlerInterface $dispatcher, ServerProtocolHandler\BindHandlerInterface $bindHandler, @@ -61,15 +59,13 @@ public function let( $queue->sendMessage(Argument::any())->willReturn($queue); $bindHandlerFactory->get(Argument::any())->willReturn($bindHandler); $protocolHandlerFactory->get(Argument::any(), Argument::any())->willReturn($protocolHandler); - $handlerFactory->makeRequestHandler()->willReturn($dispatcher); $this->beConstructedWith( $queue, - $handlerFactory, - $logger, $protocolHandlerFactory, new ServerAuthorization(new ServerOptions()), $bindHandlerFactory, + $logger, ); } @@ -106,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())->willReturn( + $bindHandler->handleBind(Argument::any())->willReturn( new BindToken('foo', 'bar') ); $protocolHandler->handleRequest(Argument::any(), Argument::any()) @@ -201,7 +197,7 @@ public function it_should_send_a_bind_request_to_the_bind_request_handler(Server null ); - $bindHandler->handleBind(Argument::any(), Argument::any()) + $bindHandler->handleBind(Argument::any()) ->shouldBeCalledOnce() ->willReturn(new BindToken('foo@bar', 'bar')); $protocolHandler->handleRequest(Argument::any(), Argument::any()) @@ -222,7 +218,7 @@ public function it_should_handle_operation_errors_thrown_from_the_request_handle null ); - $bindHandler->handleBind(Argument::any(), Argument::any()) + $bindHandler->handleBind(Argument::any()) ->willReturn(new BindToken('foo@bar', 'bar')); $protocolHandler->handleRequest(Argument::any(), Argument::any()) diff --git a/tests/spec/FreeDSx/Ldap/Server/SocketServerFactorySpec.php b/tests/spec/FreeDSx/Ldap/Server/SocketServerFactorySpec.php index 59640ada..1188ea5d 100644 --- a/tests/spec/FreeDSx/Ldap/Server/SocketServerFactorySpec.php +++ b/tests/spec/FreeDSx/Ldap/Server/SocketServerFactorySpec.php @@ -31,7 +31,7 @@ public function let(LoggerInterface $logger): void $this->tmpUnixSocketFilePath = stream_get_meta_data($this->tmpUnixSocketResource)['uri']; $this->beConstructedWith( - (new ServerOptions) + (new ServerOptions()) ->setPort(3390), $logger, ); @@ -54,7 +54,7 @@ public function it_should_make_a_unix_based_socket_server(LoggerInterface $logge throw new SkippingException('Cannot construct unix based socket on Windows.'); } $this->beConstructedWith( - (new ServerOptions) + (new ServerOptions()) ->setUnixSocket($this->tmpUnixSocketFilePath) ->setTransport('unix'), $logger,