oauth2-server/tests/authorization/PasswordGrantTest.php

613 lines
22 KiB
PHP
Raw Normal View History

2013-02-14 01:42:51 +05:30
<?php
use \Mockery as m;
class Password_Grant_Test extends PHPUnit_Framework_TestCase
{
private $client;
private $session;
private $scope;
public function setUp()
{
$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-02-14 01:42:51 +05:30
}
private function returnDefault()
{
return new League\OAuth2\Server\Authorization($this->client, $this->session, $this->scope);
2013-02-14 01:42:51 +05:30
}
/**
* @expectedException League\OAuth2\Server\Exception\ClientException
2013-02-14 01:42:51 +05:30
* @expectedExceptionCode 0
*/
public function test_issueAccessToken_passwordGrant_missingClientId()
{
$a = $this->returnDefault();
2013-12-06 01:55:55 +05:30
$a->addGrantType(new League\OAuth2\Server\Grant\Password());
2013-02-14 01:42:51 +05:30
$request = new League\OAuth2\Server\Util\Request(array(), $_POST);
2013-02-14 01:42:51 +05:30
$a->setRequest($request);
$a->issueAccessToken(array(
2013-02-14 01:42:51 +05:30
'grant_type' => 'password'
));
}
/**
* @expectedException League\OAuth2\Server\Exception\ClientException
2013-02-14 01:42:51 +05:30
* @expectedExceptionCode 0
*/
public function test_issueAccessToken_passwordGrant_missingClientPassword()
{
$a = $this->returnDefault();
2013-12-06 01:55:55 +05:30
$a->addGrantType(new League\OAuth2\Server\Grant\Password());
2013-02-14 01:42:51 +05:30
$request = new League\OAuth2\Server\Util\Request(array(), $_POST);
2013-02-14 01:42:51 +05:30
$a->setRequest($request);
$a->issueAccessToken(array(
2013-02-14 01:42:51 +05:30
'grant_type' => 'password',
'client_id' => 1234
));
}
/**
* @expectedException League\OAuth2\Server\Exception\ClientException
2013-02-14 01:42:51 +05:30
* @expectedExceptionCode 8
*/
public function test_issueAccessToken_passwordGrant_badClient()
{
$this->client->shouldReceive('getClient')->andReturn(false);
$a = $this->returnDefault();
2013-12-06 01:55:55 +05:30
$a->addGrantType(new League\OAuth2\Server\Grant\Password());
2013-02-14 01:42:51 +05:30
$request = new League\OAuth2\Server\Util\Request(array(), $_POST);
2013-02-14 01:42:51 +05:30
$a->setRequest($request);
$a->issueAccessToken(array(
2013-02-14 01:42:51 +05:30
'grant_type' => 'password',
'client_id' => 1234,
'client_secret' => 5678
));
}
/**
* @expectedException League\OAuth2\Server\Exception\InvalidGrantTypeException
2013-02-14 01:42:51 +05:30
*/
function test_issueAccessToken_passwordGrant_invalidCallback()
{
$this->client->shouldReceive('getClient')->andReturn(array(
'client_id' => 1234,
'client_secret' => 5678,
'redirect_uri' => 'http://foo/redirect',
'name' => 'Example Client'
));
$this->client->shouldReceive('validateRefreshToken')->andReturn(1);
$this->session->shouldReceive('validateAuthCode')->andReturn(1);
$this->session->shouldReceive('createSession')->andReturn(1);
$this->session->shouldReceive('deleteSession')->andReturn(null);
$this->session->shouldReceive('updateRefreshToken')->andReturn(null);
$testCredentials = null;
$a = $this->returnDefault();
2013-12-06 01:55:55 +05:30
$pgrant = new League\OAuth2\Server\Grant\Password();
2013-02-14 01:42:51 +05:30
$pgrant->setVerifyCredentialsCallback($testCredentials);
$a->addGrantType($pgrant);
$a->issueAccessToken(array(
2013-02-14 01:42:51 +05:30
'grant_type' => 'password',
'client_id' => 1234,
'client_secret' => 5678,
'username' => 'foo',
'password' => 'bar'
));
}
/**
* @expectedException League\OAuth2\Server\Exception\ClientException
2013-02-14 01:42:51 +05:30
* @expectedExceptionCode 0
*/
function test_issueAccessToken_passwordGrant_missingUsername()
{
$this->client->shouldReceive('getClient')->andReturn(array(
'client_id' => 1234,
'client_secret' => 5678,
'redirect_uri' => 'http://foo/redirect',
'name' => 'Example Client'
));
$this->client->shouldReceive('validateRefreshToken')->andReturn(1);
$this->session->shouldReceive('validateAuthCode')->andReturn(1);
$this->session->shouldReceive('createSession')->andReturn(1);
$this->session->shouldReceive('deleteSession')->andReturn(null);
$this->session->shouldReceive('updateRefreshToken')->andReturn(null);
$testCredentials = function() { return false; };
2013-02-14 01:42:51 +05:30
$a = $this->returnDefault();
2013-12-06 01:55:55 +05:30
$pgrant = new League\OAuth2\Server\Grant\Password();
2013-02-14 01:42:51 +05:30
$pgrant->setVerifyCredentialsCallback($testCredentials);
$a->addGrantType($pgrant);
$a->issueAccessToken(array(
2013-02-14 01:42:51 +05:30
'grant_type' => 'password',
'client_id' => 1234,
'client_secret' => 5678
));
}
/**
* @expectedException League\OAuth2\Server\Exception\ClientException
2013-02-14 01:42:51 +05:30
* @expectedExceptionCode 0
*/
function test_issueAccessToken_passwordGrant_missingPassword()
{
$this->client->shouldReceive('getClient')->andReturn(array(
'client_id' => 1234,
'client_secret' => 5678,
'redirect_uri' => 'http://foo/redirect',
'name' => 'Example Client'
));
$this->client->shouldReceive('validateRefreshToken')->andReturn(1);
$this->session->shouldReceive('validateAuthCode')->andReturn(1);
$this->session->shouldReceive('createSession')->andReturn(1);
$this->session->shouldReceive('deleteSession')->andReturn(null);
$this->session->shouldReceive('updateRefreshToken')->andReturn(null);
$testCredentials = function() { return false; };
2013-02-14 01:42:51 +05:30
$a = $this->returnDefault();
2013-12-06 01:55:55 +05:30
$pgrant = new League\OAuth2\Server\Grant\Password();
2013-02-14 01:42:51 +05:30
$pgrant->setVerifyCredentialsCallback($testCredentials);
$a->addGrantType($pgrant);
$a->issueAccessToken(array(
2013-02-14 01:42:51 +05:30
'grant_type' => 'password',
'client_id' => 1234,
'client_secret' => 5678,
'username' => 'foo'
));
}
/**
* @expectedException League\OAuth2\Server\Exception\ClientException
2013-02-14 01:42:51 +05:30
* @expectedExceptionCode 0
*/
function test_issueAccessToken_passwordGrant_badCredentials()
{
$this->client->shouldReceive('getClient')->andReturn(array(
'client_id' => 1234,
'client_secret' => 5678,
'redirect_uri' => 'http://foo/redirect',
'name' => 'Example Client'
));
$this->client->shouldReceive('validateRefreshToken')->andReturn(1);
$this->session->shouldReceive('validateAuthCode')->andReturn(1);
$this->session->shouldReceive('createSession')->andReturn(1);
$this->session->shouldReceive('deleteSession')->andReturn(null);
$this->session->shouldReceive('updateRefreshToken')->andReturn(null);
$testCredentials = function() { return false; };
2013-02-14 01:42:51 +05:30
$a = $this->returnDefault();
2013-12-06 01:55:55 +05:30
$pgrant = new League\OAuth2\Server\Grant\Password();
2013-02-14 01:42:51 +05:30
$pgrant->setVerifyCredentialsCallback($testCredentials);
$a->addGrantType($pgrant);
$a->issueAccessToken(array(
2013-02-14 01:42:51 +05:30
'grant_type' => 'password',
'client_id' => 1234,
'client_secret' => 5678,
'username' => 'foo',
'password' => 'bar'
));
}
2013-03-22 17:11:04 +05:30
/**
* @expectedException League\OAuth2\Server\Exception\ClientException
2013-03-22 17:11:04 +05:30
* @expectedExceptionCode 4
*/
public function test_issueAccessToken_passwordGrant_badScopes()
{
$this->scope->shouldReceive('getScope')->andReturn(false);
$this->client->shouldReceive('getClient')->andReturn(array(
'client_id' => 1234,
'client_secret' => 5678,
'redirect_uri' => 'http://foo/redirect',
'name' => 'Example Client'
));
$this->client->shouldReceive('validateRefreshToken')->andReturn(1);
$this->session->shouldReceive('validateAuthCode')->andReturn(1);
$this->session->shouldReceive('createSession')->andReturn(1);
$this->session->shouldReceive('deleteSession')->andReturn(null);
$this->session->shouldReceive('updateRefreshToken')->andReturn(null);
$testCredentials = function() { return 1; };
2013-03-22 17:11:04 +05:30
$a = $this->returnDefault();
2013-12-06 01:55:55 +05:30
$pgrant = new League\OAuth2\Server\Grant\Password();
2013-03-22 17:11:04 +05:30
$pgrant->setVerifyCredentialsCallback($testCredentials);
$a->addGrantType($pgrant);
$a->issueAccessToken(array(
2013-03-22 17:11:04 +05:30
'grant_type' => 'password',
'client_id' => 1234,
'client_secret' => 5678,
'username' => 'foo',
'password' => 'bar',
'scope' => 'blah'
));
}
/**
* @expectedException League\OAuth2\Server\Exception\ClientException
2013-03-22 17:11:04 +05:30
* @expectedExceptionCode 0
*/
public function test_issueAccessToken_passwordGrant_missingScopes()
{
$this->client->shouldReceive('getClient')->andReturn(array(
'client_id' => 1234,
'client_secret' => 5678,
'redirect_uri' => 'http://foo/redirect',
'name' => 'Example Client'
));
$this->client->shouldReceive('validateRefreshToken')->andReturn(1);
$this->session->shouldReceive('validateAuthCode')->andReturn(1);
$this->session->shouldReceive('createSession')->andReturn(1);
$this->session->shouldReceive('deleteSession')->andReturn(null);
$this->session->shouldReceive('updateRefreshToken')->andReturn(null);
$testCredentials = function() { return 1; };
2013-03-22 17:11:04 +05:30
$a = $this->returnDefault();
2013-12-06 01:55:55 +05:30
$pgrant = new League\OAuth2\Server\Grant\Password();
2013-03-22 17:11:04 +05:30
$pgrant->setVerifyCredentialsCallback($testCredentials);
$a->addGrantType($pgrant);
$a->requireScopeParam(true);
$a->issueAccessToken(array(
2013-03-22 17:11:04 +05:30
'grant_type' => 'password',
'client_id' => 1234,
'client_secret' => 5678,
'username' => 'foo',
'password' => 'bar'
));
}
public function test_issueAccessToken_passwordGrant_defaultScope()
{
$this->scope->shouldReceive('getScope')->andReturn(array(
'id' => 1,
'scope' => 'foo',
'name' => 'Foo Name',
'description' => 'Foo Name Description'
));
$this->client->shouldReceive('getClient')->andReturn(array(
'client_id' => 1234,
'client_secret' => 5678,
'redirect_uri' => 'http://foo/redirect',
'name' => 'Example Client'
));
$this->client->shouldReceive('validateRefreshToken')->andReturn(1);
$this->session->shouldReceive('validateAuthCode')->andReturn(1);
$this->session->shouldReceive('createSession')->andReturn(1);
$this->session->shouldReceive('deleteSession')->andReturn(null);
$this->session->shouldReceive('updateRefreshToken')->andReturn(null);
$this->session->shouldReceive('associateScope')->andReturn(null);
2013-05-06 23:39:36 +05:30
$this->session->shouldReceive('associateAccessToken')->andReturn(1);
2013-03-22 17:11:04 +05:30
$testCredentials = function() { return 1; };
2013-03-22 17:11:04 +05:30
$a = $this->returnDefault();
2013-12-06 01:55:55 +05:30
$pgrant = new League\OAuth2\Server\Grant\Password();
2013-03-22 17:11:04 +05:30
$pgrant->setVerifyCredentialsCallback($testCredentials);
$a->addGrantType($pgrant);
$a->requireScopeParam(false);
$a->setDefaultScope('foobar');
$v = $a->issueAccessToken(array(
'grant_type' => 'password',
'client_id' => 1234,
'client_secret' => 5678,
'username' => 'foo',
'password' => 'bar',
'scope' => ''
));
$this->assertArrayHasKey('access_token', $v);
$this->assertArrayHasKey('token_type', $v);
$this->assertArrayHasKey('expires', $v);
$this->assertArrayHasKey('expires_in', $v);
}
public function test_issueAccessToken_passwordGrant_defaultScopeArray()
{
$this->scope->shouldReceive('getScope')->andReturn(array(
'id' => 1,
'scope' => 'foo',
'name' => 'Foo Name',
'description' => 'Foo Name Description'
));
$this->client->shouldReceive('getClient')->andReturn(array(
'client_id' => 1234,
'client_secret' => 5678,
'redirect_uri' => 'http://foo/redirect',
'name' => 'Example Client'
));
$this->client->shouldReceive('validateRefreshToken')->andReturn(1);
$this->session->shouldReceive('validateAuthCode')->andReturn(1);
$this->session->shouldReceive('createSession')->andReturn(1);
$this->session->shouldReceive('deleteSession')->andReturn(null);
$this->session->shouldReceive('updateRefreshToken')->andReturn(null);
$this->session->shouldReceive('associateScope')->andReturn(null);
$this->session->shouldReceive('associateAccessToken')->andReturn(1);
$testCredentials = function() { return 1; };
$a = $this->returnDefault();
2013-12-06 01:55:55 +05:30
$pgrant = new League\OAuth2\Server\Grant\Password();
$pgrant->setVerifyCredentialsCallback($testCredentials);
$a->addGrantType($pgrant);
$a->requireScopeParam(false);
$a->setDefaultScope(array('foobar', 'barfoo'));
$v = $a->issueAccessToken(array(
'grant_type' => 'password',
'client_id' => 1234,
'client_secret' => 5678,
'username' => 'foo',
'password' => 'bar',
'scope' => ''
));
$this->assertArrayHasKey('access_token', $v);
$this->assertArrayHasKey('token_type', $v);
$this->assertArrayHasKey('expires', $v);
$this->assertArrayHasKey('expires_in', $v);
2013-03-22 17:11:04 +05:30
}
public function test_issueAccessToken_passwordGrant_goodScope()
{
$this->scope->shouldReceive('getScope')->andReturn(array(
'id' => 1,
'scope' => 'foo',
'name' => 'Foo Name',
'description' => 'Foo Name Description'
));
$this->client->shouldReceive('getClient')->andReturn(array(
'client_id' => 1234,
'client_secret' => 5678,
'redirect_uri' => 'http://foo/redirect',
'name' => 'Example Client'
));
$this->client->shouldReceive('validateRefreshToken')->andReturn(1);
$this->session->shouldReceive('validateAuthCode')->andReturn(1);
$this->session->shouldReceive('createSession')->andReturn(1);
$this->session->shouldReceive('deleteSession')->andReturn(null);
$this->session->shouldReceive('updateRefreshToken')->andReturn(null);
$this->session->shouldReceive('associateScope')->andReturn(null);
2013-05-06 23:39:36 +05:30
$this->session->shouldReceive('associateAccessToken')->andReturn(1);
2013-03-22 17:11:04 +05:30
$testCredentials = function() { return 1; };
2013-03-22 17:11:04 +05:30
$a = $this->returnDefault();
2013-12-06 01:55:55 +05:30
$pgrant = new League\OAuth2\Server\Grant\Password();
2013-03-22 17:11:04 +05:30
$pgrant->setVerifyCredentialsCallback($testCredentials);
$a->addGrantType($pgrant);
$v = $a->issueAccessToken(array(
'grant_type' => 'password',
'client_id' => 1234,
'client_secret' => 5678,
'username' => 'foo',
'password' => 'bar',
'scope' => 'blah'
));
$this->assertArrayHasKey('access_token', $v);
$this->assertArrayHasKey('token_type', $v);
$this->assertArrayHasKey('expires', $v);
$this->assertArrayHasKey('expires_in', $v);
2013-03-22 17:11:04 +05:30
}
2013-02-14 01:42:51 +05:30
function test_issueAccessToken_passwordGrant_passedInput()
{
$this->client->shouldReceive('getClient')->andReturn(array(
'client_id' => 1234,
'client_secret' => 5678,
'redirect_uri' => 'http://foo/redirect',
'name' => 'Example Client'
));
$this->client->shouldReceive('validateRefreshToken')->andReturn(1);
$this->session->shouldReceive('validateAuthCode')->andReturn(1);
$this->session->shouldReceive('createSession')->andReturn(1);
$this->session->shouldReceive('deleteSession')->andReturn(null);
$this->session->shouldReceive('updateRefreshToken')->andReturn(null);
2013-05-06 23:39:36 +05:30
$this->session->shouldReceive('associateAccessToken')->andReturn(1);
2013-02-14 01:42:51 +05:30
$testCredentials = function() { return 1; };
2013-02-14 01:42:51 +05:30
$a = $this->returnDefault();
2013-12-06 01:55:55 +05:30
$pgrant = new League\OAuth2\Server\Grant\Password();
2013-02-14 01:42:51 +05:30
$pgrant->setVerifyCredentialsCallback($testCredentials);
$a->addGrantType($pgrant);
2013-03-22 17:11:04 +05:30
$a->requireScopeParam(false);
2013-02-14 01:42:51 +05:30
$v = $a->issueAccessToken(array(
'grant_type' => 'password',
'client_id' => 1234,
'client_secret' => 5678,
'username' => 'foo',
'password' => 'bar'
));
$this->assertArrayHasKey('access_token', $v);
$this->assertArrayHasKey('token_type', $v);
$this->assertArrayHasKey('expires', $v);
$this->assertArrayHasKey('expires_in', $v);
$this->assertEquals($a->getAccessTokenTTL(), $v['expires_in']);
2013-02-14 01:42:51 +05:30
}
function test_issueAccessToken_passwordGrant()
{
$this->client->shouldReceive('getClient')->andReturn(array(
'client_id' => 1234,
'client_secret' => 5678,
'redirect_uri' => 'http://foo/redirect',
'name' => 'Example Client'
));
$this->client->shouldReceive('validateRefreshToken')->andReturn(1);
$this->session->shouldReceive('validateAuthCode')->andReturn(1);
$this->session->shouldReceive('createSession')->andReturn(1);
$this->session->shouldReceive('deleteSession')->andReturn(null);
$this->session->shouldReceive('updateRefreshToken')->andReturn(null);
2013-05-06 23:39:36 +05:30
$this->session->shouldReceive('associateAccessToken')->andReturn(1);
2013-02-14 01:42:51 +05:30
$testCredentials = function() { return 1; };
2013-02-14 01:42:51 +05:30
$a = $this->returnDefault();
2013-12-06 01:55:55 +05:30
$pgrant = new League\OAuth2\Server\Grant\Password();
2013-02-14 01:42:51 +05:30
$pgrant->setVerifyCredentialsCallback($testCredentials);
$a->addGrantType($pgrant);
2013-03-22 17:11:04 +05:30
$a->requireScopeParam(false);
2013-02-14 01:42:51 +05:30
$_POST['grant_type'] = 'password';
$_POST['client_id'] = 1234;
$_POST['client_secret'] = 5678;
$_POST['username'] = 'foo';
$_POST['password'] = 'bar';
$request = new League\OAuth2\Server\Util\Request(array(), $_POST);
2013-02-14 01:42:51 +05:30
$a->setRequest($request);
$v = $a->issueAccessToken();
$this->assertArrayHasKey('access_token', $v);
$this->assertArrayHasKey('token_type', $v);
$this->assertArrayHasKey('expires', $v);
$this->assertArrayHasKey('expires_in', $v);
$this->assertEquals($a->getAccessTokenTTL(), $v['expires_in']);
2013-02-14 01:42:51 +05:30
}
2013-05-07 04:06:59 +05:30
function test_issueAccessToken_passwordGrant_customExpiresIn()
{
$this->client->shouldReceive('getClient')->andReturn(array(
'client_id' => 1234,
'client_secret' => 5678,
'redirect_uri' => 'http://foo/redirect',
'name' => 'Example Client'
));
$this->client->shouldReceive('validateRefreshToken')->andReturn(1);
$this->session->shouldReceive('validateAuthCode')->andReturn(1);
$this->session->shouldReceive('createSession')->andReturn(1);
$this->session->shouldReceive('deleteSession')->andReturn(null);
$this->session->shouldReceive('updateRefreshToken')->andReturn(null);
$this->session->shouldReceive('associateAccessToken')->andReturn(1);
$testCredentials = function() { return 1; };
2013-05-07 04:06:59 +05:30
$a = $this->returnDefault();
2013-12-06 01:55:55 +05:30
$pgrant = new League\OAuth2\Server\Grant\Password();
2013-05-07 04:06:59 +05:30
$pgrant->setVerifyCredentialsCallback($testCredentials);
$pgrant->setAccessTokenTTL(30);
2013-05-07 04:06:59 +05:30
$a->addGrantType($pgrant);
$a->requireScopeParam(false);
$_POST['grant_type'] = 'password';
$_POST['client_id'] = 1234;
$_POST['client_secret'] = 5678;
$_POST['username'] = 'foo';
$_POST['password'] = 'bar';
$request = new League\OAuth2\Server\Util\Request(array(), $_POST);
2013-05-07 04:06:59 +05:30
$a->setRequest($request);
$v = $a->issueAccessToken();
$this->assertArrayHasKey('access_token', $v);
$this->assertArrayHasKey('token_type', $v);
$this->assertArrayHasKey('expires', $v);
$this->assertArrayHasKey('expires_in', $v);
$this->assertNotEquals($a->getAccessTokenTTL(), $v['expires_in']);
$this->assertNotEquals(time()+$a->getAccessTokenTTL(), $v['expires']);
2013-05-07 04:06:59 +05:30
$this->assertEquals(30, $v['expires_in']);
$this->assertEquals(time()+30, $v['expires']);
}
2013-02-14 01:42:51 +05:30
function test_issueAccessToken_passwordGrant_withRefreshToken()
{
$this->client->shouldReceive('getClient')->andReturn(array(
'client_id' => 1234,
'client_secret' => 5678,
'redirect_uri' => 'http://foo/redirect',
'name' => 'Example Client'
));
$this->client->shouldReceive('validateRefreshToken')->andReturn(1);
$this->session->shouldReceive('validateAuthCode')->andReturn(1);
$this->session->shouldReceive('createSession')->andReturn(1);
$this->session->shouldReceive('deleteSession')->andReturn(null);
$this->session->shouldReceive('updateRefreshToken')->andReturn(null);
2013-05-06 23:39:36 +05:30
$this->session->shouldReceive('associateAccessToken')->andReturn(1);
$this->session->shouldReceive('associateRefreshToken')->andReturn(null);
2013-02-14 01:42:51 +05:30
$testCredentials = function() { return 1; };
2013-02-14 01:42:51 +05:30
$a = $this->returnDefault();
2013-12-06 01:55:55 +05:30
$pgrant = new League\OAuth2\Server\Grant\Password();
2013-02-14 01:42:51 +05:30
$pgrant->setVerifyCredentialsCallback($testCredentials);
$a->addGrantType($pgrant);
2013-12-06 01:55:55 +05:30
$a->addGrantType(new League\OAuth2\Server\Grant\RefreshToken());
2013-03-22 17:11:04 +05:30
$a->requireScopeParam(false);
2013-02-14 01:42:51 +05:30
$_POST['grant_type'] = 'password';
$_POST['client_id'] = 1234;
$_POST['client_secret'] = 5678;
$_POST['username'] = 'foo';
$_POST['password'] = 'bar';
$request = new League\OAuth2\Server\Util\Request(array(), $_POST);
2013-02-14 01:42:51 +05:30
$a->setRequest($request);
$v = $a->issueAccessToken();
$this->assertArrayHasKey('access_token', $v);
$this->assertArrayHasKey('token_type', $v);
$this->assertArrayHasKey('expires', $v);
$this->assertArrayHasKey('expires_in', $v);
$this->assertArrayHasKey('refresh_token', $v);
$this->assertEquals($a->getAccessTokenTTL(), $v['expires_in']);
2013-02-14 01:42:51 +05:30
}
}