merged up to the latest master

1.0
Troy Pavlek 2014-11-27 07:36:10 -07:00
commit f781892dcb
10 changed files with 185 additions and 6 deletions

5
.gitattributes vendored Normal file
View File

@ -0,0 +1,5 @@
/test export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/.travis.yml export-ignore
/phpunit.xml export-ignore

View File

@ -38,7 +38,8 @@ The following versions of PHP are supported.
$provider = new League\OAuth2\Client\Provider\<ProviderName>(array(
'clientId' => 'XXXXXXXX',
'clientSecret' => 'XXXXXXXX',
'redirectUri' => 'https://your-registered-redirect-uri/'
'redirectUri' => 'https://your-registered-redirect-uri/',
'scopes' => array('email', '...', '...'),
));
if ( ! isset($_GET['code'])) {
@ -125,6 +126,7 @@ so please help them out with a pull request if you notice this.
- [QQ](https://github.com/tlikai/oauth2-client)
- [Weibo](https://github.com/tlikai/oauth2-client)
- [Meetup](https://github.com/howlowck/meetup-oauth2-provider)
### Client Packages

View File

@ -41,7 +41,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "0.3-dev"
"dev-master": "0.4-dev"
}
}
}

View File

@ -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);
}
}

33
src/Grant/Password.php Normal file
View File

@ -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);
}
}

View File

@ -85,7 +85,7 @@ abstract class AbstractProvider implements ProviderInterface
public function getAuthorizationUrl($options = array())
{
$this->state = md5(uniqid(rand(), true));
$this->state = isset($options['state']) ? $options['state'] : md5(uniqid(rand(), true));
$params = array(
'client_id' => $this->clientId,
@ -214,7 +214,7 @@ abstract class AbstractProvider implements ProviderInterface
* @param string $arg_separator
* @param null|integer $enc_type
* @return string
* @codeCoverageIgnoreStart
* @codeCoverageIgnoreStart
*/
protected function httpBuildQuery($params, $numeric_prefix = 0, $arg_separator = '&', $enc_type = null)
{

View File

@ -55,7 +55,7 @@ class AccessToken
//Battle.net uses accountId instead of uid
isset($options['accountId']) and $this->uid = $options['accountId'];
// We need to know when the token expires. Show preference to
// We need to know when the token expires. Show preference to
// 'expires_in' since it is defined in RFC6749 Section 5.1.
// Defer to 'expires' if it is provided instead.
if (!empty($options['expires_in'])) {
@ -63,7 +63,9 @@ class AccessToken
} elseif (!empty($options['expires'])) {
// Some providers supply the seconds until expiration rather than
// the exact timestamp. Take a best guess at which we received.
$this->expires = ($options['expires'] > time()) ? $options['expires'] : time() + ((int) $options['expires']);
$expires = $options['expires'];
$expiresInFuture = $expires > time();
$this->expires = $expiresInFuture ? $expires : time() + ((int) $expires);
}
// Grab a refresh token so we can update access tokens when they expires

View File

@ -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);
}
}

View File

@ -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'));
}
}

View File

@ -33,4 +33,12 @@ class AbstractProviderTest extends \PHPUnit_Framework_TestCase
$grant = new \StdClass;
$this->provider->getAccessToken($grant, array('invalid_parameter' => 'none'));
}
public function testAuthorizationUrlStateParam()
{
$this->assertContains('state=XXX', $this->provider->getAuthorizationUrl([
'state' => 'XXX'
]));
}
}