Skip to content

Commit 63d7b09

Browse files
Merge branch '6.2' into 6.3
* 6.2: cs fix [Messenger] Fix passing options set via tags to handler descriptors random_bytes length should be an int greater than 0 enforce UTC timezone in test [DependencyInjection] Fix autocasting null env values to empty string Fix executable bit Fix executable bit Readme: Replace Stack Overflow with GitHub Discussions [DoctrineBridge] Remove outdated comment [DependencyInjection] Fix annotation [SecurityBundle] Do not translate `Bearer` header’s `error_description` [String] Fix Inflector for 'status' [DependencyInjection] Fix resource tracking for lazy services [EventDispatcher] [EventDispatcher] Throw exception when listener method cannot be resolved
2 parents 1f505c9 + 5f2850e commit 63d7b09

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

Tests/TokenGenerator/UriSafeTokenGeneratorTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,28 @@ public function testGenerateToken()
5757
$this->assertStringNotMatchesFormat('%S/%S', $token, 'is URI safe');
5858
$this->assertStringNotMatchesFormat('%S=%S', $token, 'is URI safe');
5959
}
60+
61+
/**
62+
* @dataProvider validDataProvider
63+
*/
64+
public function testValidLength(int $entropy, int $length)
65+
{
66+
$generator = new UriSafeTokenGenerator($entropy);
67+
$token = $generator->generateToken();
68+
$this->assertSame($length, \strlen($token));
69+
}
70+
71+
public static function validDataProvider(): \Iterator
72+
{
73+
yield [24, 4];
74+
yield 'Float length' => [20, 3];
75+
}
76+
77+
public function testInvalidLength()
78+
{
79+
$this->expectException(\InvalidArgumentException::class);
80+
$this->expectExceptionMessage('Entropy should be greater than 7.');
81+
82+
new UriSafeTokenGenerator(7);
83+
}
6084
}

TokenGenerator/UriSafeTokenGenerator.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ class UriSafeTokenGenerator implements TokenGeneratorInterface
2727
*/
2828
public function __construct(int $entropy = 256)
2929
{
30+
if ($entropy <= 7) {
31+
throw new \InvalidArgumentException('Entropy should be greater than 7.');
32+
}
33+
3034
$this->entropy = $entropy;
3135
}
3236

@@ -35,7 +39,7 @@ public function generateToken(): string
3539
// Generate an URI safe base64 encoded string that does not contain "+",
3640
// "/" or "=" which need to be URL encoded and make URLs unnecessarily
3741
// longer.
38-
$bytes = random_bytes($this->entropy / 8);
42+
$bytes = random_bytes(intdiv($this->entropy, 8));
3943

4044
return rtrim(strtr(base64_encode($bytes), '+/', '-_'), '=');
4145
}

0 commit comments

Comments
 (0)