Skip to content

Commit

Permalink
Create API trait
Browse files Browse the repository at this point in the history
  • Loading branch information
Morgan Pichat committed Nov 8, 2023
1 parent 608c000 commit ab861ef
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 17 deletions.
@@ -0,0 +1,43 @@
<?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 Open Software License (OSL 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/OSL-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.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/

declare(strict_types=1);

namespace PrestaShopBundle\EventListener\Context\Admin;

use PrestaShopBundle\Controller\Api\OAuth2\AccessTokenController;
use Symfony\Component\HttpFoundation\Request;

trait ApiPlatformTrait
{
protected function isApiRequest(Request $request): bool
{
return in_array(
$request->attributes->get('_controller'),
[AccessTokenController::class, 'api_platform.action.placeholder']
);
}
}
Expand Up @@ -34,6 +34,8 @@

class CountryContextListener
{
use ApiPlatformTrait;

public function __construct(
private readonly CountryContextBuilder $countryContextBuilder,
private readonly ConfigurationInterface $configuration
Expand All @@ -42,7 +44,7 @@ public function __construct(

public function onKernelRequest(RequestEvent $event): void
{
if (!$event->isMainRequest()) {
if (!$event->isMainRequest() && $this->isApiRequest($event->getRequest())) {
return;
}

Expand Down
Expand Up @@ -34,6 +34,8 @@

class CurrencyContextListener
{
use ApiPlatformTrait;

public function __construct(
private readonly CurrencyContextBuilder $currencyContextBuilder,
private readonly ConfigurationInterface $configuration,
Expand All @@ -42,7 +44,7 @@ public function __construct(

public function onKernelRequest(RequestEvent $event): void
{
if (!$event->isMainRequest()) {
if (!$event->isMainRequest() && $this->isApiRequest($event->getRequest())) {
return;
}

Expand Down
Expand Up @@ -37,6 +37,8 @@
*/
class EmployeeContextListener
{
use ApiPlatformTrait;

public function __construct(
private readonly EmployeeContextBuilder $employeeContextBuilder,
private readonly LegacyContext $legacyContext
Expand All @@ -45,7 +47,7 @@ public function __construct(

public function onKernelRequest(RequestEvent $event): void
{
if (!$event->isMainRequest()) {
if (!$event->isMainRequest() && $this->isApiRequest($event->getRequest())) {
return;
}

Expand Down
Expand Up @@ -45,6 +45,8 @@
*/
class LegacyContextListener
{
use ApiPlatformTrait;

/**
* @param iterable|LegacyContextBuilderInterface[] $legacyBuilders
*/
Expand All @@ -55,7 +57,7 @@ public function __construct(

public function onKernelController(ControllerEvent $event): void
{
if (!$event->isMainRequest()) {
if (!$event->isMainRequest() && $this->isApiRequest($event->getRequest())) {
return;
}

Expand Down
Expand Up @@ -43,6 +43,8 @@
*/
class ShopContextListener
{
use ApiPlatformTrait;

public function __construct(
private readonly ShopContextBuilder $shopContextBuilder,
private readonly EmployeeContext $employeeContext,
Expand All @@ -58,7 +60,7 @@ public function onKernelRequest(RequestEvent $event): void
// either that or the listener itself should be configured in a way so that it only is used in BO context
// because in FO we don't handle shop context the same way (there can be only one shop and no shop context
// switching is possible)
if (!$event->isMainRequest()) {
if (!$event->isMainRequest() && $this->isApiRequest($event->getRequest())) {
return;
}

Expand Down
18 changes: 6 additions & 12 deletions src/PrestaShopBundle/Security/SslMiddleware.php
Expand Up @@ -29,7 +29,7 @@
namespace PrestaShopBundle\Security;

use PrestaShop\PrestaShop\Core\ConfigurationInterface;
use PrestaShopBundle\Controller\Api\OAuth2\AccessTokenController;
use PrestaShopBundle\EventListener\Context\Admin\ApiPlatformTrait;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
Expand All @@ -45,6 +45,8 @@
*/
class SslMiddleware
{
use ApiPlatformTrait;

private const AVAILABLE_SECURE_PROTOCOLS = ['tls/1.2', 'tls/1.3'];

/**
Expand Down Expand Up @@ -72,14 +74,14 @@ public function onKernelRequest(RequestEvent $event): void
}

//If It's an API call and not using https, redirect to https
if ($this->isApi($event->getRequest()) && !$event->getRequest()->isSecure()) {
if ($this->isApiRequest($event->getRequest()) && !$event->getRequest()->isSecure()) {
$this->redirectToSsl($event);

return;
}

//If It's an API call and not using TLS 1.2+, display error message
if ($this->isApi($event->getRequest())) {
if ($this->isApiRequest($event->getRequest())) {
$this->upgradeProtocol($event);

return;
Expand Down Expand Up @@ -108,21 +110,13 @@ private function isSSLrequirementsMet(Request $request): bool
if ($this->configuration->get('_PS_API_FORCE_TLS_VERSION_') === false) {
return true;
}
if ($this->isApi($request)) {
if ($this->isApiRequest($request)) {
return $this->isTLSVersionAccepted($request);
}

return $request->isSecure();
}

private function isApi(Request $request): bool
{
return in_array(
$request->attributes->get('_controller'),
[AccessTokenController::class, 'api_platform.action.placeholder']
);
}

private function isTLSVersionAccepted(Request $request): bool
{
// Probably using another webserver than Apache
Expand Down

0 comments on commit ab861ef

Please sign in to comment.