initial commit
This commit is contained in:
10
.gitattributes
vendored
Normal file
10
.gitattributes
vendored
Normal file
@@ -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
|
||||||
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
build
|
||||||
|
composer.lock
|
||||||
|
docs
|
||||||
|
vendor
|
||||||
11
.travis.yml
Normal file
11
.travis.yml
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
language: php
|
||||||
|
|
||||||
|
php:
|
||||||
|
- 5.3
|
||||||
|
- 5.4
|
||||||
|
- 5.5
|
||||||
|
- 5.6
|
||||||
|
- hhvm
|
||||||
|
|
||||||
|
install: composer install
|
||||||
|
|
||||||
21
LICENSE.md
Normal file
21
LICENSE.md
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
# The MIT License (MIT)
|
||||||
|
|
||||||
|
Copyright (c) 2014 Jack Wall <jw@jack.gd>
|
||||||
|
|
||||||
|
> 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.
|
||||||
19
README.md
Normal file
19
README.md
Normal file
@@ -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',
|
||||||
|
]);
|
||||||
|
```
|
||||||
33
composer.json
Normal file
33
composer.json
Normal file
@@ -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/"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
10
phpunit.xml
Normal file
10
phpunit.xml
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
<phpunit colors="true" strict="true" backupGlobals="false" bootstrap="vendor/autoload.php">
|
||||||
|
<testsuites>
|
||||||
|
<testsuite name="All">
|
||||||
|
<directory>tests</directory>
|
||||||
|
</testsuite>
|
||||||
|
</testsuites>
|
||||||
|
<php>
|
||||||
|
<ini name="error_reporting" value="32767" />
|
||||||
|
</php>
|
||||||
|
</phpunit>
|
||||||
102
src/Vkontakte.php
Normal file
102
src/Vkontakte.php
Normal file
@@ -0,0 +1,102 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace J4k\OAuth2\Client\Provider;
|
||||||
|
|
||||||
|
use League\OAuth2\Client\Entity\User;
|
||||||
|
use League\OAuth2\Client\Token\AccessToken;
|
||||||
|
use League\OAuth2\Client\Provider\AbstractProvider;
|
||||||
|
|
||||||
|
|
||||||
|
class Vkontakte extends AbstractProvider
|
||||||
|
{
|
||||||
|
public $scopes = [];
|
||||||
|
public $responseType = 'json';
|
||||||
|
|
||||||
|
public function urlAuthorize()
|
||||||
|
{
|
||||||
|
return 'https://oauth.vk.com/authorize';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function urlAccessToken()
|
||||||
|
{
|
||||||
|
return 'https://oauth.vk.com/access_token';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function urlUserDetails(AccessToken $token)
|
||||||
|
{
|
||||||
|
$fields = ['nickname',
|
||||||
|
'screen_name',
|
||||||
|
'sex',
|
||||||
|
'bdate',
|
||||||
|
'city',
|
||||||
|
'country',
|
||||||
|
'timezone',
|
||||||
|
'photo_50',
|
||||||
|
'photo_100',
|
||||||
|
'photo_200_orig',
|
||||||
|
'has_mobile',
|
||||||
|
'contacts',
|
||||||
|
'education',
|
||||||
|
'online',
|
||||||
|
'counters',
|
||||||
|
'relation',
|
||||||
|
'last_seen',
|
||||||
|
'status',
|
||||||
|
'can_write_private_message',
|
||||||
|
'can_see_all_posts',
|
||||||
|
'can_see_audio',
|
||||||
|
'can_post',
|
||||||
|
'universities',
|
||||||
|
'schools',
|
||||||
|
'verified', ];
|
||||||
|
|
||||||
|
return "https://api.vk.com/method/users.get?user_id={$token->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];
|
||||||
|
}
|
||||||
|
}
|
||||||
95
tests/VkontakteTest.php
Normal file
95
tests/VkontakteTest.php
Normal file
@@ -0,0 +1,95 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace J4k\OAuth2\Client\Test\Provider;
|
||||||
|
|
||||||
|
use Mockery as m;
|
||||||
|
|
||||||
|
class VkontakteTest extends \PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
protected $provider;
|
||||||
|
|
||||||
|
protected function setUp()
|
||||||
|
{
|
||||||
|
$this->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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user