Skip to content

Commit

Permalink
Merge pull request #79 from ChadSikorra/v1-interface_refactor
Browse files Browse the repository at this point in the history
[1.0.0] Refactor how server binds are handled / adjust interfaces.
  • Loading branch information
ChadSikorra authored Aug 11, 2023
2 parents 3facd95 + 72ac890 commit d9c7ae4
Show file tree
Hide file tree
Showing 14 changed files with 264 additions and 198 deletions.
43 changes: 43 additions & 0 deletions src/FreeDSx/Ldap/Protocol/Authenticator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

/**
* This file is part of the FreeDSx LDAP package.
*
* (c) Chad Sikorra <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FreeDSx\Ldap\Protocol;

use FreeDSx\Ldap\Exception\OperationException;
use FreeDSx\Ldap\Operation\ResultCode;
use FreeDSx\Ldap\Protocol\Bind\BindInterface;
use FreeDSx\Ldap\Server\Token\TokenInterface;

class Authenticator
{
/**
* @param BindInterface[] $authenticators
*/
public function __construct(private readonly array $authenticators = [])
{
}

public function bind(LdapMessageRequest $request): TokenInterface
{
foreach ($this->authenticators as $authenticator) {
if ($authenticator->supports($request)) {
return $authenticator->bind($request);
}
}

throw new OperationException(
'The authentication type requested is not supported.',
ResultCode::AUTH_METHOD_UNSUPPORTED
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@
* file that was distributed with this source code.
*/

namespace FreeDSx\Ldap\Protocol\ServerProtocolHandler;
namespace FreeDSx\Ldap\Protocol\Bind;

use FreeDSx\Asn1\Exception\EncoderException;
use FreeDSx\Ldap\Exception\OperationException;
use FreeDSx\Ldap\Exception\RuntimeException;
use FreeDSx\Ldap\Operation\Request\AnonBindRequest;
use FreeDSx\Ldap\Protocol\Factory\ResponseFactory;
Expand All @@ -28,9 +26,9 @@
*
* @author Chad Sikorra <[email protected]>
*/
class ServerAnonBindHandler implements BindHandlerInterface
class AnonymousBind implements BindInterface
{
use BindVersionValidatorTrait;
use VersionValidatorTrait;

public function __construct(
private readonly ServerQueue $queue,
Expand All @@ -40,11 +38,8 @@ public function __construct(

/**
* {@inheritDoc}
* @throws EncoderException
* @throws OperationException
* @throws RuntimeException
*/
public function handleBind(LdapMessageRequest $message): TokenInterface
public function bind(LdapMessageRequest $message): TokenInterface
{
$request = $message->getRequest();
if (!$request instanceof AnonBindRequest) {
Expand All @@ -62,4 +57,12 @@ public function handleBind(LdapMessageRequest $message): TokenInterface
$request->getVersion(),
);
}

/**
* @inheritDoc
*/
public function supports(LdapMessageRequest $request): bool
{
return $request->getRequest() instanceof AnonBindRequest;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* file that was distributed with this source code.
*/

namespace FreeDSx\Ldap\Protocol\ServerProtocolHandler;
namespace FreeDSx\Ldap\Protocol\Bind;

use FreeDSx\Ldap\Exception\OperationException;
use FreeDSx\Ldap\Protocol\LdapMessageRequest;
Expand All @@ -22,12 +22,17 @@
*
* @author Chad Sikorra <[email protected]>
*/
interface BindHandlerInterface
interface BindInterface
{
/**
* Returns a token indicating the outcome of a bind request.
*
* @throws OperationException
*/
public function handleBind(LdapMessageRequest $message): TokenInterface;
public function bind(LdapMessageRequest $message): TokenInterface;

/**
* Whether the specific bind request is supported.
*/
public function supports(LdapMessageRequest $request): bool;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* file that was distributed with this source code.
*/

namespace FreeDSx\Ldap\Protocol\ServerProtocolHandler;
namespace FreeDSx\Ldap\Protocol\Bind;

use FreeDSx\Ldap\Exception\OperationException;
use FreeDSx\Ldap\Exception\RuntimeException;
Expand All @@ -30,9 +30,9 @@
*
* @author Chad Sikorra <[email protected]>
*/
class ServerBindHandler implements BindHandlerInterface
class SimpleBind implements BindInterface
{
use BindVersionValidatorTrait;
use VersionValidatorTrait;

public function __construct(
private readonly ServerQueue $queue,
Expand All @@ -43,10 +43,8 @@ public function __construct(

/**
* {@inheritDoc}
* @throws RuntimeException
* @throws OperationException
*/
public function handleBind(LdapMessageRequest $message): TokenInterface
public function bind(LdapMessageRequest $message): TokenInterface
{
/** @var BindRequest $request */
$request = $message->getRequest();
Expand Down Expand Up @@ -81,4 +79,12 @@ private function simpleBind(SimpleBindRequest $request): TokenInterface
$request->getPassword()
);
}

/**
* @inheritDoc
*/
public function supports(LdapMessageRequest $request): bool
{
return $request->getRequest() instanceof SimpleBindRequest;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
* file that was distributed with this source code.
*/

namespace FreeDSx\Ldap\Protocol\ServerProtocolHandler;
namespace FreeDSx\Ldap\Protocol\Bind;

use FreeDSx\Ldap\Exception\OperationException;
use FreeDSx\Ldap\Operation\Request\BindRequest;
use FreeDSx\Ldap\Operation\ResultCode;

trait BindVersionValidatorTrait
trait VersionValidatorTrait
{
/**
* @throws OperationException
Expand Down
62 changes: 0 additions & 62 deletions src/FreeDSx/Ldap/Protocol/Factory/ServerBindHandlerFactory.php

This file was deleted.

9 changes: 2 additions & 7 deletions src/FreeDSx/Ldap/Protocol/ServerProtocolHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,15 @@

namespace FreeDSx\Ldap\Protocol;

use Exception;
use FreeDSx\Asn1\Exception\EncoderException;
use FreeDSx\Ldap\Exception\OperationException;
use FreeDSx\Ldap\Exception\ProtocolException;
use FreeDSx\Ldap\Exception\RuntimeException;
use FreeDSx\Ldap\Operation\Response\ExtendedResponse;
use FreeDSx\Ldap\Operation\ResultCode;
use FreeDSx\Ldap\Protocol\Factory\ResponseFactory;
use FreeDSx\Ldap\Protocol\Factory\ServerBindHandlerFactory;
use FreeDSx\Ldap\Protocol\Factory\ServerProtocolHandlerFactory;
use FreeDSx\Ldap\Protocol\Queue\ServerQueue;
use FreeDSx\Ldap\Server\HandlerFactoryInterface;
use FreeDSx\Ldap\Server\LoggerTrait;
use FreeDSx\Ldap\Server\Token\TokenInterface;
use FreeDSx\Socket\Exception\ConnectionException;
Expand All @@ -51,7 +48,7 @@ public function __construct(
private readonly ServerQueue $queue,
private readonly ServerProtocolHandlerFactory $protocolHandlerFactory,
private readonly ServerAuthorization $authorizer,
private readonly ServerBindHandlerFactory $bindHandlerFactory,
private readonly Authenticator $authenticator,
private readonly ?LoggerInterface $logger,
private readonly ResponseFactory $responseFactory = new ResponseFactory()
) {
Expand Down Expand Up @@ -222,9 +219,7 @@ private function handleAuthRequest(LdapMessageRequest $message): TokenInterface
);
}

return $this->bindHandlerFactory
->get($message->getRequest())
->handleBind($message);
return $this->authenticator->bind($message);
}

/**
Expand Down
15 changes: 10 additions & 5 deletions src/FreeDSx/Ldap/Server/ServerProtocolFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@

namespace FreeDSx\Ldap\Server;

use FreeDSx\Ldap\Protocol\Factory\ServerBindHandlerFactory;
use FreeDSx\Ldap\Protocol\Authenticator;
use FreeDSx\Ldap\Protocol\Bind\AnonymousBind;
use FreeDSx\Ldap\Protocol\Bind\SimpleBind;
use FreeDSx\Ldap\Protocol\Factory\ServerProtocolHandlerFactory;
use FreeDSx\Ldap\Protocol\Queue\ServerQueue;
use FreeDSx\Ldap\Protocol\ServerAuthorization;
Expand Down Expand Up @@ -43,10 +45,13 @@ public function make(Socket $socket): ServerProtocolHandler
queue: $serverQueue,
),
authorizer: $this->serverAuthorization,
bindHandlerFactory: new ServerBindHandlerFactory(
queue: $serverQueue,
handlerFactory: $this->handlerFactory,
),
authenticator: new Authenticator([
new SimpleBind(
queue: $serverQueue,
dispatcher: $this->handlerFactory->makeRequestHandler(),
),
new AnonymousBind($serverQueue),
]),
logger: $this->options->getLogger(),
);
}
Expand Down
Loading

0 comments on commit d9c7ae4

Please sign in to comment.