diff --git a/src/Util/KeyAlgorithm/DefaultAlgorithm.php b/src/Util/KeyAlgorithm/DefaultAlgorithm.php new file mode 100644 index 00000000..a03c4e1d --- /dev/null +++ b/src/Util/KeyAlgorithm/DefaultAlgorithm.php @@ -0,0 +1,36 @@ + + * @copyright Copyright (c) Alex Bilbie + * @license http://mit-license.org/ + * @link https://github.com/thephpleague/oauth2-server + */ + +namespace League\OAuth2\Server\Util\KeyAlgorithm; + + +class DefaultAlgorithm implements KeyAlgorithmInterface +{ + /** + * {@inheritdoc} + */ + public function generate($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); + } +} diff --git a/src/Util/KeyAlgorithm/KeyAlgorithmInterface.php b/src/Util/KeyAlgorithm/KeyAlgorithmInterface.php new file mode 100644 index 00000000..a2ba7f51 --- /dev/null +++ b/src/Util/KeyAlgorithm/KeyAlgorithmInterface.php @@ -0,0 +1,22 @@ + + * @copyright Copyright (c) Alex Bilbie + * @license http://mit-license.org/ + * @link https://github.com/thephpleague/oauth2-server + */ + +namespace League\OAuth2\Server\Util\KeyAlgorithm; + +interface KeyAlgorithmInterface +{ + /** + * Generate a new unique code + * @param integer $len Length of the generated code + * @return string + */ + public function generate($len = 40); +} diff --git a/src/Util/SecureKey.php b/src/Util/SecureKey.php index 7f579d41..51767d7c 100644 --- a/src/Util/SecureKey.php +++ b/src/Util/SecureKey.php @@ -2,39 +2,52 @@ /** * OAuth 2.0 Secure key generator * - * @package league/oauth2-server + * @package php-loep/oauth2-server * @author Alex Bilbie - * @copyright Copyright (c) Alex Bilbie + * @copyright Copyright (c) 2013 PHP League of Extraordinary Packages * @license http://mit-license.org/ - * @link https://github.com/thephpleague/oauth2-server + * @link http://github.com/php-loep/oauth2-server */ 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 * @return string */ - public static function make($len = 40) + public static function generate($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()->generate($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 (is_null(self::$algorithm)) { + self::$algorithm = new DefaultAlgorithm(); } - return substr(str_replace(array('/', '+', '='), '', base64_encode($bytes)), 0, $len); + return self::$algorithm; } -} \ No newline at end of file +} diff --git a/tests/util/SecureKeyTest.php b/tests/util/SecureKeyTest.php index 2be3e12b..f3b59483 100644 --- a/tests/util/SecureKeyTest.php +++ b/tests/util/SecureKeyTest.php @@ -2,19 +2,36 @@ namespace LeagueTests\Util; -use League\OAuth2\Server\Util\SecureKey; +use \League\OAuth2\Server\Util\SecureKey; use \Mockery as M; class SecureKeyTest extends \PHPUnit_Framework_TestCase { - function testMake() - { - $v1 = SecureKey::make(); - $v2 = SecureKey::make(); - $v3 = SecureKey::make(50); + function testGenerate() + { + $v1 = SecureKey::generate(); + $v2 = SecureKey::generate(); + $v3 = SecureKey::generate(50); - $this->assertEquals(40, strlen($v1)); - $this->assertTrue($v1 !== $v2); - $this->assertEquals(50, strlen($v3)); - } + $this->assertEquals(40, strlen($v1)); + $this->assertTrue($v1 !== $v2); + $this->assertEquals(50, strlen($v3)); + } + + public function testGenerateWithDifferentAlgorithm() + { + $algorithm = $this->getMock('League\OAuth2\Server\Util\KeyAlgorithm\KeyAlgorithmInterface'); + + $result = 'dasdsdsaads'; + $algorithm + ->expects($this->once()) + ->method('generate') + ->with(11) + ->will($this->returnValue($result)) + ; + + SecureKey::setAlgorithm($algorithm); + $this->assertSame($algorithm, SecureKey::getAlgorithm()); + $this->assertEquals($result, SecureKey::generate(11)); + } } \ No newline at end of file