Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
MeCapron committed Sep 14, 2023
0 parents commit 9096537
Show file tree
Hide file tree
Showing 21 changed files with 722 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea/
18 changes: 18 additions & 0 deletions Exceptions/InvalidBasicAuthPasswordException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Adexos\JaneSDKBridge\Exceptions;

use Exception;

class InvalidBasicAuthPasswordException extends Exception
{
public function __construct()
{
parent::__construct(
'Empty basic auth password on client instantiation',
400
);
}
}
18 changes: 18 additions & 0 deletions Exceptions/InvalidBasicAuthUsernameException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Adexos\JaneSDKBridge\Exceptions;

use Exception;

class InvalidBasicAuthUsernameException extends Exception
{
public function __construct()
{
parent::__construct(
'Empty basic auth username on client instantiation',
400
);
}
}
16 changes: 16 additions & 0 deletions Exceptions/InvalidBearerTokenException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Adexos\JaneSDKBridge\Exceptions;

class InvalidBearerTokenException extends \Exception
{
public function __construct()
{
parent::__construct(
'Empty bearer token on client instantiation',
400
);
}
}
109 changes: 109 additions & 0 deletions Http/Client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

declare(strict_types=1);

namespace Adexos\JaneSDKBridge\Http;

use Adexos\JaneSDKBridge\Http\Plugins\Auth\ApiKeyPluginWrapper;
use Adexos\JaneSDKBridge\Http\Plugins\Auth\BearerPluginWrapper;
use Adexos\JaneSDKBridge\Http\Plugins\Auth\BasicAuthPluginWrapper;
use Adexos\JaneSDKBridge\Http\Plugins\HttpPluginInterface;
use Http\Client\Common\Plugin;
use Http\Client\Common\PluginClient;
use Http\Discovery\HttpClientDiscovery;
use Jane\Component\OpenApiRuntime\Client\Plugin\AuthenticationRegistry;

class Client
{
/**
* @var Plugin[]
*/
private array $plugins;

private string $clientName;

/**
* @var Plugin[]
*/
private array $additionalPlugins;

private ?ApiKeyPluginWrapper $apiKeyPlugin;

private ?BearerPluginWrapper $bearerPlugin;

private ?BasicAuthPluginWrapper $basicAuthPlugin;

private array $clientRegistry = [];

public function __construct(
array $plugins,
string $clientName,
array $additionalPlugins = [],
?ApiKeyPluginWrapper $apiKeyPlugin = null,
?BearerPluginWrapper $bearerPlugin = null,
?BasicAuthPluginWrapper $basicAuthPlugin = null
) {
$this->plugins = $plugins;
$this->clientName = $clientName;
$this->additionalPlugins = $additionalPlugins;
$this->apiKeyPlugin = $apiKeyPlugin;
$this->bearerPlugin = $bearerPlugin;
$this->basicAuthPlugin = $basicAuthPlugin;
}

public function getClient(array $authenticationOptions = [])
{
$cacheKey = sha1(json_encode($authenticationOptions));
if (array_key_exists($cacheKey, $this->clientRegistry)) {
return $this->clientRegistry[$cacheKey];
}

$plugins = $this->getPlugins();

if ($authenticationRegistry = $this->getAuthenticationRegistry($authenticationOptions)) {
$plugins[] = $authenticationRegistry;
}

$httpClient = new PluginClient(HttpClientDiscovery::find(), $plugins);

$this->clientRegistry[$cacheKey] = $this->clientName::create($httpClient);

return $this->clientRegistry[$cacheKey];
}

/**
* @return Plugin[]
*/
private function getPlugins(): array
{
$result = array_map(static function(HttpPluginInterface $plugin): ?Plugin {
return $plugin->create();
}, array_merge($this->plugins, $this->additionalPlugins));

// remove null plugins that are not enabled yet
return array_filter($result);
}

private function getAuthenticationRegistry(array $options = []): ?AuthenticationRegistry
{
$authenticationPlugins = [];

if ($this->apiKeyPlugin !== null) {
$authenticationPlugins[] = $this->apiKeyPlugin->create();
}

if ($this->bearerPlugin !== null) {
$authenticationPlugins[] = $this->bearerPlugin->create($options);
}

if ($this->basicAuthPlugin !== null) {
$authenticationPlugins[] = $this->basicAuthPlugin->create($options);
}

if (empty($authenticationPlugins)) {
return null;
}

return new AuthenticationRegistry($authenticationPlugins);
}
}
46 changes: 46 additions & 0 deletions Http/Plugins/Auth/ApiKeyPluginWrapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace Adexos\JaneSDKBridge\Http\Plugins\Auth;

use Http\Client\Common\Plugin;
use Jane\Component\OpenApiRuntime\Client\AuthenticationPlugin;
use Jane\Component\OpenApiRuntime\Client\Plugin\AuthenticationRegistry;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Encryption\EncryptorInterface;

class ApiKeyPluginWrapper implements AuthHttpPluginInterface
{
private ScopeConfigInterface $scopeConfig;

private EncryptorInterface $encryptor;

private string $authenticationPluginClass;

private string $configPath;

public function __construct(
ScopeConfigInterface $scopeConfig,
EncryptorInterface $encryptor,
string $authenticationPluginClass,
string $configPath
) {
$this->scopeConfig = $scopeConfig;
$this->encryptor = $encryptor;
$this->authenticationPluginClass = $authenticationPluginClass;
$this->configPath = $configPath;
}

/**
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function create(array $options = []): AuthenticationPlugin
{
$apiKey = $this->encryptor->decrypt(
$this->scopeConfig->getValue($this->configPath)
);

return new $this->authenticationPluginClass($apiKey);
}
}
12 changes: 12 additions & 0 deletions Http/Plugins/Auth/AuthHttpPluginInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Adexos\JaneSDKBridge\Http\Plugins\Auth;

use Jane\Component\OpenApiRuntime\Client\AuthenticationPlugin;

interface AuthHttpPluginInterface
{
public function create(array $options = []): AuthenticationPlugin;
}
63 changes: 63 additions & 0 deletions Http/Plugins/Auth/BasicAuthPluginWrapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

namespace Adexos\JaneSDKBridge\Http\Plugins\Auth;

use Adexos\JaneSDKBridge\Exceptions\InvalidBasicAuthUsernameException;
use Adexos\JaneSDKBridge\Exceptions\InvalidBasicAuthPasswordException;
use Http\Client\Common\Plugin;
use Jane\Component\OpenApiRuntime\Client\AuthenticationPlugin;
use Jane\Component\OpenApiRuntime\Client\Plugin\AuthenticationRegistry;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Encryption\EncryptorInterface;

class BasicAuthPluginWrapper implements AuthHttpPluginInterface
{
private string $basicAuthPluginClass;

private ScopeConfigInterface $scopeConfig;

private EncryptorInterface $encryptor;

private string $configPathUsername;

private string $configPathPassword;

public function __construct(
string $basicAuthPluginClass,
ScopeConfigInterface $scopeConfig,
EncryptorInterface $encryptor,
string $configPathUsername,
string $configPathPassword
) {
$this->basicAuthPluginClass = $basicAuthPluginClass;
$this->scopeConfig = $scopeConfig;
$this->encryptor = $encryptor;
$this->configPathUsername = $configPathUsername;
$this->configPathPassword = $configPathPassword;
}

/**
* @param array $options
*
* @return AuthenticationPlugin
* @throws InvalidBasicAuthPasswordException
* @throws InvalidBasicAuthUsernameException
*/
public function create(array $options = []): AuthenticationPlugin
{
if (!($username = $this->scopeConfig->getValue($this->configPathUsername))) {
throw new InvalidBasicAuthUsernameException();
}
if (!($passwordEncrypt = $this->scopeConfig->getValue($this->configPathPassword))) {
throw new InvalidBasicAuthPasswordException();
}

$password = $this->encryptor->decrypt(
$passwordEncrypt
);

return new $this->basicAuthPluginClass($username, $password);
}
}
34 changes: 34 additions & 0 deletions Http/Plugins/Auth/BearerPluginWrapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Adexos\JaneSDKBridge\Http\Plugins\Auth;

use Adexos\JaneSDKBridge\Exceptions\InvalidBearerTokenException;
use Http\Client\Common\Plugin;
use Jane\Component\OpenApiRuntime\Client\AuthenticationPlugin;
use Jane\Component\OpenApiRuntime\Client\Plugin\AuthenticationRegistry;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Encryption\EncryptorInterface;

class BearerPluginWrapper implements AuthHttpPluginInterface
{
private string $bearerPluginClass;

public function __construct(string $bearerPluginClass)
{
$this->bearerPluginClass = $bearerPluginClass;
}

/**
* @throws InvalidBearerTokenException
*/
public function create(array $options = []): AuthenticationPlugin
{
if (!isset($options['Bearer'])) {
throw new InvalidBearerTokenException();
}

return new $this->bearerPluginClass($options['Bearer']);
}
}
32 changes: 32 additions & 0 deletions Http/Plugins/HostPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Adexos\JaneSDKBridge\Http\Plugins;

use Http\Client\Common\Plugin;
use Http\Client\Common\Plugin\AddHostPlugin;
use Http\Discovery\Psr17FactoryDiscovery;
use Magento\Framework\App\Config\ScopeConfigInterface;

class HostPlugin implements HttpPluginInterface
{
private ScopeConfigInterface $scopeConfig;

private string $configPath;

public function __construct(ScopeConfigInterface $scopeConfig, string $configPath)
{
$this->scopeConfig = $scopeConfig;
$this->configPath = $configPath;
}

public function create(): ?Plugin
{
$uri = Psr17FactoryDiscovery::findUriFactory()->createUri(
$this->scopeConfig->getValue($this->configPath)
);

return new AddHostPlugin($uri);
}
}
12 changes: 12 additions & 0 deletions Http/Plugins/HttpPluginInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Adexos\JaneSDKBridge\Http\Plugins;

use Http\Client\Common\Plugin;

interface HttpPluginInterface
{
public function create(): ?Plugin;
}
Loading

0 comments on commit 9096537

Please sign in to comment.