Skip to content

Commit

Permalink
Merge pull request #32 from vippsas/publication-2.4.4
Browse files Browse the repository at this point in the history
Publication 2.4.4
  • Loading branch information
voleye authored Nov 11, 2021
2 parents b821241 + 80f7c4f commit c632ec7
Show file tree
Hide file tree
Showing 5 changed files with 253 additions and 6 deletions.
60 changes: 60 additions & 0 deletions Api/ModuleMetadataInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
/**
* Copyright 2021 Vipps
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
* TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/

namespace Vipps\Login\Api;

/**
* Interface MetadataInterface.
*
* @package Vipps\Login\Api
*/
interface ModuleMetadataInterface
{
/**
* The name of the module for the optional Vipps HTTP headers.
*
* @var string
*/
const MODULE_NAME = 'vipps-magento-login';

/**
* Get system name, magento in out case.
*
* @return string
*/
public function getSystemName(): string;

/**
* Get the system version (eg. 2.3.0, 2.2.1).
*
* @return string
*/
public function getSystemVersion(): string;

/**
* Get the name of the current module (`vipps-magento-login`).
*
* @return string
*/
public function getModuleName(): string;

/**
* Get the version of the current module (`x.x.x`).
*
* @return string
*/
public function getModuleVersion(): string;
}
24 changes: 19 additions & 5 deletions Gateway/Command/UserInfoCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\HTTP\ClientFactory;
use Magento\Framework\Serialize\SerializerInterface;
use Vipps\Login\Api\ApiEndpointsInterface;
use Vipps\Login\Api\Data\UserInfoInterface;
use Vipps\Login\Api\Data\UserInfoInterfaceFactory;
use Vipps\Login\Api\ApiEndpointsInterface;
use Vipps\Login\Api\ModuleMetadataInterface;
use Vipps\Login\Model\TokenProviderInterface;

/**
Expand Down Expand Up @@ -62,7 +63,11 @@ class UserInfoCommand
* @var array
*/
private $cache = [];

/**
* @var ModuleMetadataInterface
*/
private $moduleMetadata;

/**
* UserInfoCommand constructor.
*
Expand All @@ -71,19 +76,22 @@ class UserInfoCommand
* @param UserInfoInterfaceFactory $userInfoFactory
* @param ApiEndpointsInterface $apiEndpoints
* @param TokenProviderInterface $tokenPayloadProvider
* @param ModuleMetadataInterface $moduleMetadata
*/
public function __construct(
SerializerInterface $serializer,
ClientFactory $httpClientFactory,
UserInfoInterfaceFactory $userInfoFactory,
ApiEndpointsInterface $apiEndpoints,
TokenProviderInterface $tokenPayloadProvider
TokenProviderInterface $tokenPayloadProvider,
ModuleMetadataInterface $moduleMetadata
) {
$this->serializer = $serializer;
$this->httpClientFactory = $httpClientFactory;
$this->userInfoFactory = $userInfoFactory;
$this->apiEndpoints = $apiEndpoints;
$this->tokenPayloadProvider = $tokenPayloadProvider;
$this->moduleMetadata = $moduleMetadata;
}

/**
Expand All @@ -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();
Expand All @@ -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);
Expand Down
171 changes: 171 additions & 0 deletions Model/ModuleMetadata.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
<?php
/**
* Copyright 2020 Vipps
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
* TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
declare(strict_types=1);

namespace Vipps\Login\Model;

use Magento\Framework\App\CacheInterface;
use Magento\Framework\App\Config;
use Magento\Framework\App\ProductMetadataInterface;
use Magento\Framework\Component\ComponentRegistrar;
use Magento\Framework\Component\ComponentRegistrarInterface;
use Magento\Framework\Filesystem\Directory\ReadFactory;
use Magento\Framework\Serialize\SerializerInterface;
use Psr\Log\LoggerInterface;
use Vipps\Login\Api\ModuleMetadataInterface;

/**
* Class ModuleMetadata
* @package Vipps\Login\Model
*/
class ModuleMetadata implements ModuleMetadataInterface
{
/**
* Module version cache key
*
* @var string
*/
const VERSION_CACHE_KEY = 'vipps-magento-login';

/**
* Product version
*
* @var string
*/
private $version;

/**
* @var ComponentRegistrar
*/
private $componentRegistrar;

/**
* @var ReadFactory
*/
private $readFactory;

/**
* ProductMetadataInterface
*/
private $systemMetadata;

/**
* @var CacheInterface
*/
private $cache;

/**
* @var SerializerInterface
*/
private $serializer;

/**
* @var LoggerInterface
*/
private $logger;

/**
* Metadata constructor.
*
* @param ResourceInterface $resource
* @param ProductMetadataInterface $systemMetadata
* @param CacheInterface $cache
*/
public function __construct(
ComponentRegistrarInterface $componentRegistrar,
ReadFactory $readFactory,
SerializerInterface $serializer,
ProductMetadataInterface $systemMetadata,
CacheInterface $cache,
LoggerInterface $logger
) {
$this->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;
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
~ IN THE SOFTWARE.
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Vipps\Login\Api\ModuleMetadataInterface" type="Vipps\Login\Model\ModuleMetadata"/>

<preference for="Vipps\Login\Api\Data\UserInfoInterface" type="Vipps\Login\Model\Data\UserInfo"/>

<preference for="Vipps\Login\Api\VippsCustomerRepositoryInterface" type="Vipps\Login\Model\ResourceModel\VippsCustomerRepository"/>
Expand Down

0 comments on commit c632ec7

Please sign in to comment.