1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* SocialConnect project |
4
|
|
|
* @author: Patsura Dmitry https://github.com/ovr <[email protected]> |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace SocialConnect\OAuth2\Provider; |
8
|
|
|
|
9
|
|
|
use SocialConnect\Provider\AccessTokenInterface; |
10
|
|
|
use SocialConnect\Provider\Exception\InvalidAccessToken; |
11
|
|
|
use SocialConnect\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
|
5 |
|
public function getBaseUri() |
32
|
|
|
{ |
33
|
5 |
|
return 'https://api.vk.com/'; |
34
|
|
|
} |
35
|
|
|
|
36
|
2 |
|
public function getAuthorizeUri() |
37
|
|
|
{ |
38
|
2 |
|
return 'https://oauth.vk.com/authorize'; |
39
|
|
|
} |
40
|
|
|
|
41
|
3 |
|
public function getRequestTokenUri() |
42
|
|
|
{ |
43
|
3 |
|
return 'https://oauth.vk.com/access_token'; |
44
|
|
|
} |
45
|
|
|
|
46
|
4 |
|
public function getName() |
47
|
|
|
{ |
48
|
4 |
|
return 'vk'; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
*/ |
54
|
3 |
|
public function parseToken($body) |
55
|
|
|
{ |
56
|
3 |
|
$result = json_decode($body, true); |
57
|
3 |
|
if (!$result) { |
58
|
2 |
|
throw new InvalidAccessToken; |
59
|
|
|
} |
60
|
|
|
|
61
|
1 |
|
if (isset($result['email'])) { |
62
|
|
|
$this->email = $result['email']; |
63
|
|
|
} |
64
|
|
|
|
65
|
1 |
|
return new AccessToken($result); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritdoc} |
70
|
|
|
*/ |
71
|
4 |
|
public function getIdentity(AccessTokenInterface $accessToken) |
72
|
|
|
{ |
73
|
4 |
|
$response = $this->httpClient->request( |
74
|
4 |
|
$this->getBaseUri() . 'method/users.get', |
75
|
|
|
[ |
76
|
4 |
|
'v' => '5.24', |
77
|
4 |
|
'access_token' => $accessToken->getToken(), |
78
|
4 |
|
'fields' => $this->getFieldsInline() |
79
|
4 |
|
] |
80
|
4 |
|
); |
81
|
|
|
|
82
|
4 |
|
if (!$response->isSuccess()) { |
83
|
1 |
|
throw new InvalidResponse( |
84
|
1 |
|
'API response with error code', |
85
|
|
|
$response |
86
|
1 |
|
); |
87
|
|
|
} |
88
|
|
|
|
89
|
3 |
|
$result = $response->json(); |
90
|
3 |
|
if (!$result) { |
91
|
2 |
|
throw new InvalidResponse( |
92
|
2 |
|
'API response is not a valid JSON object', |
93
|
2 |
|
$response->getBody() |
94
|
2 |
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
1 |
|
$hydrator = new ObjectMap( |
98
|
|
|
[ |
99
|
1 |
|
'id' => 'id', |
100
|
1 |
|
'first_name' => 'firstname', |
101
|
1 |
|
'last_name' => 'lastname', |
102
|
1 |
|
'email' => 'email', |
103
|
1 |
|
'bdate' => 'birthday', |
104
|
1 |
|
'nickname' => 'username', |
105
|
1 |
|
'sex' => 'sex', |
106
|
|
|
] |
107
|
1 |
|
); |
108
|
|
|
|
109
|
|
|
/** @var User $user */ |
110
|
1 |
|
$user = $hydrator->hydrate(new User(), $result->response[0]); |
111
|
|
|
|
112
|
1 |
|
if ($user->sex) { |
|
|
|
|
113
|
1 |
|
$user->sex = $user->sex === 1 ? 'female' : 'male'; |
|
|
|
|
114
|
1 |
|
} |
115
|
|
|
|
116
|
1 |
|
$user->email = $this->email; |
117
|
1 |
|
$user->emailVerified = true; |
118
|
|
|
|
119
|
1 |
|
return $user; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: