Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Payment] allow encryption of gatway configs #2538

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
PIMCORE_KERNEL_CLASS=Kernel
DEFUSE_SECRET=
5 changes: 5 additions & 0 deletions config/config.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
imports:
- { resource: services.yml }
- { resource: 'local/' }

#payum:
# dynamic_gateways:
# encryption:
# defuse_secret_key: "%env(DEFUSE_SECRET)%"
28 changes: 28 additions & 0 deletions docs/03_Development/10_Payment/02_Encrypt_Config.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Encrypt Payment Gatway Configuration

1. Create a new file in the root of your project and name it `encrypt.php`.

```php
<?php
use Defuse\Crypto\Key;
require_once 'vendor/autoload.php';
var_dump(Key::createNewRandomKey()->saveToAsciiSafeString());
```

2. Store your key in a safe place and add it to your `.env` file.

```env
CORESHOP_PAYUM_ENCRYPT_KEY="YOUR KEY"
```

3. Add the following code to your `config/config.yaml` file.

```yaml
payum:
dynamic_gateways:
encryption:
defuse_secret_key: "%env(CORESHOP_PAYUM_ENCRYPT_KEY)%"
```

4. Existing payment gateway configs will be automatically encrypted when updated. New payment gateway configs will be
encrypted by default.
1 change: 1 addition & 0 deletions docs/03_Development/10_Payment/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,5 @@ $this->container->get('coreshop.repository.payment')->add($payment);
For more information on payment integration and management in CoreShop:

- **[Payment Provider](./01_Payment_Provider.md)**
- **[Encrypt Payment Gateway Configuration](./02_Encrypt_Config.md)**
- **[Payum Providers](./03_Payum_Providers.md)**
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,29 @@
namespace CoreShop\Bundle\PayumPaymentBundle\Controller;

use CoreShop\Bundle\ResourceBundle\Controller\ResourceController;
use CoreShop\Component\PayumPayment\Model\PaymentProviderInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class PaymentProviderController extends ResourceController
{
public function getAction(Request $request): JsonResponse
{
$this->isGrantedOr403();

$resources = $this->findOr404((int) $this->getParameterFromRequest($request, 'id'));

$form = $this->resourceFormFactory->create($this->metadata, $resources);

/**
* @var PaymentProviderInterface $formData
*/
$formData = $form->getData();

return $this->viewHandler->handle(['data' => $formData, 'success' => true], ['group' => 'Detailed']);
}

public function getConfigAction(): Response
{
$factoryResults = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use CoreShop\Bundle\ResourceBundle\DependencyInjection\Extension\AbstractModelExtension;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;

class CoreShopPayumPaymentExtension extends AbstractModelExtension
Expand All @@ -29,9 +30,11 @@ public function load(array $configs, ContainerBuilder $container): void
{
$configs = $this->processConfiguration($this->getConfiguration([], $container), $configs);
$loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$phpLoader = new PhpFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));

$this->registerResources('coreshop', $configs['driver'], $configs['resources'], $container);

$loader->load('services.yml');
$phpLoader->load('services.php');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

declare(strict_types=1);

/*
* CoreShop
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - CoreShop Commercial License (CCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) CoreShop GmbH (https://www.coreshop.org)
* @license https://www.coreshop.org/license GPLv3 and CCL
*
*/

namespace CoreShop\Bundle\PayumPaymentBundle\Form\Extension;

use CoreShop\Bundle\PayumPaymentBundle\Form\Type\GatewayConfigType;
use Payum\Core\Model\GatewayConfigInterface;
use Payum\Core\Security\CryptedInterface;
use Payum\Core\Security\CypherInterface;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;

final class CryptedGatewayConfigTypeExtension extends AbstractTypeExtension
{
public function __construct(private ?CypherInterface $cypher = null)
{

}

public function buildForm(FormBuilderInterface $builder, array $options)
{
if (null === $this->cypher) {
return;
}

$builder
->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
$gatewayConfig = $event->getData();

/**
* @var GatewayConfigInterface $gatewayConfig
*/
if (!$gatewayConfig instanceof CryptedInterface) {
return;
}

$gatewayConfig->decrypt($this->cypher);
$gatewayConfig->setConfig($gatewayConfig->getConfig());

$event->setData($gatewayConfig);
})
->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) {
$gatewayConfig = $event->getData();

/**
* @var GatewayConfigInterface $gatewayConfig
*/
if (!$gatewayConfig instanceof CryptedInterface) {
return;
}

$gatewayConfig->setConfig($gatewayConfig->getConfig());
$gatewayConfig->encrypt($this->cypher);

$event->setData($gatewayConfig);
});
}

public static function getExtendedTypes(): array
{
return [GatewayConfigType::class];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use CoreShop\Bundle\PayumPaymentBundle\Form\Extension\CryptedGatewayConfigTypeExtension;
use CoreShop\Bundle\PayumPaymentBundle\Form\Type\GatewayConfigType;

/**
* We got this as a separate file since YAML does not allow nullOnInvalid
*/
return function (ContainerConfigurator $container): void {
$services = $container->services();

$services->set(CryptedGatewayConfigTypeExtension::class)
->args([service('payum.dynamic_gateways.cypher')->nullOnInvalid()])
->tag('form.type_extension', ['extended_type' => GatewayConfigType::class]);
};
Loading