This commit is contained in:
Alex Bilbie 2014-04-06 21:08:35 +01:00
parent b2c07aa68f
commit 5893ba4e8e
4 changed files with 113 additions and 25 deletions

View File

@ -0,0 +1,36 @@
<?php
/**
* OAuth 2.0 Secure key interface
*
* @package league/oauth2-server
* @author Alex Bilbie <hello@alexbilbie.com>
* @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);
}
}

View File

@ -0,0 +1,22 @@
<?php
/**
* OAuth 2.0 Secure key interface
*
* @package league/oauth2-server
* @author Alex Bilbie <hello@alexbilbie.com>
* @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);
}

View File

@ -2,39 +2,52 @@
/**
* OAuth 2.0 Secure key generator
*
* @package league/oauth2-server
* @package php-loep/oauth2-server
* @author Alex Bilbie <hello@alexbilbie.com>
* @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);
// 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 self::getAlgorithm()->generate($len);
}
return substr(str_replace(array('/', '+', '='), '', base64_encode($bytes)), 0, $len);
/**
* @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 self::$algorithm;
}
}

View File

@ -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()
function testGenerate()
{
$v1 = SecureKey::make();
$v2 = SecureKey::make();
$v3 = SecureKey::make(50);
$v1 = SecureKey::generate();
$v2 = SecureKey::generate();
$v3 = SecureKey::generate(50);
$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));
}
}