2014-01-17 10:36:57 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace LeagueTests\Grant;
|
|
|
|
|
2014-05-02 17:25:04 +01:00
|
|
|
use League\OAuth2\Server\Grant\ClientCredentialsGrant;
|
2014-05-02 15:14:12 +01:00
|
|
|
use League\OAuth2\Server\Entity\ScopeEntity;
|
|
|
|
use League\OAuth2\Server\Entity\ClientEntity;
|
2014-05-02 17:21:53 +01:00
|
|
|
use League\OAuth2\Server\AuthorizationServer;
|
2014-01-17 10:36:57 +00:00
|
|
|
use Mockery as M;
|
|
|
|
|
2014-05-07 17:09:52 +01:00
|
|
|
class ClientCredentialsGrantTest extends \PHPUnit_Framework_TestCase
|
2014-01-17 10:36:57 +00:00
|
|
|
{
|
2014-05-03 10:55:25 +01:00
|
|
|
public function testCompleteFlowMissingClientId()
|
2014-01-17 10:36:57 +00:00
|
|
|
{
|
2014-05-01 14:33:11 +01:00
|
|
|
$this->setExpectedException('League\OAuth2\Server\Exception\InvalidRequestException');
|
2014-01-17 10:36:57 +00:00
|
|
|
|
|
|
|
$_POST['grant_type'] = 'client_credentials';
|
|
|
|
|
2014-05-02 17:21:53 +01:00
|
|
|
$server = new AuthorizationServer;
|
2014-05-02 17:25:04 +01:00
|
|
|
$grant = new ClientCredentialsGrant;
|
2014-01-17 10:36:57 +00:00
|
|
|
|
|
|
|
$server->addGrantType($grant);
|
|
|
|
$server->issueAccessToken();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-05-03 10:55:25 +01:00
|
|
|
public function testCompleteFlowMissingClientSecret()
|
2014-01-17 10:36:57 +00:00
|
|
|
{
|
2014-05-01 14:33:11 +01:00
|
|
|
$this->setExpectedException('League\OAuth2\Server\Exception\InvalidRequestException');
|
2014-01-17 10:36:57 +00:00
|
|
|
|
|
|
|
$_POST = [
|
|
|
|
'grant_type' => 'client_credentials',
|
|
|
|
'client_id' => 'testapp'
|
|
|
|
];
|
|
|
|
|
2014-05-02 17:21:53 +01:00
|
|
|
$server = new AuthorizationServer;
|
2014-05-02 17:25:04 +01:00
|
|
|
$grant = new ClientCredentialsGrant;
|
2014-01-17 10:36:57 +00:00
|
|
|
|
|
|
|
$server->addGrantType($grant);
|
|
|
|
$server->issueAccessToken();
|
|
|
|
}
|
|
|
|
|
2014-05-03 10:55:25 +01:00
|
|
|
public function testCompleteFlowInvalidClient()
|
2014-01-17 10:36:57 +00:00
|
|
|
{
|
2014-05-01 14:33:11 +01:00
|
|
|
$this->setExpectedException('League\OAuth2\Server\Exception\InvalidClientException');
|
2014-01-17 10:36:57 +00:00
|
|
|
|
|
|
|
$_POST = [
|
|
|
|
'grant_type' => 'client_credentials',
|
|
|
|
'client_id' => 'testapp',
|
|
|
|
'client_secret' => 'foobar'
|
|
|
|
];
|
|
|
|
|
2014-05-02 17:21:53 +01:00
|
|
|
$server = new AuthorizationServer;
|
2014-05-02 17:25:04 +01:00
|
|
|
$grant = new ClientCredentialsGrant;
|
2014-01-17 10:36:57 +00:00
|
|
|
|
|
|
|
$clientStorage = M::mock('League\OAuth2\Server\Storage\ClientInterface');
|
|
|
|
$clientStorage->shouldReceive('setServer');
|
|
|
|
$clientStorage->shouldReceive('get')->andReturn(null);
|
|
|
|
|
|
|
|
$server->setClientStorage($clientStorage);
|
|
|
|
|
|
|
|
$server->addGrantType($grant);
|
|
|
|
$server->issueAccessToken();
|
|
|
|
}
|
|
|
|
|
2014-05-03 10:55:25 +01:00
|
|
|
public function testCompleteFlowInvalidScope()
|
2014-01-17 10:36:57 +00:00
|
|
|
{
|
2014-05-01 14:33:11 +01:00
|
|
|
$this->setExpectedException('League\OAuth2\Server\Exception\InvalidScopeException');
|
2014-01-17 10:36:57 +00:00
|
|
|
|
|
|
|
$_POST = [
|
|
|
|
'grant_type' => 'client_credentials',
|
|
|
|
'client_id' => 'testapp',
|
|
|
|
'client_secret' => 'foobar',
|
|
|
|
'scope' => 'foo'
|
|
|
|
];
|
|
|
|
|
2014-05-02 17:21:53 +01:00
|
|
|
$server = new AuthorizationServer;
|
2014-05-02 17:25:04 +01:00
|
|
|
$grant = new ClientCredentialsGrant;
|
2014-01-17 10:36:57 +00:00
|
|
|
|
|
|
|
$clientStorage = M::mock('League\OAuth2\Server\Storage\ClientInterface');
|
|
|
|
$clientStorage->shouldReceive('setServer');
|
|
|
|
$clientStorage->shouldReceive('get')->andReturn(
|
2014-07-11 18:19:10 +01:00
|
|
|
(new ClientEntity($server))->hydrate(['id' => 'testapp'])
|
2014-01-17 10:36:57 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$sessionStorage = M::mock('League\OAuth2\Server\Storage\SessionInterface');
|
|
|
|
$sessionStorage->shouldReceive('setServer');
|
|
|
|
$sessionStorage->shouldReceive('create');
|
|
|
|
$sessionStorage->shouldReceive('getScopes')->andReturn([]);
|
|
|
|
|
|
|
|
$accessTokenStorage = M::mock('League\OAuth2\Server\Storage\AccessTokenInterface');
|
|
|
|
$accessTokenStorage->shouldReceive('setServer');
|
|
|
|
$accessTokenStorage->shouldReceive('create');
|
|
|
|
$accessTokenStorage->shouldReceive('getScopes')->andReturn([]);
|
|
|
|
|
|
|
|
$scopeStorage = M::mock('League\OAuth2\Server\Storage\ScopeInterface');
|
|
|
|
$scopeStorage->shouldReceive('setServer');
|
|
|
|
$scopeStorage->shouldReceive('get')->andReturn(null);
|
|
|
|
|
|
|
|
$server->setClientStorage($clientStorage);
|
|
|
|
$server->setScopeStorage($scopeStorage);
|
|
|
|
$server->setSessionStorage($sessionStorage);
|
|
|
|
$server->setAccessTokenStorage($accessTokenStorage);
|
|
|
|
|
|
|
|
$server->addGrantType($grant);
|
|
|
|
$server->issueAccessToken();
|
|
|
|
}
|
|
|
|
|
2014-05-03 10:55:25 +01:00
|
|
|
public function testCompleteFlowNoScopes()
|
2014-01-17 10:36:57 +00:00
|
|
|
{
|
|
|
|
$_POST = [
|
|
|
|
'grant_type' => 'client_credentials',
|
|
|
|
'client_id' => 'testapp',
|
|
|
|
'client_secret' => 'foobar'
|
|
|
|
];
|
|
|
|
|
2014-05-02 17:21:53 +01:00
|
|
|
$server = new AuthorizationServer;
|
2014-05-02 17:25:04 +01:00
|
|
|
$grant = new ClientCredentialsGrant;
|
2014-01-17 10:36:57 +00:00
|
|
|
|
|
|
|
$clientStorage = M::mock('League\OAuth2\Server\Storage\ClientInterface');
|
|
|
|
$clientStorage->shouldReceive('setServer');
|
|
|
|
$clientStorage->shouldReceive('get')->andReturn(
|
2014-07-11 18:19:10 +01:00
|
|
|
(new ClientEntity($server))->hydrate(['id' => 'testapp'])
|
2014-01-17 10:36:57 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$sessionStorage = M::mock('League\OAuth2\Server\Storage\SessionInterface');
|
|
|
|
$sessionStorage->shouldReceive('setServer');
|
|
|
|
$sessionStorage->shouldReceive('create')->andreturn(123);
|
|
|
|
$sessionStorage->shouldReceive('getScopes')->shouldReceive('getScopes')->andReturn([]);
|
|
|
|
$sessionStorage->shouldReceive('associateScope');
|
|
|
|
|
|
|
|
$accessTokenStorage = M::mock('League\OAuth2\Server\Storage\AccessTokenInterface');
|
|
|
|
$accessTokenStorage->shouldReceive('setServer');
|
|
|
|
$accessTokenStorage->shouldReceive('create');
|
|
|
|
$accessTokenStorage->shouldReceive('getScopes')->andReturn([]);
|
|
|
|
$accessTokenStorage->shouldReceive('associateScope');
|
|
|
|
|
|
|
|
$scopeStorage = M::mock('League\OAuth2\Server\Storage\ScopeInterface');
|
|
|
|
$scopeStorage->shouldReceive('setServer');
|
|
|
|
// $scopeStorage->shouldReceive('get')->andReturn(
|
2014-07-11 18:19:10 +01:00
|
|
|
// // (new ScopeEntity($server))->hydrate(['id' => 'foo'])
|
2014-01-17 10:36:57 +00:00
|
|
|
// );
|
|
|
|
|
|
|
|
$server->setClientStorage($clientStorage);
|
|
|
|
$server->setScopeStorage($scopeStorage);
|
|
|
|
$server->setSessionStorage($sessionStorage);
|
|
|
|
$server->setAccessTokenStorage($accessTokenStorage);
|
|
|
|
|
|
|
|
$server->addGrantType($grant);
|
|
|
|
$server->issueAccessToken();
|
|
|
|
}
|
|
|
|
|
2014-05-03 10:55:25 +01:00
|
|
|
public function testCompleteFlow()
|
2014-01-17 10:36:57 +00:00
|
|
|
{
|
|
|
|
$_POST = [
|
|
|
|
'grant_type' => 'client_credentials',
|
|
|
|
'client_id' => 'testapp',
|
|
|
|
'client_secret' => 'foobar',
|
|
|
|
'scope' => 'foo'
|
|
|
|
];
|
|
|
|
|
2014-05-02 17:21:53 +01:00
|
|
|
$server = new AuthorizationServer;
|
2014-05-02 17:25:04 +01:00
|
|
|
$grant = new ClientCredentialsGrant;
|
2014-01-17 10:36:57 +00:00
|
|
|
|
|
|
|
$clientStorage = M::mock('League\OAuth2\Server\Storage\ClientInterface');
|
|
|
|
$clientStorage->shouldReceive('setServer');
|
|
|
|
$clientStorage->shouldReceive('get')->andReturn(
|
2014-07-11 18:19:10 +01:00
|
|
|
(new ClientEntity($server))->hydrate(['id' => 'testapp'])
|
2014-01-17 10:36:57 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$sessionStorage = M::mock('League\OAuth2\Server\Storage\SessionInterface');
|
|
|
|
$sessionStorage->shouldReceive('setServer');
|
|
|
|
$sessionStorage->shouldReceive('create')->andreturn(123);
|
|
|
|
$sessionStorage->shouldReceive('getScopes')->shouldReceive('getScopes')->andReturn([
|
2014-07-11 18:19:10 +01:00
|
|
|
(new ScopeEntity($server))->hydrate(['id' => 'foo'])
|
2014-01-17 10:36:57 +00:00
|
|
|
]);
|
|
|
|
$sessionStorage->shouldReceive('associateScope');
|
|
|
|
|
|
|
|
$accessTokenStorage = M::mock('League\OAuth2\Server\Storage\AccessTokenInterface');
|
|
|
|
$accessTokenStorage->shouldReceive('setServer');
|
|
|
|
$accessTokenStorage->shouldReceive('create');
|
|
|
|
$accessTokenStorage->shouldReceive('getScopes')->andReturn([
|
2014-07-11 18:19:10 +01:00
|
|
|
(new ScopeEntity($server))->hydrate(['id' => 'foo'])
|
2014-01-17 10:36:57 +00:00
|
|
|
]);
|
|
|
|
$accessTokenStorage->shouldReceive('associateScope');
|
|
|
|
|
|
|
|
$scopeStorage = M::mock('League\OAuth2\Server\Storage\ScopeInterface');
|
|
|
|
$scopeStorage->shouldReceive('setServer');
|
|
|
|
$scopeStorage->shouldReceive('get')->andReturn(
|
2014-07-11 18:19:10 +01:00
|
|
|
(new ScopeEntity($server))->hydrate(['id' => 'foo'])
|
2014-01-17 10:36:57 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$server->setClientStorage($clientStorage);
|
|
|
|
$server->setScopeStorage($scopeStorage);
|
|
|
|
$server->setSessionStorage($sessionStorage);
|
|
|
|
$server->setAccessTokenStorage($accessTokenStorage);
|
|
|
|
|
|
|
|
$server->addGrantType($grant);
|
|
|
|
$server->issueAccessToken();
|
|
|
|
}
|
2014-06-20 14:29:47 +01:00
|
|
|
|
|
|
|
public function testClientNotAuthorizedToUseGrant()
|
|
|
|
{
|
|
|
|
$this->setExpectedException('\League\OAuth2\Server\Exception\UnauthorizedClientException');
|
|
|
|
|
|
|
|
$_POST = [
|
|
|
|
'grant_type' => 'client_credentials',
|
|
|
|
'client_id' => 'testapp',
|
|
|
|
'client_secret' => 'foobar',
|
|
|
|
'scope' => 'foo'
|
|
|
|
];
|
|
|
|
|
|
|
|
$server = new AuthorizationServer;
|
|
|
|
$grant = new ClientCredentialsGrant;
|
|
|
|
|
|
|
|
$clientStorage = M::mock('League\OAuth2\Server\Storage\ClientInterface');
|
|
|
|
$clientStorage->shouldReceive('setServer');
|
|
|
|
$clientStorage->shouldReceive('get')->andThrow(
|
|
|
|
new \League\OAuth2\Server\Exception\UnauthorizedClientException
|
|
|
|
);
|
|
|
|
|
|
|
|
$sessionStorage = M::mock('League\OAuth2\Server\Storage\SessionInterface');
|
|
|
|
$sessionStorage->shouldReceive('setServer');
|
|
|
|
|
|
|
|
$accessTokenStorage = M::mock('League\OAuth2\Server\Storage\AccessTokenInterface');
|
|
|
|
$accessTokenStorage->shouldReceive('setServer');
|
|
|
|
|
|
|
|
$scopeStorage = M::mock('League\OAuth2\Server\Storage\ScopeInterface');
|
|
|
|
$scopeStorage->shouldReceive('setServer');
|
|
|
|
$scopeStorage->shouldReceive('get')->andReturn(
|
2014-07-11 18:19:10 +01:00
|
|
|
(new ScopeEntity($server))->hydrate(['id' => 'foo'])
|
2014-06-20 14:29:47 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$server->setClientStorage($clientStorage);
|
|
|
|
$server->setScopeStorage($scopeStorage);
|
|
|
|
$server->setSessionStorage($sessionStorage);
|
|
|
|
$server->setAccessTokenStorage($accessTokenStorage);
|
|
|
|
|
|
|
|
$server->addGrantType($grant);
|
|
|
|
$server->issueAccessToken();
|
|
|
|
}
|
2014-05-03 10:55:25 +01:00
|
|
|
}
|