Skip to content

Commit d761a33

Browse files
ro0NLnicolas-grekas
authored andcommitted
[Security] Revise UserPasswordEncoderInterface::needsRehash()
1 parent ac440e2 commit d761a33

File tree

4 files changed

+12
-7
lines changed

4 files changed

+12
-7
lines changed

Encoder/UserPasswordEncoder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ public function isPasswordValid(UserInterface $user, $raw)
5050
/**
5151
* {@inheritdoc}
5252
*/
53-
public function needsRehash(UserInterface $user, string $encoded): bool
53+
public function needsRehash(UserInterface $user): bool
5454
{
5555
$encoder = $this->encoderFactory->getEncoder($user);
5656

57-
return method_exists($encoder, 'needsRehash') && $encoder->needsRehash($encoded);
57+
return method_exists($encoder, 'needsRehash') && $encoder->needsRehash($user->getPassword());
5858
}
5959
}

Encoder/UserPasswordEncoderInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*
1919
* @author Ariel Ferrandini <[email protected]>
2020
*
21-
* @method bool needsRehash(UserInterface $user, string $encoded)
21+
* @method bool needsRehash(UserInterface $user)
2222
*/
2323
interface UserPasswordEncoderInterface
2424
{

Tests/Encoder/UserPasswordEncoderTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ public function testNeedsRehash()
8585

8686
$passwordEncoder = new UserPasswordEncoder($mockEncoderFactory);
8787

88-
$hash = $passwordEncoder->encodePassword($user, 'foo', 'salt');
89-
$this->assertFalse($passwordEncoder->needsRehash($user, $hash));
90-
$this->assertTrue($passwordEncoder->needsRehash($user, $hash));
91-
$this->assertFalse($passwordEncoder->needsRehash($user, $hash));
88+
$user->setPassword($passwordEncoder->encodePassword($user, 'foo', 'salt'));
89+
$this->assertFalse($passwordEncoder->needsRehash($user));
90+
$this->assertTrue($passwordEncoder->needsRehash($user));
91+
$this->assertFalse($passwordEncoder->needsRehash($user));
9292
}
9393
}

User/User.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,9 @@ public function isEqualTo(UserInterface $user)
164164

165165
return true;
166166
}
167+
168+
public function setPassword(string $password)
169+
{
170+
$this->password = $password;
171+
}
167172
}

0 commit comments

Comments
 (0)