-
Notifications
You must be signed in to change notification settings - Fork 6
/
keycloak_connector_demo.php
99 lines (86 loc) · 3.5 KB
/
keycloak_connector_demo.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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 [email protected] so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <[email protected]>
* @copyright Since 2007 PrestaShop SA and Contributors
* @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_')) {
exit;
}
if (file_exists(__DIR__ . '/vendor/autoload.php')) {
require_once __DIR__ . '/vendor/autoload.php';
}
class Keycloak_connector_demo extends \Module
{
public function __construct()
{
$this->name = 'keycloak_connector_demo';
$this->displayName = 'Keycloak OAuth2 connector demo';
$this->version = '1.2.0';
$this->author = 'PrestaShop';
$this->description = 'Demo module of how to use Keycloak as OAuth2 Authentication Server for the new API';
$this->need_instance = 0;
$this->bootstrap = true;
$this->ps_versions_compliancy = ['min' => '9.0.0', 'max' => _PS_VERSION_];
parent::__construct();
}
public function getContent(): void
{
$container = SymfonyContainer::getInstance();
if ($container === null) {
throw new RuntimeException('Could not get instance from SymfonyContainer');
}
/** @var \Symfony\Component\Routing\RouterInterface $router */
$router = $container->get('router');
Tools::redirectAdmin($router->generate('keycloak_connector_configuration'));
}
/**
* @return bool
*/
public function install()
{
if (!parent::install()) {
return false;
}
// Inject default configuration on install (the value is encrypted in the DB);
$cookieKey = null;
// On fresh install process _NEW_COOKIE_KEY_ is not available, so we fetch the config directly from the parameters file
$phpParametersFilepath = _PS_ROOT_DIR_ . '/app/config/parameters.php';
if (file_exists($phpParametersFilepath)) {
$config = require $phpParametersFilepath;
if (!empty($config['parameters']['new_cookie_key'])) {
$cookieKey = $config['parameters']['new_cookie_key'];
}
}
if (!empty($cookieKey)) {
$encryption = new PhpEncryption($cookieKey);
return Configuration::updateValue(ConfigurationDataConfiguration::REALM_ENDPOINT, $encryption->encrypt('http://localhost:8003/realms/prestashop'))
&& Configuration::updateValue(ConfigurationDataConfiguration::ALLOWED_ISSUERS, $encryption->encrypt('http://localhost:8003/realms/prestashop'));
}
return true;
}
public function uninstall()
{
if (!parent::uninstall()) {
return false;
}
// Delete configuration if present
Configuration::deleteByName(ConfigurationDataConfiguration::REALM_ENDPOINT);
return true;
}
}