mirror of
https://github.com/elyby/oauth2-server.git
synced 2024-12-23 05:29:52 +05:30
added the ability to change the algorithm used to generate the token strings. added files missing in last commit
This commit is contained in:
parent
901aab9deb
commit
b12a1d84df
@ -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);
|
||||||
|
}
|
||||||
|
}
|
@ -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);
|
||||||
|
}
|
24
tests/util/key_algorithm/DefaultAlgorithmTest.php
Normal file
24
tests/util/key_algorithm/DefaultAlgorithmTest.php
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Created by PhpStorm.
|
||||||
|
* User: jderay
|
||||||
|
* Date: 3/11/14
|
||||||
|
* Time: 12:31 PM
|
||||||
|
*/
|
||||||
|
|
||||||
|
use League\OAuth2\Server\Util\KeyAlgorithm\DefaultAlgorithm;
|
||||||
|
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user