forked from SocialiteProviders/Providers
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProvider.php
More file actions
122 lines (97 loc) · 3.2 KB
/
Provider.php
File metadata and controls
122 lines (97 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
namespace SocialiteProviders\VKontakte;
use GuzzleHttp\RequestOptions;
use Illuminate\Support\Arr;
use Laravel\Socialite\Two\InvalidStateException;
use RuntimeException;
use SocialiteProviders\Manager\OAuth2\AbstractProvider;
use SocialiteProviders\Manager\OAuth2\User;
class Provider extends AbstractProvider
{
protected $fields = ['id', 'email', 'first_name', 'last_name', 'screen_name', 'photo_200'];
public const IDENTIFIER = 'VKONTAKTE';
protected $scopes = ['email'];
/**
* Last API version.
*/
public const VERSION = '5.199';
protected function getAuthUrl($state): string
{
return $this->buildAuthUrlFromBase('https://oauth.vk.ru/authorize', $state);
}
protected function getTokenUrl(): string
{
return 'https://oauth.vk.ru/access_token';
}
/**
* {@inheritdoc}
*/
protected function getUserByToken($token)
{
$formToken = [];
if (is_array($token)) {
$formToken['email'] = $token['email'] ?? null;
$token = $token['access_token'];
}
$response = $this->getHttpClient()->get('https://api.vk.ru/method/users.get', [
RequestOptions::QUERY => [
'access_token' => $token,
'fields' => implode(',', $this->fields),
'lang' => $this->getConfig('lang', 'en'),
'v' => self::VERSION,
],
]);
$response = json_decode((string) $response->getBody(), true);
if (! is_array($response) || ! isset($response['response'][0])) {
throw new RuntimeException(sprintf(
'Invalid JSON response from VK: %s', $response->getBody()
));
}
return array_merge($formToken, $response['response'][0]);
}
/**
* {@inheritdoc}
*/
public function user()
{
if ($this->hasInvalidState()) {
throw new InvalidStateException;
}
$response = $this->getAccessTokenResponse($this->getCode());
$user = $this->mapUserToObject($this->getUserByToken($response));
$this->credentialsResponseBody = $response;
if ($user instanceof User) {
$user->setAccessTokenResponseBody($this->credentialsResponseBody);
}
return $user->setToken($this->parseAccessToken($response))
->setExpiresIn($this->parseExpiresIn($response));
}
/**
* {@inheritdoc}
*/
protected function mapUserToObject(array $user)
{
return (new User)->setRaw($user)->map([
'id' => Arr::get($user, 'id'),
'nickname' => Arr::get($user, 'screen_name'),
'name' => trim(Arr::get($user, 'first_name').' '.Arr::get($user, 'last_name')),
'email' => Arr::get($user, 'email'),
'avatar' => Arr::get($user, 'photo_200'),
]);
}
/**
* Set the user fields to request from Vkontakte.
*
* @param array $fields
* @return $this
*/
public function fields(array $fields)
{
$this->fields = $fields;
return $this;
}
public static function additionalConfigKeys(): array
{
return ['lang'];
}
}