1
|
|
|
<?php
|
2
|
|
|
namespace Mezon\SocialNetwork;
|
3
|
|
|
|
4
|
|
|
/**
|
5
|
|
|
* Class SocialNetworkAuth
|
6
|
|
|
*
|
7
|
|
|
* @package SocialNetwork
|
8
|
|
|
* @subpackage SocialNetworkAuth
|
9
|
|
|
* @author Dodonov A.A.
|
10
|
|
|
* @version v.1.0 (2019/08/17)
|
11
|
|
|
* @copyright Copyright (c) 2019, aeon.org
|
12
|
|
|
*/
|
13
|
|
|
|
14
|
|
|
/**
|
15
|
|
|
* Class provides integration with social networks authorization APIs.
|
16
|
|
|
*
|
17
|
|
|
* @author Dodonov A.A.
|
18
|
|
|
*/
|
19
|
|
|
abstract class BaseAuth
|
20
|
|
|
{
|
21
|
|
|
|
22
|
|
|
/**
|
23
|
|
|
* Authorization settings.
|
24
|
|
|
*
|
25
|
|
|
* @var array
|
26
|
|
|
*/
|
27
|
|
|
protected $settings = [];
|
28
|
|
|
|
29
|
|
|
/**
|
30
|
|
|
* Fetched user's info.
|
31
|
|
|
*
|
32
|
|
|
* @var array
|
33
|
|
|
*/
|
34
|
|
|
protected $userInfo = [];
|
35
|
|
|
|
36
|
|
|
/**
|
37
|
|
|
* Constructor.
|
38
|
|
|
*
|
39
|
|
|
* @param array $settings
|
40
|
|
|
* - Connection settings.
|
41
|
|
|
*/
|
42
|
|
|
public function __construct(array $settings)
|
43
|
|
|
{
|
44
|
|
|
if (isset($settings['client_id'], $settings['client_secret'], $settings['redirect_uri'])) {
|
45
|
|
|
$this->settings = $settings;
|
46
|
|
|
}
|
47
|
|
|
}
|
48
|
|
|
|
49
|
|
|
/**
|
50
|
|
|
* Method returns request result
|
51
|
|
|
*
|
52
|
|
|
* @param string $URL
|
53
|
|
|
* @return string Request result
|
54
|
|
|
*/
|
55
|
|
|
protected function getRequest(string $url): string
|
56
|
|
|
{
|
57
|
|
|
// @codeCoverageIgnoreStart
|
58
|
|
|
return file_get_contents($url);
|
59
|
|
|
// @codeCoverageIgnoreEnd
|
60
|
|
|
}
|
61
|
|
|
|
62
|
|
|
/**
|
63
|
|
|
* Metthod tryes to authorize user.
|
64
|
|
|
*
|
65
|
|
|
* @param string $Code
|
66
|
|
|
* - Code.
|
67
|
|
|
* @return boolean True on success. False otherwise.
|
68
|
|
|
*/
|
69
|
|
|
public function auth(string $code): bool
|
70
|
|
|
{
|
71
|
|
|
if ($code && ! empty($this->settings)) {
|
72
|
|
|
|
73
|
|
|
$params = $this->getTokenParams($code);
|
74
|
|
|
|
75
|
|
|
$token = $this->requestToken($params);
|
76
|
|
|
|
77
|
|
|
if (isset($token['access_token'])) {
|
78
|
|
|
$query = http_build_query(
|
79
|
|
|
[
|
80
|
|
|
|
81
|
|
|
'access_token' => $token['access_token'],
|
82
|
|
|
'fields' => $this->getDesiredFields()
|
83
|
|
|
]);
|
84
|
|
|
|
85
|
|
|
$query = urldecode($query);
|
86
|
|
|
|
87
|
|
|
$this->userInfo = json_decode(
|
88
|
|
|
$this->getRequest($this->getUserInfoUri($token['access_token']) . $query),
|
89
|
|
|
true);
|
90
|
|
|
|
91
|
|
|
$this->userInfo = $this->dispatchUserInfo($this->userInfo);
|
92
|
|
|
|
93
|
|
|
if (isset($this->userInfo['id'])) {
|
94
|
|
|
return true;
|
95
|
|
|
}
|
96
|
|
|
}
|
97
|
|
|
}
|
98
|
|
|
|
99
|
|
|
return false;
|
100
|
|
|
}
|
101
|
|
|
|
102
|
|
|
/**
|
103
|
|
|
* Method returns authorization URL.
|
104
|
|
|
*
|
105
|
|
|
* @return string Authorization url.
|
106
|
|
|
*/
|
107
|
|
|
public function getLink(): string
|
108
|
|
|
{
|
109
|
|
|
if (! empty($this->settings)) {
|
110
|
|
|
$query = http_build_query(
|
111
|
|
|
[
|
112
|
|
|
|
113
|
|
|
'client_id' => $this->settings['client_id'],
|
114
|
|
|
'redirect_uri' => $this->settings['redirect_uri'],
|
115
|
|
|
'response_type' => 'code'
|
116
|
|
|
]);
|
117
|
|
|
|
118
|
|
|
$query = urldecode($query);
|
119
|
|
|
|
120
|
|
|
return $this->getOauthUri() . $query;
|
121
|
|
|
}
|
122
|
|
|
|
123
|
|
|
throw (new \Exception('Social network\'s authorization URL was not found.'));
|
124
|
|
|
}
|
125
|
|
|
|
126
|
|
|
/**
|
127
|
|
|
* Method returns URL wich generates tokens.
|
128
|
|
|
*
|
129
|
|
|
* @return string URL
|
130
|
|
|
*/
|
131
|
|
|
abstract public function getOauthUri(): string;
|
132
|
|
|
|
133
|
|
|
/**
|
134
|
|
|
* Method return URL wich provides user's info
|
135
|
|
|
*
|
136
|
|
|
* @param string $token
|
137
|
|
|
* Token
|
138
|
|
|
* @return string URL
|
139
|
|
|
*/
|
140
|
|
|
abstract public function getUserInfoUri(string $token = ''): string;
|
141
|
|
|
|
142
|
|
|
/**
|
143
|
|
|
* Method returns token URL
|
144
|
|
|
*
|
145
|
|
|
* @return string URL
|
146
|
|
|
*/
|
147
|
|
|
abstract public function getTokenUri(): string;
|
148
|
|
|
|
149
|
|
|
/**
|
150
|
|
|
* Method returns a list of desired fields
|
151
|
|
|
*
|
152
|
|
|
* @return string Comma separated of the desired fields
|
153
|
|
|
*/
|
154
|
|
|
public function getDesiredFields(): string
|
155
|
|
|
{
|
156
|
|
|
return 'desired,fields';
|
157
|
|
|
}
|
158
|
|
|
|
159
|
|
|
/**
|
160
|
|
|
* Method dispatches user info
|
161
|
|
|
*
|
162
|
|
|
* @param array $userInfo
|
163
|
|
|
* User info got from social network
|
164
|
|
|
* @return array Dispatched user info. Must be as array with keys id, first_name, last_name, email, picture
|
165
|
|
|
*/
|
166
|
|
|
public function dispatchUserInfo(array $userInfo): array
|
167
|
|
|
{
|
168
|
|
|
$userInfo['picture'] = $userInfo['picture']['data']['url'];
|
169
|
|
|
|
170
|
|
|
return $userInfo;
|
171
|
|
|
}
|
172
|
|
|
|
173
|
|
|
/**
|
174
|
|
|
* Method returns params for getting token
|
175
|
|
|
*
|
176
|
|
|
* @param string $code
|
177
|
|
|
* Access code
|
178
|
|
|
* @return array Params
|
179
|
|
|
*/
|
180
|
|
|
public function getTokenParams(string $code): array
|
181
|
|
|
{
|
182
|
|
|
return [
|
183
|
|
|
'client_id' => $this->settings['client_id'],
|
184
|
|
|
'redirect_uri' => $this->settings['redirect_uri'],
|
185
|
|
|
'client_secret' => $this->settings['client_secret'],
|
186
|
|
|
'code' => $code
|
187
|
|
|
];
|
188
|
|
|
}
|
189
|
|
|
|
190
|
|
|
/**
|
191
|
|
|
* Method requests token from server
|
192
|
|
|
*
|
193
|
|
|
* @param array $params
|
194
|
|
|
* Request params
|
195
|
|
|
* @return array Token data
|
196
|
|
|
*/
|
197
|
|
|
public function requestToken(array $params): array
|
198
|
|
|
{
|
199
|
|
|
$query = urldecode(http_build_query($params));
|
200
|
|
|
|
201
|
|
|
return json_decode(file_get_contents($this->getTokenUri() . $query), true);
|
202
|
|
|
}
|
203
|
|
|
|
204
|
|
|
/**
|
205
|
|
|
* Method returns settings
|
206
|
|
|
*
|
207
|
|
|
* @return array settigs
|
208
|
|
|
*/
|
209
|
|
|
public function getSettings(): array
|
210
|
|
|
{
|
211
|
|
|
return $this->settings;
|
212
|
|
|
}
|
213
|
|
|
}
|
214
|
|
|
|