@@ -9,7 +9,7 @@ use League\OAuth2\Client\Provider\AbstractProvider;
|
|||||||
|
|
||||||
class Vkontakte extends AbstractProvider
|
class Vkontakte extends AbstractProvider
|
||||||
{
|
{
|
||||||
public $scopes = [];
|
public $scopes = ['email'];
|
||||||
public $uidKey = 'user_id';
|
public $uidKey = 'user_id';
|
||||||
public $responseType = 'json';
|
public $responseType = 'json';
|
||||||
|
|
||||||
@@ -22,10 +22,94 @@ class Vkontakte extends AbstractProvider
|
|||||||
{
|
{
|
||||||
return 'https://oauth.vk.com/access_token';
|
return 'https://oauth.vk.com/access_token';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getAccessToken($grant = 'authorization_code', $params = [])
|
||||||
|
{
|
||||||
|
if (is_string($grant)) {
|
||||||
|
// PascalCase the grant. E.g: 'authorization_code' becomes 'AuthorizationCode'
|
||||||
|
$className = str_replace(' ', '', ucwords(str_replace(['-', '_'], ' ', $grant)));
|
||||||
|
$grant = 'League\\OAuth2\\Client\\Grant\\'.$className;
|
||||||
|
if (! class_exists($grant)) {
|
||||||
|
throw new \InvalidArgumentException('Unknown grant "'.$grant.'"');
|
||||||
|
}
|
||||||
|
$grant = new $grant();
|
||||||
|
} elseif (! $grant instanceof GrantInterface) {
|
||||||
|
$message = get_class($grant).' is not an instance of League\OAuth2\Client\Grant\GrantInterface';
|
||||||
|
throw new \InvalidArgumentException($message);
|
||||||
|
}
|
||||||
|
|
||||||
|
$defaultParams = [
|
||||||
|
'client_id' => $this->clientId,
|
||||||
|
'client_secret' => $this->clientSecret,
|
||||||
|
'redirect_uri' => $this->redirectUri,
|
||||||
|
'grant_type' => $grant,
|
||||||
|
];
|
||||||
|
|
||||||
|
$requestParams = $grant->prepRequestParams($defaultParams, $params);
|
||||||
|
|
||||||
|
try {
|
||||||
|
switch (strtoupper($this->method)) {
|
||||||
|
case 'GET':
|
||||||
|
// @codeCoverageIgnoreStart
|
||||||
|
// No providers included with this library use get but 3rd parties may
|
||||||
|
$client = $this->getHttpClient();
|
||||||
|
$client->setBaseUrl($this->urlAccessToken() . '?' . $this->httpBuildQuery($requestParams, '', '&'));
|
||||||
|
$request = $client->get(null, null, $requestParams)->send();
|
||||||
|
$response = $request->getBody();
|
||||||
|
break;
|
||||||
|
// @codeCoverageIgnoreEnd
|
||||||
|
case 'POST':
|
||||||
|
$client = $this->getHttpClient();
|
||||||
|
$client->setBaseUrl($this->urlAccessToken());
|
||||||
|
$request = $client->post(null, null, $requestParams)->send();
|
||||||
|
$response = $request->getBody();
|
||||||
|
break;
|
||||||
|
// @codeCoverageIgnoreStart
|
||||||
|
default:
|
||||||
|
throw new \InvalidArgumentException('Neither GET nor POST is specified for request');
|
||||||
|
// @codeCoverageIgnoreEnd
|
||||||
|
}
|
||||||
|
} catch (BadResponseException $e) {
|
||||||
|
// @codeCoverageIgnoreStart
|
||||||
|
$response = $e->getResponse()->getBody();
|
||||||
|
// @codeCoverageIgnoreEnd
|
||||||
|
}
|
||||||
|
|
||||||
|
switch ($this->responseType) {
|
||||||
|
case 'json':
|
||||||
|
$result = json_decode($response, true);
|
||||||
|
|
||||||
|
if (JSON_ERROR_NONE !== json_last_error()) {
|
||||||
|
$result = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
case 'string':
|
||||||
|
parse_str($response, $result);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isset($result['error']) && ! empty($result['error'])) {
|
||||||
|
// @codeCoverageIgnoreStart
|
||||||
|
throw new IDPException($result);
|
||||||
|
// @codeCoverageIgnoreEnd
|
||||||
|
}
|
||||||
|
|
||||||
|
$result = $this->prepareAccessTokenResult($result);
|
||||||
|
|
||||||
|
$accessToken = $grant->handleResponse($result);
|
||||||
|
|
||||||
|
// Add email from response
|
||||||
|
if (!empty($result['email'])) {
|
||||||
|
$accessToken->email = $result['email'];
|
||||||
|
}
|
||||||
|
return $accessToken;
|
||||||
|
}
|
||||||
|
|
||||||
public function urlUserDetails(AccessToken $token)
|
public function urlUserDetails(AccessToken $token)
|
||||||
{
|
{
|
||||||
$fields = ['nickname',
|
$fields = ['email',
|
||||||
|
'nickname',
|
||||||
'screen_name',
|
'screen_name',
|
||||||
'sex',
|
'sex',
|
||||||
'bdate',
|
'bdate',
|
||||||
@@ -61,7 +145,7 @@ class Vkontakte extends AbstractProvider
|
|||||||
|
|
||||||
$user = new User();
|
$user = new User();
|
||||||
|
|
||||||
$email = (isset($response->email)) ? $response->email : null;
|
$email = (isset($token->email)) ? $token->email : null;
|
||||||
$location = (isset($response->country)) ? $response->country : null;
|
$location = (isset($response->country)) ? $response->country : null;
|
||||||
$description = (isset($response->status)) ? $response->status : null;
|
$description = (isset($response->status)) ? $response->status : null;
|
||||||
|
|
||||||
@@ -89,9 +173,7 @@ class Vkontakte extends AbstractProvider
|
|||||||
|
|
||||||
public function userEmail($response, AccessToken $token)
|
public function userEmail($response, AccessToken $token)
|
||||||
{
|
{
|
||||||
$response = $response->response[0];
|
return (isset($token->email)) ? $token->email : null;
|
||||||
|
|
||||||
return isset($response->email) && $response->email ? $response->email : null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function userScreenName($response, AccessToken $token)
|
public function userScreenName($response, AccessToken $token)
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ class VkontakteTest extends \PHPUnit_Framework_TestCase
|
|||||||
public function testGetAccessToken()
|
public function testGetAccessToken()
|
||||||
{
|
{
|
||||||
$response = m::mock('Guzzle\Http\Message\Response');
|
$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}');
|
$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 = m::mock('Guzzle\Service\Client');
|
||||||
$client->shouldReceive('setBaseUrl')->times(1);
|
$client->shouldReceive('setBaseUrl')->times(1);
|
||||||
@@ -63,6 +63,7 @@ class VkontakteTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertGreaterThanOrEqual(time(), $token->expires);
|
$this->assertGreaterThanOrEqual(time(), $token->expires);
|
||||||
$this->assertEquals('mock_refresh_token', $token->refreshToken);
|
$this->assertEquals('mock_refresh_token', $token->refreshToken);
|
||||||
$this->assertEquals('1', $token->uid);
|
$this->assertEquals('1', $token->uid);
|
||||||
|
$this->assertEquals('mock_email', $token->email);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testScopes()
|
public function testScopes()
|
||||||
@@ -73,10 +74,10 @@ class VkontakteTest extends \PHPUnit_Framework_TestCase
|
|||||||
public function testUserData()
|
public function testUserData()
|
||||||
{
|
{
|
||||||
$postResponse = m::mock('Guzzle\Http\Message\Response');
|
$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}');
|
$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 = 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"}]}');
|
$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 = m::mock('Guzzle\Service\Client');
|
||||||
$client->shouldReceive('setBaseUrl')->times(5);
|
$client->shouldReceive('setBaseUrl')->times(5);
|
||||||
|
|||||||
Reference in New Issue
Block a user