Merge pull request #107 from kevindierkx/grant-support
Added ClientCredentials and Password Grants1.0
commit
b747fdde54
|
@ -0,0 +1,25 @@
|
|||
<?php
|
||||
|
||||
namespace League\OAuth2\Client\Grant;
|
||||
|
||||
use League\OAuth2\Client\Token\AccessToken;
|
||||
|
||||
class ClientCredentials implements GrantInterface
|
||||
{
|
||||
public function __toString()
|
||||
{
|
||||
return 'client_credentials';
|
||||
}
|
||||
|
||||
public function prepRequestParams($defaultParams, $params)
|
||||
{
|
||||
$params['grant_type'] = 'client_credentials';
|
||||
|
||||
return array_merge($defaultParams, $params);
|
||||
}
|
||||
|
||||
public function handleResponse($response = array())
|
||||
{
|
||||
return new AccessToken($response);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
|
||||
namespace League\OAuth2\Client\Grant;
|
||||
|
||||
use League\OAuth2\Client\Token\AccessToken;
|
||||
|
||||
class Password implements GrantInterface
|
||||
{
|
||||
public function __toString()
|
||||
{
|
||||
return 'password';
|
||||
}
|
||||
|
||||
public function prepRequestParams($defaultParams, $params)
|
||||
{
|
||||
if (! isset($params['username']) || empty($params['username'])) {
|
||||
throw new \BadMethodCallException('Missing username');
|
||||
}
|
||||
|
||||
if (! isset($params['password']) || empty($params['password'])) {
|
||||
throw new \BadMethodCallException('Missing password');
|
||||
}
|
||||
|
||||
$params['grant_type'] = 'password';
|
||||
|
||||
return array_merge($defaultParams, $params);
|
||||
}
|
||||
|
||||
public function handleResponse($response = array())
|
||||
{
|
||||
return new AccessToken($response);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
<?php
|
||||
|
||||
namespace League\OAuth2\Client\Test\Grant;
|
||||
|
||||
use \Mockery as m;
|
||||
|
||||
class ClientCredentialsTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
protected $provider;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->provider = new \League\OAuth2\Client\Provider\Google(array(
|
||||
'clientId' => 'mock_client_id',
|
||||
'clientSecret' => 'mock_secret',
|
||||
'redirectUri' => 'none',
|
||||
));
|
||||
}
|
||||
|
||||
public function testGetAccessToken()
|
||||
{
|
||||
$response = m::mock('Guzzle\Http\Message\Response');
|
||||
$response->shouldReceive('getBody')->times(2)->andReturn('{"access_token": "mock_access_token", "expires": 3600, "refresh_token": "mock_refresh_token", "uid": 1}');
|
||||
|
||||
$client = m::mock('Guzzle\Service\Client');
|
||||
$client->shouldReceive('setBaseUrl')->times(1);
|
||||
$client->shouldReceive('post->send')->times(1)->andReturn($response);
|
||||
$this->provider->setHttpClient($client);
|
||||
|
||||
$token = $this->provider->getAccessToken('client_credentials');
|
||||
$this->assertInstanceOf('League\OAuth2\Client\Token\AccessToken', $token);
|
||||
|
||||
$grant = new \League\OAuth2\Client\Grant\ClientCredentials();
|
||||
$this->assertEquals('client_credentials', (string) $grant);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,68 @@
|
|||
<?php
|
||||
|
||||
namespace League\OAuth2\Client\Test\Grant;
|
||||
|
||||
use \Mockery as m;
|
||||
|
||||
class PasswordTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
protected $provider;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->provider = new \League\OAuth2\Client\Provider\Google(array(
|
||||
'clientId' => 'mock_client_id',
|
||||
'clientSecret' => 'mock_secret',
|
||||
'redirectUri' => 'none',
|
||||
));
|
||||
}
|
||||
|
||||
public function testGetAccessToken()
|
||||
{
|
||||
$response = m::mock('Guzzle\Http\Message\Response');
|
||||
$response->shouldReceive('getBody')->times(2)->andReturn('{"access_token": "mock_access_token", "expires": 3600, "refresh_token": "mock_refresh_token", "uid": 1}');
|
||||
|
||||
$client = m::mock('Guzzle\Service\Client');
|
||||
$client->shouldReceive('setBaseUrl')->times(1);
|
||||
$client->shouldReceive('post->send')->times(1)->andReturn($response);
|
||||
$this->provider->setHttpClient($client);
|
||||
|
||||
$token = $this->provider->getAccessToken('password', array('username' => 'mock_username', 'password' => 'mock_password'));
|
||||
$this->assertInstanceOf('League\OAuth2\Client\Token\AccessToken', $token);
|
||||
|
||||
$grant = new \League\OAuth2\Client\Grant\Password();
|
||||
$this->assertEquals('password', (string) $grant);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException BadMethodCallException
|
||||
*/
|
||||
public function testInvalidUsername()
|
||||
{
|
||||
$response = m::mock('Guzzle\Http\Message\Response');
|
||||
$response->shouldReceive('getBody')->times(2)->andReturn('{"access_token": "mock_access_token", "expires": 3600, "refresh_token": "mock_refresh_token", "uid": 1}');
|
||||
|
||||
$client = m::mock('Guzzle\Service\Client');
|
||||
$client->shouldReceive('setBaseUrl')->times(1);
|
||||
$client->shouldReceive('post->send')->times(1)->andReturn($response);
|
||||
$this->provider->setHttpClient($client);
|
||||
|
||||
$this->provider->getAccessToken('password', array('invalid_username' => 'mock_username', 'password' => 'mock_password'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException BadMethodCallException
|
||||
*/
|
||||
public function testInvalidPassword()
|
||||
{
|
||||
$response = m::mock('Guzzle\Http\Message\Response');
|
||||
$response->shouldReceive('getBody')->times(2)->andReturn('{"access_token": "mock_access_token", "expires": 3600, "refresh_token": "mock_refresh_token", "uid": 1}');
|
||||
|
||||
$client = m::mock('Guzzle\Service\Client');
|
||||
$client->shouldReceive('setBaseUrl')->times(1);
|
||||
$client->shouldReceive('post->send')->times(1)->andReturn($response);
|
||||
$this->provider->setHttpClient($client);
|
||||
|
||||
$this->provider->getAccessToken('password', array('username' => 'mock_username', 'invalid_password' => 'mock_password'));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue