Skip to content

Commit

Permalink
Refactor the authorization server to match the new interface, autocin…
Browse files Browse the repository at this point in the history
…figure the service so that it is used by the TokenAuthenticator
  • Loading branch information
jolelievre committed Apr 4, 2024
1 parent 388857b commit e956503
Show file tree
Hide file tree
Showing 7 changed files with 277 additions and 202 deletions.
2 changes: 1 addition & 1 deletion config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
<tab><![CDATA[]]></tab>
<is_configurable>1</is_configurable>
<need_instance>0</need_instance>
</module>
</module>
8 changes: 4 additions & 4 deletions config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ services:
public: true
autowire: true

PrestaShop\Module\KeycloakConnectorDemo\OAuth2\KeyCloakResourceServer:
class: PrestaShop\Module\KeycloakConnectorDemo\OAuth2\KeyCloakResourceServer
PrestaShop\Module\KeycloakConnectorDemo\OAuth2\KeycloakAuthorizationServer:
# Autoconfigure to get tag from interface and to be injected in TokenAuthenticator
autoconfigure: true
class: PrestaShop\Module\KeycloakConnectorDemo\OAuth2\KeycloakAuthorizationServer
arguments:
- '@prestashop.module.keycloak_connector_demo.client'
- '@prestashop.adapter.legacy.configuration'
- '@prestashop.module.keycloak_connector_demo.php_encrypt'
- '@PrestaShop\Module\KeycloakConnectorDemo\RequestBuilder'

PrestaShop\PrestaShop\Core\OAuth2\OAuth2Interface: '@PrestaShop\Module\KeycloakConnectorDemo\OAuth2\KeyCloakResourceServer'
25 changes: 25 additions & 0 deletions keycloak_connector_demo.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*/

use PrestaShop\Module\KeycloakConnectorDemo\Form\ConfigurationDataConfiguration;
use PrestaShop\PrestaShop\Adapter\SymfonyContainer;

if (!defined('_PS_VERSION_')) {
Expand Down Expand Up @@ -53,4 +54,28 @@ public function getContent(): void
$router = $container->get('router');
Tools::redirectAdmin($router->generate('keycloak_connector_configuration'));
}

public function install()
{
if (!parent::install()) {
return false;
}

// Inject default configuration on install (the value is encrypted in the DB);
$encryption = new PhpEncryption(_NEW_COOKIE_KEY_);

return Configuration::updateValue(ConfigurationDataConfiguration::REALM_ENDPOINT, $encryption->encrypt('http://localhost:8003/realms/prestashop'));
}

public function uninstall()
{
if (!parent::uninstall()) {
return false;
}

// Delete configuration if present
Configuration::deleteByName(ConfigurationDataConfiguration::REALM_ENDPOINT);

return true;
}
}
3 changes: 1 addition & 2 deletions src/Form/ConfigurationDataConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
use PrestaShop\PrestaShop\Core\ConfigurationInterface;
use Psr\Http\Client\ClientExceptionInterface;
use Psr\Http\Client\ClientInterface;
use RuntimeException;

final class ConfigurationDataConfiguration implements DataConfigurationInterface
{
Expand Down Expand Up @@ -72,7 +71,7 @@ public function getConfiguration(): array
if (!empty($endpoint)) {
$endpoint = $this->encryption->decrypt($endpoint);
if (!is_string($endpoint)) {
throw new RuntimeException('Unable to decrypt realm endpoint configuration');
$endpoint = '';
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Form/ConfigurationType.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder->add(ConfigurationDataConfiguration::REALM_ENDPOINT, TextType::class, [
'label' => $this->trans('Keycloak realm endpoint', 'Modules.Keycloakconnectordemo.Admin'),
'help' => $this->trans('i.e. https://my.keycloak.instance.org/realms/master', 'Modules.Keycloakconnectordemo.Admin'),
'help' => $this->trans('i.e. http://localhost:8003/realms/prestashop', 'Modules.Keycloakconnectordemo.Admin'),
]);
}
}
194 changes: 0 additions & 194 deletions src/OAuth2/KeyCloakResourceServer.php

This file was deleted.

Loading

0 comments on commit e956503

Please sign in to comment.