-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 9096537
Showing
21 changed files
with
722 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.idea/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.