Skip to content

Commit

Permalink
Remove the ServerOptions from the ServerProtocolHandlerInterface. Thi…
Browse files Browse the repository at this point in the history
…s comes from the container if needed.
  • Loading branch information
ChadSikorra committed Aug 6, 2023
1 parent 4fe9628 commit c1d5243
Show file tree
Hide file tree
Showing 26 changed files with 166 additions and 118 deletions.
26 changes: 15 additions & 11 deletions src/FreeDSx/Ldap/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use FreeDSx\Ldap\Server\RequestHistory;
use FreeDSx\Ldap\Server\ServerProtocolFactory;
use FreeDSx\Ldap\Server\ServerRunner\PcntlServerRunner;
use FreeDSx\Ldap\Server\ServerRunner\ServerRunnerInterface;
use FreeDSx\Ldap\Server\SocketServerFactory;
use FreeDSx\Socket\SocketPool;

Expand Down Expand Up @@ -145,8 +146,8 @@ className: ServerProtocolFactory::class,
factory: $this->makeServerProtocolFactory(...),
);
$this->registerFactory(
className: PcntlServerRunner::class,
factory: $this->makePcntlServerRunner(...),
className: ServerRunnerInterface::class,
factory: $this->makeServerRunner(...),
);
$this->registerFactory(
className: ServerAuthorization::class,
Expand Down Expand Up @@ -197,8 +198,10 @@ private function makeRootDseLoader(): RootDseLoader
private function makeServerProtocolFactory(): ServerProtocolFactory
{
return new ServerProtocolFactory(
$this->get(HandlerFactory::class),
$this->get(ServerOptions::class),
handlerFactory: $this->get(HandlerFactoryInterface::class),
logger: $this->get(ServerOptions::class)->getLogger(),
serverProtocolHandlerFactory: $this->get(ServerProtocolHandlerFactory::class),
serverAuthorization: $this->get(ServerAuthorization::class),
);
}

Expand All @@ -207,11 +210,11 @@ private function makeHandlerFactory(): HandlerFactory
return new HandlerFactory($this->get(ServerOptions::class));
}

private function makePcntlServerRunner(): PcntlServerRunner
private function makeServerRunner(): ServerRunnerInterface
{
return new PcntlServerRunner(
$this->get(ServerProtocolFactory::class),
$this->get(ServerOptions::class)->getLogger(),
serverProtocolFactory: $this->get(ServerProtocolFactory::class),
logger: $this->get(ServerOptions::class)->getLogger(),
);
}

Expand All @@ -220,8 +223,8 @@ private function makeSocketServerFactory(): SocketServerFactory
$serverOptions = $this->get(ServerOptions::class);

return new SocketServerFactory(
$serverOptions,
$serverOptions->getLogger(),
options: $serverOptions,
logger: $serverOptions->getLogger(),
);
}

Expand All @@ -233,8 +236,9 @@ private function makeServerAuthorizer(): ServerAuthorization
private function makeServerProtocolHandlerFactory(): ServerProtocolHandlerFactory
{
return new ServerProtocolHandlerFactory(
$this->get(HandlerFactoryInterface::class),
new RequestHistory()
handlerFactory: $this->get(HandlerFactoryInterface::class),
options: $this->get(ServerOptions::class),
requestHistory: new RequestHistory(),
);
}
}
3 changes: 0 additions & 3 deletions src/FreeDSx/Ldap/LdapServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

namespace FreeDSx\Ldap;

use FreeDSx\Ldap\Server\LoggerTrait;
use FreeDSx\Ldap\Server\RequestHandler\PagingHandlerInterface;
use FreeDSx\Ldap\Server\RequestHandler\ProxyHandler;
use FreeDSx\Ldap\Server\RequestHandler\ProxyPagingHandler;
Expand All @@ -31,8 +30,6 @@
*/
class LdapServer
{
use LoggerTrait;

private Container $container;

public function __construct(
Expand Down
19 changes: 9 additions & 10 deletions src/FreeDSx/Ldap/Protocol/Factory/ServerProtocolHandlerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use FreeDSx\Ldap\Protocol\ServerProtocolHandler\ServerProtocolHandlerInterface;
use FreeDSx\Ldap\Server\HandlerFactoryInterface;
use FreeDSx\Ldap\Server\RequestHistory;
use FreeDSx\Ldap\ServerOptions;

/**
* Determines the correct handler for the request.
Expand All @@ -31,16 +32,11 @@
*/
class ServerProtocolHandlerFactory
{
private HandlerFactoryInterface $handlerFactory;

private RequestHistory $requestHistory;

public function __construct(
HandlerFactoryInterface $handlerFactory,
RequestHistory $requestHistory
private readonly HandlerFactoryInterface $handlerFactory,
private readonly ServerOptions $options,
private readonly RequestHistory $requestHistory,
) {
$this->handlerFactory = $handlerFactory;
$this->requestHistory = $requestHistory;
}

public function get(
Expand All @@ -50,7 +46,7 @@ public function get(
if ($request instanceof ExtendedRequest && $request->getName() === ExtendedRequest::OID_WHOAMI) {
return new ServerProtocolHandler\ServerWhoAmIHandler();
} elseif ($request instanceof ExtendedRequest && $request->getName() === ExtendedRequest::OID_START_TLS) {
return new ServerProtocolHandler\ServerStartTlsHandler();
return new ServerProtocolHandler\ServerStartTlsHandler($this->options);
} elseif ($this->isRootDseSearch($request)) {
return $this->getRootDseHandler();
} elseif ($this->isPagingSearch($request, $controls)) {
Expand Down Expand Up @@ -86,7 +82,10 @@ private function getRootDseHandler(): ServerProtocolHandler\ServerRootDseHandler
{
$rootDseHandler = $this->handlerFactory->makeRootDseHandler();

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

private function getPagingHandler(): ServerProtocolHandlerInterface
Expand Down
3 changes: 1 addition & 2 deletions src/FreeDSx/Ldap/Protocol/ServerProtocolHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,7 @@ private function dispatchRequest(LdapMessageRequest $message): void
$message,
$this->authorizer->getToken(),
$this->handlerFactory->makeRequestHandler(),
$this->queue,
$this->options
$this->queue
);
# Authentication is required, but they have not authenticated...
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
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.
Expand All @@ -40,8 +39,7 @@ public function handleRequest(
LdapMessageRequest $message,
TokenInterface $token,
RequestHandlerInterface $dispatcher,
ServerQueue $queue,
ServerOptions $options
ServerQueue $queue
): void {
$context = new RequestContext($message->controls(), $token);
$request = $message->getRequest();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
use FreeDSx\Ldap\Server\RequestHandler\RequestHandlerInterface;
use FreeDSx\Ldap\Server\RequestHistory;
use FreeDSx\Ldap\Server\Token\TokenInterface;
use FreeDSx\Ldap\ServerOptions;
use Throwable;

/**
Expand Down Expand Up @@ -66,8 +65,7 @@ public function handleRequest(
LdapMessageRequest $message,
TokenInterface $token,
RequestHandlerInterface $dispatcher,
ServerQueue $queue,
ServerOptions $options
ServerQueue $queue
): void {
$context = new RequestContext(
$message->controls(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
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.
Expand All @@ -38,8 +37,7 @@ public function handleRequest(
LdapMessageRequest $message,
TokenInterface $token,
RequestHandlerInterface $dispatcher,
ServerQueue $queue,
ServerOptions $options
ServerQueue $queue
): void {
$context = new RequestContext(
$message->controls(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
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;

/**
Expand All @@ -32,7 +31,6 @@ interface ServerProtocolHandlerInterface
/**
* Handle protocol actions specific to the request received.
*
* @param ServerOptions $options
* @throws OperationException
* @throws ConnectionException
*/
Expand All @@ -41,6 +39,5 @@ public function handleRequest(
TokenInterface $token,
RequestHandlerInterface $dispatcher,
ServerQueue $queue,
ServerOptions $options
): void;
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@
*/
class ServerRootDseHandler implements ServerProtocolHandlerInterface
{
public function __construct(private ?RootDseHandlerInterface $rootDseHandler = null)
{
public function __construct(
private readonly ServerOptions $options,
private readonly ?RootDseHandlerInterface $rootDseHandler = null
) {
}

/**
Expand All @@ -50,34 +52,33 @@ public function handleRequest(
LdapMessageRequest $message,
TokenInterface $token,
RequestHandlerInterface $dispatcher,
ServerQueue $queue,
ServerOptions $options
ServerQueue $queue
): void {
$entry = Entry::fromArray('', [
'namingContexts' => $options->getDseNamingContexts(),
'namingContexts' => $this->options->getDseNamingContexts(),
'supportedExtension' => [
ExtendedRequest::OID_WHOAMI,
],
'supportedLDAPVersion' => ['3'],
'vendorName' => $options->getDseVendorName(),
'vendorName' => $this->options->getDseVendorName(),
]);
if ($options->getSslCert()) {
if ($this->options->getSslCert()) {
$entry->add(
'supportedExtension',
ExtendedRequest::OID_START_TLS
);
}
if ($options->getPagingHandler()) {
if ($this->options->getPagingHandler()) {
$entry->add(
'supportedControl',
Control::OID_PAGING
);
}
if ($options->getDseVendorVersion()) {
$entry->set('vendorVersion', (string) $options->getDseVendorVersion());
if ($this->options->getDseVendorVersion()) {
$entry->set('vendorVersion', (string) $this->options->getDseVendorVersion());

Check warning on line 78 in src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerRootDseHandler.php

View check run for this annotation

Codecov / codecov/patch

src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerRootDseHandler.php#L78

Added line #L78 was not covered by tests
}
if ($options->getDseAltServer()) {
$entry->set('altServer', (string) $options->getDseAltServer());
if ($this->options->getDseAltServer()) {
$entry->set('altServer', (string) $this->options->getDseAltServer());

Check warning on line 81 in src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerRootDseHandler.php

View check run for this annotation

Codecov / codecov/patch

src/FreeDSx/Ldap/Protocol/ServerProtocolHandler/ServerRootDseHandler.php#L81

Added line #L81 was not covered by tests
}

/** @var SearchRequest $request */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
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.
Expand All @@ -37,8 +36,7 @@ public function handleRequest(
LdapMessageRequest $message,
TokenInterface $token,
RequestHandlerInterface $dispatcher,
ServerQueue $queue,
ServerOptions $options
ServerQueue $queue
): void {
$context = new RequestContext(
$message->controls(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class ServerStartTlsHandler implements ServerProtocolHandlerInterface
{
private static ?bool $hasOpenssl = null;

public function __construct()
public function __construct(private readonly ServerOptions $options)
{
if (self::$hasOpenssl === null) {
$this::$hasOpenssl = extension_loaded('openssl');
Expand All @@ -52,11 +52,10 @@ public function handleRequest(
LdapMessageRequest $message,
TokenInterface $token,
RequestHandlerInterface $dispatcher,
ServerQueue $queue,
ServerOptions $options
ServerQueue $queue
): void {
# If we don't have a SSL cert or the OpenSSL extension is not available, then we can do nothing...
if ($options->getSslCert() === null || !self::$hasOpenssl) {
if ($this->options->getSslCert() === null || !self::$hasOpenssl) {
$queue->sendMessage(new LdapMessageResponse($message->getMessageId(), new ExtendedResponse(
new LdapResult(ResultCode::PROTOCOL_ERROR),
ExtendedRequest::OID_START_TLS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
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.
Expand All @@ -33,8 +32,7 @@ public function handleRequest(
LdapMessageRequest $message,
TokenInterface $token,
RequestHandlerInterface $dispatcher,
ServerQueue $queue,
ServerOptions $options
ServerQueue $queue
): void {
$queue->close();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
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.
Expand All @@ -41,8 +40,7 @@ public function handleRequest(
LdapMessageRequest $message,
TokenInterface $token,
RequestHandlerInterface $dispatcher,
ServerQueue $queue,
ServerOptions $options
ServerQueue $queue
): void {
$userId = $token->getUsername();

Expand Down
5 changes: 2 additions & 3 deletions src/FreeDSx/Ldap/Server/ServerProtocolFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@
use FreeDSx\Ldap\Protocol\Queue\ServerQueue;
use FreeDSx\Ldap\Protocol\ServerAuthorization;
use FreeDSx\Ldap\Protocol\ServerProtocolHandler;
use FreeDSx\Ldap\Server\RequestHandler\HandlerFactory;
use FreeDSx\Socket\Socket;
use Psr\Log\LoggerInterface;

class ServerProtocolFactory
{
public function __construct(
private readonly HandlerFactory $handlerFactory,
private readonly LoggerInterface $logger,
private readonly HandlerFactoryInterface $handlerFactory,
private readonly ?LoggerInterface $logger,
private readonly ServerProtocolHandlerFactory $serverProtocolHandlerFactory,
private readonly ServerAuthorization $serverAuthorization,
) {
Expand Down
5 changes: 3 additions & 2 deletions tests/integration/FreeDSx/Ldap/ServerTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ protected function waitForServerOutput(string $marker): string
}

throw new Exception(sprintf(
'The expected output (%s) was not received after %d seconds.',
'The expected output (%s) was not received after %d seconds. Received: %s',
$marker,
$i
$i,
PHP_EOL . $this->subject->getErrorOutput()
));
}

Expand Down
Loading

0 comments on commit c1d5243

Please sign in to comment.