1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace FAPI\Sylius\Http; |
6
|
|
|
|
7
|
|
|
use Http\Client\Common\Plugin; |
8
|
|
|
use Http\Promise\Promise; |
9
|
|
|
use Psr\Http\Message\RequestInterface; |
10
|
|
|
use Psr\Http\Message\ResponseInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* This will automatically refresh expired access token. |
14
|
|
|
* |
15
|
|
|
* @author Tobias Nyholm <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class AuthenticationPlugin implements Plugin |
18
|
|
|
{ |
19
|
|
|
const RETRY_LIMIT = 2; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
private $accessToken; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var Authenticator |
28
|
|
|
*/ |
29
|
|
|
private $authenticator; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Store the retry counter for each request. |
33
|
|
|
* |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
private $retryStorage = []; |
37
|
|
|
|
38
|
|
|
public function __construct(Authenticator $authenticator, string $accessToken) |
39
|
|
|
{ |
40
|
|
|
$this->authenticator = $authenticator; |
41
|
|
|
$this->accessToken = \json_decode($accessToken, true); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function handleRequest(RequestInterface $request, callable $next, callable $first): Promise |
45
|
|
|
{ |
46
|
|
|
if (null === $this->accessToken) { |
47
|
|
|
return $next($request); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$chainIdentifier = \spl_object_hash((object) $first); |
51
|
|
|
$header = \sprintf('Bearer %s', $this->accessToken['access_token'] ?? ''); |
52
|
|
|
$request = $request->withHeader('Authorization', $header); |
53
|
|
|
|
54
|
|
|
$promise = $next($request); |
55
|
|
|
|
56
|
|
|
return $promise->then(function (ResponseInterface $response) use ($request, $next, $first, $chainIdentifier) { |
57
|
|
|
if (!\array_key_exists($chainIdentifier, $this->retryStorage)) { |
58
|
|
|
$this->retryStorage[$chainIdentifier] = 0; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if (401 !== $response->getStatusCode() || $this->retryStorage[$chainIdentifier] >= self::RETRY_LIMIT) { |
62
|
|
|
unset($this->retryStorage[$chainIdentifier]); |
63
|
|
|
|
64
|
|
|
return $response; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$accessToken = $this->authenticator->refreshAccessToken($this->accessToken['access_token'], $this->accessToken['refresh_token']); |
68
|
|
|
if (null === $accessToken) { |
69
|
|
|
return $response; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
// Save new token |
73
|
|
|
$this->accessToken = \json_decode($accessToken, true); |
74
|
|
|
|
75
|
|
|
// Add new token to request |
76
|
|
|
$header = \sprintf('Bearer %s', $this->accessToken['access_token']); |
77
|
|
|
$request = $request->withHeader('Authorization', $header); |
|
|
|
|
78
|
|
|
|
79
|
|
|
// Retry |
80
|
|
|
++$this->retryStorage[$chainIdentifier]; |
81
|
|
|
$promise = $this->handleRequest($request, $next, $first); |
82
|
|
|
|
83
|
|
|
return $promise->wait(); |
84
|
|
|
}); |
85
|
|
|
|
86
|
|
|
return $response; |
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function getAccessToken(): string |
90
|
|
|
{ |
91
|
|
|
return \json_encode($this->accessToken); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
It seems like you are assigning to a variable which was imported through a
use
statement which was not imported by reference.For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.
Change not visible in outer-scope
Change visible in outer-scope