Skip to content

Commit ed3ec73

Browse files
dinamicdkarlovi
authored andcommitted
~ using random_bytes() to generate a random token; added a PHP 5.x polyfill for random_bytes() and random_int().
1 parent 7979afc commit ed3ec73

File tree

3 files changed

+61
-15
lines changed

3 files changed

+61
-15
lines changed

Tests/Util/RandomTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace FOS\OAuthServerBundle\Tests\Util;
4+
5+
use FOS\OAuthServerBundle\Util\Random;
6+
use phpmock\phpunit\PHPMock;
7+
8+
/**
9+
* Class RandomTest
10+
*
11+
* @author Nikola Petkanski <[email protected]
12+
*/
13+
class RandomTest extends \PHPUnit_Framework_TestCase
14+
{
15+
use PHPMock;
16+
17+
public function setUp()
18+
{
19+
parent::setUp();
20+
}
21+
22+
/**
23+
* @runInSeparateProcess
24+
*/
25+
public function testGenerateTokenWillUseRandomBytesIfAvailable()
26+
{
27+
$hashResult = \random_bytes(32);
28+
29+
$this->getFunctionMock('FOS\OAuthServerBundle\Util', 'random_bytes')
30+
->expects($this->once())
31+
->with(32)
32+
->willReturn($hashResult)
33+
;
34+
35+
$bin2hexResult = \bin2hex($hashResult);
36+
$this->getFunctionMock('FOS\OAuthServerBundle\Util', 'bin2hex')
37+
->expects($this->once())
38+
->with($hashResult)
39+
->willReturn($bin2hexResult)
40+
;
41+
42+
$baseConvertResult = \base_convert($bin2hexResult, 16, 36);
43+
$this->getFunctionMock('FOS\OAuthServerBundle\Util', 'base_convert')
44+
->expects($this->once())
45+
->with($bin2hexResult, 16, 36)
46+
->willReturn($baseConvertResult)
47+
;
48+
49+
$this->assertSame($baseConvertResult, Random::generateToken());
50+
}
51+
}

Util/Random.php

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,16 @@
1111

1212
namespace FOS\OAuthServerBundle\Util;
1313

14+
/**
15+
* Class Random
16+
*
17+
* @author Nikola Petkanski <[email protected]
18+
*/
1419
class Random
1520
{
1621
public static function generateToken()
1722
{
18-
$bytes = false;
19-
if (function_exists('openssl_random_pseudo_bytes') && 0 !== stripos(PHP_OS, 'win')) {
20-
$bytes = openssl_random_pseudo_bytes(32, $strong);
21-
22-
if (true !== $strong) {
23-
$bytes = false;
24-
}
25-
}
26-
27-
// let's just hope we got a good seed
28-
if (false === $bytes) {
29-
$bytes = hash('sha256', uniqid(mt_rand(), true), true);
30-
}
23+
$bytes = random_bytes(32);
3124

3225
return base_convert(bin2hex($bytes), 16, 36);
3326
}

composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
"friendsofsymfony/oauth2-php": "~1.1",
2121
"symfony/framework-bundle": "~2.8|~3.0|^4.0",
2222
"symfony/security-bundle": "~2.8|~3.0|^4.0",
23-
"symfony/dependency-injection": "^2.8|~3.0|^4.0"
23+
"symfony/dependency-injection": "^2.8|~3.0|^4.0",
24+
"paragonie/random_compat": "^1|^2"
2425
},
2526
"require-dev": {
2627
"symfony/class-loader": "~2.8|~3.0|^4.0",
@@ -35,7 +36,8 @@
3536
"doctrine/doctrine-bundle": "~1.0",
3637
"doctrine/orm": "~2.2",
3738
"phpunit/phpunit": "~4.8|~5.0",
38-
"symfony/phpunit-bridge": "~2.8|~3.0|^4.0"
39+
"symfony/phpunit-bridge": "~2.8|~3.0|^4.0",
40+
"php-mock/php-mock-phpunit": "^1.1"
3941
},
4042
"suggest": {
4143
"doctrine/doctrine-bundle": "*",

0 commit comments

Comments
 (0)