Skip to content

Commit

Permalink
Implement options objects in place of arrays.
Browse files Browse the repository at this point in the history
  • Loading branch information
ChadSikorra committed Jun 6, 2023
1 parent 0bbc7b1 commit 552a333
Show file tree
Hide file tree
Showing 56 changed files with 700 additions and 642 deletions.
33 changes: 28 additions & 5 deletions src/FreeDSx/Ldap/ClientOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ final class ClientOptions

private bool $sslValidateCert = true;

private ?bool $sslAllowSelfSigned = null;
private bool $sslAllowSelfSigned = false;

private ?string $sslCaCert = null;

Expand Down Expand Up @@ -161,12 +161,12 @@ public function setSslValidateCert(bool $sslValidateCert): self
return $this;
}

public function getSslAllowSelfSigned(): ?bool
public function isSslAllowSelfSigned(): bool
{
return $this->sslAllowSelfSigned;
}

public function setSslAllowSelfSigned(?bool $sslAllowSelfSigned): self
public function setSslAllowSelfSigned(bool $sslAllowSelfSigned): self
{
$this->sslAllowSelfSigned = $sslAllowSelfSigned;

Expand Down Expand Up @@ -257,6 +257,29 @@ public function setReferralLimit(int $referralLimit): self

return $this;
}



/**
* @return array{version: int, servers: string[], port: int, base_dn: ?string, page_size: int, use_ssl: bool, ssl_validate_cert: bool, ssl_allow_self_signed: bool, ssl_ca_cert: ?string, ssl_peer_name: ?string, timeout_connect: int, timeout_read: int, referral: string, referral_chaser: ?ReferralChaserInterface, referral_limit: int}
*/
public function toArray(): array
{
return [
'version' => $this->getVersion(),
'servers' => $this->getServers(),
'port' => $this->getPort(),
'transport' => $this->getTransport(),
'base_dn' => $this->getBaseDn(),
'page_size' => $this->getPageSize(),
'use_ssl' => $this->isUseSsl(),
'ssl_validate_cert' => $this->isSslValidateCert(),
'ssl_allow_self_signed' => $this->isSslAllowSelfSigned(),
'ssl_ca_cert' => $this->getSslCaCert(),
'ssl_peer_name' => $this->getSslPeerName(),
'timeout_connect' => $this->getTimeoutConnect(),
'timeout_read' => $this->getTimeoutRead(),
'referral' => $this->getReferral(),
'referral_chaser' => $this->getReferralChaser(),
'referral_limit' => $this->getReferralLimit(),
];
}
}
2 changes: 1 addition & 1 deletion src/FreeDSx/Ldap/Control/ControlBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
use Countable;
use IteratorAggregate;
use Traversable;
use function count;
use function array_search;
use function count;
use function in_array;
use function is_string;

Expand Down
50 changes: 9 additions & 41 deletions src/FreeDSx/Ldap/LdapClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,39 +52,13 @@ class LdapClient

public const REFERRAL_THROW = 'throw';

/**
* @var array<string, mixed>
*/
private array $options = [
'version' => 3,
'servers' => [],
'port' => 389,
'transport' => 'tcp',
'base_dn' => null,
'page_size' => 1000,
'use_ssl' => false,
'ssl_validate_cert' => true,
'ssl_allow_self_signed' => null,
'ssl_ca_cert' => null,
'ssl_peer_name' => null,
'timeout_connect' => 3,
'timeout_read' => 10,
'referral' => 'throw',
'referral_chaser' => null,
'referral_limit' => 10,
];
private ClientOptions $options;

private ?ClientProtocolHandler $handler = null;

/**
* @param array<string, mixed> $options
*/
public function __construct(array $options = [])
public function __construct(ClientOptions $options = new ClientOptions())
{
$this->options = array_merge(
$this->options,
$options,
);
$this->options = $options;
}

/**
Expand All @@ -98,7 +72,7 @@ public function bind(
): LdapMessageResponse {
return $this->sendAndReceive(
Operations::bind($username, $password)
->setVersion($this->options['version'])
->setVersion($this->options->getVersion())
);
}

Expand All @@ -117,7 +91,7 @@ public function bindSasl(
): LdapMessageResponse {
return $this->sendAndReceive(
Operations::bindSasl($options, $mechanism)
->setVersion($this->options['version'])
->setVersion($this->options->getVersion())
);
}

Expand Down Expand Up @@ -312,7 +286,7 @@ public function paging(
return new Paging(
client: $this,
search: $search,
size: $size ?? (int) $this->options['page_size']
size: $size ?? $this->options->getPageSize()
);
}

Expand Down Expand Up @@ -446,10 +420,8 @@ public function controls(): ControlBag

/**
* Get the options currently set.
*
* @return array<string, mixed>
*/
public function getOptions(): array
public function getOptions(): ClientOptions
{
return $this->options;
}
Expand All @@ -458,18 +430,14 @@ public function getOptions(): array
* Merge a set of options. Depending on what you are changing, you many want to set the $forceDisconnect param to
* true, which forces the client to disconnect. After which you would have to manually bind again.
*
* @param array<string, mixed> $options The set of options to merge in.
* @param bool $forceDisconnect Whether the client should disconnect; forcing a manual re-connect / bind. This is
* false by default.
*/
public function setOptions(
array $options,
ClientOptions $options,
bool $forceDisconnect = false
): self {
$this->options = array_merge(
$this->options,
$options
);
$this->options = $options;
if ($forceDisconnect) {
$this->unbindIfConnected();
}
Expand Down
74 changes: 20 additions & 54 deletions src/FreeDSx/Ldap/LdapServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace FreeDSx\Ldap;

use FreeDSx\Ldap\Exception\RuntimeException;
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 @@ -34,45 +34,13 @@ class LdapServer
{
use LoggerTrait;

/**
* @var array<string, mixed>
*/
private array $options = [
'ip' => '0.0.0.0',
'port' => 389,
'unix_socket' => '/var/run/ldap.socket',
'transport' => 'tcp',
'idle_timeout' => 600,
'require_authentication' => true,
'allow_anonymous' => false,
'request_handler' => null,
'rootdse_handler' => null,
'paging_handler' => null,
'logger' => null,
'use_ssl' => false,
'ssl_cert' => null,
'ssl_cert_passphrase' => null,
'dse_alt_server' => null,
'dse_naming_contexts' => 'dc=FreeDSx,dc=local',
'dse_vendor_name' => 'FreeDSx',
'dse_vendor_version' => null,
];

private ?ServerRunnerInterface $runner;

/**
* @param array<string, mixed> $options
*
* @throws RuntimeException
*/
public function __construct(
array $options = [],
ServerOptions $options = new ServerOptions(),
?ServerRunnerInterface $serverRunner = null
) {
$this->options = array_merge(
$this->options,
$options
);
$this->options = $options;
$this->runner = $serverRunner;
}

Expand All @@ -83,30 +51,28 @@ public function __construct(
*/
public function run(): void
{
$isUnixSocket = $this->options['transport'] === 'unix';
$isUnixSocket = $this->options->getTransport() === 'unix';
$resource = $isUnixSocket
? $this->options['unix_socket']
: $this->options['ip'];
? $this->options->getUnixSocket()
: $this->options->getIp();

if ($isUnixSocket) {
$this->removeExistingSocketIfNeeded($resource);
}

$socketServer = SocketServer::bind(
$resource,
$this->options['port'],
$this->options
$this->options->getPort(),
$this->options->toArray(),
);

$this->runner()->run($socketServer);
}

/**
* Get the options currently set for the LDAP server.
*
* @return array<string, mixed>
*/
public function getOptions(): array
public function getOptions(): ServerOptions
{
return $this->options;
}
Expand All @@ -116,7 +82,7 @@ public function getOptions(): array
*/
public function useRequestHandler(RequestHandlerInterface $requestHandler): self
{
$this->options['request_handler'] = $requestHandler;
$this->options->setRequestHandler($requestHandler);

return $this;
}
Expand All @@ -126,7 +92,7 @@ public function useRequestHandler(RequestHandlerInterface $requestHandler): self
*/
public function useRootDseHandler(RootDseHandlerInterface $rootDseHandler): self
{
$this->options['rootdse_handler'] = $rootDseHandler;
$this->options->setRootDseHandler($rootDseHandler);

return $this;
}
Expand All @@ -136,7 +102,7 @@ public function useRootDseHandler(RootDseHandlerInterface $rootDseHandler): self
*/
public function usePagingHandler(PagingHandlerInterface $pagingHandler): self
{
$this->options['paging_handler'] = $pagingHandler;
$this->options->setPagingHandler($pagingHandler);

return $this;
}
Expand All @@ -146,7 +112,7 @@ public function usePagingHandler(PagingHandlerInterface $pagingHandler): self
*/
public function useLogger(LoggerInterface $logger): self
{
$this->options['logger'] = $logger;
$this->options->setLogger($logger);

return $this;
}
Expand All @@ -157,17 +123,17 @@ public function useLogger(LoggerInterface $logger): self
* Note: This is only intended to work with the PCNTL server runner.
*
* @param string|string[] $servers The LDAP server(s) to proxy the request to.
* @param array<string, mixed> $clientOptions Any additional client options for the proxy connection.
* @param array<string, mixed> $serverOptions Any additional server options for the LDAP server.
* @param ClientOptions $clientOptions Any additional client options for the proxy connection.
* @param ServerOptions $serverOptions Any additional server options for the LDAP server.
*/
public static function makeProxy(
array|string $servers,
array $clientOptions = [],
array $serverOptions = []
ClientOptions $clientOptions = new ClientOptions(),
ServerOptions $serverOptions = new ServerOptions(),
): LdapServer {
$client = new LdapClient(array_merge([
'servers' => $servers,
], $clientOptions));
$client = new LdapClient(
$clientOptions->setServers((array) $servers)
);

$proxyRequestHandler = new ProxyHandler($client);
$server = new LdapServer($serverOptions);
Expand Down
7 changes: 4 additions & 3 deletions src/FreeDSx/Ldap/Protocol/ClientProtocolHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace FreeDSx\Ldap\Protocol;

use FreeDSx\Asn1\Exception\EncoderException;
use FreeDSx\Ldap\ClientOptions;
use FreeDSx\Ldap\Control\Control;
use FreeDSx\Ldap\Control\ControlBag;
use FreeDSx\Ldap\Entry\Entry;
Expand Down Expand Up @@ -51,7 +52,7 @@ class ClientProtocolHandler

private ?ClientQueue $queue;

private array $options;
private ClientOptions $options;

private ControlBag $controls;

Expand All @@ -60,13 +61,13 @@ class ClientProtocolHandler
private ?Entry $rootDse = null;

public function __construct(
array $options,
ClientOptions $options,
ClientQueue $queue = null,
SocketPool $pool = null,
ClientProtocolHandlerFactory $clientProtocolHandlerFactory = null
) {
$this->options = $options;
$this->pool = $pool ?? new SocketPool($options);
$this->pool = $pool ?? new SocketPool($this->options->toArray());
$this->protocolHandlerFactory = $clientProtocolHandlerFactory ?? new ClientProtocolHandlerFactory();
$this->controls = new ControlBag();
$this->queue = $queue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace FreeDSx\Ldap\Protocol\ClientProtocolHandler;

use FreeDSx\Asn1\Exception\EncoderException;
use FreeDSx\Ldap\ClientOptions;
use FreeDSx\Ldap\Exception\BindException;
use FreeDSx\Ldap\Exception\OperationException;
use FreeDSx\Ldap\Exception\ProtocolException;
Expand Down Expand Up @@ -68,7 +69,7 @@ public function handleResponse(
LdapMessageRequest $messageTo,
LdapMessageResponse $messageFrom,
ClientQueue $queue,
array $options
ClientOptions $options
): ?LdapMessageResponse {
$result = $messageFrom->getResponse();

Expand Down
Loading

0 comments on commit 552a333

Please sign in to comment.