-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOAuthProvider.php
534 lines (439 loc) · 15.6 KB
/
OAuthProvider.php
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
<?php
/**
* Class OAuthProvider
*
* @created 09.07.2017
* @author Smiley <smiley@chillerlan.net>
* @copyright 2017 Smiley
* @license MIT
*
* @filesource
*/
declare(strict_types=1);
namespace chillerlan\OAuth\Core;
use chillerlan\HTTP\Utils\{MessageUtil, QueryUtil};
use chillerlan\OAuth\OAuthOptions;
use chillerlan\OAuth\Providers\ProviderException;
use chillerlan\OAuth\Storage\{MemoryStorage, OAuthStorageInterface};
use chillerlan\Settings\SettingsContainerInterface;
use chillerlan\Utilities\Str;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\{
RequestFactoryInterface, RequestInterface, ResponseInterface,
StreamFactoryInterface, StreamInterface, UriFactoryInterface
};
use Psr\Log\{LoggerInterface, NullLogger};
use ReflectionClass;
use function array_merge, array_shift, explode, implode, in_array, is_array, is_string, ltrim,
random_bytes, rtrim, sodium_bin2hex, sprintf, str_contains, str_starts_with, strip_tags, strtolower;
use const PHP_QUERY_RFC1738;
/**
* Implements an abstract OAuth provider with all methods required by the OAuthInterface.
* It also implements a magic getter that allows to access the properties listed below.
*/
abstract class OAuthProvider implements OAuthInterface{
/**
* The PSR-18 HTTP client
*/
protected ClientInterface $http;
/**
* A PSR-17 request factory
*/
protected RequestFactoryInterface $requestFactory;
/**
* A PSR-17 stream factory
*/
protected StreamFactoryInterface $streamFactory;
/**
* A PSR-17 URI factory
*/
protected UriFactoryInterface $uriFactory;
/**
* A PSR-3 logger
*/
protected LoggerInterface $logger;
/**
* The options instance
*/
protected OAuthOptions|SettingsContainerInterface $options;
/**
* A storage instance
*/
protected OAuthStorageInterface $storage;
/**
* The authorization URL
*/
protected string $authorizationURL = '';
/**
* The access token exchange URL
*/
protected string $accessTokenURL = '';
/**
* An optional URL for application side token revocation
*
* @see \chillerlan\OAuth\Core\TokenInvalidate
* @see \chillerlan\OAuth\Core\TokenInvalidateTrait::invalidateAccessToken()
*/
protected string $revokeURL = '';
/**
* The API base URL
*/
protected string $apiURL = '';
/**
* The name of the provider/class
*/
protected string $name = '';
/**
* An optional link to the provider's API docs
*/
protected string|null $apiDocs = null;
/**
* An optional URL to the provider's credential registration/application page
*/
protected string|null $applicationURL = null;
/**
* An optional link to the page where a user can revoke access tokens
*/
protected string|null $userRevokeURL = null;
/**
* OAuthProvider constructor.
*/
final public function __construct(
OAuthOptions|SettingsContainerInterface $options,
ClientInterface $http,
RequestFactoryInterface $requestFactory,
StreamFactoryInterface $streamFactory,
UriFactoryInterface $uriFactory,
OAuthStorageInterface $storage = new MemoryStorage,
LoggerInterface $logger = new NullLogger,
){
$this->options = $options;
$this->http = $http;
$this->requestFactory = $requestFactory;
$this->streamFactory = $streamFactory;
$this->uriFactory = $uriFactory;
$this->storage = $storage;
$this->logger = $logger;
$this->name = (new ReflectionClass($this))->getShortName();
$this->construct();
}
/**
* A replacement constructor that you can call in extended classes,
* so that you don't have to implement the monstrous original `__construct()`
*/
protected function construct():void{
// noop
}
/** @codeCoverageIgnore */
final public function getName():string{
return $this->name;
}
/** @codeCoverageIgnore */
final public function getApiDocURL():string|null{
return $this->apiDocs;
}
/** @codeCoverageIgnore */
final public function getApplicationURL():string|null{
return $this->applicationURL;
}
/** @codeCoverageIgnore */
final public function getUserRevokeURL():string|null{
return $this->userRevokeURL;
}
/** @codeCoverageIgnore */
final public function setStorage(OAuthStorageInterface $storage):static{
$this->storage = $storage;
return $this;
}
/** @codeCoverageIgnore */
final public function getStorage():OAuthStorageInterface{
return $this->storage;
}
/** @codeCoverageIgnore */
final public function setLogger(LoggerInterface $logger):static{
$this->logger = $logger;
return $this;
}
/** @codeCoverageIgnore */
final public function setRequestFactory(RequestFactoryInterface $requestFactory):static{
$this->requestFactory = $requestFactory;
return $this;
}
/** @codeCoverageIgnore */
final public function setStreamFactory(StreamFactoryInterface $streamFactory):static{
$this->streamFactory = $streamFactory;
return $this;
}
/** @codeCoverageIgnore */
final public function setUriFactory(UriFactoryInterface $uriFactory):static{
$this->uriFactory = $uriFactory;
return $this;
}
/** @codeCoverageIgnore */
final public function storeAccessToken(AccessToken $token):static{
$this->storage->storeAccessToken($token, $this->name);
return $this;
}
/** @codeCoverageIgnore */
final public function getAccessTokenFromStorage():AccessToken{
return $this->storage->getAccessToken($this->name);
}
/**
* Creates an access token with the provider set to $this->name
*
* @codeCoverageIgnore
*/
final protected function createAccessToken():AccessToken{
return new AccessToken(['provider' => $this->name]);
}
/**
* Prepare request headers
*
* @param array<string, string>|null $headers
* @return array<string, string>
*/
final protected function getRequestHeaders(array|null $headers = null):array{
return array_merge($this::HEADERS_API, ($headers ?? []));
}
/**
* Prepares the request URL
*
* @param array<string, scalar|bool|null>|null $params
*/
final protected function getRequestURL(string $path, array|null $params = null):string{
return QueryUtil::merge($this->getRequestTarget($path), $this->cleanQueryParams(($params ?? [])));
}
/**
* Cleans an array of query parameters
*
* @param array<string, scalar|bool|null> $params
* @return array<string, string>
*/
protected function cleanQueryParams(iterable $params):array{
return QueryUtil::cleanParams($params, QueryUtil::BOOLEANS_AS_INT_STRING, true);
}
/**
* Cleans an array of body parameters
*
* @param array<string, scalar|bool|null> $params
* @return array<string, string>
*/
protected function cleanBodyParams(iterable $params):array{
return QueryUtil::cleanParams($params, QueryUtil::BOOLEANS_AS_BOOL, true);
}
/**
* Adds an "Authorization: Basic <base64(key:secret)>" header to the given request
*/
protected function addBasicAuthHeader(RequestInterface $request):RequestInterface{
$auth = Str::base64encode(sprintf('%s:%s', $this->options->key, $this->options->secret));
return $request->withHeader('Authorization', sprintf('Basic %s', $auth));
}
/**
* returns a 32 byte random string (in hexadecimal representation) for use as a nonce
*
* @link https://datatracker.ietf.org/doc/html/rfc5849#section-3.3
* @link https://datatracker.ietf.org/doc/html/rfc6749#section-10.12
*/
protected function nonce(int $bytes = 32):string{
return sodium_bin2hex(random_bytes($bytes));
}
/**
* implements TokenInvalidate
*
* @see \chillerlan\OAuth\Core\TokenInvalidate
* @codeCoverageIgnore
* @throws \chillerlan\OAuth\Providers\ProviderException
*/
public function InvalidateAccessToken(AccessToken|null $token = null):bool{
throw new ProviderException('not implemented');
}
/**
* @throws \chillerlan\OAuth\Core\InvalidAccessTokenException
*/
final public function sendRequest(RequestInterface $request):ResponseInterface{
// get authorization only if we request the provider API,
// shortcut reroute to the http client otherwise.
// avoid sending bearer tokens to unknown hosts
if(!str_starts_with((string)$request->getUri(), $this->apiURL)){
return $this->http->sendRequest($request);
}
$request = $this->getRequestAuthorization($request);
return $this->http->sendRequest($request);
}
/**
* @throws \chillerlan\OAuth\Core\UnauthorizedAccessException
*/
public function request(
string $path,
array|null $params = null,
string|null $method = null,
StreamInterface|array|string|null $body = null,
array|null $headers = null,
string|null $protocolVersion = null,
):ResponseInterface{
$request = $this->requestFactory->createRequest(($method ?? 'GET'), $this->getRequestURL($path, $params));
foreach($this->getRequestHeaders($headers) as $header => $value){
$request = $request->withAddedHeader($header, $value);
}
if($body !== null){
$request = $this->setRequestBody($body, $request);
}
if($protocolVersion !== null){
$request = $request->withProtocolVersion($protocolVersion);
}
$response = $this->sendRequest($request);
// we're gonna throw here immideately on unauthorized/forbidden
if(in_array($response->getStatusCode(), [401, 403], true)){
throw new UnauthorizedAccessException;
}
return $response;
}
/**
* Prepares the request body and sets it in the given RequestInterface, along with a Content-Length header
*
* @param StreamInterface|array<string, scalar|bool|null>|string $body
* @throws \chillerlan\OAuth\Providers\ProviderException
*/
final protected function setRequestBody(StreamInterface|array|string $body, RequestInterface $request):RequestInterface{
// convert the array to a string according to the Content-Type header
if(is_array($body)){
$body = $this->cleanBodyParams($body);
$contentType = strtolower($request->getHeaderLine('content-type'));
$body = match($contentType){
'application/x-www-form-urlencoded' => QueryUtil::build($body, PHP_QUERY_RFC1738),
'application/json', 'application/vnd.api+json' => Str::jsonEncode($body, 0),
default => throw new ProviderException(
sprintf('invalid content-type "%s" for the given array body', $contentType),
),
};
}
// we don't check if the given string matches the content type - this is the implementor's responsibility
if(!$body instanceof StreamInterface){
$body = $this->streamFactory->createStream($body);
}
return $request
->withHeader('Content-length', (string)$body->getSize())
->withBody($body)
;
}
/**
* Determine the request target from the given URI (path segment or URL) with respect to $apiURL,
* anything except host and path will be ignored, scheme will always be set to "https".
* Throws if the host of a given URL does not match the host of $apiURL.
*
* @see \chillerlan\OAuth\Core\OAuthInterface::request()
*
* @throws \chillerlan\OAuth\Providers\ProviderException
*/
protected function getRequestTarget(string $uri):string{
$parsedURL = $this->uriFactory->createUri($uri);
$parsedHost = $parsedURL->getHost();
$api = $this->uriFactory->createUri($this->apiURL);
if($parsedHost === ''){
$parsedPath = $parsedURL->getPath();
$apiURL = rtrim((string)$api, '/');
if($parsedPath === ''){
return $apiURL;
}
return sprintf('%s/%s', $apiURL, ltrim($parsedPath, '/'));
}
// for some reason we were given a host name
// we explicitly ignore any existing parameters here and enforce https
$parsedURL = $parsedURL->withScheme('https')->withQuery('')->withFragment('');
$apiHost = $api->getHost();
if($parsedHost === $apiHost){
return (string)$parsedURL;
}
// ok, one last chance - we might have a subdomain in any of the hosts (messy)
$strip_subdomains = function(string $host):string{
$host = explode('.', $host);
// don't come at me with .co.uk
// phpcs:ignore
while(count($host) > 2){
array_shift($host);
}
return implode('.', $host);
};
if($strip_subdomains($parsedHost) === $strip_subdomains($apiHost)){
return (string)$parsedURL;
}
throw new ProviderException(sprintf('given host (%s) does not match provider (%s)', $parsedHost , $apiHost));
}
/**
* prepares and sends the request to the provider's "me" endpoint and returns a ResponseInterface
*
* @param array<string, scalar|bool|null>|null $params
*/
protected function sendMeRequest(string $endpoint, array|null $params = null):ResponseInterface{
// we'll bypass the API check here as not all "me" endpoints align with the provider APIs
$url = $this->getRequestURL($endpoint, $params);
$request = $this->requestFactory->createRequest('GET', $url);
foreach($this->getRequestHeaders() as $header => $value){
$request = $request->withAddedHeader($header, $value);
}
$request = $this->getRequestAuthorization($request);
return $this->http->sendRequest($request);
}
/**
* fetches the provider's "me" endpoint and returns the JSON data as an array
*
* @see \chillerlan\OAuth\Core\UserInfo::me()
* @see \chillerlan\OAuth\Core\OAuthProvider::sendMeRequest()
* @see \chillerlan\OAuth\Core\OAuthProvider::handleMeResponseError()
*
* @param array<string, scalar|bool|null>|null $params
* @return array<int|string, mixed>
* @throws \chillerlan\OAuth\Providers\ProviderException
*/
final protected function getMeResponseData(string $endpoint, array|null $params = null):array{
$response = $this->sendMeRequest($endpoint, $params);
if($response->getStatusCode() === 200){
$contentType = $response->getHeaderLine('Content-Type');
// mixcloud sends a javascript content type for json...
if(!str_contains($contentType, 'json') && !str_contains($contentType, 'javascript')){
throw new ProviderException(sprintf('invalid content type "%s", expected JSON', $contentType));
}
return MessageUtil::decodeJSON($response, true);
}
// handle and throw the error
$this->handleMeResponseError($response);
/** @noinspection PhpUnreachableStatementInspection this is here because phpstan silly */
return []; // @codeCoverageIgnore
}
/**
* handles errors for the `me()` endpoints - one horrible block of code to catch them all
*
* we could simply throw a ProviderException and be done with it, but we're nice and try to provide a message too
*
* @throws \chillerlan\OAuth\Providers\ProviderException|\chillerlan\OAuth\Core\UnauthorizedAccessException
*/
final protected function handleMeResponseError(ResponseInterface $response):void{
$status = $response->getStatusCode();
// in case these slipped through
if(in_array($status, [400, 401, 403], true)){
throw new UnauthorizedAccessException;
}
// the error response may be plain text or html in some cases
if(!str_contains($response->getHeaderLine('Content-Type'), 'json')){
$body = strip_tags(MessageUtil::getContents($response));
throw new ProviderException(sprintf('user info error HTTP/%s, "%s"', $status, $body));
}
// json error, fine
$json = MessageUtil::decodeJSON($response, true);
// let's try the common fields
foreach(['error_description', 'message', 'error', 'meta', 'data', 'detail', 'status', 'text'] as $err){
if(isset($json[$err]) && is_string($json[$err])){
throw new ProviderException($json[$err]);
}
elseif(is_array($json[$err])){
foreach(['message', 'error', 'errorDetail', 'developer_message', 'msg', 'code'] as $errDetail){
if(isset($json[$err][$errDetail]) && is_string($json[$err][$errDetail])){
throw new ProviderException($json[$err][$errDetail]);
}
}
}
}
// throw the status if we can't find a message
throw new ProviderException(sprintf('user info error HTTP/%s', $status));
}
}