From 4d153e5da788477a00fb4d261367cfe858bd59f1 Mon Sep 17 00:00:00 2001 From: Volodymyr Klymenko Date: Thu, 11 Nov 2021 17:16:10 +0200 Subject: [PATCH 1/2] VIPPS-397: Add HTTP headers to Vipps Login plugin --- Api/ModuleMetadataInterface.php | 60 ++++++++++ Gateway/Command/UserInfoCommand.php | 24 +++- Model/ModuleMetadata.php | 171 ++++++++++++++++++++++++++++ etc/di.xml | 2 + 4 files changed, 252 insertions(+), 5 deletions(-) create mode 100644 Api/ModuleMetadataInterface.php create mode 100644 Model/ModuleMetadata.php diff --git a/Api/ModuleMetadataInterface.php b/Api/ModuleMetadataInterface.php new file mode 100644 index 0000000..ebe1eeb --- /dev/null +++ b/Api/ModuleMetadataInterface.php @@ -0,0 +1,60 @@ +serializer = $serializer; $this->httpClientFactory = $httpClientFactory; $this->userInfoFactory = $userInfoFactory; $this->apiEndpoints = $apiEndpoints; $this->tokenPayloadProvider = $tokenPayloadProvider; + $this->moduleMetadata = $moduleMetadata; } /** @@ -102,6 +110,12 @@ public function execute($accessToken) $httpClient = $this->httpClientFactory->create(); $httpClient->addHeader('Authorization', 'Bearer ' . $accessToken); + + $httpClient->addHeader('Vipps-System-Name', $this->moduleMetadata->getSystemName()); + $httpClient->addHeader('Vipps-System-Version', $this->moduleMetadata->getSystemVersion()); + $httpClient->addHeader('Vipps-System-Plugin-Name', $this->moduleMetadata->getModuleName()); + $httpClient->addHeader('Vipps-System-Plugin-Version', $this->moduleMetadata->getModuleVersion()); + $httpClient->get($this->apiEndpoints->getUserInfoEndpoint()); $status = $httpClient->getStatus(); @@ -119,12 +133,12 @@ public function execute($accessToken) if (400 <= $status && 500 > $status) { switch ($status) { case 401: - $message = $body['error_description'] + $message = isset($body['error_description']) ? __($body['error_description']) : __('%1 Unauthorized', $status); throw new AuthorizationException($message, null, $status); default: - $message = $body['error_description'] + $message = isset($body['error_description']) ? __($body['error_description']) : __('%1 Bad Request', $status); throw new LocalizedException($message, null, $status); diff --git a/Model/ModuleMetadata.php b/Model/ModuleMetadata.php new file mode 100644 index 0000000..f672024 --- /dev/null +++ b/Model/ModuleMetadata.php @@ -0,0 +1,171 @@ +componentRegistrar = $componentRegistrar; + $this->readFactory = $readFactory; + $this->systemMetadata = $systemMetadata; + $this->cache = $cache; + $this->serializer = $serializer; + $this->logger = $logger; + } + + /** + * Get system name, magento in out case. + * + * @return string + */ + public function getSystemName(): string + { + return sprintf( + '%s 2 %s', + $this->systemMetadata->getName(), + $this->systemMetadata->getEdition() + ); + } + + /** + * Get the system version (eg. 2.3.0, 2.2.1). + * + * @return string + */ + public function getSystemVersion(): string + { + return (string) $this->systemMetadata->getVersion(); + } + + /** + * Get the name of the current module (`vipps-magento-login`). + * + * @return string + */ + public function getModuleName(): string + { + return self::MODULE_NAME; + } + + /** + * Get the version of the current module (`x.x.x`). + * + * @return string + */ + public function getModuleVersion(): string + { + if ($this->version) { + return (string) $this->version; + } + + $this->version = (string) $this->cache->load(self::VERSION_CACHE_KEY); + if ($this->version) { + return $this->version; + } + + $path = $this->componentRegistrar->getPath( + ComponentRegistrar::MODULE, + 'Vipps_Login' + ); + + try { + $directoryRead = $this->readFactory->create($path); + $composerJsonData = $directoryRead->readFile('composer.json'); + $data = $this->serializer->unserialize($composerJsonData); + $this->version = $data['version'] ?? 'UNKNOWN'; + } catch (\Throwable $t) { + $this->logger->error($t); + $this->version = 'UNKNOWN'; + } + $this->cache->save($this->version, self::VERSION_CACHE_KEY, [Config::CACHE_TAG]); + + return $this->version; + } +} diff --git a/etc/di.xml b/etc/di.xml index 53223c4..8b82fb3 100644 --- a/etc/di.xml +++ b/etc/di.xml @@ -14,6 +14,8 @@ ~ IN THE SOFTWARE. --> + + From 80f7c4f5616b99e888f4eaf64474c61fb91d5f3d Mon Sep 17 00:00:00 2001 From: Volodymyr Klymenko Date: Thu, 11 Nov 2021 17:17:18 +0200 Subject: [PATCH 2/2] Bump module version --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 9b04544..092c40d 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,7 @@ "psr/log": "~1.0" }, "type": "magento2-module", - "version": "2.4.3", + "version": "2.4.4", "license": [ "OSL-3.0", "AFL-3.0"