From 189ffea4dd21ed8fe4c43a4e2233eac80cae18e3 Mon Sep 17 00:00:00 2001 From: Jack Wall Date: Sun, 7 Dec 2014 23:00:39 +0000 Subject: [PATCH] initial commit --- .gitattributes | 10 ++++ .gitignore | 4 ++ .travis.yml | 11 +++++ LICENSE.md | 21 +++++++++ README.md | 19 ++++++++ composer.json | 33 +++++++++++++ phpunit.xml | 10 ++++ src/Vkontakte.php | 102 ++++++++++++++++++++++++++++++++++++++++ tests/VkontakteTest.php | 95 +++++++++++++++++++++++++++++++++++++ 9 files changed, 305 insertions(+) create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 .travis.yml create mode 100644 LICENSE.md create mode 100644 README.md create mode 100644 composer.json create mode 100644 phpunit.xml create mode 100644 src/Vkontakte.php create mode 100644 tests/VkontakteTest.php diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..b263871 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,10 @@ +# Path-based git attributes +# https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html + +# Ignore all test and documentation with "export-ignore". +/.gitattributes export-ignore +/.gitignore export-ignore +/.travis.yml export-ignore +/phpunit.xml.dist export-ignore +/.scrutinizer.yml export-ignore +/tests export-ignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..751f6c5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +build +composer.lock +docs +vendor \ No newline at end of file diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..948062b --- /dev/null +++ b/.travis.yml @@ -0,0 +1,11 @@ +language: php + +php: + - 5.3 + - 5.4 + - 5.5 + - 5.6 + - hhvm + +install: composer install + diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..247d29d --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,21 @@ +# The MIT License (MIT) + +Copyright (c) 2014 Jack Wall + +> Permission is hereby granted, free of charge, to any person obtaining a copy +> of this software and associated documentation files (the "Software"), to deal +> in the Software without restriction, including without limitation the rights +> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +> copies of the Software, and to permit persons to whom the Software is +> furnished to do so, subject to the following conditions: +> +> The above copyright notice and this permission notice shall be included in +> all copies or substantial portions of the Software. +> +> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +> THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..46dd33a --- /dev/null +++ b/README.md @@ -0,0 +1,19 @@ +# Vkontakte OAuth2 client provider + +This package provides [Vkontakte](https://vk.com) integration for [OAuth2 Client](https://github.com/thephpleague/oauth2-client) by the League. + +## Installation + +```sh +composer require j4k/oauth2-vkontakte +``` + +## Usage + +```php +$provider = new Aego\OAuth2\Client\Provider\Vkontakte([ + 'clientId' => 'b80bb7740288fda1f201890375a60c8f', + 'clientSecret' => 'f23ccd066f8236c6f97a2a62d3f9f9f5', + 'redirectUri' => 'https://example.org/oauth-endpoint', +]); +``` diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..3fe6bdf --- /dev/null +++ b/composer.json @@ -0,0 +1,33 @@ +{ + "name": "j4k/oauth2-vkontakte", + "description": "Vkontakte provider for league/oauth2-client", + "keywords": [ + "league", + "package" + ], + "license": "MIT", + "authors": [ + { + "name": "Jack Wall", + "email": "jw@jack.gd" + } + ], + "require": { + "php" : "~5.4", + "league/oauth2-client": "~0.5" + }, + "require-dev": { + "phpunit/phpunit" : "~4.3", + "mockery/mockery": "~0.9" + }, + "autoload": { + "psr-4": { + "J4k\\OAuth2\\Client\\Provider\\": "src" + } + }, + "autoload-dev": { + "psr-4": { + "J4k\\OAuth2\\Client\\Test\\Provider\\": "tests/" + } + } +} diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..9434224 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,10 @@ + + + + tests + + + + + + diff --git a/src/Vkontakte.php b/src/Vkontakte.php new file mode 100644 index 0000000..0f72527 --- /dev/null +++ b/src/Vkontakte.php @@ -0,0 +1,102 @@ +uid}&fields=" + .implode(",", $fields)."&access_token={$token}"; + } + + public function userDetails($response, AccessToken $token) + { + $response = $response->response[0]; + + $user = new User(); + + $email = (isset($response->email)) ? $response->email : null; + $location = (isset($response->country)) ? $response->country : null; + $description = (isset($response->status)) ? $response->status : null; + + $user->exchangeArray([ + 'uid' => $response->uid, + 'nickname' => $response->nickname, + 'name' => $response->screen_name, + 'firstname' => $response->first_name, + 'lastname' => $response->last_name, + 'email' => $email, + 'location' => $location, + 'description' => $description, + 'imageUrl' => $response->photo_200_orig, + ]); + + return $user; + } + + public function userUid($response, AccessToken $token) + { + $response = $response->response[0]; + + return $response->uid; + } + + public function userEmail($response, AccessToken $token) + { + $response = $response->response[0]; + + return isset($response->email) && $response->email ? $response->email : null; + } + + public function userScreenName($response, AccessToken $token) + { + $response = $response->response[0]; + + return [$response->first_name, $response->last_name]; + } +} diff --git a/tests/VkontakteTest.php b/tests/VkontakteTest.php new file mode 100644 index 0000000..42e772a --- /dev/null +++ b/tests/VkontakteTest.php @@ -0,0 +1,95 @@ +provider = new \J4k\OAuth2\Client\Provider\Vkontakte([ + 'clientId' => 'mock_client_id', + 'clientSecret' => 'mock_secret', + 'redirectUri' => 'none', + ]); + } + + public function tearDown() + { + m::close(); + parent::tearDown(); + } + + 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->assertNotNull($this->provider->state); + } + + public function testUrlAccessToken() + { + $url = $this->provider->urlAccessToken(); + $uri = parse_url($url); + + $this->assertEquals('/access_token', $uri['path']); + } + + public function testGetAccessToken() + { + $response = m::mock('Guzzle\Http\Message\Response'); + $response->shouldReceive('getBody')->times(1)->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('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([], $this->provider->getScopes()); + } + + public function testUserData() + { + $postResponse = m::mock('Guzzle\Http\Message\Response'); + $postResponse->shouldReceive('getBody')->times(1)->andReturn('{"access_token": "mock_access_token", "expires": 3600, "refresh_token": "mock_refresh_token", "uid": 1}'); + + $getResponse = m::mock('Guzzle\Http\Message\Response'); + $getResponse->shouldReceive('getBody')->times(4)->andReturn('{"response": [{"uid": 12345, "nickname": "mock_nickname", "screen_name": "mock_name", "first_name": "mock_first_name", "last_name": "mock_last_name", "email": "mock_email", "country": "UK", "status": "mock_status", "photo_200_orig": "mock_image_url"}]}'); + + $client = m::mock('Guzzle\Service\Client'); + $client->shouldReceive('setBaseUrl')->times(5); + $client->shouldReceive('post->send')->times(1)->andReturn($postResponse); + $client->shouldReceive('get->send')->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); + } +}