forked from Sylius/Sylius
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Sylius#7855 from pamil/enigma
Store encrypted gateway configurations in the database
- Loading branch information
Showing
12 changed files
with
160 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
How to encrypt gateway config stored in the database? | ||
===================================================== | ||
|
||
**1.** Generate your Defuse Secret Key by executing the following script: | ||
|
||
.. code-block:: php | ||
<?php | ||
use Defuse\Crypto\Key; | ||
require_once 'vendor/autoload.php'; | ||
var_dump(Key::createNewRandomKey()->saveToAsciiSafeString()); | ||
**2.** Store your generated key in a parameter in ``app/config/parameters.yml``. | ||
|
||
.. code-block:: yaml | ||
# app/config/parameters.yml | ||
parameters: | ||
# ... | ||
defuse_secret: "YOUR_GENERATED_KEY" | ||
**3.** Add the following code to the application configuration in the ``app/config/config.yml``. | ||
|
||
.. code-block:: yaml | ||
# app/config/config.yml | ||
payum: | ||
dynamic_gateways: | ||
encryption: | ||
defuse_secret_key: "%defuse_secret%" | ||
**4.** Existing gateway configs will be automatically encrypted when updated. New gateway configs will be encrypted by default. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
src/Sylius/Bundle/PayumBundle/Form/Extension/CryptedGatewayConfigTypeExtension.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Paweł Jędrzejewski | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Sylius\Bundle\PayumBundle\Form\Extension; | ||
|
||
use Payum\Core\Security\CryptedInterface; | ||
use Payum\Core\Security\CypherInterface; | ||
use Sylius\Bundle\PayumBundle\Form\Type\GatewayConfigType; | ||
use Symfony\Component\Form\AbstractTypeExtension; | ||
use Symfony\Component\Form\FormBuilderInterface; | ||
use Symfony\Component\Form\FormEvent; | ||
use Symfony\Component\Form\FormEvents; | ||
|
||
/** | ||
* @author Kamil Kokot <kamil.kokot@lakion.com> | ||
*/ | ||
final class CryptedGatewayConfigTypeExtension extends AbstractTypeExtension | ||
{ | ||
/** | ||
* @var CypherInterface|null | ||
*/ | ||
private $cypher; | ||
|
||
/** | ||
* @param CypherInterface|null $cypher | ||
*/ | ||
public function __construct(CypherInterface $cypher = null) | ||
{ | ||
$this->cypher = $cypher; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function buildForm(FormBuilderInterface $builder, array $options) | ||
{ | ||
if (null === $this->cypher) { | ||
return; | ||
} | ||
|
||
$builder | ||
->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) { | ||
$gatewayConfig = $event->getData(); | ||
|
||
if (!$gatewayConfig instanceof CryptedInterface) { | ||
return; | ||
} | ||
|
||
$gatewayConfig->decrypt($this->cypher); | ||
|
||
$event->setData($gatewayConfig); | ||
}) | ||
->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) { | ||
$gatewayConfig = $event->getData(); | ||
|
||
if (!$gatewayConfig instanceof CryptedInterface) { | ||
return; | ||
} | ||
|
||
$gatewayConfig->encrypt($this->cypher); | ||
|
||
$event->setData($gatewayConfig); | ||
}) | ||
; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getExtendedType() | ||
{ | ||
return GatewayConfigType::class; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.