2013-03-31 18:07:02 +05:30
|
|
|
<?php
|
|
|
|
|
|
|
|
use \Mockery as m;
|
|
|
|
|
|
|
|
class Auth_Code_Grant_Test extends PHPUnit_Framework_TestCase
|
|
|
|
{
|
|
|
|
private $client;
|
|
|
|
private $session;
|
|
|
|
private $scope;
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
2013-05-09 00:12:23 +05:30
|
|
|
$this->client = M::mock('League\OAuth2\Server\Storage\ClientInterface');
|
|
|
|
$this->session = M::mock('League\OAuth2\Server\Storage\SessionInterface');
|
|
|
|
$this->scope = M::mock('League\OAuth2\Server\Storage\ScopeInterface');
|
2013-03-31 18:07:02 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
private function returnDefault()
|
|
|
|
{
|
2013-05-09 00:12:23 +05:30
|
|
|
return new League\OAuth2\Server\Authorization($this->client, $this->session, $this->scope);
|
2013-03-31 18:07:02 +05:30
|
|
|
}
|
|
|
|
|
2013-05-08 23:13:53 +05:30
|
|
|
public function test_setAuthTokenTTL()
|
|
|
|
{
|
|
|
|
$a = $this->returnDefault();
|
2013-05-09 00:12:23 +05:30
|
|
|
$grant = new League\OAuth2\Server\Grant\AuthCode($a);
|
2013-05-08 23:13:53 +05:30
|
|
|
$grant->setAuthTokenTTL(30);
|
|
|
|
|
|
|
|
$reflector = new ReflectionClass($grant);
|
|
|
|
$requestProperty = $reflector->getProperty('authTokenTTL');
|
|
|
|
$requestProperty->setAccessible(true);
|
|
|
|
$v = $requestProperty->getValue($grant);
|
|
|
|
|
|
|
|
$this->assertEquals(30, $v);
|
|
|
|
}
|
|
|
|
|
2013-03-31 18:07:02 +05:30
|
|
|
/**
|
2013-05-09 00:12:23 +05:30
|
|
|
* @expectedException League\OAuth2\Server\Exception\ClientException
|
2013-03-31 18:07:02 +05:30
|
|
|
* @expectedExceptionCode 0
|
|
|
|
*/
|
|
|
|
public function test_checkAuthoriseParams_noClientId()
|
|
|
|
{
|
|
|
|
$a = $this->returnDefault();
|
2013-05-09 00:12:23 +05:30
|
|
|
$g = new League\OAuth2\Server\Grant\AuthCode($a);
|
2013-03-31 18:07:02 +05:30
|
|
|
$a->addGrantType($g);
|
|
|
|
$g->checkAuthoriseParams();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-05-09 00:12:23 +05:30
|
|
|
* @expectedException League\OAuth2\Server\Exception\ClientException
|
2013-03-31 18:07:02 +05:30
|
|
|
* @expectedExceptionCode 0
|
|
|
|
*/
|
|
|
|
public function test_checkAuthoriseParams_noRedirectUri()
|
|
|
|
{
|
|
|
|
$a = $this->returnDefault();
|
2013-05-09 00:12:23 +05:30
|
|
|
$g = new League\OAuth2\Server\Grant\AuthCode($a);
|
2013-03-31 18:07:02 +05:30
|
|
|
$a->addGrantType($g);
|
|
|
|
$g->checkAuthoriseParams(array(
|
|
|
|
'client_id' => 1234
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-05-09 00:12:23 +05:30
|
|
|
* @expectedException League\OAuth2\Server\Exception\ClientException
|
2013-03-31 18:07:02 +05:30
|
|
|
* @expectedExceptionCode 0
|
|
|
|
*/
|
|
|
|
public function test_checkAuthoriseParams_noRequiredState()
|
|
|
|
{
|
|
|
|
$a = $this->returnDefault();
|
2013-05-09 00:12:23 +05:30
|
|
|
$g = new League\OAuth2\Server\Grant\AuthCode($a);
|
2013-03-31 18:07:02 +05:30
|
|
|
$a->addGrantType($g);
|
|
|
|
$a->requireStateParam(true);
|
|
|
|
$g->checkAuthoriseParams(array(
|
|
|
|
'client_id' => 1234,
|
|
|
|
'redirect_uri' => 'http://foo/redirect'
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2013-05-09 00:12:23 +05:30
|
|
|
* @expectedException League\OAuth2\Server\Exception\ClientException
|
2013-03-31 18:07:02 +05:30
|
|
|
* @expectedExceptionCode 8
|
|
|
|
*/
|
|
|
|
public function test_checkAuthoriseParams_badClient()
|
|
|
|
{
|
|
|
|
$this->client->shouldReceive('getClient')->andReturn(false);
|
|
|
|
|
|
|
|
$a = $this->returnDefault();
|
2013-05-09 00:12:23 +05:30
|
|
|
$g = new League\OAuth2\Server\Grant\AuthCode($a);
|
2013-03-31 18:07:02 +05:30
|
|
|
$a->addGrantType($g);
|
|
|
|
$g->checkAuthoriseParams(array(
|
|
|
|
'client_id' => 1234,
|
|
|
|
'redirect_uri' => 'http://foo/redirect'
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-05-09 00:12:23 +05:30
|
|
|
* @expectedException League\OAuth2\Server\Exception\ClientException
|
2013-03-31 18:07:02 +05:30
|
|
|
* @expectedExceptionCode 0
|
|
|
|
*/
|
|
|
|
public function test_checkAuthoriseParams_missingResponseType()
|
|
|
|
{
|
|
|
|
$this->client->shouldReceive('getClient')->andReturn(array(
|
|
|
|
'client_id' => 1234,
|
|
|
|
'client_secret' => 5678,
|
|
|
|
'redirect_uri' => 'http://foo/redirect',
|
|
|
|
'name' => 'Example Client'
|
|
|
|
));
|
|
|
|
|
|
|
|
$a = $this->returnDefault();
|
2013-05-09 00:12:23 +05:30
|
|
|
$g = new League\OAuth2\Server\Grant\AuthCode($a);
|
2013-03-31 18:07:02 +05:30
|
|
|
$a->addGrantType($g);
|
|
|
|
$g->checkAuthoriseParams(array(
|
|
|
|
'client_id' => 1234,
|
|
|
|
'redirect_uri' => 'http://foo/redirect'
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-05-09 00:12:23 +05:30
|
|
|
* @expectedException League\OAuth2\Server\Exception\ClientException
|
2013-03-31 18:07:02 +05:30
|
|
|
* @expectedExceptionCode 3
|
|
|
|
*/
|
|
|
|
public function test_checkAuthoriseParams_badResponseType()
|
|
|
|
{
|
|
|
|
$this->client->shouldReceive('getClient')->andReturn(array(
|
|
|
|
'client_id' => 1234,
|
|
|
|
'client_secret' => 5678,
|
|
|
|
'redirect_uri' => 'http://foo/redirect',
|
|
|
|
'name' => 'Example Client'
|
|
|
|
));
|
|
|
|
|
|
|
|
$a = $this->returnDefault();
|
2013-05-09 00:12:23 +05:30
|
|
|
$g = new League\OAuth2\Server\Grant\AuthCode($a);
|
2013-03-31 18:07:02 +05:30
|
|
|
$a->addGrantType($g);
|
|
|
|
$g->checkAuthoriseParams(array(
|
|
|
|
'client_id' => 1234,
|
|
|
|
'redirect_uri' => 'http://foo/redirect',
|
|
|
|
'response_type' => 'foo'
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-05-09 00:12:23 +05:30
|
|
|
* @expectedException League\OAuth2\Server\Exception\ClientException
|
2013-03-31 18:07:02 +05:30
|
|
|
* @expectedExceptionCode 0
|
|
|
|
*/
|
|
|
|
public function test_checkAuthoriseParams_missingScopes()
|
|
|
|
{
|
|
|
|
$this->client->shouldReceive('getClient')->andReturn(array(
|
|
|
|
'client_id' => 1234,
|
|
|
|
'client_secret' => 5678,
|
|
|
|
'redirect_uri' => 'http://foo/redirect',
|
|
|
|
'name' => 'Example Client'
|
|
|
|
));
|
|
|
|
|
|
|
|
$a = $this->returnDefault();
|
2013-05-09 00:12:23 +05:30
|
|
|
$g = new League\OAuth2\Server\Grant\AuthCode($a);
|
2013-03-31 18:07:02 +05:30
|
|
|
$a->addGrantType($g);
|
2013-05-09 00:12:23 +05:30
|
|
|
$a->addGrantType(new League\OAuth2\Server\Grant\AuthCode($a));
|
2013-05-09 22:32:41 +05:30
|
|
|
$a->requireScopeParam(true);
|
2013-03-31 18:07:02 +05:30
|
|
|
|
|
|
|
$g->checkAuthoriseParams(array(
|
|
|
|
'client_id' => 1234,
|
|
|
|
'redirect_uri' => 'http://foo/redirect',
|
|
|
|
'response_type' => 'code',
|
|
|
|
'scope' => ''
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_checkAuthoriseParams_defaultScope()
|
|
|
|
{
|
|
|
|
$this->client->shouldReceive('getClient')->andReturn(array(
|
|
|
|
'client_id' => 1234,
|
|
|
|
'client_secret' => 5678,
|
|
|
|
'redirect_uri' => 'http://foo/redirect',
|
|
|
|
'name' => 'Example Client'
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->scope->shouldReceive('getScope')->andReturn(array(
|
|
|
|
'id' => 1,
|
|
|
|
'scope' => 'foo',
|
|
|
|
'name' => 'Foo Name',
|
|
|
|
'description' => 'Foo Name Description'
|
|
|
|
));
|
|
|
|
|
|
|
|
$a = $this->returnDefault();
|
2013-05-09 00:12:23 +05:30
|
|
|
$g = new League\OAuth2\Server\Grant\AuthCode($a);
|
2013-03-31 18:07:02 +05:30
|
|
|
$a->addGrantType($g);
|
2013-05-09 00:12:23 +05:30
|
|
|
$a->addGrantType(new League\OAuth2\Server\Grant\AuthCode($a));
|
2013-03-31 18:07:02 +05:30
|
|
|
$a->setDefaultScope('test.scope');
|
|
|
|
$a->requireScopeParam(false);
|
|
|
|
|
|
|
|
$params = $g->checkAuthoriseParams(array(
|
|
|
|
'client_id' => 1234,
|
|
|
|
'redirect_uri' => 'http://foo/redirect',
|
|
|
|
'response_type' => 'code',
|
|
|
|
'scope' => ''
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertArrayHasKey('scopes', $params);
|
2013-05-09 22:45:36 +05:30
|
|
|
$this->assertEquals(1, count($params['scopes']));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_checkAuthoriseParams_defaultScopeArray()
|
|
|
|
{
|
|
|
|
$this->client->shouldReceive('getClient')->andReturn(array(
|
|
|
|
'client_id' => 1234,
|
|
|
|
'client_secret' => 5678,
|
|
|
|
'redirect_uri' => 'http://foo/redirect',
|
|
|
|
'name' => 'Example Client'
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->scope->shouldReceive('getScope')->andReturn(array(
|
|
|
|
'id' => 1,
|
|
|
|
'scope' => 'foo',
|
|
|
|
'name' => 'Foo Name',
|
|
|
|
'description' => 'Foo Name Description'
|
|
|
|
));
|
|
|
|
|
|
|
|
$a = $this->returnDefault();
|
|
|
|
$g = new League\OAuth2\Server\Grant\AuthCode($a);
|
|
|
|
$a->addGrantType($g);
|
|
|
|
$a->addGrantType(new League\OAuth2\Server\Grant\AuthCode($a));
|
|
|
|
$a->setDefaultScope(array('test.scope', 'test.scope2'));
|
|
|
|
$a->requireScopeParam(false);
|
|
|
|
|
|
|
|
$params = $g->checkAuthoriseParams(array(
|
|
|
|
'client_id' => 1234,
|
|
|
|
'redirect_uri' => 'http://foo/redirect',
|
|
|
|
'response_type' => 'code',
|
|
|
|
'scope' => ''
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertArrayHasKey('scopes', $params);
|
|
|
|
$this->assertEquals(2, count($params['scopes']));
|
2013-03-31 18:07:02 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-05-09 00:12:23 +05:30
|
|
|
* @expectedException League\OAuth2\Server\Exception\ClientException
|
2013-03-31 18:07:02 +05:30
|
|
|
* @expectedExceptionCode 4
|
|
|
|
*/
|
|
|
|
public function test_checkAuthoriseParams_badScopes()
|
|
|
|
{
|
|
|
|
$this->client->shouldReceive('getClient')->andReturn(array(
|
|
|
|
'client_id' => 1234,
|
|
|
|
'client_secret' => 5678,
|
|
|
|
'redirect_uri' => 'http://foo/redirect',
|
|
|
|
'name' => 'Example Client'
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->scope->shouldReceive('getScope')->andReturn(false);
|
|
|
|
|
|
|
|
$a = $this->returnDefault();
|
2013-05-09 00:12:23 +05:30
|
|
|
$g = new League\OAuth2\Server\Grant\AuthCode($a);
|
2013-03-31 18:07:02 +05:30
|
|
|
$a->addGrantType($g);
|
2013-05-09 00:12:23 +05:30
|
|
|
$a->addGrantType(new League\OAuth2\Server\Grant\AuthCode($a));
|
2013-03-31 18:07:02 +05:30
|
|
|
|
|
|
|
$g->checkAuthoriseParams(array(
|
|
|
|
'client_id' => 1234,
|
|
|
|
'redirect_uri' => 'http://foo/redirect',
|
|
|
|
'response_type' => 'code',
|
|
|
|
'scope' => 'foo'
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_checkAuthoriseParams_passedInput()
|
|
|
|
{
|
|
|
|
$a = $this->returnDefault();
|
2013-05-09 00:12:23 +05:30
|
|
|
$g = new League\OAuth2\Server\Grant\AuthCode($a);
|
2013-03-31 18:07:02 +05:30
|
|
|
$a->addGrantType($g);
|
2013-05-09 00:12:23 +05:30
|
|
|
$a->addGrantType(new League\OAuth2\Server\Grant\AuthCode($a));
|
2013-03-31 18:07:02 +05:30
|
|
|
|
|
|
|
$this->client->shouldReceive('getClient')->andReturn(array(
|
|
|
|
'client_id' => 1234,
|
|
|
|
'client_secret' => 5678,
|
|
|
|
'redirect_uri' => 'http://foo/redirect',
|
|
|
|
'name' => 'Example Client'
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->scope->shouldReceive('getScope')->andReturn(array(
|
|
|
|
'id' => 1,
|
|
|
|
'scope' => 'foo',
|
|
|
|
'name' => 'Foo Name',
|
|
|
|
'description' => 'Foo Name Description'
|
|
|
|
));
|
|
|
|
|
|
|
|
$v = $g->checkAuthoriseParams(array(
|
|
|
|
'client_id' => 1234,
|
|
|
|
'redirect_uri' => 'http://foo/redirect',
|
|
|
|
'response_type' => 'code',
|
|
|
|
'scope' => 'foo',
|
|
|
|
'state' => 'xyz'
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertEquals(array(
|
|
|
|
'client_id' => 1234,
|
|
|
|
'redirect_uri' => 'http://foo/redirect',
|
|
|
|
'client_details' => array(
|
|
|
|
'client_id' => 1234,
|
|
|
|
'client_secret' => 5678,
|
|
|
|
'redirect_uri' => 'http://foo/redirect',
|
|
|
|
'name' => 'Example Client'
|
|
|
|
),
|
|
|
|
'response_type' => 'code',
|
|
|
|
'scopes' => array(
|
|
|
|
array(
|
|
|
|
'id' => 1,
|
|
|
|
'scope' => 'foo',
|
|
|
|
'name' => 'Foo Name',
|
|
|
|
'description' => 'Foo Name Description'
|
|
|
|
)
|
|
|
|
),
|
|
|
|
'scope' => 'foo',
|
|
|
|
'state' => 'xyz'
|
|
|
|
), $v);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_checkAuthoriseParams()
|
|
|
|
{
|
|
|
|
$this->client->shouldReceive('getClient')->andReturn(array(
|
|
|
|
'client_id' => 1234,
|
|
|
|
'client_secret' => 5678,
|
|
|
|
'redirect_uri' => 'http://foo/redirect',
|
|
|
|
'name' => 'Example Client'
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->scope->shouldReceive('getScope')->andReturn(array(
|
|
|
|
'id' => 1,
|
|
|
|
'scope' => 'foo',
|
|
|
|
'name' => 'Foo Name',
|
|
|
|
'description' => 'Foo Name Description'
|
|
|
|
));
|
|
|
|
|
|
|
|
$a = $this->returnDefault();
|
2013-05-09 00:12:23 +05:30
|
|
|
$g = new League\OAuth2\Server\Grant\AuthCode($a);
|
2013-03-31 18:07:02 +05:30
|
|
|
$a->addGrantType($g);
|
2013-05-09 00:12:23 +05:30
|
|
|
$a->addGrantType(new League\OAuth2\Server\Grant\AuthCode($a));
|
2013-03-31 18:07:02 +05:30
|
|
|
|
|
|
|
$_GET['client_id'] = 1234;
|
|
|
|
$_GET['redirect_uri'] = 'http://foo/redirect';
|
|
|
|
$_GET['response_type'] = 'code';
|
|
|
|
$_GET['scope'] = 'foo';
|
|
|
|
$_GET['state'] = 'xyz';
|
|
|
|
|
2013-05-09 00:12:23 +05:30
|
|
|
$request = new League\OAuth2\Server\Util\Request($_GET);
|
2013-03-31 18:07:02 +05:30
|
|
|
$a->setRequest($request);
|
|
|
|
|
|
|
|
$v = $g->checkAuthoriseParams();
|
|
|
|
|
|
|
|
$this->assertEquals(array(
|
|
|
|
'client_id' => 1234,
|
|
|
|
'redirect_uri' => 'http://foo/redirect',
|
|
|
|
'client_details' => array(
|
|
|
|
'client_id' => 1234,
|
|
|
|
'client_secret' => 5678,
|
|
|
|
'redirect_uri' => 'http://foo/redirect',
|
|
|
|
'name' => 'Example Client'
|
|
|
|
),
|
|
|
|
'response_type' => 'code',
|
|
|
|
'scopes' => array(
|
|
|
|
array(
|
|
|
|
'id' => 1,
|
|
|
|
'scope' => 'foo',
|
|
|
|
'name' => 'Foo Name',
|
|
|
|
'description' => 'Foo Name Description'
|
|
|
|
)
|
|
|
|
),
|
|
|
|
'scope' => 'foo',
|
|
|
|
'state' => 'xyz'
|
|
|
|
), $v);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function test_newAuthoriseRequest()
|
|
|
|
{
|
|
|
|
$this->session->shouldReceive('deleteSession')->andReturn(null);
|
|
|
|
$this->session->shouldReceive('createSession')->andReturn(1);
|
|
|
|
$this->session->shouldReceive('associateScope')->andReturn(null);
|
2013-05-06 23:39:36 +05:30
|
|
|
$this->session->shouldReceive('associateRedirectUri')->andReturn(null);
|
2013-05-11 06:09:29 +05:30
|
|
|
$this->session->shouldReceive('associateAuthCode')->andReturn(1);
|
|
|
|
$this->session->shouldReceive('associateAuthCodeScope')->andReturn(null);
|
2013-03-31 18:07:02 +05:30
|
|
|
|
|
|
|
$a = $this->returnDefault();
|
2013-05-09 00:12:23 +05:30
|
|
|
$g = new League\OAuth2\Server\Grant\AuthCode($a);
|
2013-03-31 18:07:02 +05:30
|
|
|
$a->addGrantType($g);
|
|
|
|
|
|
|
|
$params = array(
|
|
|
|
'client_id' => 1234,
|
|
|
|
'redirect_uri' => 'http://foo/redirect',
|
|
|
|
'client_details' => array(
|
|
|
|
'client_id' => 1234,
|
|
|
|
'client_secret' => 5678,
|
|
|
|
'redirect_uri' => 'http://foo/redirect',
|
|
|
|
'name' => 'Example Client'
|
|
|
|
),
|
|
|
|
'response_type' => 'code',
|
|
|
|
'scopes' => array(
|
|
|
|
array(
|
|
|
|
'id' => 1,
|
|
|
|
'scope' => 'foo',
|
|
|
|
'name' => 'Foo Name',
|
|
|
|
'description' => 'Foo Name Description'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$v = $g->newAuthoriseRequest('user', 123, $params);
|
|
|
|
|
|
|
|
$this->assertEquals(40, strlen($v));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|