mirror of
https://github.com/elyby/oauth2-server.git
synced 2025-01-03 10:41:51 +05:30
Fixes #151
This commit is contained in:
parent
b2c07aa68f
commit
5893ba4e8e
36
src/Util/KeyAlgorithm/DefaultAlgorithm.php
Normal file
36
src/Util/KeyAlgorithm/DefaultAlgorithm.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
22
src/Util/KeyAlgorithm/KeyAlgorithmInterface.php
Normal file
22
src/Util/KeyAlgorithm/KeyAlgorithmInterface.php
Normal 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);
|
||||||
|
}
|
@ -2,39 +2,52 @@
|
|||||||
/**
|
/**
|
||||||
* OAuth 2.0 Secure key generator
|
* OAuth 2.0 Secure key generator
|
||||||
*
|
*
|
||||||
* @package league/oauth2-server
|
* @package php-loep/oauth2-server
|
||||||
* @author Alex Bilbie <hello@alexbilbie.com>
|
* @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/
|
* @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;
|
namespace League\OAuth2\Server\Util;
|
||||||
|
|
||||||
|
use League\OAuth2\Server\Util\KeyAlgorithm\DefaultAlgorithm;
|
||||||
|
use League\OAuth2\Server\Util\KeyAlgorithm\KeyAlgorithmInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* SecureKey class
|
* SecureKey class
|
||||||
*/
|
*/
|
||||||
class SecureKey
|
class SecureKey
|
||||||
{
|
{
|
||||||
|
protected static $algorithm;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate a new unique code
|
* Generate a new unique code
|
||||||
* @param integer $len Length of the generated code
|
* @param integer $len Length of the generated code
|
||||||
* @return string
|
* @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
|
return self::getAlgorithm()->generate($len);
|
||||||
// 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) {
|
* @param KeyAlgorithmInterface $algorithm
|
||||||
// @codeCoverageIgnoreStart
|
*/
|
||||||
throw new \Exception('Error Generating Key');
|
public static function setAlgorithm(KeyAlgorithmInterface $algorithm)
|
||||||
// @codeCoverageIgnoreEnd
|
{
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,19 +2,36 @@
|
|||||||
|
|
||||||
namespace LeagueTests\Util;
|
namespace LeagueTests\Util;
|
||||||
|
|
||||||
use League\OAuth2\Server\Util\SecureKey;
|
use \League\OAuth2\Server\Util\SecureKey;
|
||||||
use \Mockery as M;
|
use \Mockery as M;
|
||||||
|
|
||||||
class SecureKeyTest extends \PHPUnit_Framework_TestCase
|
class SecureKeyTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
function testMake()
|
function testGenerate()
|
||||||
{
|
{
|
||||||
$v1 = SecureKey::make();
|
$v1 = SecureKey::generate();
|
||||||
$v2 = SecureKey::make();
|
$v2 = SecureKey::generate();
|
||||||
$v3 = SecureKey::make(50);
|
$v3 = SecureKey::generate(50);
|
||||||
|
|
||||||
$this->assertEquals(40, strlen($v1));
|
$this->assertEquals(40, strlen($v1));
|
||||||
$this->assertTrue($v1 !== $v2);
|
$this->assertTrue($v1 !== $v2);
|
||||||
$this->assertEquals(50, strlen($v3));
|
$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));
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user