Skip to content

Commit

Permalink
Merge pull request #10 from 0x346e3730/ps9
Browse files Browse the repository at this point in the history
PrestaShop 9 Compability + Namespace fix
  • Loading branch information
mflasquin authored Jul 27, 2023
2 parents 3d57add + 3570b1e commit a14d94d
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 41 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
],
"autoload": {
"psr-4": {
"PrestaShop\\KeycloakConnectorDemo\\": "src/"
"PrestaShop\\Module\\KeycloakConnectorDemo\\": "src/"
}
},
"require-dev": {
Expand Down
2 changes: 1 addition & 1 deletion config/routes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ keycloak_connector_configuration:
path: /keycloak_connector/configuration
methods: [GET,POST]
defaults:
_controller: 'PrestaShop\KeycloakConnectorDemo\Controller\ConfigurationController::indexAction'
_controller: 'PrestaShop\Module\KeycloakConnectorDemo\Controller\ConfigurationController::indexAction'
37 changes: 19 additions & 18 deletions config/services.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
services:
PrestaShop\KeycloakConnectorDemo\Form\ConfigurationType:
class: PrestaShop\KeycloakConnectorDemo\Form\ConfigurationType
PrestaShop\Module\KeycloakConnectorDemo\Form\ConfigurationType:
class: PrestaShop\Module\KeycloakConnectorDemo\Form\ConfigurationType
parent: 'form.type.translatable.aware'
public: true
tags:
Expand All @@ -14,44 +14,45 @@ services:
prestashop.module.keycloak_connector_demo.client:
class: GuzzleHttp\Client

PrestaShop\KeycloakConnectorDemo\RequestBuilder:
class: PrestaShop\KeycloakConnectorDemo\RequestBuilder
PrestaShop\Module\KeycloakConnectorDemo\RequestBuilder:
class: PrestaShop\Module\KeycloakConnectorDemo\RequestBuilder
arguments:
- '@prestashop.adapter.legacy.configuration'
- '@prestashop.module.keycloak_connector_demo.php_encrypt'

PrestaShop\KeycloakConnectorDemo\Form\ConfigurationDataConfiguration:
class: PrestaShop\KeycloakConnectorDemo\Form\ConfigurationDataConfiguration
PrestaShop\Module\KeycloakConnectorDemo\Form\ConfigurationDataConfiguration:
class: PrestaShop\Module\KeycloakConnectorDemo\Form\ConfigurationDataConfiguration
arguments:
- '@prestashop.adapter.legacy.configuration'
- '@prestashop.module.keycloak_connector_demo.php_encrypt'
- '@PrestaShop\KeycloakConnectorDemo\RequestBuilder'
- '@PrestaShop\Module\KeycloakConnectorDemo\RequestBuilder'
- '@prestashop.module.keycloak_connector_demo.client'

PrestaShop\KeycloakConnectorDemo\Form\ConfigurationDataProvider:
class: PrestaShop\KeycloakConnectorDemo\Form\ConfigurationDataProvider
PrestaShop\Module\KeycloakConnectorDemo\Form\ConfigurationDataProvider:
class: PrestaShop\Module\KeycloakConnectorDemo\Form\ConfigurationDataProvider
arguments:
- '@PrestaShop\KeycloakConnectorDemo\Form\ConfigurationDataConfiguration'
- '@PrestaShop\Module\KeycloakConnectorDemo\Form\ConfigurationDataConfiguration'

prestashop.module.keycloak_connector_demo.form.configuration_data_handler:
class: 'PrestaShop\PrestaShop\Core\Form\Handler'
public: true
arguments:
- '@form.factory'
- '@prestashop.core.hook.dispatcher'
- '@PrestaShop\KeycloakConnectorDemo\Form\ConfigurationDataProvider'
- 'PrestaShop\KeycloakConnectorDemo\Form\ConfigurationType'
- '@PrestaShop\Module\KeycloakConnectorDemo\Form\ConfigurationDataProvider'
- 'PrestaShop\Module\KeycloakConnectorDemo\Form\ConfigurationType'
- 'KeycloakConfiguration'

PrestaShop\KeycloakConnectorDemo\Controller\ConfigurationController:
class: PrestaShop\KeycloakConnectorDemo\Controller\ConfigurationController
PrestaShop\Module\KeycloakConnectorDemo\Controller\ConfigurationController:
public: true
autowire: true

PrestaShop\KeycloakConnectorDemo\OAuth2\KeyCloakResourceServer:
class: PrestaShop\KeycloakConnectorDemo\OAuth2\KeyCloakResourceServer
PrestaShop\Module\KeycloakConnectorDemo\OAuth2\KeyCloakResourceServer:
class: PrestaShop\Module\KeycloakConnectorDemo\OAuth2\KeyCloakResourceServer
arguments:
- '@prestashop.module.keycloak_connector_demo.client'
- '@prestashop.adapter.legacy.configuration'
- '@prestashop.module.keycloak_connector_demo.php_encrypt'
- '@PrestaShop\KeycloakConnectorDemo\RequestBuilder'
- '@PrestaShop\Module\KeycloakConnectorDemo\RequestBuilder'

PrestaShop\PrestaShop\Core\OAuth2\OAuth2Interface: '@PrestaShop\KeycloakConnectorDemo\OAuth2\KeyCloakResourceServer'
PrestaShop\PrestaShop\Core\OAuth2\OAuth2Interface: '@PrestaShop\Module\KeycloakConnectorDemo\OAuth2\KeyCloakResourceServer'
4 changes: 2 additions & 2 deletions keycloak_connector_demo.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

class Keycloak_connector_demo extends \Module
{
public function __construct($name = null, Context $context = null)
public function __construct()
{
$this->name = 'keycloak_connector_demo';
$this->displayName = 'Keycloak OAuth2 connector demo';
Expand All @@ -40,7 +40,7 @@ public function __construct($name = null, Context $context = null)
$this->need_instance = 0;
$this->bootstrap = true;
$this->ps_versions_compliancy = ['min' => '8.0.0', 'max' => _PS_VERSION_];
parent::__construct($name, $context);
parent::__construct();
}

public function getContent(): void
Expand Down
2 changes: 1 addition & 1 deletion src/Controller/ConfigurationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

declare(strict_types=1);

namespace PrestaShop\KeycloakConnectorDemo\Controller;
namespace PrestaShop\Module\KeycloakConnectorDemo\Controller;

use PrestaShop\PrestaShop\Core\Form\FormHandlerInterface;
use PrestaShopBundle\Controller\Admin\FrameworkBundleAdminController;
Expand Down
4 changes: 2 additions & 2 deletions src/Form/ConfigurationDataConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@

declare(strict_types=1);

namespace PrestaShop\KeycloakConnectorDemo\Form;
namespace PrestaShop\Module\KeycloakConnectorDemo\Form;

use PhpEncryption;
use PrestaShop\KeycloakConnectorDemo\RequestBuilder;
use PrestaShop\Module\KeycloakConnectorDemo\RequestBuilder;
use PrestaShop\PrestaShop\Core\Configuration\DataConfigurationInterface;
use PrestaShop\PrestaShop\Core\ConfigurationInterface;
use Psr\Http\Client\ClientExceptionInterface;
Expand Down
2 changes: 1 addition & 1 deletion src/Form/ConfigurationDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

declare(strict_types=1);

namespace PrestaShop\KeycloakConnectorDemo\Form;
namespace PrestaShop\Module\KeycloakConnectorDemo\Form;

use PrestaShop\PrestaShop\Core\Configuration\DataConfigurationInterface;
use PrestaShop\PrestaShop\Core\Form\FormDataProviderInterface;
Expand Down
2 changes: 1 addition & 1 deletion src/Form/ConfigurationType.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

declare(strict_types=1);

namespace PrestaShop\KeycloakConnectorDemo\Form;
namespace PrestaShop\Module\KeycloakConnectorDemo\Form;

use PrestaShopBundle\Form\Admin\Type\TranslatorAwareType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
Expand Down
24 changes: 13 additions & 11 deletions src/OAuth2/KeyCloakResourceServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,22 @@

declare(strict_types=1);

namespace PrestaShop\KeycloakConnectorDemo\OAuth2;
namespace PrestaShop\Module\KeycloakConnectorDemo\OAuth2;

use Lcobucci\Clock\SystemClock;
use Lcobucci\JWT\Parser;
use Lcobucci\JWT\Encoding\JoseEncoder;
use Lcobucci\JWT\Signer\Key;
use Lcobucci\JWT\Signer\Key\InMemory;
use Lcobucci\JWT\Signer\Rsa\Sha256;
use Lcobucci\JWT\Token;
use Lcobucci\JWT\Validation\Constraint;
use Lcobucci\JWT\Token\Parser;
use Lcobucci\JWT\UnencryptedToken;
use Lcobucci\JWT\Validation\Constraint\SignedWith;
use Lcobucci\JWT\Validation\Constraint\ValidAt;
use Lcobucci\JWT\Validation\Constraint\StrictValidAt;
use Lcobucci\JWT\Validation\Validator;
use PhpEncryption;
use PrestaShop\KeycloakConnectorDemo\Form\ConfigurationDataConfiguration;
use PrestaShop\KeycloakConnectorDemo\RequestBuilder;
use PrestaShop\Module\KeycloakConnectorDemo\Form\ConfigurationDataConfiguration;
use PrestaShop\Module\KeycloakConnectorDemo\RequestBuilder;
use PrestaShop\PrestaShop\Core\ConfigurationInterface;
use PrestaShop\PrestaShop\Core\Security\OAuth2\ResourceServerInterface;
use Psr\Http\Client\ClientExceptionInterface;
Expand Down Expand Up @@ -156,23 +157,24 @@ private function getCertsRequest(): RequestInterface
/**
* @param Key $key
*
* @return Constraint[]
* @return array{SignedWith, StrictValidAt}
*/
private function getValidationConstraints(Key $key): array
{
return [
new SignedWith(new Sha256(), $key),
new ValidAt(SystemClock::fromUTC()),
new StrictValidAt(SystemClock::fromUTC()),
];
}

public function getUser(ServerRequestInterface $request): ?UserInterface
{
/** @var UnencryptedToken|null $token */
$token = $this->getTokenFromRequest($request);
if ($token === null) {
return null;
}
$audience = $token->claims()->get('clientId');
$audience = $token->claims()->get('clientId') ?? $token->claims()->get('client_id');
if (!is_string($audience)) {
return null;
}
Expand All @@ -183,10 +185,10 @@ public function getUser(ServerRequestInterface $request): ?UserInterface
private function getTokenFromRequest(ServerRequestInterface $request): ?Token
{
$authorization = $request->getHeader('Authorization')[0] ?? null;
if ($authorization === null || strpos($authorization, 'Bearer ') !== 0) {
if ($authorization === null || strpos($authorization, 'Bearer ') !== 0 || empty(explode(' ', $authorization)[1])) {
return null;
}

return (new Parser())->parse(explode(' ', $authorization)[1]);
return (new Parser(new JoseEncoder()))->parse(explode(' ', $authorization)[1]);
}
}
2 changes: 1 addition & 1 deletion src/OAuth2/KeyCloakUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

declare(strict_types=1);

namespace PrestaShop\KeycloakConnectorDemo\OAuth2;
namespace PrestaShop\Module\KeycloakConnectorDemo\OAuth2;

use Symfony\Component\Security\Core\User\UserInterface;

Expand Down
2 changes: 1 addition & 1 deletion src/RequestBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

declare(strict_types=1);

namespace PrestaShop\KeycloakConnectorDemo;
namespace PrestaShop\Module\KeycloakConnectorDemo;

use GuzzleHttp\Psr7\Request;
use Psr\Http\Message\RequestInterface;
Expand Down
2 changes: 1 addition & 1 deletion tests/phpstan.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ echo "Run PHPStan using phpstan-${PS_VERSION}.neon file"
docker run --rm --volumes-from temp-ps \
-v $PWD:/var/www/html/modules/keycloak_connector_demo \
-e _PS_ROOT_DIR_=/var/www/html \
--workdir=/var/www/html/modules/keycloak_connector_demo ghcr.io/phpstan/phpstan:1.9.8 \
--workdir=/var/www/html/modules/keycloak_connector_demo ghcr.io/phpstan/phpstan:1-php8.1 \
analyse \
--configuration=/var/www/html/modules/keycloak_connector_demo/tests/phpstan/phpstan-${PS_VERSION}.neon

0 comments on commit a14d94d

Please sign in to comment.