Merge pull request #247 from shadowhand/feature/external-google-provider

Move Google to external package
1.0
Ben Ramsey 2015-03-23 18:23:34 -05:00
commit b8fd8d49aa
9 changed files with 36 additions and 243 deletions

View File

@ -108,7 +108,6 @@ $token = $provider->getAccessToken($grant, ['refresh_token' => $refreshToken]);
This package currently has built-in support for:
- Github
- Google
- Instagram
These are as many OAuth 2 services as we plan to support officially. Maintaining a wide selection of providers
@ -127,6 +126,7 @@ so please help them out with a pull request if you notice this.
- [Eventbrite](https://github.com/stevenmaguire/oauth2-eventbrite)
- [Facebook](https://packagist.org/packages/league/oauth2-facebook)
- [FreeAgent](https://github.com/CloudManaged/oauth2-freeagent)
- [Google](https://packagist.org/packages/league/oauth2-google)
- [Google Nest](https://github.com/JC5/nest-oauth2-provider)
- [LinkedIn](https://github.com/thephpleague/oauth2-linkedin)
- [Mail.ru](https://packagist.org/packages/aego/oauth2-mailru)

View File

@ -1,124 +0,0 @@
<?php
namespace League\OAuth2\Client\Provider;
use League\OAuth2\Client\Entity\User;
class Google extends AbstractProvider
{
public $scopeSeparator = ' ';
public $scopes = [
'profile',
'email',
];
public $authorizationHeader = 'OAuth';
/**
* @var string If set, this will be sent to google as the "hd" parameter.
* @link https://developers.google.com/accounts/docs/OAuth2Login#hd-param
*/
public $hostedDomain = '';
public function setHostedDomain($hd)
{
$this->hostedDomain = $hd;
}
public function getHostedDomain()
{
return $this->hostedDomain;
}
/**
* @var string If set, this will be sent to google as the "access_type" parameter.
* @link https://developers.google.com/accounts/docs/OAuth2WebServer#offline
*/
public $accessType = '';
public function setAccessType($accessType)
{
$this->accessType = $accessType;
}
public function getAccessType()
{
return $this->accessType;
}
public function urlAuthorize()
{
return 'https://accounts.google.com/o/oauth2/auth';
}
public function urlAccessToken()
{
return 'https://accounts.google.com/o/oauth2/token';
}
public function urlUserDetails(\League\OAuth2\Client\Token\AccessToken $token)
{
return
'https://www.googleapis.com/plus/v1/people/me?'.
'fields=id%2Cname(familyName%2CgivenName)%2CdisplayName%2C'.
'emails%2Fvalue%2Cimage%2Furl&alt=json';
}
public function userDetails($response, \League\OAuth2\Client\Token\AccessToken $token)
{
$response = (array) $response;
$user = new User();
$imageUrl = (isset($response['image']) &&
$response['image']->url) ? $response['image']->url : null;
$email =
(isset($response['emails']) &&
count($response['emails']) &&
$response['emails'][0]->value)? $response['emails'][0]->value : null;
$user->exchangeArray([
'uid' => $response['id'],
'name' => $response['displayName'],
'firstname' => $response['name']->givenName,
'lastName' => $response['name']->familyName,
'email' => $email,
'imageUrl' => $imageUrl,
]);
return $user;
}
public function userUid($response, \League\OAuth2\Client\Token\AccessToken $token)
{
return $response->id;
}
public function userEmail($response, \League\OAuth2\Client\Token\AccessToken $token)
{
return ($response->emails &&
count($response->emails) &&
$response->emails[0]->value) ? $response->emails[0]->value : null;
}
public function userScreenName($response, \League\OAuth2\Client\Token\AccessToken $token)
{
return [$response->name->givenName, $response->name->familyName];
}
public function getAuthorizationUrl($options = array())
{
$url = parent::getAuthorizationUrl($options);
if (!empty($this->hostedDomain)) {
$url .= '&' . $this->httpBuildQuery(['hd' => $this->hostedDomain]);
}
if (!empty($this->accessType)) {
$url .= '&' . $this->httpBuildQuery(['access_type'=> $this->accessType]);
}
return $url;
}
}

View File

@ -11,7 +11,7 @@ class AuthorizationCodeTest extends \PHPUnit_Framework_TestCase
protected function setUp()
{
$this->provider = new \League\OAuth2\Client\Provider\Google([
$this->provider = new \League\OAuth2\Client\Test\Provider\Fake([
'clientId' => 'mock_client_id',
'clientSecret' => 'mock_secret',
'redirectUri' => 'none',

View File

@ -12,7 +12,7 @@ class ClientCredentialsTest extends \PHPUnit_Framework_TestCase
protected function setUp()
{
$this->provider = new \League\OAuth2\Client\Provider\Google(array(
$this->provider = new \League\OAuth2\Client\Test\Provider\Fake(array(
'clientId' => 'mock_client_id',
'clientSecret' => 'mock_secret',
'redirectUri' => 'none',

View File

@ -12,7 +12,7 @@ class PasswordTest extends \PHPUnit_Framework_TestCase
protected function setUp()
{
$this->provider = new \League\OAuth2\Client\Provider\Google(array(
$this->provider = new \League\OAuth2\Client\Test\Provider\Fake(array(
'clientId' => 'mock_client_id',
'clientSecret' => 'mock_secret',
'redirectUri' => 'none',

View File

@ -11,7 +11,7 @@ class RefreshTokenTest extends \PHPUnit_Framework_TestCase
protected function setUp()
{
$this->provider = new \League\OAuth2\Client\Provider\Google([
$this->provider = new \League\OAuth2\Client\Test\Provider\Fake([
'clientId' => 'mock_client_id',
'clientSecret' => 'mock_secret',
'redirectUri' => 'none',

View File

@ -15,7 +15,7 @@ class AbstractProviderTest extends \PHPUnit_Framework_TestCase
protected function setUp()
{
$this->provider = new \League\OAuth2\Client\Provider\Google([
$this->provider = new \League\OAuth2\Client\Test\Provider\Fake([
'clientId' => 'mock_client_id',
'clientSecret' => 'mock_secret',
'redirectUri' => 'none',

View File

@ -0,0 +1,30 @@
<?php
namespace League\OAuth2\Client\Test\Provider;
use League\OAuth2\Client\Entity\User;
use League\OAuth2\Client\Token\AccessToken;
use League\OAuth2\Client\Provider\AbstractProvider;
class Fake extends AbstractProvider
{
public function urlAuthorize()
{
return 'http://example.com/oauth/authorize';
}
public function urlAccessToken()
{
return 'http://example.com/oauth/token';
}
public function urlUserDetails(AccessToken $token)
{
return 'http://example.com/oauth/user';
}
public function userDetails($response, AccessToken $token)
{
return new User;
}
}

View File

@ -1,113 +0,0 @@
<?php
namespace League\OAuth2\Client\Test\Provider;
use Mockery as m;
class GoogleTest extends ConcreteProviderTest
{
/**
* @var \League\OAuth2\Client\Provider\Google
*/
protected $provider;
protected function setUp()
{
$this->provider = new \League\OAuth2\Client\Provider\Google([
'clientId' => 'mock_client_id',
'clientSecret' => 'mock_secret',
'redirectUri' => 'none',
'hostedDomain' => 'mock_domain',
'accessType' => 'mock_access_type'
]);
}
public function testAuthorizationUrl()
{
$url = $this->provider->getAuthorizationUrl();
$uri = parse_url($url);
parse_str($uri['query'], $query);
$this->assertArrayHasKey('client_id', $query);
$this->assertArrayHasKey('redirect_uri', $query);
$this->assertArrayHasKey('state', $query);
$this->assertArrayHasKey('scope', $query);
$this->assertArrayHasKey('response_type', $query);
$this->assertArrayHasKey('approval_prompt', $query);
$this->assertArrayHasKey('hd', $query);
$this->assertArrayHasKey('access_type', $query);
$this->assertNotNull($this->provider->state);
}
public function testUrlAccessToken()
{
$url = $this->provider->urlAccessToken();
$uri = parse_url($url);
$this->assertEquals('/o/oauth2/token', $uri['path']);
}
public function testGetAccessToken()
{
$client = $this->createMockHttpClient();
$client->shouldReceive('post')->times(1)->andReturn($this->createMockResponse('{"access_token": "mock_access_token", "expires": 3600, "refresh_token": "mock_refresh_token", "uid": 1}'));
$this->provider->setHttpClient($client);
$token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']);
$this->assertEquals('mock_access_token', $token->accessToken);
$this->assertLessThanOrEqual(time() + 3600, $token->expires);
$this->assertGreaterThanOrEqual(time(), $token->expires);
$this->assertEquals('mock_refresh_token', $token->refreshToken);
$this->assertEquals('1', $token->uid);
}
public function testScopes()
{
$this->assertEquals(['profile', 'email'], $this->provider->getScopes());
}
public function testUserData()
{
$client = $this->createMockHttpClient();
$postResponse = $this->createMockResponse('{"access_token": "mock_access_token", "expires": 3600, "refresh_token": "mock_refresh_token", "uid": 1}');
$getResponse = $this->createMockResponse('{"emails": [{"value": "mock_email"}],"id": "12345","displayName": "mock_name","name": {"familyName": "mock_last_name","givenName": "mock_first_name"},"image": {"url": "mock_image_url"}}');
$client->shouldReceive('post')->times(1)->andReturn($postResponse);
$client->shouldReceive('get')->times(4)->andReturn($getResponse);
$this->provider->setHttpClient($client);
$token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']);
$user = $this->provider->getUserDetails($token);
$this->assertEquals(12345, $this->provider->getUserUid($token));
$this->assertEquals(['mock_first_name', 'mock_last_name'], $this->provider->getUserScreenName($token));
$this->assertEquals('mock_email', $this->provider->getUserEmail($token));
$this->assertEquals('mock_email', $user->email);
}
public function testGetHostedDomain()
{
$this->assertEquals('mock_domain', $this->provider->getHostedDomain());
}
public function testSetHostedDomain()
{
$this->provider->setHostedDomain('changed_domain');
$this->assertEquals('changed_domain', $this->provider->hostedDomain);
}
public function testGetAccessType()
{
$this->assertEquals('mock_access_type', $this->provider->getAccessType());
}
public function testSetAccessType()
{
$this->provider->setAccessType('changed_access_type');
$this->assertEquals('changed_access_type', $this->provider->accessType);
}
}