Add Oauth2 tests

This commit is contained in:
Dennis Eichhorn 2020-10-06 14:26:36 +02:00
parent daebfe1c57
commit 77bbb3f7a7
29 changed files with 643 additions and 23 deletions

View File

@ -6,6 +6,7 @@
*
* @package phpOMS\Auth\OAuth2\Grant
* @copyright Dennis Eichhorn
* @copyright MIT - Copyright (c) 2013-2018 Alex Bilbie <hello@alexbilbie.com> - thephpleague/oauth2-client
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org

View File

@ -6,6 +6,7 @@
*
* @package phpOMS\Auth\OAuth2\Grant
* @copyright Dennis Eichhorn
* @copyright MIT - Copyright (c) 2013-2018 Alex Bilbie <hello@alexbilbie.com> - thephpleague/oauth2-client
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org

View File

@ -6,6 +6,7 @@
*
* @package phpOMS\Auth\OAuth2\Grant
* @copyright Dennis Eichhorn
* @copyright MIT - Copyright (c) 2013-2018 Alex Bilbie <hello@alexbilbie.com> - thephpleague/oauth2-client
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org

View File

@ -6,6 +6,7 @@
*
* @package phpOMS\Auth\OAuth2\Grant
* @copyright Dennis Eichhorn
* @copyright MIT - Copyright (c) 2013-2018 Alex Bilbie <hello@alexbilbie.com> - thephpleague/oauth2-client
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
@ -46,7 +47,7 @@ class GrantFactory
protected function registerDefaultGrant(string $name) : self
{
$class = \str_replace(' ', '', \ucwords(\str_replace(['-', '_'], ' ', $name)));
$class = 'phpOMS\\OAuth2\\Grant\\' . $class;
$class = 'phpOMS\\Auth\\OAuth2\\Grant\\' . $class;
if (!\is_subclass_of($class, GrantAbstract::class)) {
throw new \Exception();

View File

@ -6,6 +6,7 @@
*
* @package phpOMS\Auth\OAuth2\Grant
* @copyright Dennis Eichhorn
* @copyright MIT - Copyright (c) 2013-2018 Alex Bilbie <hello@alexbilbie.com> - thephpleague/oauth2-client
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org

View File

@ -6,6 +6,7 @@
*
* @package phpOMS\Auth\OAuth2\Grant
* @copyright Dennis Eichhorn
* @copyright MIT - Copyright (c) 2013-2018 Alex Bilbie <hello@alexbilbie.com> - thephpleague/oauth2-client
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org

View File

@ -6,6 +6,7 @@
*
* @package phpOMS\Auth\OAuth2\OptionProvider
* @copyright Dennis Eichhorn
* @copyright MIT - Copyright (c) 2013-2018 Alex Bilbie <hello@alexbilbie.com> - thephpleague/oauth2-client
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
@ -27,6 +28,10 @@ class HttpBasicAuthOptionProvider extends PostAuthOptionProvider
{
public function getAccessTokenOptions(string $method, array $params) : array
{
if (!isset($params['client_id'], $params['client_secret'])) {
return [];
}
$encoded = \base64_encode($params['client_id'] . ':' . $params['client_secret']);
unset($params['client_id'], $params['client_secret']);

View File

@ -6,6 +6,7 @@
*
* @package phpOMS\Auth\OAuth2\OptionProvider
* @copyright Dennis Eichhorn
* @copyright MIT - Copyright (c) 2013-2018 Alex Bilbie <hello@alexbilbie.com> - thephpleague/oauth2-client
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org

View File

@ -6,6 +6,7 @@
*
* @package phpOMS\Auth\OAuth2\OptionProvider
* @copyright Dennis Eichhorn
* @copyright MIT - Copyright (c) 2013-2018 Alex Bilbie <hello@alexbilbie.com> - thephpleague/oauth2-client
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
@ -43,6 +44,6 @@ class PostAuthOptionProvider implements OptionProviderInterface
protected function getAccessTokenBody(array $params) : string
{
return \http_build_query($params, null, '&', \PHP_QUERY_RFC3986);
return \http_build_query($params, '', '&', \PHP_QUERY_RFC3986);
}
}

View File

@ -6,6 +6,7 @@
*
* @package phpOMS\Auth\OAuth2\Provider
* @copyright Dennis Eichhorn
* @copyright MIT - Copyright (c) 2013-2018 Alex Bilbie <hello@alexbilbie.com> - thephpleague/oauth2-client
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org

View File

@ -6,6 +6,7 @@
*
* @package phpOMS\Auth\OAuth2\Provider
* @copyright Dennis Eichhorn
* @copyright MIT - Copyright (c) 2013-2018 Alex Bilbie <hello@alexbilbie.com> - thephpleague/oauth2-client
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org

View File

@ -6,6 +6,7 @@
*
* @package phpOMS\Auth\OAuth2\Provider
* @copyright Dennis Eichhorn
* @copyright MIT - Copyright (c) 2013-2018 Alex Bilbie <hello@alexbilbie.com> - thephpleague/oauth2-client
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org

View File

@ -6,6 +6,7 @@
*
* @package phpOMS\Auth\OAuth2\Provider
* @copyright Dennis Eichhorn
* @copyright MIT - Copyright (c) 2013-2018 Alex Bilbie <hello@alexbilbie.com> - thephpleague/oauth2-client
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org

View File

@ -6,6 +6,7 @@
*
* @package phpOMS\Auth\OAuth2\Token
* @copyright Dennis Eichhorn
* @copyright MIT - Copyright (c) 2013-2018 Alex Bilbie <hello@alexbilbie.com> - thephpleague/oauth2-client
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
@ -26,7 +27,7 @@ class AccessToken implements AccessTokenInterface, ResourceOwnerAccessTokenInter
{
protected string $accessToken;
protected ?int $expires = null;
protected int $expires = -1;
protected ?string $refreshToken = null;
@ -70,7 +71,7 @@ class AccessToken implements AccessTokenInterface, ResourceOwnerAccessTokenInter
return $this->accessToken;
}
public function getExpires() : ?int
public function getExpires() : int
{
return $this->expires;
}
@ -87,12 +88,12 @@ class AccessToken implements AccessTokenInterface, ResourceOwnerAccessTokenInter
public function hasExpired() : bool
{
return $this->expires < \time();
return $this->expires > 0 && $this->expires < \time();
}
public function getValues() : array
{
return $this->vallues;
return $this->values;
}
public function __toString()
@ -102,21 +103,18 @@ class AccessToken implements AccessTokenInterface, ResourceOwnerAccessTokenInter
public function jsonSerialize()
{
$params = $this->values;
$params = $this->values;
$params['access_token'] = $this->accessToken;
if (isset($this->accessToken)) {
$params['access_token'] = $this->accessToken;
}
if (isset($this->refreshToken)) {
if ($this->refreshToken !== null) {
$params['refresh_token'] = $this->refreshToken;
}
if (isset($this->expires)) {
if ($this->expires > 0) {
$params['expires'] = $this->expires;
}
if (isset($this->resourceOwnerId)) {
if ($this->resourceOwnerId !== null) {
$params['resource_owner_id'] = $this->resourceOwnerId;
}

View File

@ -6,6 +6,7 @@
*
* @package phpOMS\Auth\OAuth2\Token
* @copyright Dennis Eichhorn
* @copyright MIT - Copyright (c) 2013-2018 Alex Bilbie <hello@alexbilbie.com> - thephpleague/oauth2-client
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
@ -29,7 +30,7 @@ interface AccessTokenInterface extends \JsonSerializable
public function getRefreshToken() : ?string;
public function getExpires() : ?int;
public function getExpires() : int;
public function hasExpired() : bool;

View File

@ -6,6 +6,7 @@
*
* @package phpOMS\Auth\OAuth2\Token
* @copyright Dennis Eichhorn
* @copyright MIT - Copyright (c) 2013-2018 Alex Bilbie <hello@alexbilbie.com> - thephpleague/oauth2-client
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org

View File

@ -6,8 +6,8 @@
*
* @package phpOMS\Math\Matrix
* @copyright Dennis Eichhorn
* @copyright JAMA - https://math.nist.gov/javanumerics/jama/
* @license OMS License 1.0
* @license JAMA - https://math.nist.gov/javanumerics/jama/
* @version 1.0.0
* @link https://orange-management.org
*/
@ -24,7 +24,6 @@ use phpOMS\Math\Matrix\Exception\InvalidDimensionException;
*
* @package phpOMS\Math\Matrix
* @license OMS License 1.0
* @license JAMA - https://math.nist.gov/javanumerics/jama/
* @link https://orange-management.org
* @since 1.0.0
*/

View File

@ -6,8 +6,8 @@
*
* @package phpOMS\Math\Matrix
* @copyright Dennis Eichhorn
* @copyright JAMA - https://math.nist.gov/javanumerics/jama/
* @license OMS License 1.0
* @license JAMA - https://math.nist.gov/javanumerics/jama/
* @version 1.0.0
* @link https://orange-management.org
*/
@ -25,7 +25,6 @@ use phpOMS\Math\Geometry\Shape\D2\Triangle;
*
* @package phpOMS\Math\Matrix
* @license OMS License 1.0
* @license JAMA - https://math.nist.gov/javanumerics/jama/
* @link https://orange-management.org
* @since 1.0.0
*/

View File

@ -6,8 +6,8 @@
*
* @package phpOMS\Math\Matrix
* @copyright Dennis Eichhorn
* @copyright JAMA - https://math.nist.gov/javanumerics/jama/
* @license OMS License 1.0
* @license JAMA - https://math.nist.gov/javanumerics/jama/
* @version 1.0.0
* @link https://orange-management.org
*/
@ -24,7 +24,6 @@ use phpOMS\Math\Matrix\Exception\InvalidDimensionException;
*
* @package phpOMS\Math\Matrix
* @license OMS License 1.0
* @license JAMA - https://math.nist.gov/javanumerics/jama/
* @link https://orange-management.org
* @since 1.0.0
*/

View File

@ -6,8 +6,8 @@
*
* @package phpOMS\Math\Matrix
* @copyright Dennis Eichhorn
* @copyright JAMA - https://math.nist.gov/javanumerics/jama/
* @license OMS License 1.0
* @license JAMA - https://math.nist.gov/javanumerics/jama/
* @version 1.0.0
* @link https://orange-management.org
*/
@ -25,7 +25,6 @@ use phpOMS\Math\Matrix\Exception\InvalidDimensionException;
*
* @package phpOMS\Math\Matrix
* @license OMS License 1.0
* @license JAMA - https://math.nist.gov/javanumerics/jama/
* @link https://orange-management.org
* @since 1.0.0
*/

View File

@ -0,0 +1,65 @@
<?php
/**
* Orange Management
*
* PHP Version 7.4
*
* @package tests
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
*/
declare(strict_types=1);
namespace phpOMS\tests\Auth\OAuth2\Grant;
use phpOMS\Auth\OAuth2\Grant\AuthorizationCode;
/**
* @internal
*/
class AuthorizationCodeTest extends \PHPUnit\Framework\TestCase
{
private AuthorizationCode $grant;
public function setUp() : void
{
$this->grant = new AuthorizationCode();
}
public function testDefault() : void
{
self::assertEquals('authorization_code', $this->grant->__toString());
self::assertEquals(
[
'code' => 'value',
'option' => '2',
'test' => 'value2',
],
$this->grant->prepareRequestParamters(
[
'code' => 'value'
],
[
'option' => '2',
'test' => 'value2'
]
)
);
}
public function testMissingDefaultOption() : void
{
$this->expectException(\Exception::class);
$this->grant->prepareRequestParamters(
[
'test' => 'value'
],
[
'option' => '2',
'test' => 'value2'
]
);
}
}

View File

@ -0,0 +1,51 @@
<?php
/**
* Orange Management
*
* PHP Version 7.4
*
* @package tests
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
*/
declare(strict_types=1);
namespace phpOMS\tests\Auth\OAuth2\Grant;
use phpOMS\Auth\OAuth2\Grant\ClientCredentials;
/**
* @internal
*/
class ClientCredentialsTest extends \PHPUnit\Framework\TestCase
{
private ClientCredentials $grant;
public function setUp() : void
{
$this->grant = new ClientCredentials();
}
public function testDefault() : void
{
self::assertEquals('client_credentials', $this->grant->__toString());
self::assertEquals(
[
'code' => 'value',
'option' => '2',
'test' => 'value2',
],
$this->grant->prepareRequestParamters(
[
'code' => 'value'
],
[
'option' => '2',
'test' => 'value2'
]
)
);
}
}

View File

@ -0,0 +1,73 @@
<?php
/**
* Orange Management
*
* PHP Version 7.4
*
* @package tests
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
*/
declare(strict_types=1);
namespace phpOMS\tests\Auth\OAuth2\Grant;
use phpOMS\Auth\OAuth2\Grant\GrantAbstract;
/**
* @internal
*/
class GrantAbstractTest extends \PHPUnit\Framework\TestCase
{
private GrantAbstract $grant;
public function setUp() : void
{
$this->grant = new class extends GrantAbstract {
protected function getName() : string
{
return 'TestGrant';
}
protected function getRequiredRequestParameters() : array
{
return ['test'];
}
};
}
public function testDefault() : void
{
self::assertEquals('TestGrant', $this->grant->__toString());
self::assertEquals(
[
'test' => 'value2',
'option' => '2'
],
$this->grant->prepareRequestParamters(
[
'test' => 'value'
],
[
'option' => '2',
'test' => 'value2'
]
)
);
}
public function testMissingDefaultOption() : void
{
$this->expectException(\Exception::class);
$this->grant->prepareRequestParamters(
[
'something' => 'value'
],
[
'option' => '2',
]
);
}
}

View File

@ -0,0 +1,62 @@
<?php
/**
* Orange Management
*
* PHP Version 7.4
*
* @package tests
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
*/
declare(strict_types=1);
namespace phpOMS\tests\Auth\OAuth2\Grant;
use phpOMS\Auth\OAuth2\Grant\AuthorizationCode;
use phpOMS\Auth\OAuth2\Grant\GrantFactory;
use phpOMS\Auth\OAuth2\Grant\GrantAbstract;
/**
* @internal
*/
class GrantFactoryTest extends \PHPUnit\Framework\TestCase
{
private GrantFactory $factory;
public function setUp() : void
{
$this->factory = new GrantFactory();
}
public function testGrantGet() : void
{
$grant = $this->factory->getGrant('AuthorizationCode');
self::assertInstanceOf(AuthorizationCode::class, $grant);
}
public function testGrantInputOutput() : void
{
$grant = new class extends GrantAbstract {
protected function getName() : string
{
return 'TestGrant';
}
protected function getRequiredRequestParameters() : array
{
return ['test'];
}
};
$this->factory->setGrant('test', $grant);
self::assertInstanceOf(\get_class($grant), $this->factory->getGrant('test'));
}
public function testInvalidGrantGet() : void
{
$this->expectException(\Exception::class);
$this->factory->getGrant('invalid');
}
}

View File

@ -0,0 +1,65 @@
<?php
/**
* Orange Management
*
* PHP Version 7.4
*
* @package tests
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
*/
declare(strict_types=1);
namespace phpOMS\tests\Auth\OAuth2\Grant;
use phpOMS\Auth\OAuth2\Grant\Password;
/**
* @internal
*/
class PasswordTest extends \PHPUnit\Framework\TestCase
{
private Password $grant;
public function setUp() : void
{
$this->grant = new Password();
}
public function testDefault() : void
{
self::assertEquals('password', $this->grant->__toString());
self::assertEquals(
[
'username' => 'value',
'password' => '2',
'test' => 'value2',
],
$this->grant->prepareRequestParamters(
[
'username' => 'value'
],
[
'password' => '2',
'test' => 'value2'
]
)
);
}
public function testMissingDefaultOption() : void
{
$this->expectException(\Exception::class);
$this->grant->prepareRequestParamters(
[
'username' => 'value'
],
[
'option' => '2',
'test' => 'value2'
]
);
}
}

View File

@ -0,0 +1,65 @@
<?php
/**
* Orange Management
*
* PHP Version 7.4
*
* @package tests
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
*/
declare(strict_types=1);
namespace phpOMS\tests\Auth\OAuth2\Grant;
use phpOMS\Auth\OAuth2\Grant\RefreshToken;
/**
* @internal
*/
class RefreshTokenTest extends \PHPUnit\Framework\TestCase
{
private RefreshToken $grant;
public function setUp() : void
{
$this->grant = new RefreshToken();
}
public function testDefault() : void
{
self::assertEquals('refresh_token', $this->grant->__toString());
self::assertEquals(
[
'refresh_token' => 'value',
'option' => '2',
'test' => 'value2',
],
$this->grant->prepareRequestParamters(
[
'refresh_token' => 'value'
],
[
'option' => '2',
'test' => 'value2'
]
)
);
}
public function testMissingDefaultOption() : void
{
$this->expectException(\Exception::class);
$this->grant->prepareRequestParamters(
[
'test' => 'value'
],
[
'option' => '2',
'test' => 'value2'
]
);
}
}

View File

@ -0,0 +1,64 @@
<?php
/**
* Orange Management
*
* PHP Version 7.4
*
* @package tests
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
*/
declare(strict_types=1);
namespace phpOMS\tests\Auth\OAuth2\OptionProvider;
use phpOMS\Auth\OAuth2\OptionProvider\HttpBasicAuthOptionProvider;
use phpOMS\Message\Http\RequestMethod;
use phpOMS\System\MimeType;
/**
* @internal
*/
class HttpBasicAuthOptionProviderTest extends \PHPUnit\Framework\TestCase
{
private HttpBasicAuthOptionProvider $provider;
public function setUp() : void
{
$this->provider = new HttpBasicAuthOptionProvider();
}
public function testDefault() : void
{
self::assertEquals(
[
'headers' => [
'content-type' => MimeType::M_POST,
'Authorization' => 'Basic aWQ6c2VjcmV0'
],
'body' => 'para=test&para2=test2',
],
$this->provider->getAccessTokenOptions(RequestMethod::POST, [
'para' => 'test',
'para2' => 'test2',
'client_id' => 'id',
'client_secret' => 'secret'
]
)
);
}
public function testInvalidParams() : void
{
self::assertEquals([],
$this->provider->getAccessTokenOptions(RequestMethod::POST, [
'para' => 'test',
'para2' => 'test2',
'client_id' => 'id',
]
)
);
}
}

View File

@ -0,0 +1,43 @@
<?php
/**
* Orange Management
*
* PHP Version 7.4
*
* @package tests
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
*/
declare(strict_types=1);
namespace phpOMS\tests\Auth\OAuth2\OptionProvider;
use phpOMS\Auth\OAuth2\OptionProvider\PostAuthOptionProvider;
use phpOMS\Message\Http\RequestMethod;
use phpOMS\System\MimeType;
/**
* @internal
*/
class PostAuthOptionProviderTest extends \PHPUnit\Framework\TestCase
{
private PostAuthOptionProvider $provider;
public function setUp() : void
{
$this->provider = new PostAuthOptionProvider();
}
public function testDefault() : void
{
self::assertEquals(
[
'headers' => ['content-type' => MimeType::M_POST],
'body' => 'para=test&para2=test2',
],
$this->provider->getAccessTokenOptions(RequestMethod::POST, ['para' => 'test', 'para2' => 'test2'])
);
}
}

View File

@ -0,0 +1,119 @@
<?php
/**
* Orange Management
*
* PHP Version 7.4
*
* @package tests
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
*/
declare(strict_types=1);
namespace phpOMS\tests\Auth\OAuth2\Token;
use phpOMS\Auth\OAuth2\Token\AccessToken;
/**
* @internal
*/
class AccessTokenTest extends \PHPUnit\Framework\TestCase
{
public function testDefault() : void
{
$token = new AccessToken(['access_token' => 'token']);
self::assertEquals('token', $token->getToken());
self::assertEquals('token', $token->__toString());
self::assertEquals(-1, $token->getExpires());
self::assertEquals(null, $token->getRefreshToken());
self::assertEquals(null, $token->getResourceOwnerId());
self::assertEquals(false, $token->hasExpired());
self::assertEquals([], $token->getValues());
self::assertEquals(['access_token' => 'token'], $token->jsonSerialize());
}
public function testExpiresInputOutput() : void
{
$expires = \time();
$token = new AccessToken(['access_token' => 'token', 'expires' => $expires]);
self::assertEquals($expires, $token->getExpires());
}
public function testExpiresInInputOutput() : void
{
$expires = \time();
$token = new AccessToken(['access_token' => 'token', 'expires_in' => 10]);
self::assertFalse($token->hasExpired());
self::assertTrue($expires < $token->getExpires() && $token->getExpires() < $expires + 20);
}
public function testHasExpired() : void
{
$token = new AccessToken(['access_token' => 'token', 'expires_in' => -5]);
self::assertTrue($token->hasExpired());
$expires = \time();
$token = new AccessToken(['access_token' => 'token', 'expires' => $expires - 5]);
self::assertTrue($token->hasExpired());
}
public function testResourceOwnerIdInputOutput() : void
{
$token = new AccessToken(['access_token' => 'token', 'resource_owner_id' => 'owner']);
self::assertEquals('owner', $token->getResourceOwnerId());
}
public function testRefreshTokenInputOutput() : void
{
$token = new AccessToken(['access_token' => 'token', 'refresh_token' => 'refresh']);
self::assertEquals('refresh', $token->getRefreshToken());
}
public function testValuesInputOutput() : void
{
$token = new AccessToken([
'access_token' => 'token',
'resource_owner_id' => 'owner',
'expires_in' => 10,
'refresh_token' => 'refresh',
'more' => 'values'
]);
self::assertEquals(
['more' => 'values'],
$token->getValues()
);
}
public function testJsonSeriaize() : void
{
$expires = \time() + 10;
$token = new AccessToken([
'access_token' => 'token',
'resource_owner_id' => 'owner',
'expires' => $expires,
'refresh_token' => 'refresh',
'more' => 'values'
]);
self::assertEquals(
[
'access_token' => 'token',
'resource_owner_id' => 'owner',
'expires' => $expires,
'refresh_token' => 'refresh',
'more' => 'values'
],
$token->jsonSerialize()
);
}
public function testMissingAccessToken() : void
{
$this->expectException(\InvalidArgumentException::class);
$token = new AccessToken([]);
}
}