15
15
use Symfony \Bundle \SecurityBundle \Command \UserPasswordEncoderCommand ;
16
16
use Symfony \Component \Console \Application as ConsoleApplication ;
17
17
use Symfony \Component \Console \Tester \CommandTester ;
18
+ use Symfony \Component \Security \Core \Encoder \Argon2idPasswordEncoder ;
18
19
use Symfony \Component \Security \Core \Encoder \Argon2iPasswordEncoder ;
19
20
use Symfony \Component \Security \Core \Encoder \BCryptPasswordEncoder ;
20
21
use Symfony \Component \Security \Core \Encoder \EncoderFactoryInterface ;
@@ -72,7 +73,7 @@ public function testEncodePasswordBcrypt()
72
73
73
74
public function testEncodePasswordArgon2i ()
74
75
{
75
- if (!Argon2iPasswordEncoder::isSupported ()) {
76
+ if (!Argon2iPasswordEncoder::isSupported () || \defined ( ' SODIUM_CRYPTO_PWHASH_ALG_ARGON2ID13 ' ) ) {
76
77
$ this ->markTestSkipped ('Argon2i algorithm not available. ' );
77
78
}
78
79
$ this ->setupArgon2i ();
@@ -85,6 +86,27 @@ public function testEncodePasswordArgon2i()
85
86
$ output = $ this ->passwordEncoderCommandTester ->getDisplay ();
86
87
$ this ->assertContains ('Password encoding succeeded ' , $ output );
87
88
89
+ $ encoder = new Argon2iPasswordEncoder ();
90
+ preg_match ('# Encoded password\s+(\$argon2i?\$[\w,=\$+\/]+={0,2})\s+# ' , $ output , $ matches );
91
+ $ hash = $ matches [1 ];
92
+ $ this ->assertTrue ($ encoder ->isPasswordValid ($ hash , 'password ' , null ));
93
+ }
94
+
95
+ public function testEncodePasswordArgon2id ()
96
+ {
97
+ if (!Argon2idPasswordEncoder::isSupported ()) {
98
+ $ this ->markTestSkipped ('Argon2i algorithm not available. ' );
99
+ }
100
+ $ this ->setupArgon2id ();
101
+ $ this ->passwordEncoderCommandTester ->execute ([
102
+ 'command ' => 'security:encode-password ' ,
103
+ 'password ' => 'password ' ,
104
+ 'user-class ' => 'Custom\Class\Argon2id\User ' ,
105
+ ], ['interactive ' => false ]);
106
+
107
+ $ output = $ this ->passwordEncoderCommandTester ->getDisplay ();
108
+ $ this ->assertContains ('Password encoding succeeded ' , $ output );
109
+
88
110
$ encoder = new Argon2iPasswordEncoder ();
89
111
preg_match ('# Encoded password\s+(\$argon2id?\$[\w,=\$+\/]+={0,2})\s+# ' , $ output , $ matches );
90
112
$ hash = $ matches [1 ];
@@ -153,8 +175,8 @@ public function testEncodePasswordBcryptOutput()
153
175
154
176
public function testEncodePasswordArgon2iOutput ()
155
177
{
156
- if (!Argon2iPasswordEncoder::isSupported ()) {
157
- $ this ->markTestSkipped ('Argon2i algorithm not available. ' );
178
+ if (!Argon2iPasswordEncoder::isSupported () || \defined ( ' SODIUM_CRYPTO_PWHASH_ALG_ARGON2ID13 ' ) ) {
179
+ $ this ->markTestSkipped ('Argon2id algorithm not available. ' );
158
180
}
159
181
160
182
$ this ->setupArgon2i ();
@@ -167,6 +189,22 @@ public function testEncodePasswordArgon2iOutput()
167
189
$ this ->assertNotContains (' Generated salt ' , $ this ->passwordEncoderCommandTester ->getDisplay ());
168
190
}
169
191
192
+ public function testEncodePasswordArgon2idOutput ()
193
+ {
194
+ if (!Argon2idPasswordEncoder::isSupported ()) {
195
+ $ this ->markTestSkipped ('Argon2id algorithm not available. ' );
196
+ }
197
+
198
+ $ this ->setupArgon2id ();
199
+ $ this ->passwordEncoderCommandTester ->execute ([
200
+ 'command ' => 'security:encode-password ' ,
201
+ 'password ' => 'p@ssw0rd ' ,
202
+ 'user-class ' => 'Custom\Class\Argon2id\User ' ,
203
+ ], ['interactive ' => false ]);
204
+
205
+ $ this ->assertNotContains (' Generated salt ' , $ this ->passwordEncoderCommandTester ->getDisplay ());
206
+ }
207
+
170
208
public function testEncodePasswordNoConfigForGivenUserClass ()
171
209
{
172
210
if (method_exists ($ this , 'expectException ' )) {
@@ -259,4 +297,17 @@ private function setupArgon2i()
259
297
260
298
$ this ->passwordEncoderCommandTester = new CommandTester ($ passwordEncoderCommand );
261
299
}
300
+
301
+ private function setupArgon2id ()
302
+ {
303
+ putenv ('COLUMNS= ' .(119 + \strlen (PHP_EOL )));
304
+ $ kernel = $ this ->createKernel (['test_case ' => 'PasswordEncode ' , 'root_config ' => 'argon2id.yml ' ]);
305
+ $ kernel ->boot ();
306
+
307
+ $ application = new Application ($ kernel );
308
+
309
+ $ passwordEncoderCommand = $ application ->get ('security:encode-password ' );
310
+
311
+ $ this ->passwordEncoderCommandTester = new CommandTester ($ passwordEncoderCommand );
312
+ }
262
313
}
0 commit comments