1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* SocialConnect project |
4
|
|
|
* @author: Patsura Dmitry https://github.com/ovr <[email protected]> |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace SocialConnect\Auth\Provider; |
8
|
|
|
|
9
|
|
|
use SocialConnect\Auth\AccessTokenInterface; |
10
|
|
|
use SocialConnect\Auth\Provider\Exception\InvalidAccessToken; |
11
|
|
|
use SocialConnect\Auth\Provider\Exception\InvalidResponse; |
12
|
|
|
use SocialConnect\OAuth2\AccessToken; |
13
|
|
|
use SocialConnect\Common\Entity\User; |
14
|
|
|
use SocialConnect\Common\Http\Client\Client; |
15
|
|
|
use SocialConnect\Common\Hydrator\ObjectMap; |
16
|
|
|
|
17
|
|
|
class Vk extends \SocialConnect\OAuth2\AbstractProvider |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* {@inheritdoc} |
21
|
|
|
*/ |
22
|
|
|
protected $requestHttpMethod = Client::GET; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Vk returns email inside AccessToken |
26
|
|
|
* |
27
|
|
|
* @var string|null |
28
|
|
|
*/ |
29
|
|
|
protected $email; |
30
|
|
|
|
31
|
1 |
|
public function getBaseUri() |
32
|
|
|
{ |
33
|
1 |
|
return 'https://api.vk.com/'; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function getAuthorizeUri() |
37
|
|
|
{ |
38
|
|
|
return 'https://oauth.vk.com/authorize'; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function getRequestTokenUri() |
42
|
|
|
{ |
43
|
|
|
return 'https://oauth.vk.com/access_token'; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function getName() |
47
|
|
|
{ |
48
|
|
|
return 'vk'; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
*/ |
54
|
3 |
|
public function parseToken($body) |
55
|
|
|
{ |
56
|
3 |
|
$result = json_decode($body); |
57
|
3 |
|
if (!$result) { |
58
|
2 |
|
throw new InvalidAccessToken; |
59
|
|
|
} |
60
|
|
|
|
61
|
1 |
|
if (!isset($result->access_token) || empty($result->access_token)) { |
62
|
|
|
throw new InvalidAccessToken; |
63
|
|
|
} |
64
|
|
|
|
65
|
1 |
|
if (isset($result->email)) { |
66
|
|
|
$this->email = $result->email; |
67
|
|
|
} |
68
|
|
|
|
69
|
1 |
|
$token = new AccessToken($result->access_token); |
70
|
1 |
|
$token->setUid($result->user_id); |
71
|
|
|
|
72
|
1 |
|
return $token; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritdoc} |
77
|
|
|
*/ |
78
|
1 |
|
public function getIdentity(AccessTokenInterface $accessToken) |
79
|
|
|
{ |
80
|
1 |
|
$response = $this->service->getHttpClient()->request( |
81
|
1 |
|
$this->getBaseUri() . 'method/users.get', |
82
|
|
|
[ |
83
|
1 |
|
'v' => '5.24', |
84
|
1 |
|
'access_token' => $accessToken->getToken(), |
85
|
1 |
|
'fields' => $this->getFieldsInline() |
86
|
1 |
|
] |
87
|
1 |
|
); |
88
|
|
|
|
89
|
1 |
|
if (!$response->isSuccess()) { |
90
|
|
|
throw new InvalidResponse( |
91
|
|
|
'API response with error code', |
92
|
|
|
$response |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
1 |
|
$result = $response->json(); |
97
|
1 |
|
if (!$result) { |
98
|
|
|
throw new InvalidResponse( |
99
|
|
|
'API response is not a valid JSON object', |
100
|
|
|
$response->getBody() |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
|
104
|
1 |
|
$hydrator = new ObjectMap(array( |
105
|
1 |
|
'id' => 'id', |
106
|
1 |
|
'first_name' => 'firstname', |
107
|
1 |
|
'last_name' => 'lastname', |
108
|
1 |
|
'email' => 'email', |
109
|
1 |
|
'bdate' => 'birthday', |
110
|
1 |
|
'nickname' => 'username', |
111
|
1 |
|
'sex' => 'sex', |
112
|
1 |
|
)); |
113
|
|
|
|
114
|
1 |
|
$user = $hydrator->hydrate(new User(), $result->response[0]); |
115
|
|
|
|
116
|
1 |
|
if ($user->sex) { |
117
|
|
|
$user->sex = $user->sex === 1 ? 'female' : 'male'; |
118
|
|
|
} |
119
|
|
|
|
120
|
1 |
|
$user->email = $this->email; |
121
|
|
|
|
122
|
1 |
|
return $user; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|