Skip to content

Commit dd030c0

Browse files
authored
Replace OpenSSL token generator with bin2hex + random_bytes PHP native functions (#64)
1 parent 1800e2e commit dd030c0

File tree

8 files changed

+67
-62
lines changed

8 files changed

+67
-62
lines changed

composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
],
1111
"require": {
1212
"php": "^8.1",
13-
"ext-openssl": "*",
1413
"symfony/framework-bundle": "^6.4|^7.4|^8.0",
1514
"doctrine/common": "^3.0",
1615
"doctrine/orm": "^3.0",

config/services.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
<argument type="service" id="yokai_security_token.token_entity_repository"/>
2727
</service>
2828

29-
<service id="yokai_security_token.open_ssl_token_generator"
30-
class="Yokai\SecurityTokenBundle\Generator\OpenSslTokenGenerator"
29+
<service id="yokai_security_token.bin2hex_token_generator"
30+
class="Yokai\SecurityTokenBundle\Generator\Bin2HexTokenGenerator"
3131
public="false"/>
3232

3333
<service id="yokai_security_token.default_information_guesser"

doc/2-configuration.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Each token can have following options :
2020

2121
Default values fallback to :
2222

23-
- `generator` : [`yokai_security_token.open_ssl_token_generator`](../src/Generator/OpenSslTokenGenerator.php)
23+
- `generator` : [`yokai_security_token.bin2hex_token_generator`](../src/Generator/Bin2HexTokenGenerator.php)
2424
- `duration` : `+2 days`
2525
- `usages` : `1`
2626
- `keep` : `+1 month`

src/DependencyInjection/Configuration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ private function getTokensNode(): ArrayNodeDefinition
6464
->prototype('array')
6565
->children()
6666
->scalarNode('generator')
67-
->defaultValue('yokai_security_token.open_ssl_token_generator')
67+
->defaultValue('yokai_security_token.bin2hex_token_generator')
6868
->end()
6969
->scalarNode('duration')
7070
->defaultValue('+2 days')
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yokai\SecurityTokenBundle\Generator;
6+
7+
/**
8+
* This token generator is using `bin2hex` along with `random_bytes` to generate random token values.
9+
*
10+
* @author Yann Eugoné <[email protected]>
11+
*/
12+
final class Bin2HexTokenGenerator implements TokenGeneratorInterface
13+
{
14+
public function __construct(
15+
/**
16+
* @var int<1, max> The token length
17+
*/
18+
private readonly int $length = 64,
19+
) {
20+
}
21+
22+
public function generate(): string
23+
{
24+
$byteLength = (int)\ceil($this->length / 2);
25+
\assert($byteLength >= 1);
26+
$hex = \bin2hex(\random_bytes($byteLength));
27+
28+
return \substr($hex, 0, $this->length);
29+
}
30+
}

src/Generator/OpenSslTokenGenerator.php

Lines changed: 0 additions & 28 deletions
This file was deleted.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yokai\SecurityTokenBundle\Tests\Generator;
6+
7+
use PHPUnit\Framework\Attributes\DataProvider;
8+
use Yokai\SecurityTokenBundle\Generator\Bin2HexTokenGenerator;
9+
use PHPUnit\Framework\TestCase;
10+
11+
final class Bin2HexTokenGeneratorTest extends TestCase
12+
{
13+
#[DataProvider('length')]
14+
public function test_it_generate_unique_token(int $length): void
15+
{
16+
$generator = new Bin2HexTokenGenerator($length);
17+
18+
$tokens = [];
19+
for ($i = 1; $i <= 1000; $i++) {
20+
$tokens[] = $value = $generator->generate();
21+
self::assertSame($length, \mb_strlen($value));
22+
}
23+
24+
self::assertSame(\array_unique($tokens), $tokens);
25+
}
26+
27+
public static function length(): \Generator
28+
{
29+
yield [64];
30+
yield [10];
31+
yield [11];
32+
}
33+
}

tests/Generator/OpenSslTokenGeneratorTest.php

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)