Merge pull request #151 from giftcards/develop

add the ability to change the algorithm that is used to generate the token strings
This commit is contained in:
Alex Bilbie 2014-04-06 21:13:58 +01:00
commit 785d3bd21f
5 changed files with 113 additions and 10 deletions

View File

@ -0,0 +1,35 @@
<?php
/**
* Created by PhpStorm.
* User: jderay
* Date: 3/11/14
* Time: 12:22 PM
*/
namespace League\OAuth2\Server\Util\KeyAlgorithm;
class DefaultAlgorithm implements KeyAlgorithmInterface
{
/**
* @param int $len
* @return string
* @throws \Exception
*/
public function make($len = 40)
{
// We generate twice as many bytes here because we want to ensure we have
// enough after we base64 encode it to get the length we need because we
// take out the "/", "+", and "=" characters.
$bytes = openssl_random_pseudo_bytes($len * 2, $strong);
// We want to stop execution if the key fails because, well, that is bad.
if ($bytes === false || $strong === false) {
// @codeCoverageIgnoreStart
throw new \Exception('Error Generating Key');
// @codeCoverageIgnoreEnd
}
return substr(str_replace(array('/', '+', '='), '', base64_encode($bytes)), 0, $len);
}
}

View File

@ -0,0 +1,15 @@
<?php
/**
* Created by PhpStorm.
* User: jderay
* Date: 3/11/14
* Time: 12:22 PM
*/
namespace League\OAuth2\Server\Util\KeyAlgorithm;
interface KeyAlgorithmInterface
{
public function make($len = 40);
}

View File

@ -11,11 +11,16 @@
namespace League\OAuth2\Server\Util;
use League\OAuth2\Server\Util\KeyAlgorithm\DefaultAlgorithm;
use League\OAuth2\Server\Util\KeyAlgorithm\KeyAlgorithmInterface;
/**
* SecureKey class
*/
class SecureKey
{
protected static $algorithm;
/**
* Generate a new unique code
* @param integer $len Length of the generated code
@ -23,18 +28,27 @@ class SecureKey
*/
public static function make($len = 40)
{
// We generate twice as many bytes here because we want to ensure we have
// enough after we base64 encode it to get the length we need because we
// take out the "/", "+", and "=" characters.
$bytes = openssl_random_pseudo_bytes($len * 2, $strong);
return self::getAlgorithm()->make($len);
}
// We want to stop execution if the key fails because, well, that is bad.
if ($bytes === false || $strong === false) {
// @codeCoverageIgnoreStart
throw new \Exception('Error Generating Key');
// @codeCoverageIgnoreEnd
/**
* @param KeyAlgorithmInterface $algorithm
*/
public static function setAlgorithm(KeyAlgorithmInterface $algorithm)
{
self::$algorithm = $algorithm;
}
/**
* @return KeyAlgorithmInterface
*/
public static function getAlgorithm()
{
if (!self::$algorithm) {
self::$algorithm = new DefaultAlgorithm();
}
return substr(str_replace(array('/', '+', '='), '', base64_encode($bytes)), 0, $len);
return self::$algorithm;
}
}

View File

@ -12,4 +12,21 @@ class Secure_Key_test extends PHPUnit_Framework_TestCase
$this->assertTrue($v1 !== $v2);
$this->assertEquals(50, strlen($v3));
}
public function test_make_with_different_algorithm()
{
$algorithm = $this->getMock('League\OAuth2\Server\Util\KeyAlgorithm\KeyAlgorithmInterface');
$result = 'dasdsdsaads';
$algorithm
->expects($this->once())
->method('make')
->with(11)
->will($this->returnValue($result))
;
League\OAuth2\Server\Util\SecureKey::setAlgorithm($algorithm);
$this->assertSame($algorithm, League\OAuth2\Server\Util\SecureKey::getAlgorithm());
$this->assertEquals($result, League\OAuth2\Server\Util\SecureKey::make(11));
}
}

View File

@ -0,0 +1,22 @@
<?php
/**
* Created by PhpStorm.
* User: jderay
* Date: 3/11/14
* Time: 12:31 PM
*/
class Default_Algorithm_test extends PHPUnit_Framework_TestCase
{
public function test_make()
{
$algorithm = new League\OAuth2\Server\Util\KeyAlgorithm\DefaultAlgorithm();
$v1 = $algorithm->make();
$v2 = $algorithm->make();
$v3 = $algorithm->make(50);
$this->assertEquals(40, strlen($v1));
$this->assertTrue($v1 !== $v2);
$this->assertEquals(50, strlen($v3));
}
}