Skip to content

Commit

Permalink
FFWEB-3052: Refactor authorization by using API key
Browse files Browse the repository at this point in the history
Refactor authorisation by using API key for: test connection btn, SSR search, Field Roles update, Proxy
  • Loading branch information
Rayn93 authored Jun 6, 2024
1 parent f965e0a commit caa7264
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 74 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
],
"require": {
"php": "~8.1.0||~8.2.0||~8.3.0",
"omikron/factfinder-communication-sdk": "^0.9.8",
"omikron/factfinder-communication-sdk": "^0.9.9",
"magento/framework": "^103.0.7",
"magento/module-catalog": "^104.0.7",
"magento/module-configurable-product": "^100.4.7",
Expand Down
2 changes: 1 addition & 1 deletion src/Controller/Adminhtml/Export/Feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public function execute()
$this->pushImport->execute($storeId);
$result = $this->pushImport->getPushImportResult();
$messages[] = __('<li>Push import result</li><ul>' . $result . '</ul>');
} catch (Exception $exception) {
} catch (\Exception $exception) {
$messages[] = __('<li>Push import failed.</li>');
}
}
Expand Down
31 changes: 9 additions & 22 deletions src/Controller/Adminhtml/FieldRoles/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,47 +10,34 @@
use Magento\Store\Model\StoreManagerInterface;
use Omikron\FactFinder\Communication\Client\ClientBuilder;
use Omikron\FactFinder\Communication\Resource\AdapterFactory;
use Omikron\Factfinder\Model\Api\CredentialsFactory;
use Omikron\Factfinder\Model\Config\AuthConfig;
use Omikron\Factfinder\Model\Config\CommunicationConfig;
use Omikron\Factfinder\Model\FieldRoles;
use Psr\Http\Client\ClientExceptionInterface;

class Update extends Action
{
private JsonFactory $jsonResultFactory;
private StoreManagerInterface $storeManager;
private CommunicationConfig $communicationConfig;
private CredentialsFactory $credentialsFactory;
private FieldRoles $fieldRoles;
private ClientBuilder $clientBuilder;

public function __construct(
Context $context,
JsonFactory $jsonFactory,
StoreManagerInterface $storeManager,
CommunicationConfig $communicationConfig,
CredentialsFactory $credentialsFactory,
FieldRoles $fieldRoles,
ClientBuilder $clientBuilder
private readonly JsonFactory $jsonFactory,
private readonly StoreManagerInterface $storeManager,
private readonly CommunicationConfig $communicationConfig,
private readonly AuthConfig $authConfig,
private readonly FieldRoles $fieldRoles,
private readonly ClientBuilder $clientBuilder
) {
parent::__construct($context);
$this->jsonResultFactory = $jsonFactory;
$this->storeManager = $storeManager;
$this->communicationConfig = $communicationConfig;
$this->credentialsFactory = $credentialsFactory;
$this->fieldRoles = $fieldRoles;
$this->clientBuilder = $clientBuilder;
}

public function execute()
{
$result = $this->jsonResultFactory->create();
$result = $this->jsonFactory->create();
try {
//@phpcs:ignore Magento2.Legacy.ObsoleteResponse.RedirectResponseMethodFound
preg_match('@/store/([0-9]+)/@', (string) $this->_redirect->getRefererUrl(), $match);
$storeId = (int) ($match[1] ?? $this->storeManager->getDefaultStoreView()->getId());
$client = $this->clientBuilder
->withCredentials($this->credentialsFactory->create())
->withApiKey($this->authConfig->getApiKey())
->withServerUrl($this->communicationConfig->getAddress());

$adapterFactory = new AdapterFactory(
Expand Down
31 changes: 9 additions & 22 deletions src/Controller/Adminhtml/TestConnection/TestConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,45 +8,32 @@
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\Phrase;
use Omikron\FactFinder\Communication\Client\ClientBuilder;
use Omikron\FactFinder\Communication\Credentials;
use Omikron\FactFinder\Communication\Resource\AdapterFactory;
use Omikron\FactFinder\Communication\Version;
use Omikron\Factfinder\Logger\FactFinderLogger;
use Omikron\Factfinder\Model\Api\CredentialsFactory;
use Omikron\Factfinder\Model\Config\AuthConfig;
use Psr\Http\Client\ClientExceptionInterface;

class TestConnection extends Action
{
private string $obscuredValue = '******';
private JsonFactory $jsonResultFactory;
private CredentialsFactory $credentialsFactory;
private AuthConfig $authConfig;
private ClientBuilder $clientBuilder;
private FactFinderLogger $logger;

public function __construct(
Action\Context $context,
JsonFactory $jsonResultFactory,
CredentialsFactory $credentialsFactory,
AuthConfig $authConfig,
ClientBuilder $clientBuilder,
FactFinderLogger $logger
private readonly JsonFactory $jsonResultFactory,
private readonly AuthConfig $authConfig,
private readonly ClientBuilder $clientBuilder,
private readonly FactFinderLogger $logger
) {
parent::__construct($context);
$this->jsonResultFactory = $jsonResultFactory;
$this->credentialsFactory = $credentialsFactory;
$this->authConfig = $authConfig;
$this->clientBuilder = $clientBuilder;
$this->logger = $logger;
}

public function execute()
{
try {
$request = $this->getRequest();
$clientBuilder = $this->clientBuilder
->withCredentials($this->getCredentials($this->getRequest()->getParams()))
->withApiKey($this->getApiKey($this->getRequest()->getParams()))
->withServerUrl($request->getParam('address'));

$adapterFactory = new AdapterFactory(
Expand All @@ -69,13 +56,13 @@ public function execute()
return $this->jsonResultFactory->create()->setData(['message' => $message]);
}

private function getCredentials(array $params): Credentials
private function getApiKey(array $params): string
{
// The password wasn't edited, load it from config
if (!isset($params['password']) || $params['password'] === $this->obscuredValue) {
$params['password'] = $this->authConfig->getPassword();
if (!isset($params['ff_api_key']) || $params['ff_api_key'] === $this->obscuredValue) {
$params['ff_api_key'] = $this->authConfig->getApiKey();
}

return $this->credentialsFactory->create($params);
return $params['ff_api_key'];
}
}
8 changes: 5 additions & 3 deletions src/Controller/Proxy/Call.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use Magento\Framework\Exception\NotFoundException;
use Omikron\FactFinder\Communication\Client\ClientBuilder;
use Omikron\Factfinder\Controller\SkipCsrfValidation;
use Omikron\Factfinder\Model\Api\CredentialsFactory;
use Omikron\Factfinder\Model\Config\AuthConfig;
use Omikron\Factfinder\Model\Config\CommunicationConfig;
use Psr\Http\Client\ClientExceptionInterface;
use Psr\Http\Message\ResponseInterface;
Expand All @@ -29,7 +29,7 @@ public function __construct(
private readonly JsonResultFactory $jsonResultFactory,
private readonly RawResultFactory $rawResultFactory,
private readonly CommunicationConfig $communicationConfig,
private readonly CredentialsFactory $credentialsFactory,
private readonly AuthConfig $authConfig,
private readonly ClientBuilder $clientBuilder
) {
parent::__construct($context);
Expand All @@ -47,12 +47,13 @@ public function execute()

try {
$client = $this->clientBuilder
->withCredentials($this->credentialsFactory->create())
->withApiKey($this->authConfig->getApiKey())
->withServerUrl($this->communicationConfig->getAddress())
->withVersion($this->communicationConfig->getVersion())
->build();

$method = $this->getRequest()->getMethod();

switch ($method) {
case 'GET':
$query = (string) parse_url($url, PHP_URL_QUERY); // phpcs:ignore
Expand All @@ -73,6 +74,7 @@ public function execute()
private function getEndpoint(string $currentUrl): string
{
preg_match('#/([A-Za-z]+\.ff|rest/v[^\?]*)#', $currentUrl, $match);

return $match[1] ?? '';
}

Expand Down
10 changes: 8 additions & 2 deletions src/Model/Config/AuthConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@

class AuthConfig
{
private const PATH_USERNAME = 'factfinder/general/username';
private const PATH_PASSWORD = 'factfinder/general/password';
private const PATH_USERNAME = 'factfinder/general/username';
private const PATH_PASSWORD = 'factfinder/general/password';
private const PATH_API_KEY = 'factfinder/general/ff_api_key';

private ScopeConfigInterface $scopeConfig;

Expand All @@ -28,4 +29,9 @@ public function getPassword(): string
{
return (string) $this->scopeConfig->getValue(self::PATH_PASSWORD, Scope::SCOPE_STORE);
}

public function getApiKey(): string
{
return (string) $this->scopeConfig->getValue(self::PATH_API_KEY, Scope::SCOPE_STORE);
}
}
6 changes: 3 additions & 3 deletions src/Model/Ssr/SearchAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Omikron\FactFinder\Communication\Client\ClientBuilder;
use Omikron\FactFinder\Communication\Client\ClientException;
use Omikron\Factfinder\Model\Api\CredentialsFactory;
use Omikron\Factfinder\Model\Config\AuthConfig;
use Omikron\Factfinder\Model\Config\CommunicationConfig;
use Psr\Http\Message\ResponseInterface;

Expand All @@ -15,7 +15,7 @@ class SearchAdapter
public function __construct(
private readonly ClientBuilder $clientBuilder,
private readonly CommunicationConfig $communicationConfig,
private readonly CredentialsFactory $credentialsFactory,
private readonly AuthConfig $authConfig,
private readonly PriceFormatter $priceFormatter,
) {
}
Expand All @@ -24,7 +24,7 @@ public function search(string $paramString, bool $navigationRequest): array
{
$client = $this->clientBuilder
->withServerUrl($this->communicationConfig->getAddress())
->withCredentials($this->credentialsFactory->create())
->withApiKey($this->authConfig->getApiKey())
->withVersion($this->communicationConfig->getVersion())
->build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
use Magento\Framework\Controller\Result\JsonFactory;
use Omikron\FactFinder\Communication\Client\ClientBuilder;
use Omikron\FactFinder\Communication\Client\ClientInterface;
use Omikron\FactFinder\Communication\Credentials;
use Omikron\Factfinder\Logger\FactFinderLogger;
use Omikron\Factfinder\Model\Api\CredentialsFactory;
use Omikron\Factfinder\Model\Config\AuthConfig;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -48,21 +46,19 @@ public function test_prevent_errors_without_post_data()

protected function setUp(): void
{
$credentialsFactory = $this->createConfiguredMock(CredentialsFactory::class, ['create' => $this->createMock(Credentials::class)]);
$this->request = $this->createMock(RequestInterface::class);
$body = $this->createConfiguredMock(StreamInterface::class, ['getContents' => '{"status":"200"}']);
$clientMock = $this->createConfiguredMock(ClientInterface::class, ['request' => $this->createConfiguredMock(ResponseInterface::class, ['getBody' => $body])]);
$this->builderMock = $this->createMock(ClientBuilder::class);

$this->builderMock->method('withVersion')->willReturn($this->builderMock);
$this->builderMock->method('withServerUrl')->willReturn($this->builderMock);
$this->builderMock->method('withCredentials')->willReturn($this->builderMock);
$this->builderMock->method('withApiKey')->willReturn($this->builderMock);
$this->builderMock->method('build')->willReturn($clientMock);

$this->controller = new TestConnection(
$this->createConfiguredMock(Context::class, ['getRequest' => $this->request]),
$this->createConfiguredMock(JsonFactory::class, ['create' => $this->createMock(JsonResult::class)]),
$credentialsFactory,
$this->createMock(AuthConfig::class),
$this->builderMock,
$this->createMock(FactFinderLogger::class)
Expand Down
15 changes: 0 additions & 15 deletions src/etc/adminhtml/system/general.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,6 @@
<label>Channel</label>
<validate>required-entry</validate>
</field>
<!-- <field id="version" translate="label comment" type="select" sortOrder="12" showInDefault="1" showInWebsite="1" showInStore="1">-->
<!-- <label>FACT-Finder version</label>-->
<!-- <options>-->
<!-- <option label="NG">ng</option>-->
<!-- <option label="7.3">7.3</option>-->
<!-- <option label="7.2">7.2</option>-->
<!-- </options>-->
<!-- </field>-->
<!-- <field id="ff_api_version" translate="label comment" type="select" sortOrder="13" showInDefault="1" showInWebsite="1" showInStore="1">-->
<!-- <label>FACT-Finder Api version</label>-->
<!-- <options>-->
<!-- <option label="v4">v4</option>-->
<!-- <option label="v5">v5</option>-->
<!-- </options>-->
<!-- </field>-->
<field id="username" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Username</label>
<validate>required-entry</validate>
Expand Down

0 comments on commit caa7264

Please sign in to comment.