Skip to content

Commit 771d672

Browse files
authored
Merge pull request #70 from jrushlow/refactor/generator
fixed selector length
2 parents 841b86d + 8e440b8 commit 771d672

8 files changed

+32
-21
lines changed

src/Generator/ResetPasswordRandomGenerator.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@ class ResetPasswordRandomGenerator
2020
{
2121
/**
2222
* Original credit to Laravel's Str::random() method.
23+
*
24+
* String length is 20 characters
2325
*/
24-
public function getRandomAlphaNumStr(int $length): string
26+
public function getRandomAlphaNumStr(): string
2527
{
2628
$string = '';
2729

28-
while (($len = \strlen($string)) < $length) {
29-
$size = $length - $len;
30+
while (($len = \strlen($string)) < 20) {
31+
$size = 20 - $len;
3032

3133
$bytes = \random_bytes($size);
3234

src/Generator/ResetPasswordTokenGenerator.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
*/
2121
class ResetPasswordTokenGenerator
2222
{
23-
private const RANDOM_STR_LENGTH = 20;
24-
2523
/**
2624
* @var string Unique, random, cryptographically secure string
2725
*/
@@ -47,10 +45,10 @@ public function __construct(string $signingKey, ResetPasswordRandomGenerator $ge
4745
public function createToken(\DateTimeInterface $expiresAt, $userId, string $verifier = null): ResetPasswordTokenComponents
4846
{
4947
if (null === $verifier) {
50-
$verifier = $this->randomGenerator->getRandomAlphaNumStr(self::RANDOM_STR_LENGTH);
48+
$verifier = $this->randomGenerator->getRandomAlphaNumStr();
5149
}
5250

53-
$selector = $this->randomGenerator->getRandomAlphaNumStr(self::RANDOM_STR_LENGTH);
51+
$selector = $this->randomGenerator->getRandomAlphaNumStr();
5452

5553
$encodedData = \json_encode([$verifier, $userId, $expiresAt->getTimestamp()]);
5654

src/Model/ResetPasswordRequestTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
trait ResetPasswordRequestTrait
1919
{
2020
/**
21-
* @ORM\Column(type="string", length=100)
21+
* @ORM\Column(type="string", length=20)
2222
*/
2323
private $selector;
2424

src/ResetPasswordHelper.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ public function validateTokenAndFetchUser(string $fullToken): object
108108
{
109109
$this->resetPasswordCleaner->handleGarbageCollection();
110110

111+
if (40 !== \strlen($fullToken)) {
112+
throw new InvalidResetPasswordTokenException();
113+
}
114+
111115
$resetRequest = $this->findResetPasswordRequest($fullToken);
112116

113117
if (null === $resetRequest) {

tests/UnitTests/Generator/ResetPasswordRandomGeneratorTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,20 @@
1818
*/
1919
class ResetPasswordRandomGeneratorTest extends TestCase
2020
{
21-
public function testIsProvidedLength(): void
21+
public function testLengthIs20(): void
2222
{
2323
$generator = new ResetPasswordRandomGenerator();
24-
$result = $generator->getRandomAlphaNumStr(100);
24+
$result = $generator->getRandomAlphaNumStr();
2525

26-
self::assertSame(100, \strlen($result));
26+
self::assertSame(20, \strlen($result));
2727
}
2828

2929
public function testIsRandom(): void
3030
{
3131
$generator = new ResetPasswordRandomGenerator();
3232

33-
$resultA = $generator->getRandomAlphaNumStr(20);
34-
$resultB = $generator->getRandomAlphaNumStr(20);
33+
$resultA = $generator->getRandomAlphaNumStr();
34+
$resultB = $generator->getRandomAlphaNumStr();
3535

3636
self::assertNotSame($resultA, $resultB);
3737
}

tests/UnitTests/Generator/ResetPasswordTokenGeneratorTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ public function testSelectorGeneratedByRandomGenerator(): void
4343
$this->mockRandomGenerator
4444
->expects($this->exactly(2))
4545
->method('getRandomAlphaNumStr')
46-
->with(20)
4746
;
4847

4948
$generator = $this->getTokenGenerator();

tests/UnitTests/Model/ResetPasswordRequestTraitTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function testIsCompatibleWithInterface(): void
2626

2727
public function propertyDataProvider(): \Generator
2828
{
29-
yield ['selector', '@ORM\Column(type="string", length=100)'];
29+
yield ['selector', '@ORM\Column(type="string", length=20)'];
3030
yield ['hashedToken', '@ORM\Column(type="string", length=100)'];
3131
yield ['requestedAt', '@ORM\Column(type="datetime_immutable")'];
3232
yield ['expiresAt', '@ORM\Column(type="datetime_immutable")'];

tests/UnitTests/ResetPasswordHelperTest.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
/**
2424
* @author Jesse Rushlow <[email protected]>
25-
* @authot Ryan Weaver <[email protected]>
25+
* @author Ryan Weaver <[email protected]>
2626
*/
2727
class ResetPasswordHelperTest extends TestCase
2828
{
@@ -65,7 +65,7 @@ protected function setUp(): void
6565
$this->mockTokenGenerator = $this->createMock(ResetPasswordTokenGenerator::class);
6666
$this->mockCleaner = $this->createMock(ResetPasswordCleaner::class);
6767
$this->mockResetRequest = $this->createMock(ResetPasswordRequestInterface::class);
68-
$this->randomToken = \bin2hex(\random_bytes(10));
68+
$this->randomToken = \bin2hex(\random_bytes(20));
6969
$this->mockUser = new class() {};
7070
}
7171

@@ -173,7 +173,7 @@ public function testRemoveResetRequestRetrievesTokenFromRepository(): void
173173
$this->mockRepo
174174
->expects($this->once())
175175
->method('findResetPasswordRequest')
176-
->with($this->randomToken)
176+
->with(\substr($this->randomToken, 0, 20))
177177
->willReturn($this->mockResetRequest)
178178
;
179179

@@ -198,6 +198,14 @@ public function testRemoveResetRequestCallsRepositoryToRemoveResetRequestObject(
198198
$helper->removeResetRequest('1234');
199199
}
200200

201+
public function testExceptionThrownIfTokenLengthIsNotOfCorrectSize(): void
202+
{
203+
$this->expectException(InvalidResetPasswordTokenException::class);
204+
205+
$helper = $this->getPasswordResetHelper();
206+
$helper->validateTokenAndFetchUser(\substr($this->randomToken, 0, 39));
207+
}
208+
201209
public function testExceptionIsThrownIfTokenNotFoundDuringValidation(): void
202210
{
203211
$this->mockRepo
@@ -209,7 +217,7 @@ public function testExceptionIsThrownIfTokenNotFoundDuringValidation(): void
209217
$this->expectException(InvalidResetPasswordTokenException::class);
210218

211219
$helper = $this->getPasswordResetHelper();
212-
$helper->validateTokenAndFetchUser('1234');
220+
$helper->validateTokenAndFetchUser($this->randomToken);
213221
}
214222

215223
public function testValidateTokenThrowsExceptionOnExpiredResetRequest(): void
@@ -223,7 +231,7 @@ public function testValidateTokenThrowsExceptionOnExpiredResetRequest(): void
223231
$this->mockRepo
224232
->expects($this->once())
225233
->method('findResetPasswordRequest')
226-
->with($this->randomToken)
234+
->with(\substr($this->randomToken, 0, 20))
227235
->willReturn($this->mockResetRequest)
228236
;
229237

@@ -256,7 +264,7 @@ public function testValidateTokenFetchesUserIfTokenNotExpired(): void
256264
$this->mockRepo
257265
->expects($this->once())
258266
->method('findResetPasswordRequest')
259-
->with($this->randomToken)
267+
->with(\substr($this->randomToken, 0, 20))
260268
->willReturn($this->mockResetRequest)
261269
;
262270

0 commit comments

Comments
 (0)