-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathProvider.php
More file actions
111 lines (94 loc) · 2.57 KB
/
Provider.php
File metadata and controls
111 lines (94 loc) · 2.57 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
<?php
namespace SocialiteProviders\VKID;
use RuntimeException;
use Illuminate\Support\Arr;
use GuzzleHttp\RequestOptions;
use SocialiteProviders\Manager\OAuth2\User;
use Laravel\Socialite\Two\InvalidStateException;
use SocialiteProviders\Manager\OAuth2\AbstractProvider;
class Provider extends AbstractProvider
{
/**
* Unique Provider Identifier.
*/
public const IDENTIFIER = 'VKID';
/**
* {@inheritdoc}
*/
protected $stateless = false;
/**
* {@inheritdoc}
*/
protected $usesPKCE = true;
/**
* {@inheritdoc}
*/
protected $scopes = ['email'];
/**
* {@inheritdoc}
*/
protected function getAuthUrl($state)
{
return $this->buildAuthUrlFromBase('https://id.vk.ru/authorize', $state);
}
/**
* {@inheritdoc}
*/
protected function getTokenUrl()
{
return 'https://id.vk.ru/oauth2/auth';
}
/**
* {@inheritdoc}
*/
protected function getUserByToken($token)
{
$response = $this->getHttpClient()->post('https://id.vk.ru/oauth2/user_info', [
RequestOptions::HEADERS => ['Accept' => 'application/json'],
RequestOptions::FORM_PARAMS => [
'access_token' => is_array($token) ? $token['access_token'] : $token,
'client_id' => $this->clientId,
],
]);
$contents = (string) $response->getBody();
$response = json_decode($contents, true);
if (!is_array($response) || !isset($response['user'])) {
throw new RuntimeException(sprintf(
'Invalid JSON response from VK: %s',
$contents
));
}
return $response['user'];
}
/**
* {@inheritdoc}
*/
protected function mapUserToObject(array $user)
{
return (new User())->setRaw($user)->map([
'id' => Arr::get($user, 'user_id'),
'name' => trim(Arr::get($user, 'first_name') . ' ' . Arr::get($user, 'last_name')),
'email' => Arr::get($user, 'email'),
'avatar' => Arr::get($user, 'avatar'),
]);
}
/**
* {@inheritdoc}
*/
protected function getTokenFields($code)
{
return array_merge(parent::getTokenFields($code), [
'grant_type' => 'authorization_code',
'device_id' => $this->getDeviceId(),
]);
}
/**
* Get the device_id from the request.
*
* @return string
*/
protected function getDeviceId()
{
return $this->request->input('device_id');
}
}