Subida del módulo y tema de PrestaShop

This commit is contained in:
Kaloyan
2026-04-09 18:31:51 +02:00
parent 12c253296f
commit 16b3ff9424
39262 changed files with 7418797 additions and 0 deletions

View File

@@ -0,0 +1,85 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License version 3.0
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*/
namespace PrestaShop\Module\PsAccounts\Account;
use DateTime;
use PrestaShop\Module\PsAccounts\Http\Resource\Resource;
use PrestaShop\Module\PsAccounts\Service\Accounts\Resource\ShopStatus;
class CachedShopStatus extends Resource
{
/**
* @var ShopStatus
*/
public $shopStatus;
/**
* @var bool
*/
public $isValid = false;
/**
* @var DateTime|null
*/
public $updatedAt;
// /**
// * @var string[]
// */
// protected $required = [
// 'shopStatus',
// ];
public function __construct($values = [])
{
$this->castDateTime($values, [
'updatedAt',
]);
$this->castBool($values, [
'isValid',
]);
$this->castChildResource($values, ShopStatus::class, [
'shopStatus',
]);
parent::__construct($values);
}
/**
* @param bool $all
*
* @return array
*/
public function toArray($all = true)
{
$array = parent::toArray($all);
$this->uncastChildResource($array, ShopStatus::class, [
'shopStatus',
], $all);
$this->uncastDateTime($array, [
'updatedAt',
]);
return $array;
}
}

View File

@@ -0,0 +1,38 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License version 3.0
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*/
namespace PrestaShop\Module\PsAccounts\Account\Command;
class CleanupIdentityCommand
{
/**
* @var int|null
*/
public $shopId;
/**
* @param int|null $shopId
*/
public function __construct($shopId = null)
{
$this->shopId = $shopId ?: \Shop::getContextShopID(true);
}
}

View File

@@ -0,0 +1,34 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License version 3.0
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*/
namespace PrestaShop\Module\PsAccounts\Account\Command;
use PrestaShop\Module\PsAccounts\Traits\WithOriginAndSourceTrait;
class CreateIdentitiesCommand
{
use withOriginAndSourceTrait;
public function __construct()
{
$this->resetProperties();
}
}

View File

@@ -0,0 +1,51 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License version 3.0
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*/
namespace PrestaShop\Module\PsAccounts\Account\Command;
use PrestaShop\Module\PsAccounts\Traits\WithOriginAndSourceTrait;
class CreateIdentityCommand
{
use WithOriginAndSourceTrait;
/**
* @var int|null
*/
public $shopId;
/**
* @var bool
*/
public $renew;
/**
* @param int|null $shopId
* @param bool $renew
*/
public function __construct($shopId, $renew = false
) {
$this->shopId = $shopId;
$this->renew = $renew;
$this->resetProperties();
}
}

View File

@@ -0,0 +1,53 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License version 3.0
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*/
namespace PrestaShop\Module\PsAccounts\Account\Command;
use PrestaShop\Module\PsAccounts\Service\OAuth2\Resource\AccessToken;
use PrestaShop\Module\PsAccounts\Service\OAuth2\Resource\UserInfo;
use PrestaShop\Module\PsAccounts\Traits\WithOriginAndSourceTrait;
class IdentifyContactCommand
{
use WithOriginAndSourceTrait;
/**
* @var AccessToken
*/
public $accessToken;
/**
* @var UserInfo
*/
public $userInfo;
/**
* @param AccessToken $accessToken
* @param UserInfo $userInfo
*/
public function __construct($accessToken, $userInfo)
{
$this->accessToken = $accessToken;
$this->userInfo = $userInfo;
$this->resetProperties();
}
}

View File

@@ -0,0 +1,34 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License version 3.0
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*/
namespace PrestaShop\Module\PsAccounts\Account\Command;
use PrestaShop\Module\PsAccounts\Traits\WithOriginAndSourceTrait;
class MigrateOrCreateIdentitiesV8Command
{
use WithOriginAndSourceTrait;
public function __construct()
{
$this->resetProperties();
}
}

View File

@@ -0,0 +1,44 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License version 3.0
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*/
namespace PrestaShop\Module\PsAccounts\Account\Command;
use PrestaShop\Module\PsAccounts\Traits\WithOriginAndSourceTrait;
class MigrateOrCreateIdentityV8Command
{
use WithOriginAndSourceTrait;
/**
* @var int|null
*/
public $shopId;
/**
* @param int|null $shopId
*/
public function __construct($shopId)
{
$this->shopId = $shopId;
$this->resetProperties();
}
}

View File

@@ -0,0 +1,90 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License version 3.0
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*/
namespace PrestaShop\Module\PsAccounts\Account\Command;
use PrestaShop\Module\PsAccounts\Traits\WithOriginAndSourceTrait;
class RestoreIdentityCommand
{
use WithOriginAndSourceTrait;
/**
* @var int|null
*/
public $shopId;
/**
* @var string
*/
public $cloudShopId;
/**
* @var string
*/
public $clientId;
/**
* @var string
*/
public $clientSecret;
/**
* @var bool
*/
public $verify;
/**
* @var bool
*/
public $migrate;
/**
* @var string|null
*/
public $migrateFrom;
/**
* @param string $cloudShopId
* @param string $clientId
* @param string $clientSecret
* @param bool $verify
* @param bool $migrate
* @param string $migrateFrom
*/
public function __construct(
$cloudShopId,
$clientId,
$clientSecret,
$verify = false,
$migrate = false,
$migrateFrom = null
) {
$this->cloudShopId = $cloudShopId;
$this->clientId = $clientId;
$this->clientSecret = $clientSecret;
$this->verify = $verify;
$this->migrate = $migrate;
$this->migrateFrom = $migrateFrom;
$this->resetProperties();
}
}

View File

@@ -0,0 +1,38 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License version 3.0
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*/
namespace PrestaShop\Module\PsAccounts\Account\Command;
class UpdateBackOfficeUrlCommand
{
/**
* @var int
*/
public $shopId;
/**
* @param int $shopId
*/
public function __construct($shopId)
{
$this->shopId = $shopId;
}
}

View File

@@ -0,0 +1,26 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License version 3.0
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*/
namespace PrestaShop\Module\PsAccounts\Account\Command;
class UpdateBackOfficeUrlsCommand
{
}

View File

@@ -0,0 +1,34 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License version 3.0
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*/
namespace PrestaShop\Module\PsAccounts\Account\Command;
use PrestaShop\Module\PsAccounts\Traits\WithOriginAndSourceTrait;
class VerifyIdentitiesCommand
{
use WithOriginAndSourceTrait;
public function __construct()
{
$this->resetProperties();
}
}

View File

@@ -0,0 +1,51 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License version 3.0
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*/
namespace PrestaShop\Module\PsAccounts\Account\Command;
use PrestaShop\Module\PsAccounts\Traits\WithOriginAndSourceTrait;
class VerifyIdentityCommand
{
use WithOriginAndSourceTrait;
/**
* @var int|null
*/
public $shopId;
/**
* @var bool
*/
public $force;
/**
* @param int|null $shopId
* @param bool $force
*/
public function __construct($shopId, $force = false)
{
$this->shopId = $shopId;
$this->force = $force;
$this->resetProperties();
}
}

View File

@@ -0,0 +1,11 @@
<?php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header("Location: ../");
exit;

View File

@@ -0,0 +1,42 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License version 3.0
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*/
namespace PrestaShop\Module\PsAccounts\Account\CommandHandler;
use PrestaShop\Module\PsAccounts\Account\Command\CleanupIdentityCommand;
class CleanupIdentityHandler
{
/**
* @param CleanupIdentityCommand $command
*
* @return void
*/
public function handle(CleanupIdentityCommand $command)
{
$id_shop = $command->shopId;
\Db::getInstance()->execute(
'DELETE FROM `' . _DB_PREFIX_ . bqSQL('configuration') . '`'
. ' WHERE (name like \'PS_ACCOUNTS%\' or name = \'PSX_UUID_V4\' or name = \'PS_CHECKOUT_SHOP_UUID_V4\')'
. ' AND ' . ($id_shop ? 'id_shop = ' . (int) $id_shop : 'id_shop IS NULL')
);
}
}

View File

@@ -0,0 +1,52 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License version 3.0
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*/
namespace PrestaShop\Module\PsAccounts\Account\CommandHandler;
use PrestaShop\Module\PsAccounts\Account\Command\CreateIdentitiesCommand;
use PrestaShop\Module\PsAccounts\Account\Command\CreateIdentityCommand;
use PrestaShop\Module\PsAccounts\Account\Exception\RefreshTokenException;
use PrestaShop\Module\PsAccounts\Log\Logger;
use PrestaShop\Module\PsAccounts\Service\Accounts\AccountsException;
class CreateIdentitiesHandler extends MultiShopHandler
{
/**
* @param CreateIdentitiesCommand $command
*
* @return void
*/
public function handle(CreateIdentitiesCommand $command)
{
$this->handleMulti(function ($multiShopId) use ($command) {
try {
$this->commandBus->handle(
(new CreateIdentityCommand($multiShopId, false))
->withOrigin($command->origin)
->withSource($command->source)
);
} catch (RefreshTokenException $e) {
Logger::getInstance()->error($e->getMessage());
} catch (AccountsException $e) {
Logger::getInstance()->error($e->getMessage());
}
});
}
}

View File

@@ -0,0 +1,136 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License version 3.0
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*/
namespace PrestaShop\Module\PsAccounts\Account\CommandHandler;
use PrestaShop\Module\PsAccounts\Account\Command\CreateIdentityCommand;
use PrestaShop\Module\PsAccounts\Account\Command\VerifyIdentityCommand;
use PrestaShop\Module\PsAccounts\Account\Exception\RefreshTokenException;
use PrestaShop\Module\PsAccounts\Account\Exception\UnknownStatusException;
use PrestaShop\Module\PsAccounts\Account\StatusManager;
use PrestaShop\Module\PsAccounts\Cqrs\CommandBus;
use PrestaShop\Module\PsAccounts\Provider\ShopProvider;
use PrestaShop\Module\PsAccounts\Service\Accounts\AccountsException;
use PrestaShop\Module\PsAccounts\Service\Accounts\AccountsService;
use PrestaShop\Module\PsAccounts\Service\OAuth2\OAuth2Client;
class CreateIdentityHandler
{
/**
* @var AccountsService
*/
private $accountsService;
/**
* @var OAuth2Client
*/
private $oAuth2Client;
/**
* @var ShopProvider
*/
private $shopProvider;
/**
* @var StatusManager
*/
private $statusManager;
/**
* @var CommandBus
*/
private $commandBus;
/**
* @param AccountsService $accountsService
* @param ShopProvider $shopProvider
* @param OAuth2Client $oauth2Client
* @param StatusManager $shopStatus
* @param CommandBus $commandBus
*/
public function __construct(
AccountsService $accountsService,
ShopProvider $shopProvider,
OAuth2Client $oauth2Client,
StatusManager $shopStatus,
CommandBus $commandBus
) {
$this->accountsService = $accountsService;
$this->shopProvider = $shopProvider;
$this->oAuth2Client = $oauth2Client;
$this->statusManager = $shopStatus;
$this->commandBus = $commandBus;
}
/**
* @param CreateIdentityCommand $command
*
* @return void
*
* @throws RefreshTokenException
* @throws UnknownStatusException
* @throws AccountsException
*/
public function handle(CreateIdentityCommand $command)
{
if ($command->renew || !$this->isAlreadyCreated()) {
$shopId = $command->shopId;
$identityCreated = $this->accountsService
->withSource($command->source)
->withOrigin($command->origin)
->createShopIdentity(
$this->shopProvider->getUrl($shopId),
$this->shopProvider->getName($shopId)
);
$this->oAuth2Client->update(
$identityCreated->clientId,
$identityCreated->clientSecret
);
$this->statusManager->setCloudShopId($identityCreated->cloudShopId);
$this->statusManager->setIsVerified(false);
$this->statusManager->invalidateCache();
$this->commandBus->handle(
(new VerifyIdentityCommand($shopId, true))
->withOrigin($command->origin)
->withSource($command->source)
);
} else {
$this->commandBus->handle(
(new VerifyIdentityCommand($command->shopId))
->withOrigin($command->origin)
->withSource($command->source)
);
}
}
/**
* Idempotency check
*
* @return bool
*/
private function isAlreadyCreated()
{
// FIXME: define where this code belongs
return $this->oAuth2Client->exists() && $this->statusManager->identityCreated();
}
}

View File

@@ -0,0 +1,99 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License version 3.0
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*/
namespace PrestaShop\Module\PsAccounts\Account\CommandHandler;
use PrestaShop\Module\PsAccounts\Account\Command\IdentifyContactCommand;
use PrestaShop\Module\PsAccounts\Account\Session\Firebase\OwnerSession;
use PrestaShop\Module\PsAccounts\Account\Session\ShopSession;
use PrestaShop\Module\PsAccounts\Account\StatusManager;
use PrestaShop\Module\PsAccounts\Service\Accounts\AccountsException;
use PrestaShop\Module\PsAccounts\Service\Accounts\AccountsService;
class IdentifyContactHandler
{
/**
* @var AccountsService
*/
private $accountsService;
/**
* @var StatusManager
*/
private $statusManager;
/**
* @var ShopSession
*/
private $shopSession;
/**
* @var OwnerSession
*/
private $ownerSession;
/**
* @param AccountsService $accountsService
* @param StatusManager $statusManager
* @param ShopSession $shopSession
* @param OwnerSession $ownerSession
*/
public function __construct(
AccountsService $accountsService,
StatusManager $statusManager,
ShopSession $shopSession,
OwnerSession $ownerSession
) {
$this->accountsService = $accountsService;
$this->statusManager = $statusManager;
$this->shopSession = $shopSession;
$this->ownerSession = $ownerSession;
}
/**
* @param IdentifyContactCommand $command
*
* @return void
*
* @throws AccountsException
*/
public function handle(IdentifyContactCommand $command)
{
$status = $this->statusManager->withSource($command->source)->getStatus();
if (!$status->isVerified) {
return;
}
$this->accountsService
->withSource($command->source)
->setPointOfContact(
$this->statusManager->getCloudShopId(),
$this->shopSession->getValidToken(),
$command->accessToken->access_token
);
// cleanup user token
$this->ownerSession->cleanup();
// optimistic update cached status
$this->statusManager->setPointOfContactUuid($command->userInfo->sub);
$this->statusManager->setPointOfContactEmail($command->userInfo->email);
}
}

View File

@@ -0,0 +1,49 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License version 3.0
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*/
namespace PrestaShop\Module\PsAccounts\Account\CommandHandler;
use Exception;
use PrestaShop\Module\PsAccounts\Account\Command\MigrateOrCreateIdentitiesV8Command;
use PrestaShop\Module\PsAccounts\Account\Command\MigrateOrCreateIdentityV8Command;
use PrestaShop\Module\PsAccounts\Log\Logger;
class MigrateOrCreateIdentitiesV8Handler extends MultiShopHandler
{
/**
* @param MigrateOrCreateIdentitiesV8Command $command
*
* @return void
*/
public function handle(MigrateOrCreateIdentitiesV8Command $command)
{
$this->handleMulti(function ($multiShopId) use ($command) {
try {
$this->commandBus->handle(
(new MigrateOrCreateIdentityV8Command($multiShopId))
->withOrigin($command->origin)
->withSource($command->source)
);
} catch (Exception $e) {
Logger::getInstance()->error($e->getMessage());
}
});
}
}

View File

@@ -0,0 +1,274 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License version 3.0
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*/
namespace PrestaShop\Module\PsAccounts\Account\CommandHandler;
use InvalidArgumentException;
use PrestaShop\Module\PsAccounts\Account\Command\CreateIdentityCommand;
use PrestaShop\Module\PsAccounts\Account\Command\MigrateOrCreateIdentityV8Command;
use PrestaShop\Module\PsAccounts\Account\Exception\RefreshTokenException;
use PrestaShop\Module\PsAccounts\Account\Exception\UnknownStatusException;
use PrestaShop\Module\PsAccounts\Account\ProofManager;
use PrestaShop\Module\PsAccounts\Account\StatusManager;
use PrestaShop\Module\PsAccounts\Cqrs\CommandBus;
use PrestaShop\Module\PsAccounts\Provider\ShopProvider;
use PrestaShop\Module\PsAccounts\Repository\ConfigurationRepository;
use PrestaShop\Module\PsAccounts\Service\Accounts\AccountsException;
use PrestaShop\Module\PsAccounts\Service\Accounts\AccountsService;
use PrestaShop\Module\PsAccounts\Service\Accounts\Exception\StoreLegacyNotFoundException;
use PrestaShop\Module\PsAccounts\Service\OAuth2\OAuth2Exception;
use PrestaShop\Module\PsAccounts\Service\OAuth2\OAuth2Service;
use PrestaShop\Module\PsAccounts\Service\UpgradeService;
class MigrateOrCreateIdentityV8Handler
{
/**
* @var AccountsService
*/
private $accountsService;
/**
* @var OAuth2Service
*/
protected $oAuth2Service;
/**
* @var ShopProvider
*/
private $shopProvider;
/**
* @var StatusManager
*/
private $statusManager;
/**
* @var ProofManager
*/
protected $proofManager;
/**
* @var ConfigurationRepository
*/
private $configurationRepository;
/**
* @var CommandBus
*/
private $commandBus;
/**
* @var UpgradeService
*/
private $upgradeService;
/**
* @param AccountsService $accountsService
* @param OAuth2Service $oAuth2Service
* @param ShopProvider $shopProvider
* @param StatusManager $shopStatus
* @param ProofManager $proofManager
* @param ConfigurationRepository $configurationRepository
* @param CommandBus $commandBus
* @param UpgradeService $upgradeService
*/
public function __construct(
AccountsService $accountsService,
OAuth2Service $oAuth2Service,
ShopProvider $shopProvider,
StatusManager $shopStatus,
ProofManager $proofManager,
ConfigurationRepository $configurationRepository,
CommandBus $commandBus,
UpgradeService $upgradeService
) {
$this->accountsService = $accountsService;
$this->oAuth2Service = $oAuth2Service;
$this->shopProvider = $shopProvider;
$this->statusManager = $shopStatus;
$this->proofManager = $proofManager;
$this->configurationRepository = $configurationRepository;
$this->commandBus = $commandBus;
$this->upgradeService = $upgradeService;
}
/**
* @param MigrateOrCreateIdentityV8Command $command
*
* @return void
*
* @throws AccountsException
* @throws RefreshTokenException
* @throws UnknownStatusException
*/
public function handle(MigrateOrCreateIdentityV8Command $command)
{
$shopId = $command->shopId;
$shopUuid = $this->configurationRepository->getShopUuid();
// FIXME: command can hold that property depending on context
$fromVersion = $this->upgradeService->getRegisteredVersion();
$migratedToV8 = version_compare($fromVersion, '8', '>=');
$notIdentified = !$shopUuid;
// FIXME: shouldn't this condition be a specific flag
if ($notIdentified || $migratedToV8) {
$this->registerLatestVersion();
$this->createOrVerifyIdentity($command);
return;
}
try {
// Register cloudShopId locally
$this->statusManager->setCloudShopId($shopUuid);
$identityCreated = $this->accountsService
->withSource($command->source)
->migrateShopIdentity(
$shopUuid,
$this->getTokenV6OrV7($shopUuid),
$this->shopProvider->getUrl($shopId),
$this->shopProvider->getName($shopId),
$fromVersion,
$this->proofManager->generateProof()
);
if (!empty($identityCreated->clientId) &&
!empty($identityCreated->clientSecret)) {
$this->oAuth2Service->getOAuth2Client()->update(
$identityCreated->clientId,
$identityCreated->clientSecret
);
}
$this->clearTokens();
$this->statusManager->invalidateCache();
$this->registerLatestVersion();
} catch (StoreLegacyNotFoundException $e) {
if ($command->origin !== AccountsService::ORIGIN_ADVANCED_SETTINGS) {
$this->registerLatestVersion();
$this->cleanupIdentity();
$this->createOrVerifyIdentity($command);
return;
} else {
throw $e;
}
}
}
/**
* @param string $shopUuid
*
* @return string
*
* @throws OAuth2Exception
*/
private function getAccessTokenV7($shopUuid)
{
return $this->oAuth2Service->getAccessTokenByClientCredentials([], [
// audience v7
'shop_' . $shopUuid,
])->access_token;
}
/**
* @param string $shopUuid
*
* @return string
*
* @throws AccountsException
* @throws InvalidArgumentException
*/
private function getFirebaseTokenV6($shopUuid)
{
return $this->accountsService->refreshShopToken(
$this->configurationRepository->getFirebaseRefreshToken(),
$shopUuid
)->token;
}
/**
* @param string $shopUuid
*
* @return string
*
* @throws AccountsException
*/
private function getTokenV6OrV7($shopUuid)
{
try {
return $this->getAccessTokenV7($shopUuid);
} catch (OAuth2Exception $e) {
return $this->getFirebaseTokenV6($shopUuid);
}
}
/**
* @return void
*/
private function cleanupIdentity()
{
// Will trigger reset banner
//$this->upgradeService->setVersion('');
$this->statusManager->clearStatus();
$this->oAuth2Service->getOAuth2Client()->delete();
$this->clearTokens();
}
/**
* Create Or Verify Or Do Nothing
*
* @param MigrateOrCreateIdentityV8Command $command
*
* @return void
*
* @throws RefreshTokenException
* @throws UnknownStatusException
* @throws AccountsException
*/
private function createOrVerifyIdentity(MigrateOrCreateIdentityV8Command $command)
{
$this->commandBus->handle(
(new CreateIdentityCommand($command->shopId, false))
->withOrigin($command->origin)
->withSource($command->source)
);
}
/**
* @return void
*/
private function clearTokens()
{
$this->configurationRepository->updateAccessToken('');
$this->configurationRepository->updateFirebaseIdAndRefreshTokens('', '');
$this->configurationRepository->updateUserFirebaseIdAndRefreshToken('', '');
}
/**
* @return void
*/
private function registerLatestVersion()
{
$this->upgradeService->setVersion();
}
}

View File

@@ -0,0 +1,71 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License version 3.0
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*/
namespace PrestaShop\Module\PsAccounts\Account\CommandHandler;
use PrestaShop\Module\PsAccounts\Context\ShopContext;
use PrestaShop\Module\PsAccounts\Cqrs\CommandBus;
abstract class MultiShopHandler
{
/**
* @var ShopContext
*/
protected $shopContext;
/**
* @var CommandBus
*/
protected $commandBus;
public function __construct(
ShopContext $shopContext,
CommandBus $commandBus
) {
$this->shopContext = $shopContext;
$this->commandBus = $commandBus;
}
/**
* @param \Closure $handler
*
* @return void
*/
protected function handleMulti($handler)
{
foreach ($this->getShopIds() as $multiShopId) {
$this->shopContext->execInShopContext($multiShopId, function () use ($handler, $multiShopId) {
$handler($multiShopId);
});
}
}
/**
* @return int[]
*/
protected function getShopIds()
{
if ($this->shopContext->isMultishopActive()) {
return $this->shopContext->getMultiShopIds();
}
//return [\Shop::getContextShopID(true)];
return [\Shop::getContextShopID()];
}
}

View File

@@ -0,0 +1,158 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License version 3.0
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*/
namespace PrestaShop\Module\PsAccounts\Account\CommandHandler;
use Exception;
use PrestaShop\Module\PsAccounts\Account\Command\MigrateOrCreateIdentityV8Command;
use PrestaShop\Module\PsAccounts\Account\Command\RestoreIdentityCommand;
use PrestaShop\Module\PsAccounts\Account\Command\VerifyIdentityCommand;
use PrestaShop\Module\PsAccounts\Account\Exception\UnknownStatusException;
use PrestaShop\Module\PsAccounts\Account\StatusManager;
use PrestaShop\Module\PsAccounts\Cqrs\CommandBus;
use PrestaShop\Module\PsAccounts\Log\Logger;
use PrestaShop\Module\PsAccounts\Service\Accounts\Resource\ShopStatus;
use PrestaShop\Module\PsAccounts\Service\OAuth2\OAuth2Client;
use PrestaShop\Module\PsAccounts\Service\UpgradeService;
use Throwable;
class RestoreIdentityHandler
{
/**
* @var OAuth2Client
*/
private $oAuth2Client;
/**
* @var StatusManager
*/
private $statusManager;
/**
* @var UpgradeService
*/
private $upgradeService;
/**
* @var CommandBus
*/
private $commandBus;
/**
* @param OAuth2Client $oauth2Client
* @param StatusManager $shopStatus
* @param UpgradeService $upgradeService
* @param CommandBus $commandBus
*/
public function __construct(
OAuth2Client $oauth2Client,
StatusManager $shopStatus,
UpgradeService $upgradeService,
CommandBus $commandBus
) {
$this->oAuth2Client = $oauth2Client;
$this->statusManager = $shopStatus;
$this->upgradeService = $upgradeService;
$this->commandBus = $commandBus;
}
/**
* @param RestoreIdentityCommand $command
*
* @return void
*
* @throws Exception|Throwable
*/
public function handle(RestoreIdentityCommand $command)
{
try {
$currentStatus = $this->statusManager->getStatus(true);
} catch (UnknownStatusException $e) {
$currentStatus = null;
}
$registeredVersion = $this->upgradeService->getRegisteredVersion();
$shopId = $command->shopId ?: \Shop::getContextShopID();
try {
$this->statusManager->clearStatus();
// Update OAuth client
$this->oAuth2Client->update(
$command->clientId,
$command->clientSecret ?: $this->oAuth2Client->getClientSecret()
);
// Update cloudShopId
$this->statusManager->setCloudShopId($command->cloudShopId);
// Fix version number when not set
$this->upgradeService->setVersion();
if ($command->migrate) {
// this will trigger migration
$this->upgradeService->setVersion($command->migrateFrom);
}
$this->commandBus->handle(
// FIXME: $cloudShopId (should not be necessary to read it from db)
(new MigrateOrCreateIdentityV8Command($shopId))
->withOrigin($command->origin)
->withSource($command->source)
);
if ($command->verify) {
// force verify
$this->commandBus->handle(
(new VerifyIdentityCommand($shopId, true))
->withOrigin($command->origin)
->withSource($command->source)
);
}
//$this->statusManager->invalidateCache();
} catch (Exception $e) {
$this->handleError($currentStatus, $registeredVersion, $e);
} catch (Throwable $e) {
$this->handleError($currentStatus, $registeredVersion, $e);
}
}
/**
* @param ShopStatus|null $currentStatus
* @param string $registeredVersion
* @param Exception|Throwable $e
*
* @return void
*
* @throws Exception|Throwable
*/
private function handleError($currentStatus, $registeredVersion, $e)
{
if (isset($currentStatus)) {
$this->statusManager->restoreStatus($currentStatus);
}
$this->upgradeService->setVersion($registeredVersion);
Logger::getInstance()->error($e);
throw $e;
}
}

View File

@@ -0,0 +1,109 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License version 3.0
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*/
namespace PrestaShop\Module\PsAccounts\Account\CommandHandler;
use PrestaShop\Module\PsAccounts\Account\Command\UpdateBackOfficeUrlCommand;
use PrestaShop\Module\PsAccounts\Account\Session\ShopSession;
use PrestaShop\Module\PsAccounts\Account\ShopUrl;
use PrestaShop\Module\PsAccounts\Account\StatusManager;
use PrestaShop\Module\PsAccounts\Log\Logger;
use PrestaShop\Module\PsAccounts\Provider\ShopProvider;
use PrestaShop\Module\PsAccounts\Repository\ConfigurationRepository;
use PrestaShop\Module\PsAccounts\Service\Accounts\AccountsService;
class UpdateBackOfficeUrlHandler
{
/**
* @var AccountsService
*/
private $accountsService;
/**
* @var StatusManager
*/
private $statusManager;
/**
* @var ShopProvider
*/
private $shopProvider;
/**
* @var ShopSession
*/
private $shopSession;
/**
* @var ConfigurationRepository
*/
private $configurationRepository;
/**
* @param AccountsService $accountsService
* @param StatusManager $statusManager
* @param ShopProvider $shopProvider
* @param ShopSession $shopSession
* @param ConfigurationRepository $configurationRepository
*/
public function __construct(
AccountsService $accountsService,
StatusManager $statusManager,
ShopProvider $shopProvider,
ShopSession $shopSession,
ConfigurationRepository $configurationRepository
) {
$this->accountsService = $accountsService;
$this->statusManager = $statusManager;
$this->shopProvider = $shopProvider;
$this->shopSession = $shopSession;
$this->configurationRepository = $configurationRepository;
}
/**
* @param UpdateBackOfficeUrlCommand $command
*
* @return void
*/
public function handle(UpdateBackOfficeUrlCommand $command)
{
// On disconnected context with single shop mode, the contextual shop id is not defined.
$shopId = $command->shopId ?: $this->configurationRepository->getMainShopId();
// TODO: rework parameters priority
$status = $this->statusManager->withSource('ps_accounts')->getStatus();
$cloudShopUrl = ShopUrl::createFromStatus($status, $shopId);
$localShopUrl = $this->shopProvider->getUrl($shopId);
try {
// Check if BO url changed and urls aren't empty
if (!$cloudShopUrl->backOfficeUrlEquals($localShopUrl)) {
$this->accountsService->updateBackOfficeUrl(
$status->cloudShopId,
$this->shopSession->getValidToken(),
$localShopUrl
);
}
} catch (\InvalidArgumentException $e) {
Logger::getInstance()->error($e->getMessage());
}
}
}

View File

@@ -0,0 +1,49 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License version 3.0
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*/
namespace PrestaShop\Module\PsAccounts\Account\CommandHandler;
use Exception;
use PrestaShop\Module\PsAccounts\Account\Command\UpdateBackOfficeUrlCommand;
use PrestaShop\Module\PsAccounts\Account\Command\UpdateBackOfficeUrlsCommand;
use PrestaShop\Module\PsAccounts\Log\Logger;
use Throwable;
class UpdateBackOfficeUrlsHandler extends MultiShopHandler
{
/**
* @param UpdateBackOfficeUrlsCommand $command
*
* @return void
*/
public function handle(UpdateBackOfficeUrlsCommand $command)
{
$this->handleMulti(function ($multiShopId) {
try {
$updateBackOfficeUrlCommand = new UpdateBackOfficeUrlCommand($multiShopId);
$this->commandBus->handle($updateBackOfficeUrlCommand);
} catch (Exception $e) {
Logger::getInstance()->error($e->getMessage());
} catch (Throwable $e) {
Logger::getInstance()->error($e->getMessage());
}
});
}
}

View File

@@ -0,0 +1,55 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License version 3.0
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*/
namespace PrestaShop\Module\PsAccounts\Account\CommandHandler;
use PrestaShop\Module\PsAccounts\Account\Command\VerifyIdentitiesCommand;
use PrestaShop\Module\PsAccounts\Account\Command\VerifyIdentityCommand;
use PrestaShop\Module\PsAccounts\Account\Exception\RefreshTokenException;
use PrestaShop\Module\PsAccounts\Account\Exception\UnknownStatusException;
use PrestaShop\Module\PsAccounts\Log\Logger;
use PrestaShop\Module\PsAccounts\Service\Accounts\AccountsException;
class VerifyIdentitiesHandler extends MultiShopHandler
{
/**
* @param VerifyIdentitiesCommand $command
*
* @return void
*/
public function handle(VerifyIdentitiesCommand $command)
{
$this->handleMulti(function ($multiShopId) use ($command) {
try {
$this->commandBus->handle(
(new VerifyIdentityCommand($multiShopId, false))
->withOrigin($command->origin)
->withSource($command->source)
);
} catch (RefreshTokenException $e) {
Logger::getInstance()->error($e->getMessage());
} catch (AccountsException $e) {
Logger::getInstance()->error($e->getMessage());
} catch (UnknownStatusException $e) {
Logger::getInstance()->error($e->getMessage());
}
});
}
}

View File

@@ -0,0 +1,124 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License version 3.0
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*/
namespace PrestaShop\Module\PsAccounts\Account\CommandHandler;
use PrestaShop\Module\PsAccounts\Account\Command\VerifyIdentityCommand;
use PrestaShop\Module\PsAccounts\Account\Exception\RefreshTokenException;
use PrestaShop\Module\PsAccounts\Account\Exception\UnknownStatusException;
use PrestaShop\Module\PsAccounts\Account\ProofManager;
use PrestaShop\Module\PsAccounts\Account\Session\ShopSession;
use PrestaShop\Module\PsAccounts\Account\ShopUrl;
use PrestaShop\Module\PsAccounts\Account\StatusManager;
use PrestaShop\Module\PsAccounts\Log\Logger;
use PrestaShop\Module\PsAccounts\Provider\ShopProvider;
use PrestaShop\Module\PsAccounts\Service\Accounts\AccountsException;
use PrestaShop\Module\PsAccounts\Service\Accounts\AccountsService;
class VerifyIdentityHandler
{
/**
* @var AccountsService
*/
private $accountsService;
/**
* @var StatusManager
*/
private $statusManager;
/**
* @var ShopProvider
*/
private $shopProvider;
/**
* @var ShopSession
*/
private $shopSession;
/**
* @var ProofManager
*/
private $proofManager;
/**
* @param AccountsService $accountsService
* @param ShopProvider $shopProvider
* @param StatusManager $statusManager
* @param ShopSession $shopSession
* @param ProofManager $proofManager
*/
public function __construct(
AccountsService $accountsService,
ShopProvider $shopProvider,
StatusManager $statusManager,
ShopSession $shopSession,
ProofManager $proofManager
) {
$this->accountsService = $accountsService;
$this->shopProvider = $shopProvider;
$this->statusManager = $statusManager;
$this->shopSession = $shopSession;
$this->proofManager = $proofManager;
}
/**
* @param VerifyIdentityCommand $command
*
* @return void
*
* @throws RefreshTokenException
* @throws UnknownStatusException
* @throws AccountsException
*/
public function handle(VerifyIdentityCommand $command)
{
$shopId = $command->shopId;
$status = $this->statusManager->withSource($command->source)->getStatus();
$cloudShopUrl = ShopUrl::createFromStatus($status, $shopId);
if (!$command->force && $status->isVerified) {
return;
}
try {
if (!$command->force && !$cloudShopUrl->frontendUrlEquals($this->shopProvider->getUrl($shopId))) {
return;
}
} catch (\InvalidArgumentException $e) {
Logger::getInstance()->error($e->getMessage());
return;
}
$this->accountsService
->withOrigin($command->origin)
->withSource($command->source)
->verifyShopIdentity(
$this->statusManager->getCloudShopId(),
$this->shopSession->getValidToken(),
$this->shopProvider->getUrl($shopId),
$this->shopProvider->getName($shopId),
$this->proofManager->generateProof()
);
$this->statusManager->invalidateCache();
}
}

View File

@@ -0,0 +1,11 @@
<?php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header("Location: ../");
exit;

View File

@@ -0,0 +1,75 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License version 3.0
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*/
namespace PrestaShop\Module\PsAccounts\Account\Dto;
use PrestaShop\Module\PsAccounts\Type\Dto;
class Shop extends Dto
{
/** @var int */
public $id;
/** @var string */
public $name;
/** @var string */
public $domain;
/** @var string */
public $domainSsl;
/** @var string */
public $physicalUri;
/** @var string */
public $virtualUri;
/** @var string */
public $frontUrl;
/** @var string */
public $uuid;
/** @var string */
public $publicKey;
/** @var string */
public $employeeId;
/** @var User */
public $user;
/** @var string */
public $url;
/** @var bool */
public $isLinkedV4;
/** @var bool */
public $unlinkedAuto;
public function __construct($values = [])
{
if (isset($values['user']) && is_array($values['user'])) {
$values['user'] = new User($values['user']);
} else {
$values['user'] = new User();
}
parent::__construct($values);
}
/**
* @return array|mixed
*/
public function jsonSerialize()
{
return array_merge(parent::jsonSerialize(), [
'user' => $this->user->jsonSerialize(),
]);
}
}

View File

@@ -0,0 +1,74 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License version 3.0
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*/
namespace PrestaShop\Module\PsAccounts\Account\Dto;
use PrestaShop\Module\PsAccounts\Type\Dto;
class UpdateShop extends Dto
{
/**
* @var string
*/
public $shopId;
/**
* @var string
*/
public $name;
/**
* @var string
*/
public $virtualUri;
/**
* @var string
*/
public $physicalUri;
/**
* @var string
*/
public $domain;
/**
* @var string
*/
public $sslDomain;
/**
* @var string
*/
public $boBaseUrl;
/**
* @var string[]
*/
public $required = [
'shopId',
'name',
'virtualUri',
'physicalUri',
'domain',
'sslDomain',
'boBaseUrl',
];
}

View File

@@ -0,0 +1,33 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License version 3.0
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*/
namespace PrestaShop\Module\PsAccounts\Account\Dto;
use PrestaShop\Module\PsAccounts\Type\Dto;
class User extends Dto
{
/** @var string */
public $email;
/** @var bool */
public $emailIsValidated;
/** @var string */
public $uuid;
}

View File

@@ -0,0 +1,11 @@
<?php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header("Location: ../");
exit;

View File

@@ -0,0 +1,25 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License version 3.0
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*/
namespace PrestaShop\Module\PsAccounts\Account\Exception;
class RefreshTokenException extends \PrestaShop\Module\PsAccounts\Exception\RefreshTokenException
{
}

View File

@@ -0,0 +1,25 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License version 3.0
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*/
namespace PrestaShop\Module\PsAccounts\Account\Exception;
class UnknownStatusException extends \Exception
{
}

View File

@@ -0,0 +1,11 @@
<?php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header("Location: ../");
exit;

View File

@@ -0,0 +1,70 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License version 3.0
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*/
namespace PrestaShop\Module\PsAccounts\Account;
use PrestaShop\Module\PsAccounts\Repository\ConfigurationRepository;
class ProofManager
{
/**
* @var ConfigurationRepository
*/
private $configuration;
/**
* ManageProof constructor.
*
* @param ConfigurationRepository $configuration
*/
public function __construct(
ConfigurationRepository $configuration
) {
$this->configuration = $configuration;
}
/**
* @return string
*/
public function generateProof()
{
$proof = bin2hex(openssl_random_pseudo_bytes(32));
$this->configuration->updateShopProof($proof);
return $proof;
}
/**
* @return string|null
*/
public function getProof()
{
return $this->configuration->getShopProof();
}
/**
* @return void
*/
public function deleteProof()
{
$this->configuration->updateShopProof(null);
}
}

View File

@@ -0,0 +1,59 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License version 3.0
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*/
namespace PrestaShop\Module\PsAccounts\Account\Query;
class GetContextQuery
{
/**
* @var string|null
*/
public $source;
/**
* @var int
*/
public $contextType;
/**
* @var int|null
*/
public $contextId;
/**
* @var bool
*/
public $refresh;
/**
* @param string|null $source
* @param int|null $contextType
* @param int|null $contextId
* @param bool $refresh
*/
public function __construct($source = null, $contextType = \Shop::CONTEXT_ALL, $contextId = null, $refresh = false)
{
$this->source = $source;
$this->contextType = $contextType;
$this->contextId = $contextId;
$this->refresh = $refresh;
}
}

View File

@@ -0,0 +1,11 @@
<?php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header("Location: ../");
exit;

View File

@@ -0,0 +1,73 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License version 3.0
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*/
namespace PrestaShop\Module\PsAccounts\Account\QueryHandler;
use PrestaShop\Module\PsAccounts\Account\Query\GetContextQuery;
use PrestaShop\Module\PsAccounts\Provider\ShopProvider;
use PrestaShop\Module\PsAccounts\Service\Accounts\AccountsException;
use PrestaShop\Module\PsAccounts\Service\UpgradeService;
class GetContextHandler
{
/**
* @var ShopProvider
*/
private $shopProvider;
/**
* @var UpgradeService
*/
private $upgradeService;
/**
* @param ShopProvider $shopProvider
*/
public function __construct(
ShopProvider $shopProvider,
UpgradeService $upgradeService
) {
$this->shopProvider = $shopProvider;
$this->upgradeService = $upgradeService;
}
/**
* @param GetContextQuery $query
*
* @return array
*
* @throws AccountsException
*/
public function handle(GetContextQuery $query)
{
return [
'ps_accounts' => [
'last_succeeded_upgrade_version' => $this->upgradeService->getVersion(),
'module_version_from_files' => \Ps_accounts::VERSION,
],
'groups' => $this->shopProvider->getShops(
$query->source,
$query->contextType,
$query->contextId,
$query->refresh
),
];
}
}

View File

@@ -0,0 +1,11 @@
<?php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header("Location: ../");
exit;

View File

@@ -0,0 +1,131 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License version 3.0
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*/
namespace PrestaShop\Module\PsAccounts\Account\Session\Firebase;
use PrestaShop\Module\PsAccounts\Account\Exception\RefreshTokenException;
use PrestaShop\Module\PsAccounts\Account\Session\Firebase;
use PrestaShop\Module\PsAccounts\Account\Session\Session;
use PrestaShop\Module\PsAccounts\Account\Session\SessionInterface;
use PrestaShop\Module\PsAccounts\Account\Session\ShopSession;
use PrestaShop\Module\PsAccounts\Account\Token\Token;
use PrestaShop\Module\PsAccounts\Log\Logger;
use PrestaShop\Module\PsAccounts\Service\Accounts\AccountsException;
use PrestaShop\Module\PsAccounts\Service\Accounts\AccountsService;
abstract class FirebaseSession extends Session implements SessionInterface
{
/**
* @var ShopSession
*/
protected $shopSession;
public function __construct(ShopSession $shopSession)
{
parent::__construct();
$this->shopSession = $shopSession;
}
/**
* @return AccountsService
*/
public function getAccountsService()
{
return $this->module->getService(AccountsService::class);
}
/**
* @return Firebase\OwnerSession
*/
public function getOwnerSession()
{
return $this->module->getService(Firebase\OwnerSession::class);
}
/**
* @return Firebase\ShopSession
*/
public function getShopSession()
{
return $this->module->getService(Firebase\ShopSession::class);
}
/**
* @param string $refreshToken
* @param array $scope
* @param array $audience
*
* @return Token
*
* @throws RefreshTokenException
*/
public function refreshToken($refreshToken = null, array $scope = [], array $audience = [])
{
try {
$token = $this->shopSession->getValidToken();
$cloudShopId = $this->getStatusManager()->getCloudShopId();
$this->refreshFirebaseTokens($cloudShopId, $token);
} catch (RefreshTokenException $e) {
Logger::getInstance()->error('Unable to get or refresh owner/shop token : ' . $e->getMessage());
throw $e;
}
return $this->getToken();
}
/**
* @param string $cloudShopId
* @param Token $token
*
* @return void
*
* @throws RefreshTokenException
*/
protected function refreshFirebaseTokens($cloudShopId, $token)
{
try {
$firebaseTokens = $this->getAccountsService()->firebaseTokens($cloudShopId, $token);
} catch (AccountsException $e) {
throw new RefreshTokenException($e->getMessage());
}
$shopToken = new Token(
$firebaseTokens->shop->token,
$firebaseTokens->shop->refreshToken
);
$pointOfContactToken = null;
if (isset($firebaseTokens->pointOfContact->token) && isset($firebaseTokens->pointOfContact->refreshToken)) {
$pointOfContactToken = new Token(
$firebaseTokens->pointOfContact->token,
$firebaseTokens->pointOfContact->refreshToken
);
}
// saving both tokens here
$this->getShopSession()->setToken((string) $shopToken->getJwt(), $shopToken->getRefreshToken());
if (isset($pointOfContactToken)) {
$this->getOwnerSession()->setToken((string) $pointOfContactToken->getJwt(), $pointOfContactToken->getRefreshToken());
}
}
}

View File

@@ -0,0 +1,76 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License version 3.0
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*/
namespace PrestaShop\Module\PsAccounts\Account\Session\Firebase;
use PrestaShop\Module\PsAccounts\Account\Session\SessionInterface;
use PrestaShop\Module\PsAccounts\Account\Token\Token;
use PrestaShop\Module\PsAccounts\Repository\ConfigurationRepository;
class OwnerSession extends FirebaseSession implements SessionInterface
{
/**
* @var ConfigurationRepository
*/
protected $configurationRepository;
/**
* @param ConfigurationRepository $configurationRepository
* @param \PrestaShop\Module\PsAccounts\Account\Session\ShopSession $shopSession
*/
public function __construct(
ConfigurationRepository $configurationRepository,
\PrestaShop\Module\PsAccounts\Account\Session\ShopSession $shopSession
) {
$this->configurationRepository = $configurationRepository;
parent::__construct($shopSession);
}
/**
* @return Token
*/
public function getToken()
{
return new Token(
$this->configurationRepository->getUserFirebaseIdToken(),
$this->configurationRepository->getUserFirebaseRefreshToken()
);
}
/**
* @return void
*/
public function cleanup()
{
$this->configurationRepository->updateUserFirebaseIdToken('');
}
/**
* @param string $token
* @param string $refreshToken
*
* @return void
*/
public function setToken($token, $refreshToken = null)
{
$this->configurationRepository->updateUserFirebaseIdAndRefreshToken($token, $refreshToken);
}
}

View File

@@ -0,0 +1,76 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License version 3.0
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*/
namespace PrestaShop\Module\PsAccounts\Account\Session\Firebase;
use PrestaShop\Module\PsAccounts\Account\Session\SessionInterface;
use PrestaShop\Module\PsAccounts\Account\Token\Token;
use PrestaShop\Module\PsAccounts\Repository\ConfigurationRepository;
class ShopSession extends FirebaseSession implements SessionInterface
{
/**
* @var ConfigurationRepository
*/
protected $configurationRepository;
/**
* @param ConfigurationRepository $configurationRepository
* @param \PrestaShop\Module\PsAccounts\Account\Session\ShopSession $shopSession
*/
public function __construct(
ConfigurationRepository $configurationRepository,
\PrestaShop\Module\PsAccounts\Account\Session\ShopSession $shopSession
) {
$this->configurationRepository = $configurationRepository;
parent::__construct($shopSession);
}
/**
* @return Token
*/
public function getToken()
{
return new Token(
$this->configurationRepository->getFirebaseIdToken(),
$this->configurationRepository->getFirebaseRefreshToken()
);
}
/**
* @return void
*/
public function cleanup()
{
$this->configurationRepository->updateFirebaseIdAndRefreshTokens('', '');
}
/**
* @param string $token
* @param string $refreshToken
*
* @return void
*/
public function setToken($token, $refreshToken = null)
{
$this->configurationRepository->updateFirebaseIdAndRefreshTokens($token, $refreshToken);
}
}

View File

@@ -0,0 +1,11 @@
<?php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header("Location: ../");
exit;

View File

@@ -0,0 +1,161 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License version 3.0
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*/
namespace PrestaShop\Module\PsAccounts\Account\Session;
use PrestaShop\Module\PsAccounts\Account\Exception\RefreshTokenException;
use PrestaShop\Module\PsAccounts\Account\StatusManager;
use PrestaShop\Module\PsAccounts\Account\Token\NullToken;
use PrestaShop\Module\PsAccounts\Account\Token\Token;
use PrestaShop\Module\PsAccounts\Log\Logger;
abstract class Session implements SessionInterface
{
/**
* @var array
*/
protected $refreshTokenErrors = [];
/**
* @var \Ps_accounts
*/
protected $module;
public function __construct()
{
/* @phpstan-ignore-next-line */
$this->module = \Module::getInstanceByName('ps_accounts');
}
/**
* @deprecated use getValidToken instead
*
* @param bool $forceRefresh
*
* @return Token
*/
public function getOrRefreshToken($forceRefresh = false)
{
return $this->getValidToken($forceRefresh, false);
}
/**
* @param bool $forceRefresh
* @param bool $throw
* @param array $scope
* @param array $audience
*
* @return Token
*
* @throws RefreshTokenException
*/
public function getValidToken($forceRefresh = false, $throw = true, array $scope = [], array $audience = [])
{
/*
* Avoid multiple refreshToken calls in the same runtime:
* if it fails once, it will subsequently fail
*/
if ($e = $this->getRefreshTokenError(static::class)) {
$this->setToken('');
if ($throw) {
throw $e;
}
return $this->getToken();
}
if (true === $forceRefresh || false === $this->getToken()->isValid($scope, $audience)) {
try {
$this->refreshToken(null, $scope, $audience);
} catch (RefreshTokenException $e) {
$this->setToken('');
$this->setRefreshTokenError(static::class, $e);
if ($throw) {
throw $e;
}
Logger::getInstance()->error($e->getMessage());
}
}
return $this->getToken();
}
/**
* @return bool
*
* @deprecated since v8.0.0
*/
public function isEmailVerified()
{
try {
$jwt = $this->getToken()->getJwt();
// FIXME : just query sso api and don't refresh token everytime
if (!$jwt instanceof NullToken &&
!$jwt->claims()->get('email_verified')
) {
$jwt = $this->getValidToken(true)->getJwt();
}
return (bool) $jwt->claims()->get('email_verified');
} catch (RefreshTokenException $e) {
return false;
}
}
/**
* @param string $className
*
* @return RefreshTokenException|false
*/
public function getRefreshTokenError($className)
{
return isset($this->refreshTokenErrors[$className]) ? $this->refreshTokenErrors[$className] : false;
}
/**
* @return void
*/
public function resetRefreshTokenErrors()
{
$this->refreshTokenErrors = [];
}
/**
* @param string $className
* @param RefreshTokenException $e
*
* @return void
*/
protected function setRefreshTokenError($className, RefreshTokenException $e)
{
$this->refreshTokenErrors[$className] = $e;
}
/**
* @return StatusManager
*/
protected function getStatusManager()
{
return $this->module->getService(StatusManager::class);
}
}

View File

@@ -0,0 +1,83 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License version 3.0
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*/
namespace PrestaShop\Module\PsAccounts\Account\Session;
use PrestaShop\Module\PsAccounts\Account\Exception\RefreshTokenException;
use PrestaShop\Module\PsAccounts\Account\Token\Token;
interface SessionInterface
{
/**
* @return Token
*/
public function getToken();
/**
* @param string $token
* @param string $refreshToken
*
* @return void
*/
public function setToken($token, $refreshToken = null);
/**
* Refreshes and saves refreshed token
*
* @param string|null $refreshToken
* @param array $scope
* @param array $audience
*
* @return Token
*
* @throws RefreshTokenException
*/
public function refreshToken($refreshToken = null, array $scope = [], array $audience = []);
/**
* @deprecated use getValidToken instead
*
* Get or refreshes and saves token
*
* @param bool $forceRefresh
*
* @return Token
*/
public function getOrRefreshToken($forceRefresh = false);
/**
* Get or refreshes and saves token
*
* @param bool $forceRefresh
* @param bool $throw
* @param array $scope
* @param array $audience
*
* @return Token
*
* @throws RefreshTokenException
*/
public function getValidToken($forceRefresh = false, $throw = true, array $scope = [], array $audience = []);
/**
* @return void
*/
public function cleanup();
}

View File

@@ -0,0 +1,196 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License version 3.0
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*/
namespace PrestaShop\Module\PsAccounts\Account\Session;
use PrestaShop\Module\PsAccounts\Account\Exception\RefreshTokenException;
use PrestaShop\Module\PsAccounts\Account\Token\Token;
use PrestaShop\Module\PsAccounts\Hook\ActionShopAccessTokenRefreshAfter;
use PrestaShop\Module\PsAccounts\Repository\ConfigurationRepository;
use PrestaShop\Module\PsAccounts\Service\OAuth2\Exception\InvalidScopeException;
use PrestaShop\Module\PsAccounts\Service\OAuth2\OAuth2Exception;
use PrestaShop\Module\PsAccounts\Service\OAuth2\OAuth2ServerException;
use PrestaShop\Module\PsAccounts\Service\OAuth2\OAuth2Service;
use PrestaShop\Module\PsAccounts\Service\OAuth2\Resource\AccessToken;
class ShopSession extends Session implements SessionInterface
{
/**
* @var ConfigurationRepository
*/
protected $configurationRepository;
/**
* @var OAuth2Service
*/
protected $oAuth2Service;
/**
* @var string
*/
protected $tokenAudience;
/**
* @param ConfigurationRepository $configurationRepository
* @param OAuth2Service $oAuth2Service
* @param string $tokenAudience
*/
public function __construct(
ConfigurationRepository $configurationRepository,
OAuth2Service $oAuth2Service,
$tokenAudience
) {
parent::__construct();
$this->configurationRepository = $configurationRepository;
$this->oAuth2Service = $oAuth2Service;
$this->tokenAudience = $tokenAudience;
}
/**
* @param bool $forceRefresh
* @param bool $throw
* @param array|null $scope
* @param array|null $audience
*
* @return Token
*
* @throws RefreshTokenException
*/
public function getValidToken($forceRefresh = false, $throw = true, array $scope = null, array $audience = null)
{
if ($scope === null) {
$scope = ($this->getStatusManager()->identityVerified() ? [
'shop.verified',
] : []);
}
if ($audience === null) {
$audience = [
'store/' . $this->getStatusManager()->getCloudShopId(),
$this->tokenAudience,
];
}
return parent::getValidToken($forceRefresh, $throw, $scope, $audience);
}
/**
* @param string $refreshToken
* @param array $scope
* @param array $audience
*
* @return Token
*
* @throws RefreshTokenException
*/
public function refreshToken($refreshToken = null, array $scope = [], array $audience = [])
{
try {
try {
$accessToken = $this->getAccessToken($scope, $audience);
} catch (InvalidScopeException $e) {
$accessToken = $this->fallbackRefresh($e, 'shop.verified', $scope, $audience);
}
$this->setToken(
$accessToken->access_token,
$accessToken->refresh_token
);
$token = $this->getToken();
\Hook::exec(ActionShopAccessTokenRefreshAfter::getName(), ['token' => $token]);
return $token;
} catch (OAuth2Exception $e) {
} catch (\Exception $e) {
} catch (\Throwable $e) {
}
throw new RefreshTokenException('Unable to refresh shop token : ' . $e->getMessage(), 0, $e);
}
/**
* @return Token
*/
public function getToken()
{
return new Token($this->configurationRepository->getAccessToken());
}
/**
* @param string $token
* @param string $refreshToken
*
* @return void
*/
public function setToken($token, $refreshToken = null)
{
$this->configurationRepository->updateAccessToken($token);
}
/**
* @return void
*/
public function cleanup()
{
$this->configurationRepository->updateAccessToken('');
}
/**
* @param array $scope
* @param array $audience
*
* @return AccessToken
*
* @throws OAuth2Exception
*/
protected function getAccessToken(array $scope = [], array $audience = [])
{
return $this->oAuth2Service->getAccessTokenByClientCredentials($scope, $audience);
}
/**
* @param OAuth2ServerException $e
* @param string $filterScope
* @param array $scope
* @param array $audience
*
* @return AccessToken
*
* @throws OAuth2Exception
*/
protected function fallbackRefresh($e, $filterScope, array $scope, array $audience)
{
if (in_array($filterScope, $scope) &&
str_contains($e->getMessage(), $filterScope)
) {
$this->getStatusManager()->setIsVerified(false);
$this->resetRefreshTokenErrors();
$accessToken = $this->getAccessToken(array_filter($scope, function ($scp) use ($filterScope) {
return $scp !== $filterScope;
}), $audience);
} else {
throw $e;
}
return $accessToken;
}
}

View File

@@ -0,0 +1,11 @@
<?php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header("Location: ../");
exit;

View File

@@ -0,0 +1,161 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License version 3.0
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*/
namespace PrestaShop\Module\PsAccounts\Account;
use PrestaShop\Module\PsAccounts\Service\Accounts\Resource\ShopStatus;
class ShopUrl
{
/**
* @var string
*/
private $backOfficeUrl;
/**
* @var string
*/
private $frontendUrl;
/**
* @var int
*/
private $multiShopId;
/**
* ShopUrl constructor.
*
* @param string $backOfficeUrl
* @param string $frontendUrl
* @param int $multiShopId
*/
public function __construct($backOfficeUrl, $frontendUrl, $multiShopId)
{
$this->backOfficeUrl = $backOfficeUrl;
$this->frontendUrl = $frontendUrl;
$this->multiShopId = $multiShopId;
}
/**
* @param array $shop
*
* @return ShopUrl
*/
public static function createFromShopData($shop)
{
$backOfficeUrl = explode('/index.php', $shop['url'])[0];
$frontendUrl = rtrim($shop['frontUrl'], '/');
$multiShopId = (int) $shop['id'];
return new ShopUrl($backOfficeUrl, $frontendUrl, $multiShopId);
}
/**
* @return string
*/
public function getBackOfficeUrl()
{
return $this->backOfficeUrl;
}
/**
* @return string
*/
public function getFrontendUrl()
{
return $this->frontendUrl;
}
/**
* @return int
*/
public function getMultiShopId()
{
return $this->multiShopId;
}
/**
* Check if the frontend URL has changed compared to the remote status
*
* @param ShopUrl $shopUrl
*
* @return bool
*
* @throws \InvalidArgumentException
*/
public function frontendUrlEquals(ShopUrl $shopUrl)
{
$cloudFrontendUrl = rtrim($this->frontendUrl, '/');
$localFrontendUrl = rtrim($shopUrl->getFrontendUrl(), '/');
if (empty($cloudFrontendUrl) || empty($localFrontendUrl)) {
throw new \InvalidArgumentException('Frontend URL cannot be empty');
}
return $cloudFrontendUrl === $localFrontendUrl;
}
/**
* Check if the backOffice URL has changed compared to the remote status
* Returns true if backOfficeUrl changed
*
* @param ShopUrl $shopUrl
*
* @return bool
*
* @throws \InvalidArgumentException
*/
public function backOfficeUrlEquals(ShopUrl $shopUrl)
{
$cloudBackOfficeUrl = rtrim($this->getBackOfficeUrl(), '/');
$localBackOfficeUrl = rtrim($shopUrl->getBackOfficeUrl(), '/');
if (empty($cloudBackOfficeUrl) || empty($localBackOfficeUrl)) {
throw new \InvalidArgumentException('BackOffice URL cannot be empty');
}
return $cloudBackOfficeUrl === $localBackOfficeUrl;
}
/**
* @return ShopUrl
*/
public function trimmed()
{
return new ShopUrl(
rtrim($this->getBackOfficeUrl(), '/'),
rtrim($this->getFrontendUrl(), '/'),
$this->getMultiShopId()
);
}
/**
* Create a new ShopUrl from the status
*
* @param ShopStatus $status
* @param int $multiShopId
*
* @return ShopUrl
*/
public static function createFromStatus(ShopStatus $status, $multiShopId)
{
return new ShopUrl($status->backOfficeUrl, $status->frontendUrl, $multiShopId);
}
}

View File

@@ -0,0 +1,408 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License version 3.0
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*/
namespace PrestaShop\Module\PsAccounts\Account;
use DateTime;
use PrestaShop\Module\PsAccounts\Account\Exception\RefreshTokenException;
use PrestaShop\Module\PsAccounts\Account\Exception\UnknownStatusException;
use PrestaShop\Module\PsAccounts\Account\Session\ShopSession;
use PrestaShop\Module\PsAccounts\Log\Logger;
use PrestaShop\Module\PsAccounts\Repository\ConfigurationRepository;
use PrestaShop\Module\PsAccounts\Service\Accounts\AccountsException;
use PrestaShop\Module\PsAccounts\Service\Accounts\AccountsService;
use PrestaShop\Module\PsAccounts\Service\Accounts\Resource\ShopStatus;
use PrestaShop\Module\PsAccounts\Traits\WithOriginAndSourceTrait;
/**
* @method $this withThrowException(bool $throwException)
* @method bool getThrowException(bool $restoreDefault = true)
* @method void resetThrowException()
*/
class StatusManager
{
use WithOriginAndSourceTrait {
getDefaults as WithOriginAndSourceTrait_getDefaults;
}
/**
* Status Cache TTL in seconds
*/
const CACHE_TTL = 30;
/**
* Infinite Status Cache
*/
const CACHE_TTL_INFINITE = -1;
/**
* @var ConfigurationRepository
*/
private $repository;
/**
* @var ShopSession
*/
private $shopSession;
/**
* @var AccountsService
*/
private $accountsService;
/**
* @var bool
*/
private $throwException;
/**
* @param ShopSession $shopSession
* @param AccountsService $accountsService
* @param ConfigurationRepository $repository
*/
public function __construct(
ShopSession $shopSession,
AccountsService $accountsService,
ConfigurationRepository $repository
) {
$this->repository = $repository;
$this->shopSession = $shopSession;
$this->accountsService = $accountsService;
$this->resetProperties();
}
/**
* @return array
*/
public function getDefaults()
{
return array_merge($this->WithOriginAndSourceTrait_getDefaults(), [
'throwException' => false,
]);
}
/**
* @return bool
*/
public function identityCreated()
{
return !empty($this->getCloudShopId());
}
/**
* @param bool $cachedStatus
*
* @return bool
*/
public function identityVerified($cachedStatus = true)
{
try {
return $this->getStatus($cachedStatus)->isVerified;
} catch (UnknownStatusException $e) {
return false;
}
}
/**
* @param bool $cachedOnly
* @param int $cacheTtl
*
* @return ShopStatus
*
* @throws AccountsException
* @throws RefreshTokenException
* @throws UnknownStatusException
*/
public function getStatus($cachedOnly = false, $cacheTtl = self::CACHE_TTL)
{
$handleException = function ($e) {
Logger::getInstance()->error($e->getMessage());
if ($this->getThrowException(false)) {
throw $e;
}
};
if (!$cachedOnly) {
try {
$cachedShopStatus = $this->getCachedStatus();
} catch (UnknownStatusException $e) {
$cachedShopStatus = null;
}
if (!$cachedShopStatus ||
$this->cacheInvalidated($cachedShopStatus) ||
$this->cacheExpired($cachedShopStatus, $cacheTtl)
) {
try {
$this->upsetCachedStatus(new CachedShopStatus([
'isValid' => true,
'updatedAt' => date('Y-m-d H:i:s'),
'shopStatus' => $this->accountsService
->withSource($this->getSource())
->shopStatus(
$this->getCloudShopId(),
$this->shopSession->getValidToken()
),
]));
} catch (AccountsException $e) {
$handleException($e);
} catch (RefreshTokenException $e) {
$handleException($e);
}
}
}
return $this->getCachedStatus()->shopStatus;
}
/**
* @return void
*/
public function invalidateCache()
{
$this->upsetCachedStatus(new CachedShopStatus([
'isValid' => false,
]));
}
/**
* @param CachedShopStatus $cachedStatus
*
* @return bool
*/
public function cacheInvalidated(CachedShopStatus $cachedStatus = null)
{
try {
$cachedStatus = $cachedStatus ?: $this->getCachedStatus();
$isValid = $cachedStatus->isValid;
} catch (UnknownStatusException $e) {
$isValid = false;
}
return !$isValid;
}
/**
* @param CachedShopStatus $cachedStatus
* @param int $cacheTtl
*
* @return bool
*/
public function cacheExpired(CachedShopStatus $cachedStatus = null, $cacheTtl = self::CACHE_TTL)
{
try {
//$dateUpd = $this->getCacheDateUpd();
$cachedStatus = $cachedStatus ?: $this->getCachedStatus();
$dateUpd = $cachedStatus->updatedAt;
return $dateUpd instanceof DateTime &&
$cacheTtl != self::CACHE_TTL_INFINITE &&
time() - $dateUpd->getTimestamp() >= $cacheTtl;
} catch (UnknownStatusException $e) {
return true;
}
}
// /**
// * @return \DateTime|null
// */
// public function getCacheDateUpd()
// {
// return $this->repository->getCachedShopStatusDateUpd();
// }
/**
* @param bool $cachedStatus
*
* @return string|null
*/
public function getCloudShopId($cachedStatus = true)
{
try {
return $this->getStatus($cachedStatus)->cloudShopId;
} catch (UnknownStatusException $e) {
return null;
}
}
/**
* @param string $cloudShopId
*
* @return void
*/
public function setCloudShopId($cloudShopId)
{
$this->upsetCachedStatus(new CachedShopStatus([
'shopStatus' => [
'cloudShopId' => $cloudShopId,
],
]));
}
/**
* @param bool $cachedStatus
*
* @return string|null
*/
public function getPointOfContactUuid($cachedStatus = true)
{
try {
return $this->getStatus($cachedStatus)->pointOfContactUuid;
} catch (UnknownStatusException $e) {
return null;
}
}
/**
* @param string $pointOfContactUuid
*
* @return void
*/
public function setPointOfContactUuid($pointOfContactUuid)
{
$this->upsetCachedStatus(new CachedShopStatus([
'shopStatus' => [
'pointOfContactUuid' => $pointOfContactUuid,
],
]));
}
/**
* @param bool $cachedStatus
*
* @return string|null
*/
public function getPointOfContactEmail($cachedStatus = true)
{
try {
return $this->getStatus($cachedStatus)->pointOfContactEmail;
} catch (UnknownStatusException $e) {
return null;
}
}
/**
* @param string $pointOfContactEmail
*
* @return void
*/
public function setPointOfContactEmail($pointOfContactEmail)
{
$this->upsetCachedStatus(new CachedShopStatus([
'shopStatus' => [
'pointOfContactEmail' => $pointOfContactEmail,
],
]));
}
/**
* @param bool $isVerified
*
* @return void
*/
public function setIsVerified($isVerified)
{
$this->upsetCachedStatus(new CachedShopStatus([
'shopStatus' => [
'isVerified' => (bool) $isVerified,
],
]));
}
/**
* @param ShopStatus $status
*
* @return void
*/
public function restoreStatus(ShopStatus $status)
{
$this->upsetCachedStatus(new CachedShopStatus([
'isValid' => true,
'updatedAt' => date('Y-m-d H:i:s'),
'shopStatus' => $status,
]));
}
/**
* @return void
*/
public function clearStatus()
{
$this->upsetCachedStatus(new CachedShopStatus([
'shopStatus' => [
'isVerified' => false,
'cloudShopId' => '',
'pointOfContactUuid' => '',
'pointOfContactEmail' => '',
'frontendUrl' => '',
'backOfficeUrl' => '',
'shopVerificationErrorCode' => '',
],
]));
$this->invalidateCache();
}
/**
* @return CachedShopStatus
*
* @throws UnknownStatusException
*/
protected function getCachedStatus()
{
$status = $this->repository->getCachedShopStatus();
if (!$status) {
throw new UnknownStatusException('Unknown status');
}
return new CachedShopStatus(json_decode($status, true));
}
/**
* @param CachedShopStatus $cachedShopStatus
*
* @return void
*/
protected function setCachedStatus(CachedShopStatus $cachedShopStatus)
{
$this->repository->updateCachedShopStatus(json_encode($cachedShopStatus->toArray()) ?: null);
$this->repository->updateShopUuid($cachedShopStatus->shopStatus->cloudShopId);
}
/**
* @param CachedShopStatus $cachedShopStatus
* @param bool $all all fields or only explicitly initialized fields
*
* @return void
*/
protected function upsetCachedStatus(CachedShopStatus $cachedShopStatus, $all = false)
{
try {
$this->setCachedStatus(new CachedShopStatus(array_replace_recursive(
$this->getCachedStatus()->toArray(),
$cachedShopStatus->toArray($all)
)));
} catch (UnknownStatusException $e) {
$this->setCachedStatus($cachedShopStatus);
}
}
}

View File

@@ -0,0 +1,32 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License version 3.0
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*/
namespace PrestaShop\Module\PsAccounts\Account\Token;
class NullToken extends \PrestaShop\Module\PsAccounts\Vendor\Lcobucci\JWT\Token
{
/**
* @return string
*/
public function toString()
{
return '';
}
}

View File

@@ -0,0 +1,192 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License version 3.0
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*/
namespace PrestaShop\Module\PsAccounts\Account\Token;
use PrestaShop\Module\PsAccounts\Log\Logger;
use PrestaShop\Module\PsAccounts\Vendor\Lcobucci\JWT\Parser;
use PrestaShop\Module\PsAccounts\Vendor\Lcobucci\JWT\Token\InvalidTokenStructure;
class Token
{
const ID_OWNER_CLAIM = 'sub';
/**
* @var string
*/
private $token;
/**
* @var string
*/
private $refreshToken;
/**
* @param string $token
* @param string $refreshToken
*/
public function __construct($token, $refreshToken = null)
{
$this->token = $token;
$this->refreshToken = $refreshToken;
}
/**
* @return NullToken|\PrestaShop\Module\PsAccounts\Vendor\Lcobucci\JWT\Token
*/
public function getJwt()
{
return $this->parseToken($this->token);
}
/**
* @return string
*/
public function getRefreshToken()
{
return $this->refreshToken;
}
/**
* @return bool
*/
public function isExpired()
{
$token = $this->getJwt();
return $token->isExpired(new \DateTime());
}
/**
* @param array $scope
*
* @return bool
*/
public function hasScope(array $scope)
{
if ($scope === []) {
return true;
}
$claims = $this->getJwt()->claims();
if (!$claims->has('scp')) {
return false;
}
$scp = $claims->get('scp');
return count(array_intersect($scope, $scp)) == count($scope);
}
/**
* @param array $audience
*
* @return bool
*/
public function hasAudience(array $audience)
{
if ($audience === []) {
return true;
}
$claims = $this->getJwt()->claims();
if (!$claims->has('aud')) {
return false;
}
$aud = $claims->get('aud');
return count(array_intersect($audience, $aud)) == count($audience);
}
/**
* @param array $scope
* @param array $audience
*
* @return bool
*/
public function isValid(array $scope, array $audience)
{
$isValid = true;
if ($this->isExpired()) {
Logger::getInstance()->info(__METHOD__ . ': token isExpired ');
$isValid = false;
}
if ($isValid && !$this->hasScope($scope)) {
Logger::getInstance()->info(__METHOD__ . ': token scope invalid ');
$isValid = false;
}
if ($isValid && !$this->hasAudience($audience)) {
Logger::getInstance()->info(__METHOD__ . ': token audience invalid ');
$isValid = false;
}
return $isValid;
}
/**
* @return string|null
*/
public function getUuid()
{
return $this->getJwt()->claims()->get(static::ID_OWNER_CLAIM);
}
/**
* @return string|null
*/
public function getEmail()
{
// return $this->configuration->getFirebaseEmail();
return $this->getJwt()->claims()->get('email');
}
/**
* @return string
*/
public function __toString()
{
return (string) $this->token;
}
/**
* @param string $token
*
* @return \PrestaShop\Module\PsAccounts\Vendor\Lcobucci\JWT\Token
*/
protected function parseToken($token)
{
try {
return (new Parser())->parse((string) $token);
} catch (InvalidTokenStructure $e) {
return $this->getNullToken();
}
}
/**
* @return \PrestaShop\Module\PsAccounts\Vendor\Lcobucci\JWT\Token
*/
protected function getNullToken()
{
//return new \Lcobucci\JWT\Token([], ['exp' => new \DateTime()]);
return new NullToken([], ['exp' => new \DateTime()]);
}
}

View File

@@ -0,0 +1,11 @@
<?php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header("Location: ../");
exit;

View File

@@ -0,0 +1,11 @@
<?php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header("Location: ../");
exit;