Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5129c4c

Browse files
committed
feature #19843 [Security] Allow run-time configuration of hash algo (nicolas-grekas)
This PR was merged into the 3.2-dev branch. Discussion ---------- [Security] Allow run-time configuration of hash algo | Q | A | ------------- | --- | Branch? | master | New feature? | yes | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Required if we want run-time config with env vars. See #19681 Commits ------- 7903a46 [Security] Allow run-time configuration of hash algo
2 parents e5088ac + 7903a46 commit 5129c4c

File tree

4 files changed

+57
-14
lines changed

4 files changed

+57
-14
lines changed

src/Symfony/Bundle/SecurityBundle/DependencyInjection/SecurityExtension.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -493,15 +493,8 @@ private function createEncoder($config, ContainerBuilder $container)
493493
);
494494
}
495495

496-
// message digest encoder
497-
return array(
498-
'class' => 'Symfony\Component\Security\Core\Encoder\MessageDigestPasswordEncoder',
499-
'arguments' => array(
500-
$config['algorithm'],
501-
$config['encode_as_base64'],
502-
$config['iterations'],
503-
),
504-
);
496+
// run-time configured encoder
497+
return $config;
505498
}
506499

507500
// Parses user providers and returns an array of their ids

src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/CompleteConfigurationTest.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,12 +191,22 @@ public function testEncoders()
191191
'arguments' => array(false),
192192
),
193193
'JMS\FooBundle\Entity\User2' => array(
194-
'class' => 'Symfony\Component\Security\Core\Encoder\MessageDigestPasswordEncoder',
195-
'arguments' => array('sha1', false, 5),
194+
'algorithm' => 'sha1',
195+
'encode_as_base64' => false,
196+
'iterations' => 5,
197+
'hash_algorithm' => 'sha512',
198+
'key_length' => 40,
199+
'ignore_case' => false,
200+
'cost' => 13,
196201
),
197202
'JMS\FooBundle\Entity\User3' => array(
198-
'class' => 'Symfony\Component\Security\Core\Encoder\MessageDigestPasswordEncoder',
199-
'arguments' => array('md5', true, 5000),
203+
'algorithm' => 'md5',
204+
'hash_algorithm' => 'sha512',
205+
'key_length' => 40,
206+
'ignore_case' => false,
207+
'encode_as_base64' => true,
208+
'iterations' => 5000,
209+
'cost' => 13,
200210
),
201211
'JMS\FooBundle\Entity\User4' => new Reference('security.encoder.foo'),
202212
'JMS\FooBundle\Entity\User5' => array(

src/Symfony/Bundle/SecurityBundle/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
],
1818
"require": {
1919
"php": ">=5.5.9",
20-
"symfony/security": "~3.1,>=3.1.2",
20+
"symfony/security": "~3.2",
2121
"symfony/http-kernel": "~3.1",
2222
"symfony/polyfill-php70": "~1.0"
2323
},

src/Symfony/Component/Security/Core/Encoder/EncoderFactory.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ public function getEncoder($user)
6969
*/
7070
private function createEncoder(array $config)
7171
{
72+
if (isset($config['algorithm'])) {
73+
$config = $this->getEncoderConfigFromAlgorithm($config);
74+
}
7275
if (!isset($config['class'])) {
7376
throw new \InvalidArgumentException(sprintf('"class" must be set in %s.', json_encode($config)));
7477
}
@@ -80,4 +83,41 @@ private function createEncoder(array $config)
8083

8184
return $reflection->newInstanceArgs($config['arguments']);
8285
}
86+
87+
private function getEncoderConfigFromAlgorithm($config)
88+
{
89+
switch ($config['algorithm']) {
90+
case 'plaintext':
91+
return array(
92+
'class' => PlaintextPasswordEncoder::class,
93+
'arguments' => array($config['ignore_case']),
94+
);
95+
96+
case 'pbkdf2':
97+
return array(
98+
'class' => Pbkdf2PasswordEncoder::class,
99+
'arguments' => array(
100+
$config['hash_algorithm'],
101+
$config['encode_as_base64'],
102+
$config['iterations'],
103+
$config['key_length'],
104+
),
105+
);
106+
107+
case 'bcrypt':
108+
return array(
109+
'class' => BCryptPasswordEncoder::class,
110+
'arguments' => array($config['cost']),
111+
);
112+
}
113+
114+
return array(
115+
'class' => MessageDigestPasswordEncoder::class,
116+
'arguments' => array(
117+
$config['algorithm'],
118+
$config['encode_as_base64'],
119+
$config['iterations'],
120+
),
121+
);
122+
}
83123
}

0 commit comments

Comments
 (0)