oauth2-server/tests/AuthorizationServerTest.php

349 lines
14 KiB
PHP
Raw Permalink Normal View History

2016-02-18 17:37:50 +05:30
<?php
namespace LeagueTests;
use DateInterval;
2016-04-17 17:36:17 +05:30
use League\OAuth2\Server\AuthorizationServer;
2018-11-02 20:08:07 +05:30
use League\OAuth2\Server\CryptKey;
2016-03-10 22:04:32 +05:30
use League\OAuth2\Server\Exception\OAuthServerException;
use League\OAuth2\Server\Grant\AuthCodeGrant;
2016-02-18 17:37:50 +05:30
use League\OAuth2\Server\Grant\ClientCredentialsGrant;
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
2016-03-10 22:04:32 +05:30
use League\OAuth2\Server\Repositories\AuthCodeRepositoryInterface;
2016-02-20 04:39:39 +05:30
use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
2016-03-10 22:04:32 +05:30
use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface;
2016-02-18 17:37:50 +05:30
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface;
2016-04-10 20:28:01 +05:30
use League\OAuth2\Server\RequestTypes\AuthorizationRequest;
2016-03-10 22:04:32 +05:30
use League\OAuth2\Server\ResponseTypes\BearerTokenResponse;
2016-04-09 20:16:40 +05:30
use LeagueTests\Stubs\AccessTokenEntity;
use LeagueTests\Stubs\AuthCodeEntity;
2016-03-15 05:40:47 +05:30
use LeagueTests\Stubs\ClientEntity;
use LeagueTests\Stubs\ScopeEntity;
2016-02-20 04:39:39 +05:30
use LeagueTests\Stubs\StubResponseType;
use LeagueTests\Stubs\UserEntity;
use PHPUnit\Framework\TestCase;
2018-02-18 01:01:59 +05:30
use Psr\Http\Message\ResponseInterface;
use Zend\Diactoros\Response;
2016-04-10 20:28:01 +05:30
use Zend\Diactoros\ServerRequest;
use Zend\Diactoros\ServerRequestFactory;
2016-02-18 17:37:50 +05:30
class AuthorizationServerTest extends TestCase
2016-02-18 17:37:50 +05:30
{
2017-11-14 04:01:50 +05:30
const DEFAULT_SCOPE = 'basic';
public function setUp(): void
{
// Make sure the keys have the correct permissions.
chmod(__DIR__ . '/Stubs/private.key', 0600);
chmod(__DIR__ . '/Stubs/public.key', 0600);
chmod(__DIR__ . '/Stubs/private.key.crlf', 0600);
}
2016-02-18 17:37:50 +05:30
public function testRespondToRequestInvalidGrantType()
{
2016-04-17 17:24:25 +05:30
$server = new AuthorizationServer(
2016-07-08 18:59:21 +05:30
$this->getMockBuilder(ClientRepositoryInterface::class)->getMock(),
$this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock(),
$this->getMockBuilder(ScopeRepositoryInterface::class)->getMock(),
2016-03-28 20:40:41 +05:30
'file://' . __DIR__ . '/Stubs/private.key',
2017-07-01 22:41:19 +05:30
base64_encode(random_bytes(36)),
2016-02-18 17:37:50 +05:30
new StubResponseType()
);
$server->enableGrantType(new ClientCredentialsGrant(), new DateInterval('PT1M'));
2016-02-18 17:37:50 +05:30
2016-03-11 04:53:29 +05:30
try {
$server->respondToAccessTokenRequest(ServerRequestFactory::fromGlobals(), new Response);
2016-03-11 04:53:29 +05:30
} catch (OAuthServerException $e) {
$this->assertEquals('unsupported_grant_type', $e->getErrorType());
$this->assertEquals(400, $e->getHttpStatusCode());
}
2016-02-18 17:37:50 +05:30
}
public function testRespondToRequest()
{
$client = new ClientEntity();
$client->setConfidential();
2016-07-08 18:59:21 +05:30
$clientRepository = $this->getMockBuilder(ClientRepositoryInterface::class)->getMock();
$clientRepository->method('getClientEntity')->willReturn($client);
2016-02-18 17:37:50 +05:30
$scope = new ScopeEntity();
2016-03-24 00:20:14 +05:30
$scopeRepositoryMock = $this->getMockBuilder(ScopeRepositoryInterface::class)->getMock();
$scopeRepositoryMock->method('getScopeEntityByIdentifier')->willReturn($scope);
2016-03-24 00:20:14 +05:30
$scopeRepositoryMock->method('finalizeScopes')->willReturnArgument(0);
2016-07-08 18:59:21 +05:30
$accessTokenRepositoryMock = $this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock();
$accessTokenRepositoryMock->method('getNewToken')->willReturn(new AccessTokenEntity());
2016-04-17 17:24:25 +05:30
$server = new AuthorizationServer(
2016-02-18 17:37:50 +05:30
$clientRepository,
$accessTokenRepositoryMock,
2016-03-24 00:20:14 +05:30
$scopeRepositoryMock,
2016-03-28 20:40:41 +05:30
'file://' . __DIR__ . '/Stubs/private.key',
2017-07-01 22:41:19 +05:30
base64_encode(random_bytes(36)),
2016-02-18 17:37:50 +05:30
new StubResponseType()
);
$server->setDefaultScope(self::DEFAULT_SCOPE);
$server->enableGrantType(new ClientCredentialsGrant(), new DateInterval('PT1M'));
2016-02-18 17:37:50 +05:30
$_POST['grant_type'] = 'client_credentials';
$_POST['client_id'] = 'foo';
$_POST['client_secret'] = 'bar';
$response = $server->respondToAccessTokenRequest(ServerRequestFactory::fromGlobals(), new Response);
2016-02-18 17:37:50 +05:30
$this->assertEquals(200, $response->getStatusCode());
}
2016-03-10 22:04:32 +05:30
2016-04-10 20:28:01 +05:30
public function testGetResponseType()
2016-03-10 22:04:32 +05:30
{
2016-07-08 18:59:21 +05:30
$clientRepository = $this->getMockBuilder(ClientRepositoryInterface::class)->getMock();
2016-03-24 00:20:14 +05:30
2016-04-17 17:24:25 +05:30
$server = new AuthorizationServer(
2016-03-10 22:04:32 +05:30
$clientRepository,
2016-07-08 18:59:21 +05:30
$this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock(),
$this->getMockBuilder(ScopeRepositoryInterface::class)->getMock(),
'file://' . __DIR__ . '/Stubs/private.key',
2016-04-10 20:28:01 +05:30
'file://' . __DIR__ . '/Stubs/public.key'
2016-03-10 22:04:32 +05:30
);
2016-04-10 20:28:01 +05:30
$abstractGrantReflection = new \ReflectionClass($server);
$method = $abstractGrantReflection->getMethod('getResponseType');
$method->setAccessible(true);
2017-12-07 01:54:42 +05:30
$this->assertInstanceOf(BearerTokenResponse::class, $method->invoke($server));
2016-04-10 20:28:01 +05:30
}
2016-04-17 17:36:17 +05:30
2018-11-16 17:27:37 +05:30
public function testGetResponseTypeExtended()
{
$clientRepository = $this->getMockBuilder(ClientRepositoryInterface::class)->getMock();
$privateKey = 'file://' . __DIR__ . '/Stubs/private.key';
$encryptionKey = 'file://' . __DIR__ . '/Stubs/public.key';
$server = new AuthorizationServer(
$clientRepository,
$this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock(),
$this->getMockBuilder(ScopeRepositoryInterface::class)->getMock(),
'file://' . __DIR__ . '/Stubs/private.key',
'file://' . __DIR__ . '/Stubs/public.key'
);
2018-11-16 17:27:37 +05:30
$abstractGrantReflection = new \ReflectionClass($server);
$method = $abstractGrantReflection->getMethod('getResponseType');
$method->setAccessible(true);
2018-11-16 17:27:37 +05:30
$responseType = $method->invoke($server);
$responseTypeReflection = new \ReflectionClass($responseType);
$privateKeyProperty = $responseTypeReflection->getProperty('privateKey');
$privateKeyProperty->setAccessible(true);
$encryptionKeyProperty = $responseTypeReflection->getProperty('encryptionKey');
$encryptionKeyProperty->setAccessible(true);
2018-11-16 17:27:37 +05:30
// generated instances should have keys setup
$this->assertSame($privateKey, $privateKeyProperty->getValue($responseType)->getKeyPath());
$this->assertSame($encryptionKey, $encryptionKeyProperty->getValue($responseType));
2018-11-16 17:27:37 +05:30
}
public function testMultipleRequestsGetDifferentResponseTypeInstances()
2018-11-02 20:08:07 +05:30
{
$privateKey = 'file://' . __DIR__ . '/Stubs/private.key';
$encryptionKey = 'file://' . __DIR__ . '/Stubs/public.key';
$responseTypePrototype = new class extends BearerTokenResponse {
/* @return null|CryptKey */
public function getPrivateKey()
{
return $this->privateKey;
}
public function getEncryptionKey()
{
return $this->encryptionKey;
}
};
$clientRepository = $this->getMockBuilder(ClientRepositoryInterface::class)->getMock();
$server = new AuthorizationServer(
$clientRepository,
$this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock(),
$this->getMockBuilder(ScopeRepositoryInterface::class)->getMock(),
$privateKey,
$encryptionKey,
$responseTypePrototype
);
$abstractGrantReflection = new \ReflectionClass($server);
$method = $abstractGrantReflection->getMethod('getResponseType');
2018-11-02 20:08:07 +05:30
$method->setAccessible(true);
$responseTypeA = $method->invoke($server);
$responseTypeB = $method->invoke($server);
// prototype should not get changed
$this->assertNull($responseTypePrototype->getPrivateKey());
$this->assertNull($responseTypePrototype->getEncryptionKey());
// generated instances should have keys setup
$this->assertSame($privateKey, $responseTypeA->getPrivateKey()->getKeyPath());
$this->assertSame($encryptionKey, $responseTypeA->getEncryptionKey());
// all instances should be different but based on the same prototype
$this->assertSame(get_class($responseTypePrototype), get_class($responseTypeA));
$this->assertSame(get_class($responseTypePrototype), get_class($responseTypeB));
$this->assertNotSame($responseTypePrototype, $responseTypeA);
$this->assertNotSame($responseTypePrototype, $responseTypeB);
$this->assertNotSame($responseTypeA, $responseTypeB);
2016-04-10 20:28:01 +05:30
}
2016-04-17 17:36:17 +05:30
2016-04-10 20:28:01 +05:30
public function testCompleteAuthorizationRequest()
2016-03-10 22:04:32 +05:30
{
2016-07-08 18:59:21 +05:30
$clientRepository = $this->getMockBuilder(ClientRepositoryInterface::class)->getMock();
2016-03-10 22:04:32 +05:30
2016-04-17 17:24:25 +05:30
$server = new AuthorizationServer(
2016-03-10 22:04:32 +05:30
$clientRepository,
2016-07-08 18:59:21 +05:30
$this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock(),
$this->getMockBuilder(ScopeRepositoryInterface::class)->getMock(),
2016-03-28 20:40:41 +05:30
'file://' . __DIR__ . '/Stubs/private.key',
'file://' . __DIR__ . '/Stubs/public.key'
2016-03-10 22:04:32 +05:30
);
2016-04-10 20:28:01 +05:30
$authCodeRepository = $this->getMockBuilder(AuthCodeRepositoryInterface::class)->getMock();
$authCodeRepository->method('getNewAuthCode')->willReturn(new AuthCodeEntity());
2016-03-10 22:04:32 +05:30
2016-04-10 20:28:01 +05:30
$grant = new AuthCodeGrant(
$authCodeRepository,
2016-07-08 18:59:21 +05:30
$this->getMockBuilder(RefreshTokenRepositoryInterface::class)->getMock(),
new DateInterval('PT10M')
2016-04-10 20:28:01 +05:30
);
$server->enableGrantType($grant);
$authRequest = new AuthorizationRequest();
$authRequest->setAuthorizationApproved(true);
$authRequest->setClient(new ClientEntity());
$authRequest->setGrantTypeId('authorization_code');
$authRequest->setUser(new UserEntity());
2017-12-07 01:54:42 +05:30
$this->assertInstanceOf(
ResponseInterface::class,
$server->completeAuthorizationRequest($authRequest, new Response)
2016-04-10 20:28:01 +05:30
);
2016-03-10 22:04:32 +05:30
}
2016-04-10 20:28:01 +05:30
public function testValidateAuthorizationRequest()
2016-03-10 22:04:32 +05:30
{
2016-04-10 20:28:01 +05:30
$client = new ClientEntity();
$client->setRedirectUri('http://foo/bar');
$client->setConfidential();
2016-04-10 20:28:01 +05:30
$clientRepositoryMock = $this->getMockBuilder(ClientRepositoryInterface::class)->getMock();
$clientRepositoryMock->method('getClientEntity')->willReturn($client);
$scope = new ScopeEntity();
$scopeRepositoryMock = $this->getMockBuilder(ScopeRepositoryInterface::class)->getMock();
$scopeRepositoryMock->method('getScopeEntityByIdentifier')->willReturn($scope);
2016-04-10 20:28:01 +05:30
$grant = new AuthCodeGrant(
2016-07-08 18:59:21 +05:30
$this->getMockBuilder(AuthCodeRepositoryInterface::class)->getMock(),
$this->getMockBuilder(RefreshTokenRepositoryInterface::class)->getMock(),
new DateInterval('PT10M')
2016-04-10 20:28:01 +05:30
);
$grant->setClientRepository($clientRepositoryMock);
2016-03-10 22:04:32 +05:30
2016-04-17 17:24:25 +05:30
$server = new AuthorizationServer(
2016-04-10 20:28:01 +05:30
$clientRepositoryMock,
2016-07-08 18:59:21 +05:30
$this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock(),
$scopeRepositoryMock,
2016-03-28 20:40:41 +05:30
'file://' . __DIR__ . '/Stubs/private.key',
'file://' . __DIR__ . '/Stubs/public.key'
2016-03-10 22:04:32 +05:30
);
$server->setDefaultScope(self::DEFAULT_SCOPE);
2016-04-10 20:28:01 +05:30
$server->enableGrantType($grant);
$request = new ServerRequest(
[],
[],
null,
null,
'php://input',
$headers = [],
$cookies = [],
$queryParams = [
'response_type' => 'code',
'client_id' => 'foo',
]
);
2016-03-10 22:04:32 +05:30
2017-12-07 01:54:42 +05:30
$this->assertInstanceOf(AuthorizationRequest::class, $server->validateAuthorizationRequest($request));
2016-04-10 20:28:01 +05:30
}
public function testValidateAuthorizationRequestWithMissingRedirectUri()
{
$client = new ClientEntity();
$clientRepositoryMock = $this->getMockBuilder(ClientRepositoryInterface::class)->getMock();
$clientRepositoryMock->method('getClientEntity')->willReturn($client);
$grant = new AuthCodeGrant(
$this->getMockBuilder(AuthCodeRepositoryInterface::class)->getMock(),
$this->getMockBuilder(RefreshTokenRepositoryInterface::class)->getMock(),
new DateInterval('PT10M')
);
$grant->setClientRepository($clientRepositoryMock);
$server = new AuthorizationServer(
$clientRepositoryMock,
$this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock(),
$this->getMockBuilder(ScopeRepositoryInterface::class)->getMock(),
'file://' . __DIR__ . '/Stubs/private.key',
'file://' . __DIR__ . '/Stubs/public.key'
);
$server->enableGrantType($grant);
$request = new ServerRequest(
[],
[],
null,
null,
'php://input',
$headers = [],
$cookies = [],
$queryParams = [
'response_type' => 'code',
'client_id' => 'foo',
]
);
try {
$server->validateAuthorizationRequest($request);
} catch (OAuthServerException $e) {
$this->assertEquals('invalid_client', $e->getErrorType());
$this->assertEquals(401, $e->getHttpStatusCode());
}
}
2016-04-10 20:28:01 +05:30
public function testValidateAuthorizationRequestUnregistered()
{
2016-04-17 17:24:25 +05:30
$server = new AuthorizationServer(
2016-07-08 18:59:21 +05:30
$this->getMockBuilder(ClientRepositoryInterface::class)->getMock(),
$this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock(),
$this->getMockBuilder(ScopeRepositoryInterface::class)->getMock(),
2016-04-10 20:28:01 +05:30
'file://' . __DIR__ . '/Stubs/private.key',
'file://' . __DIR__ . '/Stubs/public.key'
);
$request = (new ServerRequest())->withQueryParams([
'response_type' => 'code',
'client_id' => 'foo',
]);
$this->expectException(\League\OAuth2\Server\Exception\OAuthServerException::class);
$this->expectExceptionCode(2);
2016-04-10 20:28:01 +05:30
$server->validateAuthorizationRequest($request);
2016-03-10 22:04:32 +05:30
}
2016-02-18 17:37:50 +05:30
}