From ccb2a5994b658688e542689eae96c7d08de01344 Mon Sep 17 00:00:00 2001 From: Woody Gilk Date: Sun, 22 Mar 2015 00:13:28 -0500 Subject: [PATCH 1/2] Move Google provider to an external repository --- README.md | 2 +- src/Provider/Google.php | 124 ------------------------------- test/src/Provider/GoogleTest.php | 113 ---------------------------- 3 files changed, 1 insertion(+), 238 deletions(-) delete mode 100644 src/Provider/Google.php delete mode 100644 test/src/Provider/GoogleTest.php diff --git a/README.md b/README.md index 2811b36..525141a 100644 --- a/README.md +++ b/README.md @@ -109,7 +109,6 @@ This package currently has built-in support for: - Eventbrite - 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. - [Dropbox](https://github.com/pixelfear/oauth2-dropbox) - [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) diff --git a/src/Provider/Google.php b/src/Provider/Google.php deleted file mode 100644 index 87393ee..0000000 --- a/src/Provider/Google.php +++ /dev/null @@ -1,124 +0,0 @@ -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; - } -} diff --git a/test/src/Provider/GoogleTest.php b/test/src/Provider/GoogleTest.php deleted file mode 100644 index eed8d0e..0000000 --- a/test/src/Provider/GoogleTest.php +++ /dev/null @@ -1,113 +0,0 @@ -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); - } -} From 2aae576f04e974d689db35c88207af63e5f6f3bc Mon Sep 17 00:00:00 2001 From: Woody Gilk Date: Sun, 22 Mar 2015 00:18:59 -0500 Subject: [PATCH 2/2] Use Fake provider for testing, instead of Google --- test/src/Grant/AuthorizationCodeTest.php | 2 +- test/src/Grant/ClientCredentialsTest.php | 2 +- test/src/Grant/PasswordTest.php | 2 +- test/src/Grant/RefreshTokenTest.php | 2 +- test/src/Provider/AbstractProviderTest.php | 2 +- test/src/Provider/Fake.php | 30 ++++++++++++++++++++++ 6 files changed, 35 insertions(+), 5 deletions(-) create mode 100644 test/src/Provider/Fake.php diff --git a/test/src/Grant/AuthorizationCodeTest.php b/test/src/Grant/AuthorizationCodeTest.php index 969685b..5fd097c 100644 --- a/test/src/Grant/AuthorizationCodeTest.php +++ b/test/src/Grant/AuthorizationCodeTest.php @@ -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', diff --git a/test/src/Grant/ClientCredentialsTest.php b/test/src/Grant/ClientCredentialsTest.php index 115fbd7..3d704f0 100644 --- a/test/src/Grant/ClientCredentialsTest.php +++ b/test/src/Grant/ClientCredentialsTest.php @@ -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', diff --git a/test/src/Grant/PasswordTest.php b/test/src/Grant/PasswordTest.php index bf6725a..7abff8d 100644 --- a/test/src/Grant/PasswordTest.php +++ b/test/src/Grant/PasswordTest.php @@ -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', diff --git a/test/src/Grant/RefreshTokenTest.php b/test/src/Grant/RefreshTokenTest.php index 75b3bdb..7576b1c 100644 --- a/test/src/Grant/RefreshTokenTest.php +++ b/test/src/Grant/RefreshTokenTest.php @@ -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', diff --git a/test/src/Provider/AbstractProviderTest.php b/test/src/Provider/AbstractProviderTest.php index b27e832..6092c3a 100644 --- a/test/src/Provider/AbstractProviderTest.php +++ b/test/src/Provider/AbstractProviderTest.php @@ -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', diff --git a/test/src/Provider/Fake.php b/test/src/Provider/Fake.php new file mode 100644 index 0000000..0302de5 --- /dev/null +++ b/test/src/Provider/Fake.php @@ -0,0 +1,30 @@ +