Skip to content

Commit c09fc01

Browse files
authored
Merge pull request #156 from jrushlow/feature/fake-token
add ability to generate a fake reset token
2 parents 6825b9f + 4937355 commit c09fc01

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/ResetPasswordHelper.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,25 @@ public function getTokenLifetime(): int
154154
return $this->resetRequestLifetime;
155155
}
156156

157+
/**
158+
* Generate a fake reset token.
159+
*
160+
* Use this to generate a fake token so that you can, for example, show a
161+
* "reset confirmation email sent" page that includes a valid "expiration date",
162+
* even if the email was not actually found (and so, a true ResetPasswordToken
163+
* was not actually created).
164+
*
165+
* This method should not be used when timing attacks are a concern.
166+
*/
167+
public function generateFakeResetToken(): ResetPasswordToken
168+
{
169+
$expiresAt = new \DateTimeImmutable(\sprintf('+%d seconds', $this->resetRequestLifetime));
170+
171+
$generatedAt = ($expiresAt->getTimestamp() - $this->resetRequestLifetime);
172+
173+
return new ResetPasswordToken('fake-token', $expiresAt, $generatedAt);
174+
}
175+
157176
private function findResetPasswordRequest(string $token): ?ResetPasswordRequestInterface
158177
{
159178
$selector = \substr($token, 0, self::SELECTOR_LENGTH);

src/ResetPasswordHelperInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
/**
1616
* @author Jesse Rushlow <jr@rushlow.dev>
1717
* @author Ryan Weaver <ryan@symfonycasts.com>
18+
*
19+
* @method ResetPasswordToken generateFakeResetToken() Generates a fake ResetPasswordToken.
1820
*/
1921
interface ResetPasswordHelperInterface
2022
{

0 commit comments

Comments
 (0)