From 55b10df441dd863b03cda325451b44a00068cb4a Mon Sep 17 00:00:00 2001 From: Robert Saternus Date: Thu, 18 Jul 2024 13:40:22 +0200 Subject: [PATCH] FFWEB-3114: Introduce API key authorisation Introduce API key authorisation --- spec/Subscriber/ConfigurationSubscriberSpec.php | 3 +++ src/Config/Communication.php | 5 +++++ src/Resources/config/config.xml | 7 +++++++ src/Resources/views/storefront/base.html.twig | 2 +- src/Subscriber/ConfigurationSubscriber.php | 1 + 5 files changed, 17 insertions(+), 1 deletion(-) diff --git a/spec/Subscriber/ConfigurationSubscriberSpec.php b/spec/Subscriber/ConfigurationSubscriberSpec.php index 52c4cc38..4a9ed432 100644 --- a/spec/Subscriber/ConfigurationSubscriberSpec.php +++ b/spec/Subscriber/ConfigurationSubscriberSpec.php @@ -64,6 +64,7 @@ public function it_will_return_factfinderchannel_for_specific_sales_channel_id( $salesChannel->getId()->willReturn('main_sales_channel'); $communication->getVersion()->willReturn(Version::NG); $communication->getApiVersion()->willReturn('v5'); + $communication->getApiKey()->willReturn('abc-123'); $communication->isSsrActive()->willReturn(false); $communication->isProxyEnabled()->willReturn(false); $communication->getChannel('main_sales_channel')->willReturn('some_ff_channel'); @@ -109,6 +110,7 @@ public function it_will_add_page_extension_for_storefront_render_event( $communication->getFieldRoles(Argument::any())->willReturn([]); $communication->getVersion()->willReturn(Version::NG); $communication->getApiVersion()->willReturn('v5'); + $communication->getApiKey()->willReturn('abc-123'); $communication->isSsrActive()->willReturn(false); $communication->isProxyEnabled()->willReturn(false); $event->getRequest()->willReturn($request); @@ -150,6 +152,7 @@ public function it_will_throw_exception_when_event_does_not_have_page( $communication->getFieldRoles(Argument::any())->willReturn([]); $communication->getVersion()->willReturn(Version::NG); $communication->getApiVersion()->willReturn('v5'); + $communication->getApiKey()->willReturn('abc-123'); $communication->isSsrActive()->willReturn(false); $communication->isProxyEnabled()->willReturn(false); $event->getRequest()->willReturn($request); diff --git a/src/Config/Communication.php b/src/Config/Communication.php index b42a790c..ca07d9eb 100644 --- a/src/Config/Communication.php +++ b/src/Config/Communication.php @@ -26,6 +26,11 @@ public function getCredentials(): array ]; } + public function getApiKey(): string + { + return (string) $this->config('apiKey'); + } + public function isSsrActive(): bool { return (bool) $this->config('useSsr'); diff --git a/src/Resources/config/config.xml b/src/Resources/config/config.xml index 33df5b11..0a22c02f 100644 --- a/src/Resources/config/config.xml +++ b/src/Resources/config/config.xml @@ -34,6 +34,13 @@ true + + apiKey + + + true + + apiVersion diff --git a/src/Resources/views/storefront/base.html.twig b/src/Resources/views/storefront/base.html.twig index 5570e866..1601cf4c 100644 --- a/src/Resources/views/storefront/base.html.twig +++ b/src/Resources/views/storefront/base.html.twig @@ -13,7 +13,7 @@ ff: { url: `{{page.extensions.factfinder.communication.url}}`, channel: `{{page.extensions.factfinder.communication.channel}}`, - apiKey: `ffng-kwU8a6Sv.aCt0sDZbBLqTerkuuHLG8fhstllFXF`, + apiKey: `{{page.extensions.factfinder.communication.apiKey}}`, }, appConfig: { debug: true, diff --git a/src/Subscriber/ConfigurationSubscriber.php b/src/Subscriber/ConfigurationSubscriber.php index c022a821..b1c3fcaa 100755 --- a/src/Subscriber/ConfigurationSubscriber.php +++ b/src/Subscriber/ConfigurationSubscriber.php @@ -55,6 +55,7 @@ public function onPageLoaded(ShopwareSalesChannelEvent $event): void $communication = [ 'url' => $this->getServerUrl(), 'channel' => $this->config->getChannel($salesChannelId), + 'apiKey' => $this->config->getApiKey(), 'version' => $this->config->getVersion(), 'api' => $this->config->getApiVersion(), 'currencyCode' => $event->getSalesChannelContext()->getCurrency()->getIsoCode(),