Skip to content

Commit

Permalink
Remove the ServerQueue from the ServerProtocolHandlerInterface. The h…
Browse files Browse the repository at this point in the history
…andler should be constructed with it.
  • Loading branch information
ChadSikorra committed Aug 6, 2023
1 parent 85c5547 commit 10c88b0
Show file tree
Hide file tree
Showing 31 changed files with 233 additions and 162 deletions.
17 changes: 1 addition & 16 deletions src/FreeDSx/Ldap/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
use FreeDSx\Ldap\Protocol\ServerAuthorization;
use FreeDSx\Ldap\Server\HandlerFactoryInterface;
use FreeDSx\Ldap\Server\RequestHandler\HandlerFactory;
use FreeDSx\Ldap\Server\RequestHistory;
use FreeDSx\Ldap\Server\ServerProtocolFactory;
use FreeDSx\Ldap\Server\ServerRunner\PcntlServerRunner;
use FreeDSx\Ldap\Server\ServerRunner\ServerRunnerInterface;
Expand Down Expand Up @@ -153,10 +152,6 @@ className: ServerRunnerInterface::class,
className: ServerAuthorization::class,
factory: $this->makeServerAuthorizer(...),
);
$this->registerFactory(
className: ServerProtocolHandlerFactory::class,
factory: $this->makeServerProtocolHandlerFactory(...),
);
}

private function makeClientProtocolHandler(): ClientProtocolHandler
Expand Down Expand Up @@ -199,8 +194,7 @@ private function makeServerProtocolFactory(): ServerProtocolFactory
{
return new ServerProtocolFactory(
handlerFactory: $this->get(HandlerFactoryInterface::class),
logger: $this->get(ServerOptions::class)->getLogger(),
serverProtocolHandlerFactory: $this->get(ServerProtocolHandlerFactory::class),
options: $this->get(ServerOptions::class),
serverAuthorization: $this->get(ServerAuthorization::class),
);
}
Expand Down Expand Up @@ -232,13 +226,4 @@ private function makeServerAuthorizer(): ServerAuthorization
{
return new ServerAuthorization($this->get(ServerOptions::class));
}

private function makeServerProtocolHandlerFactory(): ServerProtocolHandlerFactory
{
return new ServerProtocolHandlerFactory(
handlerFactory: $this->get(HandlerFactoryInterface::class),
options: $this->get(ServerOptions::class),
requestHistory: new RequestHistory(),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use FreeDSx\Ldap\Operation\Request\RequestInterface;
use FreeDSx\Ldap\Operation\Request\SimpleBindRequest;
use FreeDSx\Ldap\Operation\ResultCode;
use FreeDSx\Ldap\Protocol\Queue\ServerQueue;
use FreeDSx\Ldap\Protocol\ServerProtocolHandler\BindHandlerInterface;
use FreeDSx\Ldap\Protocol\ServerProtocolHandler\ServerAnonBindHandler;
use FreeDSx\Ldap\Protocol\ServerProtocolHandler\ServerBindHandler;
Expand All @@ -29,6 +30,10 @@
*/
class ServerBindHandlerFactory
{
public function __construct(private readonly ServerQueue $queue)
{
}

/**
* Get the bind handler specific to the request.
*
Expand All @@ -37,9 +42,9 @@ class ServerBindHandlerFactory
public function get(RequestInterface $request): BindHandlerInterface
{
if ($request instanceof SimpleBindRequest) {
return new ServerBindHandler();
return new ServerBindHandler($this->queue);
} elseif ($request instanceof AnonBindRequest) {
return new ServerAnonBindHandler();
return new ServerAnonBindHandler($this->queue);
} else {
throw new OperationException(
'The authentication type requested is not supported.',
Expand Down
27 changes: 17 additions & 10 deletions src/FreeDSx/Ldap/Protocol/Factory/ServerProtocolHandlerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use FreeDSx\Ldap\Operation\Request\RequestInterface;
use FreeDSx\Ldap\Operation\Request\SearchRequest;
use FreeDSx\Ldap\Operation\Request\UnbindRequest;
use FreeDSx\Ldap\Protocol\Queue\ServerQueue;
use FreeDSx\Ldap\Protocol\ServerProtocolHandler;
use FreeDSx\Ldap\Protocol\ServerProtocolHandler\ServerProtocolHandlerInterface;
use FreeDSx\Ldap\Server\HandlerFactoryInterface;
Expand All @@ -36,6 +37,7 @@ public function __construct(
private readonly HandlerFactoryInterface $handlerFactory,
private readonly ServerOptions $options,
private readonly RequestHistory $requestHistory,
private readonly ServerQueue $queue,
) {
}

Expand All @@ -44,19 +46,22 @@ public function get(
ControlBag $controls
): ServerProtocolHandlerInterface {
if ($request instanceof ExtendedRequest && $request->getName() === ExtendedRequest::OID_WHOAMI) {
return new ServerProtocolHandler\ServerWhoAmIHandler();
return new ServerProtocolHandler\ServerWhoAmIHandler($this->queue);
} elseif ($request instanceof ExtendedRequest && $request->getName() === ExtendedRequest::OID_START_TLS) {
return new ServerProtocolHandler\ServerStartTlsHandler($this->options);
return new ServerProtocolHandler\ServerStartTlsHandler(
options: $this->options,
queue: $this->queue,
);
} elseif ($this->isRootDseSearch($request)) {
return $this->getRootDseHandler();
} elseif ($this->isPagingSearch($request, $controls)) {
return $this->getPagingHandler();
} elseif ($request instanceof SearchRequest) {
return new ServerProtocolHandler\ServerSearchHandler();
return new ServerProtocolHandler\ServerSearchHandler($this->queue);
} elseif ($request instanceof UnbindRequest) {
return new ServerProtocolHandler\ServerUnbindHandler();
return new ServerProtocolHandler\ServerUnbindHandler($this->queue);
} else {
return new ServerProtocolHandler\ServerDispatchHandler();
return new ServerProtocolHandler\ServerDispatchHandler($this->queue);
}
}

Expand All @@ -83,8 +88,9 @@ private function getRootDseHandler(): ServerProtocolHandler\ServerRootDseHandler
$rootDseHandler = $this->handlerFactory->makeRootDseHandler();

return new ServerProtocolHandler\ServerRootDseHandler(
$this->options,
$rootDseHandler,
options: $this->options,
queue: $this->queue,
rootDseHandler: $rootDseHandler,
);
}

Expand All @@ -93,12 +99,13 @@ private function getPagingHandler(): ServerProtocolHandlerInterface
$pagingHandler = $this->handlerFactory->makePagingHandler();

if (!$pagingHandler) {
return new ServerProtocolHandler\ServerPagingUnsupportedHandler();
return new ServerProtocolHandler\ServerPagingUnsupportedHandler($this->queue);
}

return new ServerProtocolHandler\ServerPagingHandler(
$pagingHandler,
$this->requestHistory
queue: $this->queue,
pagingHandler: $pagingHandler,
requestHistory: $this->requestHistory,
);
}
}
5 changes: 2 additions & 3 deletions src/FreeDSx/Ldap/Protocol/ServerProtocolHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function __construct(
private readonly ?LoggerInterface $logger,
private readonly ServerProtocolHandlerFactory $protocolHandlerFactory,
private readonly ServerAuthorization $authorizer,
private readonly ServerBindHandlerFactory $bindHandlerFactory = new ServerBindHandlerFactory(),
private readonly ServerBindHandlerFactory $bindHandlerFactory,
private readonly ResponseFactory $responseFactory = new ResponseFactory()
) {
}
Expand Down Expand Up @@ -169,8 +169,7 @@ private function dispatchRequest(LdapMessageRequest $message): void
$handler->handleRequest(
$message,
$this->authorizer->getToken(),
$this->handlerFactory->makeRequestHandler(),
$this->queue
$this->handlerFactory->makeRequestHandler()
);
# Authentication is required, but they have not authenticated...
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@
*/
abstract class BaseServerHandler
{
protected ResponseFactory $responseFactory;

public function __construct(ResponseFactory $responseFactory = null)
public function __construct(protected readonly ResponseFactory $responseFactory = new ResponseFactory())
{
$this->responseFactory = $responseFactory ?? new ResponseFactory();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,6 +31,11 @@
*/
class ServerAnonBindHandler extends ServerBindHandler
{
public function __construct(private readonly ServerQueue $queue)
{
parent::__construct($this->queue);
}

/**
* {@inheritDoc}
* @throws EncoderException
Expand All @@ -50,7 +56,7 @@ public function handleBind(
}

$this->validateVersion($request);
$queue->sendMessage($this->responseFactory->getStandardResponse($message));
$this->queue->sendMessage($this->responseFactory->getStandardResponse($message));

return new AnonToken(
$request->getUsername(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,6 +32,11 @@
*/
class ServerBindHandler extends BaseServerHandler implements BindHandlerInterface
{
public function __construct(private readonly ServerQueue $queue)
{
parent::__construct();
}

/**
* {@inheritDoc}
* @throws RuntimeException
Expand All @@ -52,7 +58,7 @@ public function handleBind(

$this->validateVersion($request);
$token = $this->simpleBind($dispatcher, $request);
$queue->sendMessage($this->responseFactory->getStandardResponse($message));
$this->queue->sendMessage($this->responseFactory->getStandardResponse($message));

return $token;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,6 +31,11 @@
*/
class ServerDispatchHandler extends BaseServerHandler implements ServerProtocolHandlerInterface
{
public function __construct(private readonly ServerQueue $queue)
{
parent::__construct();
}

/**
* {@inheritDoc}
* @throws OperationException
Expand All @@ -38,8 +44,7 @@ class ServerDispatchHandler extends BaseServerHandler implements ServerProtocolH
public function handleRequest(
LdapMessageRequest $message,
TokenInterface $token,
RequestHandlerInterface $dispatcher,
ServerQueue $queue
RequestHandlerInterface $dispatcher
): void {
$context = new RequestContext($message->controls(), $token);
$request = $message->getRequest();
Expand All @@ -63,6 +68,6 @@ public function handleRequest(
);
}

$queue->sendMessage($this->responseFactory->getStandardResponse($message));
$this->queue->sendMessage($this->responseFactory->getStandardResponse($message));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,12 @@ class ServerPagingHandler implements ServerProtocolHandlerInterface
{
use ServerSearchTrait;

private PagingHandlerInterface $pagingHandler;

private RequestHistory $requestHistory;

private PagingRequestComparator $requestComparator;

public function __construct(
PagingHandlerInterface $pagingHandler,
RequestHistory $requestHistory,
?PagingRequestComparator $requestComparator = null
private readonly ServerQueue $queue,
private readonly PagingHandlerInterface $pagingHandler,
private readonly RequestHistory $requestHistory,
private readonly PagingRequestComparator $requestComparator = new PagingRequestComparator(),
) {
$this->pagingHandler = $pagingHandler;
$this->requestHistory = $requestHistory;
$this->requestComparator = $requestComparator ?? new PagingRequestComparator();
}

/**
Expand All @@ -64,8 +56,7 @@ public function __construct(
public function handleRequest(
LdapMessageRequest $message,
TokenInterface $token,
RequestHandlerInterface $dispatcher,
ServerQueue $queue
RequestHandlerInterface $dispatcher
): void {
$context = new RequestContext(
$message->controls(),
Expand Down Expand Up @@ -128,7 +119,7 @@ public function handleRequest(
$this->sendEntriesToClient(
$searchResult,
$message,
$queue,
$this->queue,
...$controls
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,17 @@ class ServerPagingUnsupportedHandler implements ServerProtocolHandlerInterface
{
use ServerSearchTrait;

public function __construct(private readonly ServerQueue $queue)
{
}

/**
* @inheritDoc
*/
public function handleRequest(
LdapMessageRequest $message,
TokenInterface $token,
RequestHandlerInterface $dispatcher,
ServerQueue $queue
RequestHandlerInterface $dispatcher
): void {
$context = new RequestContext(
$message->controls(),
Expand Down Expand Up @@ -80,7 +83,7 @@ public function handleRequest(
$this->sendEntriesToClient(
$searchResult,
$message,
$queue
$this->queue
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
use FreeDSx\Socket\Exception\ConnectionException;
Expand All @@ -38,6 +37,5 @@ public function handleRequest(
LdapMessageRequest $message,
TokenInterface $token,
RequestHandlerInterface $dispatcher,
ServerQueue $queue,
): void;
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class ServerRootDseHandler implements ServerProtocolHandlerInterface
{
public function __construct(
private readonly ServerOptions $options,
private readonly ServerQueue $queue,
private readonly ?RootDseHandlerInterface $rootDseHandler = null
) {
}
Expand All @@ -51,8 +52,7 @@ public function __construct(
public function handleRequest(
LdapMessageRequest $message,
TokenInterface $token,
RequestHandlerInterface $dispatcher,
ServerQueue $queue
RequestHandlerInterface $dispatcher
): void {
$entry = Entry::fromArray('', [
'namingContexts' => $this->options->getDseNamingContexts(),
Expand Down Expand Up @@ -93,7 +93,7 @@ public function handleRequest(
);
}

$queue->sendMessage(
$this->queue->sendMessage(
new LdapMessageResponse(
$message->getMessageId(),
new SearchResultEntry($entry)
Expand Down
Loading

0 comments on commit 10c88b0

Please sign in to comment.