From ac4ca02c45b6a5eb2322cc06b571a5c9ba54007d Mon Sep 17 00:00:00 2001 From: Joshwin Zuidema Date: Wed, 29 Jan 2020 16:05:54 +0100 Subject: [PATCH 1/2] feature/hash-config --- README.md | 1 + src/Constants/ConfigConstants.php | 1 + src/DependencyInjection/Configuration.php | 3 +++ .../EsitesEncryptionExtension.php | 9 ++++++++- src/Helper/EncryptionHelper.php | 14 +++++++++++--- src/Resources/config/services.yml | 7 +++++++ 6 files changed, 31 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 0c53347..ddca3b2 100644 --- a/README.md +++ b/README.md @@ -18,4 +18,5 @@ esites_encryption: username_field: username # default value, the property name for the username in the user entity user_class: App\Entity\User # not required, full namespace of the user entity encryption_key_file: '%kernel.root_dir%/encryption/key' # default value, directory and filename for the encryption key + hash_algorithm: 'sha256' # default value, algorithm used to hash strings. uses the native php hash() function ``` \ No newline at end of file diff --git a/src/Constants/ConfigConstants.php b/src/Constants/ConfigConstants.php index d0647d3..1fb43f4 100644 --- a/src/Constants/ConfigConstants.php +++ b/src/Constants/ConfigConstants.php @@ -9,6 +9,7 @@ class ConfigConstants public const CONFIG_USERNAME_FIELD = 'username_field'; public const CONFIG_USER_CLASS = 'user_class'; public const CONFIG_ENCRYPTION_KEY_FILE = 'encryption_key_file'; + public const CONFIG_HASH_ALGORITHM = 'hash_algorithm'; public static function getParameterKeyName(string $key): string { diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 5463d65..89d598c 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -23,6 +23,9 @@ public function getConfigTreeBuilder(): TreeBuilder ->scalarNode(ConfigConstants::CONFIG_ENCRYPTION_KEY_FILE) ->defaultValue('%kernel.root_dir%/encryption/key') ->end() + ->scalarNode(ConfigConstants::CONFIG_HASH_ALGORITHM) + ->defaultValue('sha256') + ->end() ; return $treeBuilder; diff --git a/src/DependencyInjection/EsitesEncryptionExtension.php b/src/DependencyInjection/EsitesEncryptionExtension.php index c1228ca..0cfab96 100644 --- a/src/DependencyInjection/EsitesEncryptionExtension.php +++ b/src/DependencyInjection/EsitesEncryptionExtension.php @@ -15,7 +15,7 @@ class EsitesEncryptionExtension extends Extension /** * @throws Exception */ - public function load(array $configs, ContainerBuilder $container) + public function load(array $configs, ContainerBuilder $container): void { $configuration = new Configuration(); $processor = new Processor(); @@ -43,6 +43,13 @@ public function load(array $configs, ContainerBuilder $container) $config[ConfigConstants::CONFIG_ENCRYPTION_KEY_FILE] ); + $container->setParameter( + ConfigConstants::getParameterKeyName( + ConfigConstants::CONFIG_HASH_ALGORITHM + ), + $config[ConfigConstants::CONFIG_HASH_ALGORITHM] + ); + $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); $loader->load('services.yml'); } diff --git a/src/Helper/EncryptionHelper.php b/src/Helper/EncryptionHelper.php index 9945373..f5b8d86 100644 --- a/src/Helper/EncryptionHelper.php +++ b/src/Helper/EncryptionHelper.php @@ -32,13 +32,21 @@ class EncryptionHelper */ private $fileSystem; + /** + * @var string|null + */ + private $hashAlgorithm; + /** * {@inheritdoc} */ - public function __construct(string $encryptionKeyFile) - { + public function __construct( + string $encryptionKeyFile, + string $hashAlgorithm + ) { $this->encryptionKeyFile = $encryptionKeyFile; $this->fileSystem = new Filesystem(); + $this->hashAlgorithm = $hashAlgorithm; } /** @@ -98,7 +106,7 @@ public function hash(?string $string): ?string } return hash( - 'sha256', + $this->hashAlgorithm, $string ); } diff --git a/src/Resources/config/services.yml b/src/Resources/config/services.yml index 7f31ebb..d8658bc 100644 --- a/src/Resources/config/services.yml +++ b/src/Resources/config/services.yml @@ -1,3 +1,9 @@ +parameters: + esites_encryption_bundle.encryption_key_file: ~ + esites_encryption_bundle.hash_algorithm: ~ + esites_encryption_bundle.username_field: ~ + esites_encryption_bundle.user_class: ~ + services: _defaults: autowire: true @@ -7,6 +13,7 @@ services: Esites\EncryptionBundle\Helper\EncryptionHelper: arguments: $encryptionKeyFile: '%esites_encryption_bundle.encryption_key_file%' + $hashAlgorithm: '%esites_encryption_bundle.hash_algorithm%' public: true Esites\EncryptionBundle\Provider\UserProvider: From cead5923d6d8e7db22af8ba3cf4eeb2f7cf51899 Mon Sep 17 00:00:00 2001 From: Joshwin Zuidema Date: Wed, 29 Jan 2020 16:17:12 +0100 Subject: [PATCH 2/2] Remove default parameters --- src/Resources/config/services.yml | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/Resources/config/services.yml b/src/Resources/config/services.yml index d8658bc..a4fd6e1 100644 --- a/src/Resources/config/services.yml +++ b/src/Resources/config/services.yml @@ -1,9 +1,3 @@ -parameters: - esites_encryption_bundle.encryption_key_file: ~ - esites_encryption_bundle.hash_algorithm: ~ - esites_encryption_bundle.username_field: ~ - esites_encryption_bundle.user_class: ~ - services: _defaults: autowire: true