|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\AuthClient\Client; |
|
6
|
|
|
|
|
7
|
|
|
use Psr\Http\Message\RequestInterface; |
|
8
|
|
|
use RuntimeException; |
|
9
|
|
|
use Yiisoft\Json\Json; |
|
10
|
|
|
use Yiisoft\Yii\AuthClient\OAuth2; |
|
11
|
|
|
use Yiisoft\Yii\AuthClient\OAuthToken; |
|
12
|
|
|
use Yiisoft\Yii\AuthClient\RequestUtil; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* VKontakte allows authentication via VKontakte OAuth. |
|
16
|
|
|
* |
|
17
|
|
|
* In order to use VKontakte OAuth you must register your application at <https://vk.com/editapp?act=create>. |
|
18
|
|
|
* |
|
19
|
|
|
* @link https://vk.com/editapp?act=create |
|
20
|
|
|
* @link https://vk.com/developers.php?oid=-1&p=users.get |
|
21
|
|
|
*/ |
|
22
|
|
|
final class VKontakte extends OAuth2 |
|
23
|
|
|
{ |
|
24
|
|
|
protected string $authUrl = 'https://oauth.vk.com/authorize'; |
|
25
|
|
|
protected string $tokenUrl = 'https://oauth.vk.com/access_token'; |
|
26
|
|
|
protected string $endpoint = 'https://api.vk.com/method'; |
|
27
|
|
|
/** |
|
28
|
|
|
* @var array list of attribute names, which should be requested from API to initialize user attributes. |
|
29
|
|
|
*/ |
|
30
|
|
|
private array $attributeNames = [ |
|
31
|
|
|
'uid', |
|
32
|
|
|
'first_name', |
|
33
|
|
|
'last_name', |
|
34
|
|
|
'nickname', |
|
35
|
|
|
'screen_name', |
|
36
|
|
|
'sex', |
|
37
|
|
|
'bdate', |
|
38
|
|
|
'city', |
|
39
|
|
|
'country', |
|
40
|
|
|
'timezone', |
|
41
|
|
|
'photo', |
|
42
|
|
|
]; |
|
43
|
|
|
/** |
|
44
|
|
|
* @var string the API version to send in the API request. |
|
45
|
|
|
* |
|
46
|
|
|
* @see https://vk.com/dev/versions |
|
47
|
|
|
*/ |
|
48
|
|
|
private string $apiVersion = '3.0'; |
|
49
|
|
|
|
|
50
|
|
|
public function applyAccessTokenToRequest(RequestInterface $request, OAuthToken $accessToken): RequestInterface |
|
51
|
|
|
{ |
|
52
|
|
|
return RequestUtil::addParams( |
|
53
|
|
|
$request, |
|
54
|
|
|
[ |
|
55
|
|
|
'v' => $this->apiVersion, |
|
56
|
|
|
'uids' => $accessToken->getParam('user_id'), |
|
57
|
|
|
'access_token' => $accessToken->getToken(), |
|
58
|
|
|
] |
|
59
|
|
|
); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @return string service name. |
|
64
|
|
|
*/ |
|
65
|
|
|
public function getName(): string |
|
66
|
|
|
{ |
|
67
|
|
|
return 'vkontakte'; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @return string service title. |
|
72
|
|
|
*/ |
|
73
|
|
|
public function getTitle(): string |
|
74
|
|
|
{ |
|
75
|
|
|
return 'VKontakte'; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
protected function initUserAttributes(): array |
|
79
|
|
|
{ |
|
80
|
|
|
$response = $this->api( |
|
81
|
|
|
'users.get.json', |
|
82
|
|
|
'GET', |
|
83
|
|
|
[ |
|
84
|
|
|
'fields' => implode(',', $this->attributeNames), |
|
85
|
|
|
] |
|
86
|
|
|
); |
|
87
|
|
|
|
|
88
|
|
|
if (empty($response['response'])) { |
|
89
|
|
|
throw new RuntimeException( |
|
90
|
|
|
'Unable to init user attributes due to unexpected response: ' . Json::encode($response) |
|
91
|
|
|
); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$attributes = array_shift($response['response']); |
|
95
|
|
|
|
|
96
|
|
|
$accessToken = $this->getAccessToken(); |
|
97
|
|
|
if (is_object($accessToken)) { |
|
98
|
|
|
$accessTokenParams = $accessToken->getParams(); |
|
99
|
|
|
unset($accessTokenParams['access_token'], $accessTokenParams['expires_in']); |
|
100
|
|
|
$attributes = array_merge($accessTokenParams, $attributes); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return $attributes; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
protected function defaultNormalizeUserAttributeMap(): array |
|
107
|
|
|
{ |
|
108
|
|
|
return [ |
|
109
|
|
|
'id' => 'uid', |
|
110
|
|
|
]; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|