Skip to content

Commit

Permalink
Merge pull request #1 from Nafania/3122076
Browse files Browse the repository at this point in the history
Added mandatory version for vkontakte api
  • Loading branch information
sergey-sla authored Apr 24, 2018
2 parents e6393bc + e7cfb82 commit 71188a0
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 60 deletions.
77 changes: 68 additions & 9 deletions src/Vkontakte.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,62 @@

namespace J4k\OAuth2\Client\Provider;

use League\OAuth2\Client\Entity\User as OAuthUser;
use League\OAuth2\Client\Token\AccessToken;
use League\OAuth2\Client\Provider\AbstractProvider;
use J4k\OAuth2\Client\Provider\Exception\VkontakteProviderException;
use League\OAuth2\Client\Provider\Exception\VkontakteProviderException;
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
use Psr\Http\Message\ResponseInterface;


/**
* Class Vkontakte
* @package J4k\OAuth2\Client\Provider
*/
class Vkontakte extends AbstractProvider
{
protected $scopes = ['email'];
protected $responseType = 'json';

/**
* @var bool version of vkontakte api
*/
protected $version;

/**
* Vkontakte constructor.
* @param array $options
* @param array $collaborators
* @throws VkontakteProviderException
*/
public function __construct(array $options = [], array $collaborators = [])
{
if (empty($options['version'])) {
throw new VkontakteProviderException('Version param is mandatory');
}

parent::__construct($options, $collaborators);
}

/**
* @return string
*/
public function getBaseAuthorizationUrl()
{
return 'https://oauth.vk.com/authorize';
}

public function getBaseAccessTokenUrl(array $params)
/**
* @param array $params
* @return string
*/
public function getBaseAccessTokenUrl(array $params = [])
{
return 'https://oauth.vk.com/access_token';
}

/**
* @param AccessToken $token
* @return string
*/
public function getResourceOwnerDetailsUrl(AccessToken $token)
{
$fields = [
Expand Down Expand Up @@ -54,25 +87,38 @@ public function getResourceOwnerDetailsUrl(AccessToken $token)
'can_post',
'universities',
'schools',
'verified'];
'verified'
];

$userId = $token->getResourceOwnerId();
$tokenValue = $token->getToken();
return "https://api.vk.com/method/users.get?user_id={$userId}&fields="
.implode($this->getScopeSeparator(), $fields)."&access_token={$tokenValue}";
. implode($this->getScopeSeparator(), $fields) . "&access_token={$tokenValue}&v=" . $this->version;
}

/**
* @return array
*/
protected function getDefaultScopes()
{
return $this->scopes;
}


/**
* @param string $grant
* @param array $params
* @return AccessToken
*/
public function getAccessToken($grant = 'authorization_code', array $params = [])
{
$accessToken = parent::getAccessToken($grant, $params);
return $accessToken;
return parent::getAccessToken($grant, $params);
}

/**
* @param ResponseInterface $response
* @param array|string $data
* @throws IdentityProviderException
*/
protected function checkResponse(ResponseInterface $response, $data)
{
if (isset($data['error'])) {
Expand All @@ -84,16 +130,29 @@ protected function checkResponse(ResponseInterface $response, $data)
}
}

/**
* @param array $response
* @param AccessToken $token
* @return VkontakteResourceOwner|\League\OAuth2\Client\Provider\ResourceOwnerInterface
*/
protected function createResourceOwner(array $response, AccessToken $token)
{
return new VkontakteResourceOwner($response, $token);
}

/**
* @return string
*/
protected function getAccessTokenResourceOwnerId()
{
return 'user_id';
}

/**
* @param array $response
* @param \League\OAuth2\Client\Grant\AbstractGrant $grant
* @return AccessToken
*/
protected function createAccessToken(array $response, \League\OAuth2\Client\Grant\AbstractGrant $grant)
{
$token = parent::createAccessToken($response, $grant);
Expand Down
2 changes: 1 addition & 1 deletion src/VkontakteProviderException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

namespace League\OAuth2\Client\Provider\Exception;

class VkontakteProviderException extends Exception
class VkontakteProviderException extends \Exception
{
}
57 changes: 7 additions & 50 deletions tests/VkontakteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

namespace J4k\OAuth2\Client\Test\Provider;

use J4k\OAuth2\Client\Provider\Vkontakte;
use Mockery as m;

class VkontakteTest extends \PHPUnit_Framework_TestCase
{
/**
* @var Vkontakte
*/
protected $provider;

protected function setUp()
Expand All @@ -14,6 +18,7 @@ protected function setUp()
'clientId' => 'mock_client_id',
'clientSecret' => 'mock_secret',
'redirectUri' => 'none',
'version' => '5.73'
]);
}

Expand All @@ -35,62 +40,14 @@ public function testAuthorizationUrl()
$this->assertArrayHasKey('scope', $query);
$this->assertArrayHasKey('response_type', $query);
$this->assertArrayHasKey('approval_prompt', $query);
$this->assertNotNull($this->provider->state);
$this->assertNotNull($this->provider->getState());
}

public function testUrlAccessToken()
{
$url = $this->provider->urlAccessToken();
$url = $this->provider->getBaseAccessTokenUrl();
$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, "email": "mock_email"}');

$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);
$this->assertEquals('mock_email', $token->email);
}

public function testScopes()
{
$this->assertEquals(['email'], $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, "email": "mock_email"}');

$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", "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);
}
}

0 comments on commit 71188a0

Please sign in to comment.