Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.0.0] Server classes clean-up #78

Merged
merged 2 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/FreeDSx/Ldap/Protocol/Factory/ServerBindHandlerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -30,8 +32,10 @@
*/
class ServerBindHandlerFactory
{
public function __construct(private readonly ServerQueue $queue)
{
public function __construct(
private readonly ServerQueue $queue,
private readonly HandlerFactoryInterface $handlerFactory,
) {
}

/**
Expand All @@ -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 {
Expand Down
17 changes: 7 additions & 10 deletions src/FreeDSx/Ldap/Protocol/ServerProtocolHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
) {
}
Expand Down Expand Up @@ -92,14 +91,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(
Expand Down Expand Up @@ -223,11 +222,9 @@ private function handleAuthRequest(LdapMessageRequest $message): TokenInterface
);
}

return $this->bindHandlerFactory->get($message->getRequest())->handleBind(
$message,
$this->handlerFactory->makeRequestHandler(),
$this->queue
);
return $this->bindHandlerFactory
->get($message->getRequest())
->handleBind($message);
}

/**
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +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;

/**
Expand All @@ -31,9 +29,5 @@ interface BindHandlerInterface
*
* @throws OperationException
*/
public function handleBind(
LdapMessageRequest $message,
RequestHandlerInterface $dispatcher,
ServerQueue $queue
): TokenInterface;
public function handleBind(LdapMessageRequest $message): TokenInterface;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

/**
* This file is part of the FreeDSx LDAP package.
*
* (c) Chad Sikorra <Chad.Sikorra@gmail.com>
*
* 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
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
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;
use FreeDSx\Ldap\Server\Token\AnonToken;
use FreeDSx\Ldap\Server\Token\TokenInterface;

Expand All @@ -28,11 +28,14 @@
*
* @author Chad Sikorra <Chad.Sikorra@gmail.com>
*/
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()
) {
}

/**
Expand All @@ -41,11 +44,8 @@ public function __construct(private readonly ServerQueue $queue)
* @throws OperationException
* @throws RuntimeException
*/
public function handleBind(
LdapMessageRequest $message,
RequestHandlerInterface $dispatcher,
ServerQueue $queue
): TokenInterface {
public function handleBind(LdapMessageRequest $message): TokenInterface
{
$request = $message->getRequest();
if (!$request instanceof AnonBindRequest) {
throw new RuntimeException(sprintf(
Expand All @@ -54,12 +54,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(),
);
}
}
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 @@ -29,23 +30,24 @@
*
* @author Chad Sikorra <Chad.Sikorra@gmail.com>
*/
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 RequestHandlerInterface $dispatcher,
private readonly ResponseFactory $responseFactory = new ResponseFactory(),
) {
}

/**
* {@inheritDoc}
* @throws RuntimeException
* @throws OperationException
*/
public function handleBind(
LdapMessageRequest $message,
RequestHandlerInterface $dispatcher,
ServerQueue $queue
): TokenInterface {
public function handleBind(LdapMessageRequest $message): TokenInterface
{
/** @var BindRequest $request */
$request = $message->getRequest();
if (!$request instanceof SimpleBindRequest) {
Expand All @@ -55,8 +57,8 @@ public function handleBind(
));
}

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

return $token;
Expand All @@ -65,31 +67,18 @@ public function handleBind(
/**
* @throws OperationException
*/
protected function validateVersion(BindRequest $request): void
private function simpleBind(SimpleBindRequest $request): TokenInterface
{
# 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
*/
private function simpleBind(
RequestHandlerInterface $dispatcher,
SimpleBindRequest $request
): TokenInterface {
if (!$dispatcher->bind($request->getUsername(), $request->getPassword())) {
if (!$this->dispatcher->bind($request->getUsername(), $request->getPassword())) {
throw new OperationException(
'Invalid credentials.',
ResultCode::INVALID_CREDENTIALS
);
}

return new BindToken($request->getUsername(), $request->getPassword());
return new BindToken(
$request->getUsername(),
$request->getPassword()
);
}
}
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 @@ -28,13 +29,13 @@
*
* @author Chad Sikorra <Chad.Sikorra@gmail.com>
*/
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();
}

/**
Expand Down
8 changes: 5 additions & 3 deletions src/FreeDSx/Ldap/Server/ServerProtocolFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,18 @@ 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,
requestHistory: new RequestHistory(),
queue: $serverQueue,
),
authorizer: $this->serverAuthorization,
bindHandlerFactory: new ServerBindHandlerFactory($serverQueue),
bindHandlerFactory: new ServerBindHandlerFactory(
queue: $serverQueue,
handlerFactory: $this->handlerFactory,
),
logger: $this->options->getLogger(),
);
}
}
Loading
Loading