1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* SocialConnect project |
4
|
|
|
* @author: Patsura Dmitry https://github.com/ovr <[email protected]> |
5
|
|
|
*/ |
6
|
|
|
declare(strict_types=1); |
7
|
|
|
|
8
|
|
|
namespace SocialConnect\OAuth2; |
9
|
|
|
|
10
|
|
|
use Psr\Http\Message\RequestInterface; |
11
|
|
|
use SocialConnect\OAuth2\Exception\InvalidState; |
12
|
|
|
use SocialConnect\OAuth2\Exception\Unauthorized; |
13
|
|
|
use SocialConnect\OAuth2\Exception\UnknownAuthorization; |
14
|
|
|
use SocialConnect\OAuth2\Exception\UnknownState; |
15
|
|
|
use SocialConnect\Provider\AbstractBaseProvider; |
16
|
|
|
use SocialConnect\Provider\Exception\InvalidAccessToken; |
17
|
|
|
use SocialConnect\Provider\Exception\InvalidResponse; |
18
|
|
|
|
19
|
|
|
abstract class AbstractProvider extends AbstractBaseProvider |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* HTTP method for access token request |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $requestHttpMethod = 'POST'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @return string |
30
|
|
|
*/ |
31
|
|
|
abstract public function getAuthorizeUri(); |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @return string |
35
|
|
|
*/ |
36
|
|
|
abstract public function getRequestTokenUri(); |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritdoc} |
40
|
|
|
*/ |
41
|
23 |
|
public function getAuthUrlParameters(): array |
42
|
|
|
{ |
43
|
23 |
|
$parameters = parent::getAuthUrlParameters(); |
44
|
|
|
|
45
|
|
|
// special parameters only required for OAuth2 |
46
|
23 |
|
$parameters['client_id'] = $this->consumer->getKey(); |
47
|
23 |
|
$parameters['redirect_uri'] = $this->getRedirectUrl(); |
48
|
23 |
|
$parameters['response_type'] = 'code'; |
49
|
|
|
|
50
|
23 |
|
return $parameters; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
23 |
|
public function makeAuthUrl(): string |
57
|
|
|
{ |
58
|
23 |
|
$urlParameters = $this->getAuthUrlParameters(); |
59
|
|
|
|
60
|
23 |
|
if (!$this->getBoolOption('stateless', false)) { |
61
|
23 |
|
$this->session->set( |
62
|
23 |
|
'oauth2_state', |
63
|
23 |
|
$urlParameters['state'] = $this->generateState() |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
|
67
|
23 |
|
if (count($this->scope) > 0) { |
68
|
23 |
|
$urlParameters['scope'] = $this->getScopeInline(); |
69
|
|
|
} |
70
|
|
|
|
71
|
23 |
|
return $this->getAuthorizeUri() . '?' . http_build_query($urlParameters); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Parse access token from response's $body |
76
|
|
|
* |
77
|
|
|
* @param string $body |
78
|
|
|
* @return AccessToken |
79
|
|
|
* @throws InvalidAccessToken |
80
|
|
|
*/ |
81
|
82 |
|
public function parseToken(string $body) |
82
|
|
|
{ |
83
|
82 |
|
if (empty($body)) { |
84
|
20 |
|
throw new InvalidAccessToken('Provider response with empty body'); |
85
|
|
|
} |
86
|
|
|
|
87
|
62 |
|
$token = json_decode($body, true); |
88
|
62 |
|
if ($token) { |
89
|
21 |
|
if (!is_array($token)) { |
90
|
|
|
throw new InvalidAccessToken('Response must be array'); |
91
|
|
|
} |
92
|
|
|
|
93
|
21 |
|
return new AccessToken($token); |
94
|
|
|
} |
95
|
|
|
|
96
|
41 |
|
throw new InvalidAccessToken('Server response with not valid/empty JSON'); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param string $code |
101
|
|
|
* @return RequestInterface |
102
|
|
|
*/ |
103
|
22 |
|
protected function makeAccessTokenRequest(string $code): RequestInterface |
104
|
|
|
{ |
105
|
|
|
$parameters = [ |
106
|
22 |
|
'client_id' => $this->consumer->getKey(), |
107
|
22 |
|
'client_secret' => $this->consumer->getSecret(), |
108
|
22 |
|
'code' => $code, |
109
|
22 |
|
'grant_type' => 'authorization_code', |
110
|
22 |
|
'redirect_uri' => $this->getRedirectUrl() |
111
|
|
|
]; |
112
|
|
|
|
113
|
22 |
|
return $this->httpStack->createRequest($this->requestHttpMethod, $this->getRequestTokenUri()) |
114
|
22 |
|
->withHeader('Content-Type', 'application/x-www-form-urlencoded') |
115
|
22 |
|
->withBody($this->httpStack->createStream(http_build_query($parameters, '', '&'))) |
116
|
|
|
; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param string $refreshToken |
121
|
|
|
* @return RequestInterface |
122
|
|
|
*/ |
123
|
|
|
protected function makeRefreshAccessTokenRequest(string $refreshToken): RequestInterface |
124
|
|
|
{ |
125
|
|
|
$parameters = [ |
126
|
24 |
|
'refresh_token' => $refreshToken, |
127
|
|
|
'client_id' => $this->consumer->getKey(), |
128
|
24 |
|
'client_secret' => $this->consumer->getSecret(), |
129
|
24 |
|
'grant_type' => 'refresh_token', |
130
|
|
|
]; |
131
|
|
|
|
132
|
|
|
return $this->httpStack->createRequest($this->requestHttpMethod, $this->getRequestTokenUri()) |
133
|
|
|
->withHeader('Content-Type', 'application/x-www-form-urlencoded') |
134
|
|
|
->withBody($this->httpStack->createStream(http_build_query($parameters, '', '&'))) |
135
|
|
|
; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param string $code |
140
|
|
|
* @return AccessToken |
141
|
|
|
* @throws InvalidAccessToken |
142
|
|
|
* @throws InvalidResponse |
143
|
|
|
* @throws \Psr\Http\Client\ClientExceptionInterface |
144
|
|
|
*/ |
145
|
|
|
public function getAccessToken(string $code): AccessToken |
146
|
23 |
|
{ |
147
|
|
|
$response = $this->executeRequest( |
148
|
23 |
|
$this->makeAccessTokenRequest($code) |
149
|
23 |
|
); |
150
|
|
|
|
151
|
|
|
return $this->parseToken($response->getBody()->getContents()); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param string $refreshToken |
157
|
|
|
* |
158
|
|
|
* @return AccessToken |
159
|
|
|
* @throws InvalidAccessToken |
160
|
|
|
* @throws InvalidResponse |
161
|
|
|
* @throws \Psr\Http\Client\ClientExceptionInterface |
162
|
|
|
*/ |
163
|
|
|
public function refreshAccessToken(string $refreshToken): AccessToken |
164
|
|
|
{ |
165
|
|
|
$response = $this->executeRequest( |
166
|
|
|
$this->makeRefreshAccessTokenRequest($refreshToken) |
167
|
|
|
); |
168
|
|
|
|
169
|
|
|
return $this->parseToken($response->getBody()->getContents()); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @param array $parameters |
174
|
|
|
* @return AccessToken |
175
|
|
|
* @throws InvalidAccessToken |
176
|
|
|
* @throws InvalidResponse |
177
|
|
|
* @throws InvalidState |
178
|
|
|
* @throws Unauthorized |
179
|
|
|
* @throws UnknownAuthorization |
180
|
|
|
* @throws UnknownState |
181
|
|
|
* @throws \Psr\Http\Client\ClientExceptionInterface |
182
|
|
|
*/ |
183
|
|
|
public function getAccessTokenByRequestParameters(array $parameters) |
184
|
|
|
{ |
185
|
|
|
if (isset($parameters['error']) && $parameters['error'] === 'access_denied') { |
186
|
|
|
throw new Unauthorized(); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
if (!isset($parameters['code'])) { |
190
|
|
|
throw new Unauthorized('Unknown code'); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
if (!$this->getBoolOption('stateless', false)) { |
194
|
|
|
$state = $this->session->get('oauth2_state'); |
195
|
|
|
if (!$state) { |
196
|
|
|
throw new UnknownAuthorization(); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
if (!isset($parameters['state'])) { |
200
|
|
|
throw new UnknownState(); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
if ($state !== $parameters['state']) { |
204
|
|
|
throw new InvalidState(); |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
return $this->getAccessToken($parameters['code']); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* {@inheritDoc} |
213
|
|
|
*/ |
214
|
|
|
public function createAccessToken(array $information) |
215
|
|
|
{ |
216
|
|
|
return new AccessToken($information); |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|